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.

print_test.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package update
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/fluxcd/flux/image"
  6. "github.com/fluxcd/flux/resource"
  7. )
  8. func mustParseRef(s string) image.Ref {
  9. ref, err := image.ParseRef(s)
  10. if err != nil {
  11. panic(err)
  12. }
  13. return ref
  14. }
  15. func TestPrintResults(t *testing.T) {
  16. for _, example := range []struct {
  17. name string
  18. result Result
  19. verbosity int
  20. expected string
  21. }{
  22. {
  23. name: "basic, just results",
  24. result: Result{
  25. resource.MustParseID("default/helloworld"): WorkloadResult{
  26. Status: ReleaseStatusSuccess,
  27. Error: "",
  28. PerContainer: []ContainerUpdate{
  29. {
  30. Container: "helloworld",
  31. Current: mustParseRef("quay.io/weaveworks/helloworld:master-a000002"),
  32. Target: mustParseRef("quay.io/weaveworks/helloworld:master-a000001"),
  33. },
  34. },
  35. },
  36. },
  37. expected: `
  38. WORKLOAD STATUS UPDATES
  39. default/helloworld success helloworld: quay.io/weaveworks/helloworld:master-a000002 -> master-a000001
  40. `,
  41. },
  42. {
  43. name: "With an error, *and* results",
  44. result: Result{
  45. resource.MustParseID("default/helloworld"): WorkloadResult{
  46. Status: ReleaseStatusSuccess,
  47. Error: "test error",
  48. PerContainer: []ContainerUpdate{
  49. {
  50. Container: "helloworld",
  51. Current: mustParseRef("quay.io/weaveworks/helloworld:master-a000002"),
  52. Target: mustParseRef("quay.io/weaveworks/helloworld:master-a000001"),
  53. },
  54. },
  55. },
  56. },
  57. expected: `
  58. WORKLOAD STATUS UPDATES
  59. default/helloworld success test error
  60. helloworld: quay.io/weaveworks/helloworld:master-a000002 -> master-a000001
  61. `,
  62. },
  63. {
  64. name: "Service results should be sorted",
  65. result: Result{
  66. resource.MustParseID("default/d"): WorkloadResult{Status: ReleaseStatusSuccess},
  67. resource.MustParseID("default/c"): WorkloadResult{Status: ReleaseStatusSuccess},
  68. resource.MustParseID("default/b"): WorkloadResult{Status: ReleaseStatusSuccess},
  69. resource.MustParseID("default/a"): WorkloadResult{Status: ReleaseStatusSuccess},
  70. },
  71. expected: `
  72. WORKLOAD STATUS UPDATES
  73. default/a success
  74. default/b success
  75. default/c success
  76. default/d success
  77. `,
  78. },
  79. } {
  80. out := &bytes.Buffer{}
  81. out.WriteString("\n") // All our "expected" values start with a newline, to make maintaining them easier.
  82. PrintResults(out, example.result, example.verbosity)
  83. if out.String() != example.expected {
  84. t.Errorf(
  85. "Name: %s\nPrintResults(out, %#v, %v)\nExpected\n-------%s-------\nGot\n-------%s-------",
  86. example.name,
  87. example.result,
  88. example.verbosity,
  89. example.expected,
  90. out.String(),
  91. )
  92. }
  93. }
  94. }