|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func writeTree(t *testing.T, files map[string]string) string { |
| 12 | + t.Helper() |
| 13 | + root := t.TempDir() |
| 14 | + for rel, content := range files { |
| 15 | + p := filepath.Join(root, rel) |
| 16 | + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { |
| 17 | + t.Fatal(err) |
| 18 | + } |
| 19 | + if err := os.WriteFile(p, []byte(content), 0o644); err != nil { |
| 20 | + t.Fatal(err) |
| 21 | + } |
| 22 | + } |
| 23 | + return root |
| 24 | +} |
| 25 | + |
| 26 | +func TestLoadValid(t *testing.T) { |
| 27 | + m, err := LoadAt(filepath.Join("testdata", "valid")) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("LoadAt: %v", err) |
| 30 | + } |
| 31 | + if m.Workspace.Name != "acme" { |
| 32 | + t.Errorf("workspace name = %q, want acme", m.Workspace.Name) |
| 33 | + } |
| 34 | + if got := len(m.Projects); got != 2 { |
| 35 | + t.Errorf("projects = %d, want 2", got) |
| 36 | + } |
| 37 | + if got := len(m.Workspace.Shared); got != 3 { |
| 38 | + t.Errorf("shared = %d, want 3", got) |
| 39 | + } |
| 40 | + api, ok := m.Projects["api"] |
| 41 | + if !ok { |
| 42 | + t.Fatal("project api missing") |
| 43 | + } |
| 44 | + if got := len(api.Services["api"].Uses); got != 2 { |
| 45 | + t.Errorf("api.uses = %d, want 2", got) |
| 46 | + } |
| 47 | + if dir := m.ProjectDir("api"); !strings.HasSuffix(dir, filepath.Join("services", "api")) { |
| 48 | + t.Errorf("ProjectDir(api) = %q, want suffix services/api", dir) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +const sharedPGOnly = `apiVersion: devstack/v1 |
| 53 | +kind: Workspace |
| 54 | +name: acme |
| 55 | +shared: |
| 56 | + postgres: { template: postgres } |
| 57 | +projects: |
| 58 | + - { name: api, path: api } |
| 59 | +` |
| 60 | + |
| 61 | +func TestDanglingUses(t *testing.T) { |
| 62 | + root := writeTree(t, map[string]string{ |
| 63 | + "workspace.yaml": sharedPGOnly, |
| 64 | + "api/devstack.yaml": `apiVersion: devstack/v1 |
| 65 | +kind: Project |
| 66 | +name: api |
| 67 | +services: |
| 68 | + api: |
| 69 | + template: t |
| 70 | + uses: |
| 71 | + - workspace.shared.kafka |
| 72 | +`, |
| 73 | + }) |
| 74 | + _, err := LoadAt(root) |
| 75 | + if err == nil { |
| 76 | + t.Fatal("expected error for dangling uses") |
| 77 | + } |
| 78 | + var pe *PosError |
| 79 | + if !errors.As(err, &pe) { |
| 80 | + t.Fatalf("want *PosError, got %T: %v", err, err) |
| 81 | + } |
| 82 | + if pe.Line == 0 { |
| 83 | + t.Errorf("want a positioned error (line>0), got %v", pe) |
| 84 | + } |
| 85 | + if !strings.Contains(pe.Msg, "kafka") { |
| 86 | + t.Errorf("error %q should name the missing service kafka", pe.Msg) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestReferenceCycle(t *testing.T) { |
| 91 | + root := writeTree(t, map[string]string{ |
| 92 | + "workspace.yaml": `apiVersion: devstack/v1 |
| 93 | +kind: Workspace |
| 94 | +name: acme |
| 95 | +projects: |
| 96 | + - { name: a, path: a } |
| 97 | + - { name: b, path: b } |
| 98 | +`, |
| 99 | + "a/devstack.yaml": `apiVersion: devstack/v1 |
| 100 | +kind: Project |
| 101 | +name: a |
| 102 | +services: |
| 103 | + svc: |
| 104 | + template: t |
| 105 | + env: |
| 106 | + import: |
| 107 | + - { from: workspace.b.svc } |
| 108 | +`, |
| 109 | + "b/devstack.yaml": `apiVersion: devstack/v1 |
| 110 | +kind: Project |
| 111 | +name: b |
| 112 | +services: |
| 113 | + svc: |
| 114 | + template: t |
| 115 | + env: |
| 116 | + import: |
| 117 | + - { from: workspace.a.svc } |
| 118 | +`, |
| 119 | + }) |
| 120 | + _, err := LoadAt(root) |
| 121 | + if err == nil { |
| 122 | + t.Fatal("expected a cycle error") |
| 123 | + } |
| 124 | + if !strings.Contains(err.Error(), "cycle") { |
| 125 | + t.Errorf("error %q should report a cycle", err.Error()) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestBadName(t *testing.T) { |
| 130 | + root := writeTree(t, map[string]string{ |
| 131 | + "workspace.yaml": `apiVersion: devstack/v1 |
| 132 | +kind: Workspace |
| 133 | +name: Acme_Bad! |
| 134 | +projects: [] |
| 135 | +`, |
| 136 | + }) |
| 137 | + _, err := LoadAt(root) |
| 138 | + if err == nil || !strings.Contains(err.Error(), "name") { |
| 139 | + t.Fatalf("want a name validation error, got %v", err) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestWrongAPIVersion(t *testing.T) { |
| 144 | + root := writeTree(t, map[string]string{ |
| 145 | + "workspace.yaml": `apiVersion: devstack/v2 |
| 146 | +kind: Workspace |
| 147 | +name: acme |
| 148 | +projects: [] |
| 149 | +`, |
| 150 | + }) |
| 151 | + if _, err := LoadAt(root); err == nil { |
| 152 | + t.Fatal("want an apiVersion mismatch error") |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestDiscoverWalksUp(t *testing.T) { |
| 157 | + t.Setenv("DEVSTACK_WORKSPACE", "") |
| 158 | + root := writeTree(t, map[string]string{ |
| 159 | + "workspace.yaml": "apiVersion: devstack/v1\nkind: Workspace\nname: acme\n", |
| 160 | + "a/b/c/.keep": "", |
| 161 | + }) |
| 162 | + got, err := Discover(filepath.Join(root, "a", "b", "c")) |
| 163 | + if err != nil { |
| 164 | + t.Fatalf("Discover: %v", err) |
| 165 | + } |
| 166 | + if eval(t, got) != eval(t, root) { |
| 167 | + t.Errorf("Discover = %q, want %q", got, root) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func TestDiscoverNotFound(t *testing.T) { |
| 172 | + t.Setenv("DEVSTACK_WORKSPACE", "") |
| 173 | + if _, err := Discover(t.TempDir()); err == nil { |
| 174 | + t.Fatal("want a not-found error when no workspace.yaml exists") |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +func eval(t *testing.T, p string) string { |
| 179 | + t.Helper() |
| 180 | + r, err := filepath.EvalSymlinks(p) |
| 181 | + if err != nil { |
| 182 | + return p |
| 183 | + } |
| 184 | + return r |
| 185 | +} |
| 186 | + |
| 187 | +// fakeResolver implements Resolver for interpolation tests. |
| 188 | +type fakeResolver struct{} |
| 189 | + |
| 190 | +func (fakeResolver) Env(n string) (string, bool) { |
| 191 | + if n == "FOO" { |
| 192 | + return "E", true |
| 193 | + } |
| 194 | + return "", false |
| 195 | +} |
| 196 | +func (fakeResolver) Self(a string) (string, bool) { |
| 197 | + if a == "host" { |
| 198 | + return "h", true |
| 199 | + } |
| 200 | + return "", false |
| 201 | +} |
| 202 | +func (fakeResolver) Ref(path string) (string, error) { return "R:" + path, nil } |
| 203 | +func (fakeResolver) Profile() string { return "dev" } |
| 204 | +func (fakeResolver) WorkspaceName() string { return "acme" } |
| 205 | + |
| 206 | +func TestInterpolate(t *testing.T) { |
| 207 | + r := fakeResolver{} |
| 208 | + ok := []struct{ in, want string }{ |
| 209 | + {"${profile}", "dev"}, |
| 210 | + {"${workspace.name}", "acme"}, |
| 211 | + {"${env.FOO}", "E"}, |
| 212 | + {"${self.host}", "h"}, |
| 213 | + {"${ref:workspace.shared.postgres.host}", "R:workspace.shared.postgres.host"}, |
| 214 | + {"a$$b", "a$b"}, |
| 215 | + {"x${profile}y", "xdevy"}, |
| 216 | + {"no interpolation", "no interpolation"}, |
| 217 | + {"price is $5", "price is $5"}, |
| 218 | + } |
| 219 | + for _, c := range ok { |
| 220 | + got, err := Interpolate(c.in, r) |
| 221 | + if err != nil { |
| 222 | + t.Errorf("Interpolate(%q) error: %v", c.in, err) |
| 223 | + continue |
| 224 | + } |
| 225 | + if got != c.want { |
| 226 | + t.Errorf("Interpolate(%q) = %q, want %q", c.in, got, c.want) |
| 227 | + } |
| 228 | + } |
| 229 | + bad := []string{"${env.MISSING}", "${self.nope}", "${bogus}", "${unterminated", "${}"} |
| 230 | + for _, in := range bad { |
| 231 | + if _, err := Interpolate(in, r); err == nil { |
| 232 | + t.Errorf("Interpolate(%q) want error, got nil", in) |
| 233 | + } |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +func TestInterpolationRefs(t *testing.T) { |
| 238 | + got := InterpolationRefs("a ${ref:workspace.shared.pg.host} b ${profile} ${ref:workspace.api.x.port}") |
| 239 | + want := []string{"workspace.shared.pg.host", "workspace.api.x.port"} |
| 240 | + if len(got) != len(want) { |
| 241 | + t.Fatalf("refs = %v, want %v", got, want) |
| 242 | + } |
| 243 | + for i := range want { |
| 244 | + if got[i] != want[i] { |
| 245 | + t.Errorf("ref[%d] = %q, want %q", i, got[i], want[i]) |
| 246 | + } |
| 247 | + } |
| 248 | +} |
0 commit comments