GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

release_cmd_test.go 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package main //+integration
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "testing"
  6. "github.com/fluxcd/flux/resource"
  7. "github.com/fluxcd/flux/update"
  8. )
  9. func TestReleaseCommand_CLIConversion(t *testing.T) {
  10. for _, v := range []struct {
  11. args []string
  12. expectedSpec update.ReleaseImageSpec
  13. }{
  14. {[]string{"--update-all-images", "--all"}, update.ReleaseImageSpec{
  15. ServiceSpecs: []update.ResourceSpec{update.ResourceSpecAll},
  16. ImageSpec: update.ImageSpecLatest,
  17. Kind: update.ReleaseKindExecute,
  18. }},
  19. {[]string{"--update-all-images", "--all", "--dry-run"}, update.ReleaseImageSpec{
  20. ServiceSpecs: []update.ResourceSpec{update.ResourceSpecAll},
  21. ImageSpec: update.ImageSpecLatest,
  22. Kind: update.ReleaseKindPlan,
  23. }},
  24. {[]string{"--update-image=alpine:latest", "--all"}, update.ReleaseImageSpec{
  25. ServiceSpecs: []update.ResourceSpec{update.ResourceSpecAll},
  26. ImageSpec: "alpine:latest",
  27. Kind: update.ReleaseKindExecute,
  28. }},
  29. {[]string{"--update-all-images", "--workload=deployment/flux"}, update.ReleaseImageSpec{
  30. ServiceSpecs: []update.ResourceSpec{"default:deployment/flux"},
  31. ImageSpec: update.ImageSpecLatest,
  32. Kind: update.ReleaseKindExecute,
  33. }},
  34. {[]string{"--update-all-images", "--all", "--exclude=deployment/test,deployment/yeah"}, update.ReleaseImageSpec{
  35. ServiceSpecs: []update.ResourceSpec{update.ResourceSpecAll},
  36. ImageSpec: update.ImageSpecLatest,
  37. Kind: update.ReleaseKindExecute,
  38. Excludes: []resource.ID{
  39. resource.MustParseID("default:deployment/test"),
  40. resource.MustParseID("default:deployment/yeah"),
  41. },
  42. }},
  43. } {
  44. svc := testArgs(t, v.args, false, "")
  45. // Check that UpdateManifests was called with correct body
  46. method := "UpdateManifests"
  47. if svc.calledURL(method) == nil {
  48. t.Fatalf("Expecting fluxctl to request %q, but did not.", method)
  49. }
  50. r := svc.calledRequest(method)
  51. var actualSpec update.Spec
  52. if err := json.NewDecoder(r.Body).Decode(&actualSpec); err != nil {
  53. t.Fatal("Failed to decode spec")
  54. }
  55. if !reflect.DeepEqual(v.expectedSpec, actualSpec.Spec) {
  56. t.Fatalf("Expected %#v but got %#v", v.expectedSpec, actualSpec.Spec)
  57. }
  58. // Check that GetRelease was polled for status
  59. method = "JobStatus"
  60. if svc.calledURL(method) == nil {
  61. t.Fatalf("Expecting fluxctl to request %q, but did not.", method)
  62. }
  63. }
  64. }
  65. func TestReleaseCommand_InputFailures(t *testing.T) {
  66. for _, v := range []struct {
  67. args []string
  68. msg string
  69. }{
  70. {[]string{}, "Should error when no args"},
  71. {[]string{"--all"}, "Should error when not specifying image spec"},
  72. {[]string{"--all", "--update-image=alpine"}, "Should error with invalid image spec"},
  73. {[]string{"--update-all-images"}, "Should error when not specifying workload spec"},
  74. {[]string{"--workload=invalid&workload", "--update-all-images"}, "Should error with invalid workload"},
  75. {[]string{"subcommand"}, "Should error when given subcommand"},
  76. } {
  77. testArgs(t, v.args, true, v.msg)
  78. }
  79. }