chore: initialize containerized Go learning workbook (2026 DevOps/SRE)
This commit is contained in:
commit
84be3e5d77
64 files changed
+1382
No files matched your search
@@ -0,0 +1,8 @@
|
||||
# 01 — Audyt i Refaktoryzacja Kodu AI (Data Race + Edge Cases)
|
||||
|
||||
W `audit.go` znajduje się „działający” kod w stylu szybkiego draftu z AI, który:
|
||||
* ma **data race** wykrywany przez `go test -race` (równoległy zapis do licznika bez synchronizacji),
|
||||
* panikuje na pustym wycinku lub źle sformatowanym nagłówku `Bearer`.
|
||||
|
||||
## Twoje zadanie
|
||||
Napraw kod w `audit.go`, aż `make test PKG=./exercises/02-engineering-2026/01-ai-code-audit` przejdzie czysto pod `-race`.
|
||||
@@ -0,0 +1,22 @@
|
||||
package audit
|
||||
|
||||
import "strings"
|
||||
|
||||
// BUG: brak synchronizacji (sync.Mutex lub atomic.Int64) -> data race pod `go test -race`!
|
||||
type Counter struct {
|
||||
value int64
|
||||
}
|
||||
|
||||
func (c *Counter) Inc() {
|
||||
c.value++
|
||||
}
|
||||
|
||||
func (c *Counter) Value() int64 {
|
||||
return c.value
|
||||
}
|
||||
|
||||
// BUG: panikuje gdy brak spacji, nie sprawdza prefiksu "Bearer ", przepuszcza pusty token!
|
||||
func ExtractBearerToken(header string) (string, bool) {
|
||||
parts := strings.Split(header, " ")
|
||||
return parts[1], true
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCounterNoDataRace(t *testing.T) {
|
||||
var c Counter
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 50; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
c.Inc()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
if c.Value() != 50 {
|
||||
t.Fatalf("got %d, want 50", c.Value())
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractBearerToken(t *testing.T) {
|
||||
if tok, ok := ExtractBearerToken("Bearer secret123"); !ok || tok != "secret123" {
|
||||
t.Fatalf("got (%q, %v), want (secret123, true)", tok, ok)
|
||||
}
|
||||
for _, bad := range []string{"Bearer ", "Basic abc", "Malformed", ""} {
|
||||
if _, ok := ExtractBearerToken(bad); ok {
|
||||
t.Errorf("expected ok=false for %q", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
# 02 — `context.Context` i Odporność (Retry + Anulowanie)
|
||||
|
||||
## Zadania (`resilience.go`)
|
||||
1. `RetryWithContext(ctx context.Context, attempts int, delay time.Duration, fn func(context.Context) error) error` — ponawiaj `fn(ctx)` do `attempts` razy, czekając `delay` (z przerwaniem przez `ctx.Done()` w `select`).
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=./exercises/02-engineering-2026/02-context-and-resilience
|
||||
```
|
||||
@@ -0,0 +1,15 @@
|
||||
package resilience
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
func RetryWithContext(
|
||||
ctx context.Context,
|
||||
attempts int,
|
||||
delay time.Duration,
|
||||
fn func(context.Context) error,
|
||||
) error {
|
||||
panic("TODO: ponawiaj fn(ctx) do attempts razy, respektując ctx.Done() podczas czekania delay")
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package resilience
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRetrySucceedsOnThirdAttempt(t *testing.T) {
|
||||
calls := 0
|
||||
err := RetryWithContext(context.Background(), 3, time.Millisecond, func(ctx context.Context) error {
|
||||
calls++
|
||||
if calls < 3 {
|
||||
return errors.New("transient")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil || calls != 3 {
|
||||
t.Fatalf("got err=%v calls=%d, want nil and 3", err, calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetryStopsOnContextCancel(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Millisecond)
|
||||
defer cancel()
|
||||
err := RetryWithContext(ctx, 10, 50*time.Millisecond, func(ctx context.Context) error {
|
||||
return errors.New("always fails")
|
||||
})
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("expected context.DeadlineExceeded, got %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
# 03 — Programowanie Cross-Platform (`path/filepath`, env vars, `runtime.GOOS`)
|
||||
|
||||
## Zadania (`crossos.go`)
|
||||
1. `ConfigPath(baseDir, appName string) string` — użyj `filepath.Join(baseDir, appName, "config.yaml")`.
|
||||
2. `EnvOrDefault(lookup func(string) (string, bool), key, fallback string) string` — pobierz zmienną ze wstrzykniętej funkcji `lookup` (np. `os.LookupEnv`) lub zwróć `fallback` gdy brak lub pusta.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=./exercises/02-engineering-2026/03-cross-platform-os
|
||||
```
|
||||
@@ -0,0 +1,9 @@
|
||||
package crossos
|
||||
|
||||
func ConfigPath(baseDir, appName string) string {
|
||||
panic("TODO: użyj filepath.Join(baseDir, appName, \"config.yaml\")")
|
||||
}
|
||||
|
||||
func EnvOrDefault(lookup func(string) (string, bool), key, fallback string) string {
|
||||
panic("TODO: wywołaj lookup(key); jeśli brak lub puste po TrimSpace -> fallback")
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package crossos
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfigPath(t *testing.T) {
|
||||
base := filepath.Join("home", "ops")
|
||||
got := ConfigPath(base, "monitor")
|
||||
want := filepath.Join(base, "monitor", "config.yaml")
|
||||
if got != want {
|
||||
t.Fatalf("got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvOrDefault(t *testing.T) {
|
||||
env := map[string]string{"LOG_LEVEL": "debug", "EMPTY": " "}
|
||||
lookup := func(k string) (string, bool) { v, ok := env[k]; return v, ok }
|
||||
|
||||
if got := EnvOrDefault(lookup, "LOG_LEVEL", "info"); got != "debug" {
|
||||
t.Fatalf("got %q, want debug", got)
|
||||
}
|
||||
if got := EnvOrDefault(lookup, "EMPTY", "info"); got != "info" {
|
||||
t.Fatalf("got %q, want info", got)
|
||||
}
|
||||
if got := EnvOrDefault(lookup, "MISSING", "info"); got != "info" {
|
||||
t.Fatalf("got %q, want info", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
# 04 — Dobre Praktyki Testowania (`t.TempDir()`, In-Memory Fakes)
|
||||
|
||||
## Zadania (`testing_ex.go`)
|
||||
1. `WriteAndReadLines(path string, lines []string) ([]string, error)` — zapisz linie do pliku i odczytaj je z powrotem.
|
||||
2. `FeatureFlags.IsEnabled(name string) bool` — korzysta z małego interfejsu `Store` (łatwego do testowania z In-Memory Fake bez mocków).
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=./exercises/02-engineering-2026/04-testing-best-practices
|
||||
```
|
||||
@@ -0,0 +1,17 @@
|
||||
package testing_ex
|
||||
|
||||
type Store interface {
|
||||
Get(key string) (string, bool)
|
||||
}
|
||||
|
||||
type FeatureFlags struct {
|
||||
Store Store
|
||||
}
|
||||
|
||||
func (f FeatureFlags) IsEnabled(name string) bool {
|
||||
panic("TODO: pobierz z f.Store.Get(name); zwróć true dla 'true' lub '1' (case-insensitive)")
|
||||
}
|
||||
|
||||
func WriteAndReadLines(path string, lines []string) ([]string, error) {
|
||||
panic("TODO: zapisz lines (os.WriteFile) i odczytaj z powrotem (os.ReadFile)")
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package testing_ex
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type fakeStore map[string]string
|
||||
|
||||
func (f fakeStore) Get(key string) (string, bool) { v, ok := f[key]; return v, ok }
|
||||
|
||||
func TestFeatureFlagsTableDriven(t *testing.T) {
|
||||
flags := FeatureFlags{Store: fakeStore{"a": "true", "b": "1", "c": "false"}}
|
||||
cases := []struct {
|
||||
name string
|
||||
want bool
|
||||
}{{"a", true}, {"b", true}, {"c", false}, {"missing", false}}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := flags.IsEnabled(tc.name); got != tc.want {
|
||||
t.Errorf("IsEnabled(%q) = %v, want %v", tc.name, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteAndReadLines(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "lines.txt")
|
||||
want := []string{"alpha", "beta", "gamma"}
|
||||
got, err := WriteAndReadLines(path, want)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user