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.

container_test.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package v6
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/fluxcd/flux/image"
  6. "github.com/fluxcd/flux/policy"
  7. "github.com/fluxcd/flux/update"
  8. )
  9. type justSlice []image.Info
  10. func (j justSlice) Images() []image.Info {
  11. return []image.Info(j)
  12. }
  13. func (j justSlice) SortedImages(p policy.Pattern) update.SortedImageInfos {
  14. return update.SortImages(j.Images(), p)
  15. }
  16. func TestNewContainer(t *testing.T) {
  17. testImage := image.Info{ImageID: "test"}
  18. currentSemver := image.Info{ID: image.Ref{Tag: "1.0.0"}}
  19. oldSemver := image.Info{ID: image.Ref{Tag: "0.9.0"}}
  20. newSemver := image.Info{ID: image.Ref{Tag: "1.2.3"}}
  21. type args struct {
  22. name string
  23. images []image.Info
  24. currentImage image.Info
  25. tagPattern policy.Pattern
  26. fields []string
  27. }
  28. tests := []struct {
  29. name string
  30. args args
  31. want Container
  32. wantErr bool
  33. }{
  34. {
  35. name: "Simple",
  36. args: args{
  37. name: "container1",
  38. images: []image.Info{testImage},
  39. currentImage: testImage,
  40. tagPattern: policy.PatternAll,
  41. },
  42. want: Container{
  43. Name: "container1",
  44. Current: testImage,
  45. LatestFiltered: testImage,
  46. Available: []image.Info{testImage},
  47. AvailableImagesCount: 1,
  48. NewAvailableImagesCount: 0,
  49. FilteredImagesCount: 1,
  50. NewFilteredImagesCount: 0,
  51. },
  52. wantErr: false,
  53. },
  54. {
  55. name: "Semver filtering and sorting",
  56. args: args{
  57. name: "container-semver",
  58. images: []image.Info{currentSemver, newSemver, oldSemver, testImage},
  59. currentImage: currentSemver,
  60. tagPattern: policy.NewPattern("semver:*"),
  61. },
  62. want: Container{
  63. Name: "container-semver",
  64. Current: currentSemver,
  65. LatestFiltered: newSemver,
  66. Available: []image.Info{newSemver, currentSemver, oldSemver, testImage},
  67. AvailableImagesCount: 4,
  68. NewAvailableImagesCount: 1,
  69. FilteredImagesCount: 3,
  70. NewFilteredImagesCount: 1,
  71. },
  72. wantErr: false,
  73. },
  74. {
  75. name: "Require only some calculations",
  76. args: args{
  77. name: "container-some",
  78. images: []image.Info{currentSemver, newSemver, oldSemver, testImage},
  79. currentImage: currentSemver,
  80. tagPattern: policy.NewPattern("semver:*"),
  81. fields: []string{"Name", "NewFilteredImagesCount"}, // but not, e.g., "FilteredImagesCount"
  82. },
  83. want: Container{
  84. Name: "container-some",
  85. NewFilteredImagesCount: 1,
  86. },
  87. },
  88. {
  89. name: "Fields in one order",
  90. args: args{
  91. name: "container-ordered1",
  92. images: []image.Info{currentSemver, newSemver, oldSemver, testImage},
  93. currentImage: currentSemver,
  94. tagPattern: policy.NewPattern("semver:*"),
  95. fields: []string{"Name",
  96. "AvailableImagesCount", "Available", // these two both depend on the same intermediate result
  97. "LatestFiltered", "FilteredImagesCount", // these two both depend on another intermediate result
  98. },
  99. },
  100. want: Container{
  101. Name: "container-ordered1",
  102. Available: []image.Info{newSemver, currentSemver, oldSemver, testImage},
  103. AvailableImagesCount: 4,
  104. LatestFiltered: newSemver,
  105. FilteredImagesCount: 3,
  106. },
  107. },
  108. {
  109. name: "Fields in another order",
  110. args: args{
  111. name: "container-ordered2",
  112. images: []image.Info{currentSemver, newSemver, oldSemver, testImage},
  113. currentImage: currentSemver,
  114. tagPattern: policy.NewPattern("semver:*"),
  115. fields: []string{"Name",
  116. "Available", "AvailableImagesCount", // these two latter depend on the same intermediate result, as above
  117. "FilteredImagesCount", "LatestFiltered", // as above, similarly
  118. },
  119. },
  120. want: Container{
  121. Name: "container-ordered2",
  122. Available: []image.Info{newSemver, currentSemver, oldSemver, testImage},
  123. AvailableImagesCount: 4,
  124. LatestFiltered: newSemver,
  125. FilteredImagesCount: 3,
  126. },
  127. },
  128. }
  129. for _, tt := range tests {
  130. t.Run(tt.name, func(t *testing.T) {
  131. got, err := NewContainer(tt.args.name, justSlice(tt.args.images), tt.args.currentImage, tt.args.tagPattern, tt.args.fields)
  132. assert.Equal(t, tt.wantErr, err != nil)
  133. assert.Equal(t, tt.want, got)
  134. })
  135. }
  136. }