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.

image_test.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package flux
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "sort"
  6. "strconv"
  7. "testing"
  8. "time"
  9. )
  10. const constTime = "2017-01-13T16:22:58.009923189Z"
  11. var (
  12. testTime, _ = time.Parse(time.RFC3339Nano, constTime)
  13. )
  14. func TestImageID_ParseImageID(t *testing.T) {
  15. for _, x := range []struct {
  16. test string
  17. expected string
  18. }{
  19. {"alpine", "alpine:latest"},
  20. {"library/alpine", "alpine:latest"},
  21. {"alpine:mytag", "alpine:mytag"},
  22. {"quay.io/library/alpine", "quay.io/library/alpine:latest"},
  23. {"quay.io/library/alpine:latest", "quay.io/library/alpine:latest"},
  24. {"quay.io/library/alpine:mytag", "quay.io/library/alpine:mytag"},
  25. } {
  26. i, err := ParseImageID(x.test)
  27. if err != nil {
  28. t.Fatalf("Failed parsing %q, expected %q", x.test, x.expected)
  29. }
  30. if i.String() != x.expected {
  31. t.Fatalf("%q does not match expected %q", i.String(), x.expected)
  32. }
  33. }
  34. }
  35. func TestImageID_ParseImageIDErrorCases(t *testing.T) {
  36. for _, x := range []struct {
  37. test string
  38. }{
  39. {""},
  40. {":tag"},
  41. {"alpine::"},
  42. {"alpine:invalid:"},
  43. {"/too/many/slashes/"},
  44. } {
  45. _, err := ParseImageID(x.test)
  46. if err == nil {
  47. t.Fatalf("Expected parse failure for %q", x.test)
  48. }
  49. }
  50. }
  51. func TestImageID_TestComponents(t *testing.T) {
  52. host := "quay.io"
  53. namespace := "namespace"
  54. image := "myrepo"
  55. tag := "mytag"
  56. fqn := fmt.Sprintf("%v/%v/%v:%v", host, namespace, image, tag)
  57. i, err := ParseImageID(fqn)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. for _, x := range []struct {
  62. test string
  63. expected string
  64. }{
  65. {i.Host, host},
  66. {i.Namespace, namespace},
  67. {i.Image, image},
  68. {i.Tag, tag},
  69. {i.String(), fqn},
  70. } {
  71. if x.test != x.expected {
  72. t.Fatalf("Expected %v, but got %v", x.expected, x.test)
  73. }
  74. }
  75. }
  76. func TestImageID_Serialization(t *testing.T) {
  77. for _, x := range []struct {
  78. test ImageID
  79. expected string
  80. }{
  81. {ImageID{Host: dockerHubHost, Namespace: dockerHubLibrary, Image: "alpine", Tag: "a123"}, `"alpine:a123"`},
  82. {ImageID{Host: "quay.io", Namespace: "weaveworks", Image: "foobar", Tag: "baz"}, `"quay.io/weaveworks/foobar:baz"`},
  83. } {
  84. serialized, err := json.Marshal(x.test)
  85. if err != nil {
  86. t.Fatalf("Error encoding %v: %v", x.test, err)
  87. }
  88. if string(serialized) != x.expected {
  89. t.Fatalf("Encoded %v as %s, but expected %s", x.test, string(serialized), x.expected)
  90. }
  91. var decoded ImageID
  92. if err := json.Unmarshal([]byte(x.expected), &decoded); err != nil {
  93. t.Fatalf("Error decoding %v: %v", x.expected, err)
  94. }
  95. if decoded != x.test {
  96. t.Fatalf("Decoded %s as %v, but expected %v", x.expected, decoded, x.test)
  97. }
  98. }
  99. }
  100. func TestImage_OrderByCreationDate(t *testing.T) {
  101. fmt.Printf("testTime: %s\n", testTime)
  102. time0 := testTime.Add(time.Second)
  103. time2 := testTime.Add(-time.Second)
  104. imA, _ := ParseImage("my/Image:3", testTime)
  105. imB, _ := ParseImage("my/Image:1", time0)
  106. imC, _ := ParseImage("my/Image:4", time2)
  107. imD, _ := ParseImage("my/Image:0", time.Time{}) // test nil
  108. imE, _ := ParseImage("my/Image:2", testTime) // test equal
  109. imgs := []Image{imA, imB, imC, imD, imE}
  110. sort.Sort(ByCreatedDesc(imgs))
  111. for i, im := range imgs {
  112. if strconv.Itoa(i) != im.ID.Tag {
  113. for j, jim := range imgs {
  114. t.Logf("%v: %v %s", j, jim.ID.String(), jim.CreatedAt)
  115. }
  116. t.Fatalf("Not sorted in expected order: %#v", imgs)
  117. }
  118. }
  119. }