package gitops import ( "context" "encoding/base64" "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "go.yaml.in/yaml/v3" ) func TestCreateWorldPreservesValuesAndRetries(t *testing.T) { current := "# retained operator comment\nimage:\n digest: sha256:keep\nportal:\n host: srs.example.test\nworlds:\n - id: world-001\n seed: \"12\"\n host: srs.example.test\n" puts := 0 server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Authorization") != "token test-token" { t.Error("missing token") } if r.URL.Path != "/api/v1/repos/owner/game/contents/deploy/values.yaml" { t.Error("wrong path", r.URL.Path) } if r.Method == "GET" { if r.URL.Query().Get("ref") != "master" { t.Error("wrong branch") } _ = json.NewEncoder(w).Encode(map[string]string{"sha": "revision", "content": base64.StdEncoding.EncodeToString([]byte(current))}) return } var body map[string]string _ = json.NewDecoder(r.Body).Decode(&body) if body["sha"] != "revision" || body["branch"] != "master" { t.Error("missing optimistic revision") } puts++ if puts == 1 { w.WriteHeader(409) return } raw, err := base64.StdEncoding.DecodeString(body["content"]) if err != nil { t.Fatal(err) } current = string(raw) w.WriteHeader(200) _, _ = w.Write([]byte(`{}`)) })) defer server.Close() c, err := New(Config{server.URL + "/owner/game.git", "master", "deploy/values.yaml", "test-token", "https://srs.example.test"}) if err != nil { t.Fatal(err) } c.http = server.Client() if _, err = c.CreateWorld(context.Background(), "world-002", "42"); err != nil { t.Fatal(err) } if puts != 2 || !strings.Contains(current, "retained operator comment") || !strings.Contains(current, "sha256:keep") { t.Fatal("unrelated values changed", current) } var values struct { Worlds []struct{ ID, Seed, Host string } } if err = yaml.Unmarshal([]byte(current), &values); err != nil || len(values.Worlds) != 2 || values.Worlds[1].Seed != "42" { t.Fatal("bad world definition", err, current) } if _, err = c.CreateWorld(context.Background(), "world-002", "42"); err != nil || puts != 2 { t.Fatal("retry duplicated world", err) } if _, err = c.CreateWorld(context.Background(), "world-002", "43"); err == nil { t.Fatal("existing ID reused") } if _, err = c.CreateWorld(context.Background(), "../bad", "42"); err == nil { t.Fatal("invalid ID accepted") } if _, err = c.CreateInstance(context.Background(), "hub-001", "50", "hub"); err != nil || !strings.Contains(current, "kind: hub") { t.Fatal("hub definition missing", err, current) } if _, err = c.CreateInstance(context.Background(), "hub-001", "50", "hub"); err != nil || puts != 3 { t.Fatal("hub retry duplicated instance", err) } if _, err = c.CreateWorld(context.Background(), "hub-001", "50"); err == nil { t.Fatal("existing hub changed to survival world") } } func TestGitOpsConfigurationValidation(t *testing.T) { for _, repository := range []string{"http://example.test/o/r", "https://token@example.test/o/r", "https://example.test/only-one", "https://example.test/../repo"} { if _, err := New(Config{repository, "master", "deploy/values.yaml", "token", "https://srs.example.test"}); err == nil { t.Fatal("unsafe repository accepted", repository) } } for _, file := range []string{"../secret.yaml", "/absolute.yaml", "values.json", "a/../values.yaml"} { if _, err := New(Config{"https://example.test/o/r", "master", file, "token", "https://srs.example.test"}); err == nil { t.Fatal("unsafe file accepted", file) } } }