|
6 | 6 | "os"
|
7 | 7 | "testing"
|
8 | 8 |
|
| 9 | + yaml3 "gopkg.in/yaml.v3" |
9 | 10 | v1 "k8s.io/api/apps/v1"
|
10 | 11 | corev1 "k8s.io/api/core/v1"
|
11 | 12 | "sigs.k8s.io/yaml"
|
@@ -324,14 +325,140 @@ services:
|
324 | 325 | Environment map[string]string `yaml:"environment"`
|
325 | 326 | } `yaml:"web"`
|
326 | 327 | }{}
|
327 |
| - if err := yaml.Unmarshal(valuesContent, &mapping); err != nil { |
| 328 | + if err := yaml3.Unmarshal(valuesContent, &mapping); err != nil { |
328 | 329 | t.Errorf(unmarshalError, err)
|
329 | 330 | }
|
330 | 331 |
|
331 |
| - if _, ok := mapping.Web.Environment["FOO"]; !ok { |
| 332 | + if v, ok := mapping.Web.Environment["FOO"]; !ok { |
332 | 333 | t.Errorf("Expected FOO in web environment")
|
| 334 | + if v != "bar" { |
| 335 | + t.Errorf("Expected FOO to be bar, got %s", v) |
| 336 | + } |
333 | 337 | }
|
334 |
| - if _, ok := mapping.Web.Environment["BAZ"]; ok { |
| 338 | + if v, ok := mapping.Web.Environment["BAZ"]; ok { |
335 | 339 | t.Errorf("Expected BAZ not in web environment")
|
| 340 | + if v != "qux" { |
| 341 | + t.Errorf("Expected BAZ to be qux, got %s", v) |
| 342 | + } |
| 343 | + } |
| 344 | +} |
| 345 | + |
| 346 | +func TestWithDashes(t *testing.T) { |
| 347 | + composeFile := ` |
| 348 | +services: |
| 349 | + web-app: |
| 350 | + image: nginx:1.29 |
| 351 | + environment: |
| 352 | + FOO: BAR |
| 353 | + labels: |
| 354 | + %s/values: | |
| 355 | + - FOO |
| 356 | +` |
| 357 | + |
| 358 | + composeFile = fmt.Sprintf(composeFile, labels.Prefix()) |
| 359 | + tmpDir := setup(composeFile) |
| 360 | + defer teardown(tmpDir) |
| 361 | + |
| 362 | + currentDir, _ := os.Getwd() |
| 363 | + os.Chdir(tmpDir) |
| 364 | + defer os.Chdir(currentDir) |
| 365 | + |
| 366 | + output := internalCompileTest(t, "-s", "templates/web_app/deployment.yaml") |
| 367 | + dt := v1.Deployment{} |
| 368 | + if err := yaml.Unmarshal([]byte(output), &dt); err != nil { |
| 369 | + t.Errorf(unmarshalError, err) |
| 370 | + } |
| 371 | + |
| 372 | + valuesFile := "./chart/values.yaml" |
| 373 | + if _, err := os.Stat(valuesFile); os.IsNotExist(err) { |
| 374 | + t.Errorf("values.yaml does not exist") |
| 375 | + } |
| 376 | + valuesContent, err := os.ReadFile(valuesFile) |
| 377 | + if err != nil { |
| 378 | + t.Errorf("Error reading values.yaml: %s", err) |
| 379 | + } |
| 380 | + mapping := struct { |
| 381 | + Web struct { |
| 382 | + Environment map[string]string `yaml:"environment"` |
| 383 | + } `yaml:"web_app"` |
| 384 | + }{} |
| 385 | + if err := yaml3.Unmarshal(valuesContent, &mapping); err != nil { |
| 386 | + t.Errorf(unmarshalError, err) |
| 387 | + } |
| 388 | + |
| 389 | + // we must have FOO in web_app environment (not web-app) |
| 390 | + // this validates that the service name is converted to a valid k8s name |
| 391 | + if v, ok := mapping.Web.Environment["FOO"]; !ok { |
| 392 | + t.Errorf("Expected FOO in web_app environment") |
| 393 | + if v != "BAR" { |
| 394 | + t.Errorf("Expected FOO to be BAR, got %s", v) |
| 395 | + } |
| 396 | + } |
| 397 | +} |
| 398 | + |
| 399 | +func TestDashesWithValueFrom(t *testing.T) { |
| 400 | + composeFile := ` |
| 401 | +services: |
| 402 | + web-app: |
| 403 | + image: nginx:1.29 |
| 404 | + environment: |
| 405 | + FOO: BAR |
| 406 | + labels: |
| 407 | + %[1]s/values: | |
| 408 | + - FOO |
| 409 | + web2: |
| 410 | + image: nginx:1.29 |
| 411 | + labels: |
| 412 | + %[1]s/values-from: | |
| 413 | + BAR: web-app.FOO |
| 414 | +` |
| 415 | + |
| 416 | + composeFile = fmt.Sprintf(composeFile, labels.Prefix()) |
| 417 | + tmpDir := setup(composeFile) |
| 418 | + defer teardown(tmpDir) |
| 419 | + |
| 420 | + currentDir, _ := os.Getwd() |
| 421 | + os.Chdir(tmpDir) |
| 422 | + defer os.Chdir(currentDir) |
| 423 | + |
| 424 | + output := internalCompileTest(t, "-s", "templates/web2/deployment.yaml") |
| 425 | + dt := v1.Deployment{} |
| 426 | + if err := yaml.Unmarshal([]byte(output), &dt); err != nil { |
| 427 | + t.Errorf(unmarshalError, err) |
| 428 | + } |
| 429 | + |
| 430 | + valuesFile := "./chart/values.yaml" |
| 431 | + if _, err := os.Stat(valuesFile); os.IsNotExist(err) { |
| 432 | + t.Errorf("values.yaml does not exist") |
| 433 | + } |
| 434 | + valuesContent, err := os.ReadFile(valuesFile) |
| 435 | + if err != nil { |
| 436 | + t.Errorf("Error reading values.yaml: %s", err) |
| 437 | + } |
| 438 | + mapping := struct { |
| 439 | + Web struct { |
| 440 | + Environment map[string]string `yaml:"environment"` |
| 441 | + } `yaml:"web_app"` |
| 442 | + }{} |
| 443 | + if err := yaml3.Unmarshal(valuesContent, &mapping); err != nil { |
| 444 | + t.Errorf(unmarshalError, err) |
| 445 | + } |
| 446 | + |
| 447 | + // we must have FOO in web_app environment (not web-app) |
| 448 | + // this validates that the service name is converted to a valid k8s name |
| 449 | + if v, ok := mapping.Web.Environment["FOO"]; !ok { |
| 450 | + t.Errorf("Expected FOO in web_app environment") |
| 451 | + if v != "BAR" { |
| 452 | + t.Errorf("Expected FOO to be BAR, got %s", v) |
| 453 | + } |
| 454 | + } |
| 455 | + |
| 456 | + // ensure that the deployment has the value from the other service |
| 457 | + barenv := dt.Spec.Template.Spec.Containers[0].Env[0] |
| 458 | + if barenv.Value != "" { |
| 459 | + t.Errorf("Expected value to be empty") |
| 460 | + } |
| 461 | + if barenv.ValueFrom == nil { |
| 462 | + t.Errorf("Expected valueFrom to be set") |
336 | 463 | }
|
337 | 464 | }
|
0 commit comments