|
| 1 | +package generate |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/open-source-cloud/devstack/internal/config" |
| 11 | + "github.com/open-source-cloud/devstack/internal/template" |
| 12 | + "github.com/open-source-cloud/devstack/templates" |
| 13 | +) |
| 14 | + |
| 15 | +func TestHealthcheckTestKinds(t *testing.T) { |
| 16 | + cases := []struct { |
| 17 | + name string |
| 18 | + hc config.Healthcheck |
| 19 | + want []any |
| 20 | + }{ |
| 21 | + {"tcp", config.Healthcheck{Kind: "tcp", Port: 5432}, |
| 22 | + []any{"CMD-SHELL", "nc -z localhost 5432"}}, |
| 23 | + {"http", config.Healthcheck{Kind: "http", Port: 8080, Path: "/healthz"}, |
| 24 | + []any{"CMD", "curl", "-fsS", "-o", "/dev/null", "http://localhost:8080/healthz"}}, |
| 25 | + {"https-skipverify", config.Healthcheck{Kind: "https", Port: 443, Path: "/"}, |
| 26 | + []any{"CMD", "curl", "-fsS", "-k", "-o", "/dev/null", "https://localhost:443/"}}, |
| 27 | + {"exec", config.Healthcheck{Kind: "exec", Command: []string{"mysqladmin", "ping"}}, |
| 28 | + []any{"CMD", "mysqladmin", "ping"}}, |
| 29 | + {"pg_isready", config.Healthcheck{Kind: "pg_isready", User: "app", DB: "appdb"}, |
| 30 | + []any{"CMD-SHELL", "pg_isready -p 5432 -U app -d appdb"}}, |
| 31 | + {"redis-default", config.Healthcheck{Kind: "redis"}, |
| 32 | + []any{"CMD-SHELL", "redis-cli -p 6379 PING"}}, |
| 33 | + {"redis-literal-auth", config.Healthcheck{Kind: "redis", Auth: "devpass"}, |
| 34 | + []any{"CMD-SHELL", "redis-cli -p 6379 -a devpass PING"}}, |
| 35 | + {"redis-secret-auth-omitted", config.Healthcheck{Kind: "redis", Auth: "secret://vault/redis#pw"}, |
| 36 | + []any{"CMD-SHELL", "redis-cli -p 6379 PING"}}, |
| 37 | + } |
| 38 | + for _, c := range cases { |
| 39 | + t.Run(c.name, func(t *testing.T) { |
| 40 | + got, err := healthcheckTest(&c.hc) |
| 41 | + if err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + if fmt.Sprint(got) != fmt.Sprint(c.want) { |
| 45 | + t.Errorf("test = %v, want %v", got, c.want) |
| 46 | + } |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestHealthcheckTestErrors(t *testing.T) { |
| 52 | + for _, hc := range []config.Healthcheck{ |
| 53 | + {Kind: "tcp"}, // no port |
| 54 | + {Kind: "exec"}, // no command |
| 55 | + } { |
| 56 | + if _, err := healthcheckTest(&hc); err == nil { |
| 57 | + t.Errorf("kind %q with missing params should error", hc.Kind) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestHealthcheckBlockTimingOnlyWhenSet(t *testing.T) { |
| 63 | + // Only the test is present when timing is unset. |
| 64 | + bare, _ := healthcheckBlock(&config.Healthcheck{Kind: "tcp", Port: 1}) |
| 65 | + if len(bare) != 1 { |
| 66 | + t.Errorf("bare block = %v, want only test", bare) |
| 67 | + } |
| 68 | + full, _ := healthcheckBlock(&config.Healthcheck{ |
| 69 | + Kind: "tcp", Port: 1, Interval: "5s", Timeout: "3s", Retries: 7, StartPeriod: "20s", |
| 70 | + }) |
| 71 | + for _, k := range []string{"test", "interval", "timeout", "retries", "start_period"} { |
| 72 | + if _, ok := full[k]; !ok { |
| 73 | + t.Errorf("full block missing %q: %v", k, full) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestDependsOnBlockClassification(t *testing.T) { |
| 79 | + m := &config.Model{Projects: map[string]config.Project{ |
| 80 | + "api": {Services: map[string]config.Service{ |
| 81 | + "web": {Template: "t"}, |
| 82 | + "cache": {Template: "t"}, |
| 83 | + }}, |
| 84 | + }} |
| 85 | + deps := []config.DependsOn{ |
| 86 | + {Service: "cache", Condition: "healthy"}, // intra (bare) |
| 87 | + {Service: "workspace.api.web", Condition: "started"}, // intra (qualified) |
| 88 | + {Service: "workspace.shared.postgres", Condition: "healthy"}, // shared → skip |
| 89 | + {Service: "workspace.other.svc", Condition: "healthy"}, // other project → skip |
| 90 | + } |
| 91 | + got, err := dependsOnBlock(m, "api", deps) |
| 92 | + if err != nil { |
| 93 | + t.Fatal(err) |
| 94 | + } |
| 95 | + if len(got) != 2 { |
| 96 | + t.Fatalf("depends_on = %v, want 2 intra-project edges", got) |
| 97 | + } |
| 98 | + if got["cache"].(map[string]any)["condition"] != "service_healthy" { |
| 99 | + t.Errorf("cache condition = %v, want service_healthy", got["cache"]) |
| 100 | + } |
| 101 | + if got["web"].(map[string]any)["condition"] != "service_started" { |
| 102 | + t.Errorf("web condition = %v, want service_started", got["web"]) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestDependsOnBlockMissingTarget(t *testing.T) { |
| 107 | + m := &config.Model{Projects: map[string]config.Project{ |
| 108 | + "api": {Services: map[string]config.Service{"web": {Template: "t"}}}, |
| 109 | + }} |
| 110 | + _, err := dependsOnBlock(m, "api", []config.DependsOn{{Service: "ghost"}}) |
| 111 | + if err == nil || !strings.Contains(err.Error(), "ghost") { |
| 112 | + t.Fatalf("want a missing-target error naming ghost, got %v", err) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// TestIntraProjectDependsOn_EndToEnd generates a real project where one service |
| 117 | +// depends on a sibling (condition healthy) and the sibling declares a |
| 118 | +// healthcheck, asserting the lowering reaches compose AND compose-go accepts the |
| 119 | +// service_healthy edge. |
| 120 | +func TestIntraProjectDependsOn_EndToEnd(t *testing.T) { |
| 121 | + root := t.TempDir() |
| 122 | + write := func(rel, body string) { |
| 123 | + p := filepath.Join(root, rel) |
| 124 | + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { |
| 125 | + t.Fatal(err) |
| 126 | + } |
| 127 | + if err := os.WriteFile(p, []byte(body), 0o644); err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + } |
| 131 | + write("workspace.yaml", "apiVersion: devstack/v1\nkind: Workspace\nname: demo\nprojects:\n - { name: app, path: app }\n") |
| 132 | + write("app/devstack.yaml", `apiVersion: devstack/v1 |
| 133 | +kind: Project |
| 134 | +name: app |
| 135 | +services: |
| 136 | + cache: |
| 137 | + template: node.vite |
| 138 | + healthcheck: { kind: tcp, port: 6379, interval: 2s } |
| 139 | + web: |
| 140 | + template: node.vite |
| 141 | + dependsOn: |
| 142 | + - { service: cache, condition: healthy } |
| 143 | +`) |
| 144 | + m, err := config.LoadAt(root) |
| 145 | + if err != nil { |
| 146 | + t.Fatalf("load: %v", err) |
| 147 | + } |
| 148 | + g, err := New(m, template.NewFSSource(templates.FS), WithEnv(map[string]string{})) |
| 149 | + if err != nil { |
| 150 | + t.Fatalf("New: %v", err) |
| 151 | + } |
| 152 | + st, err := g.GenerateProject("app") |
| 153 | + if err != nil { |
| 154 | + t.Fatalf("GenerateProject: %v", err) |
| 155 | + } |
| 156 | + compose := string(st.Compose) |
| 157 | + if !strings.Contains(compose, "depends_on:") || !strings.Contains(compose, "condition: service_healthy") { |
| 158 | + t.Errorf("compose missing lowered depends_on:\n%s", compose) |
| 159 | + } |
| 160 | + if !strings.Contains(compose, "cache") { |
| 161 | + t.Errorf("compose should reference the cache dependency:\n%s", compose) |
| 162 | + } |
| 163 | +} |
0 commit comments