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.

images_test.go 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package daemon
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/go-kit/kit/log"
  6. "github.com/fluxcd/flux/cluster"
  7. "github.com/fluxcd/flux/image"
  8. "github.com/fluxcd/flux/policy"
  9. "github.com/fluxcd/flux/registry"
  10. registryMock "github.com/fluxcd/flux/registry/mock"
  11. "github.com/fluxcd/flux/resource"
  12. "github.com/fluxcd/flux/update"
  13. )
  14. const (
  15. container1 = "container1"
  16. container2 = "container2"
  17. container3 = "container3"
  18. currentContainer1Image = "container1/application:current"
  19. newContainer1Image = "container1/application:new"
  20. currentContainer2Image = "container2/application:current"
  21. newContainer2Image = "container2/application:new"
  22. noTagContainer2Image = "container2/application"
  23. currentContainer3Image = "container3/application:1.0.0"
  24. newContainer3Image = "container3/application:1.1.0"
  25. )
  26. type candidate struct {
  27. resourceID resource.ID
  28. policies policy.Set
  29. }
  30. func (c candidate) ResourceID() resource.ID {
  31. return c.resourceID
  32. }
  33. func (c candidate) Policies() policy.Set {
  34. return c.policies
  35. }
  36. func (candidate) Source() string {
  37. return ""
  38. }
  39. func (candidate) Bytes() []byte {
  40. return []byte{}
  41. }
  42. func TestCalculateChanges_Automated(t *testing.T) {
  43. logger := log.NewNopLogger()
  44. resourceID := resource.MakeID(ns, "deployment", "application")
  45. candidateWorkloads := resources{
  46. resourceID: candidate{
  47. resourceID: resourceID,
  48. policies: policy.Set{
  49. policy.Automated: "true",
  50. },
  51. },
  52. }
  53. workloads := []cluster.Workload{
  54. cluster.Workload{
  55. ID: resourceID,
  56. Containers: cluster.ContainersOrExcuse{
  57. Containers: []resource.Container{
  58. {
  59. Name: container1,
  60. Image: mustParseImageRef(currentContainer1Image),
  61. },
  62. },
  63. },
  64. },
  65. }
  66. var imageRegistry registry.Registry
  67. {
  68. current := makeImageInfo(currentContainer1Image, time.Now())
  69. new := makeImageInfo(newContainer1Image, time.Now().Add(1*time.Second))
  70. imageRegistry = &registryMock.Registry{
  71. Images: []image.Info{
  72. current,
  73. new,
  74. },
  75. }
  76. }
  77. imageRepos, err := update.FetchImageRepos(imageRegistry, clusterContainers(workloads), logger)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. changes := calculateChanges(logger, candidateWorkloads, workloads, imageRepos)
  82. if len := len(changes.Changes); len != 1 {
  83. t.Errorf("Expected exactly 1 change, got %d changes", len)
  84. } else if newImage := changes.Changes[0].ImageID.String(); newImage != newContainer1Image {
  85. t.Errorf("Expected changed image to be %s, got %s", newContainer1Image, newImage)
  86. }
  87. }
  88. func TestCalculateChanges_UntaggedImage(t *testing.T) {
  89. logger := log.NewNopLogger()
  90. resourceID := resource.MakeID(ns, "deployment", "application")
  91. candidateWorkloads := resources{
  92. resourceID: candidate{
  93. resourceID: resourceID,
  94. policies: policy.Set{
  95. policy.Automated: "true",
  96. },
  97. },
  98. }
  99. workloads := []cluster.Workload{
  100. cluster.Workload{
  101. ID: resourceID,
  102. Containers: cluster.ContainersOrExcuse{
  103. Containers: []resource.Container{
  104. {
  105. Name: container1,
  106. Image: mustParseImageRef(currentContainer1Image),
  107. },
  108. {
  109. Name: container2,
  110. Image: mustParseImageRef(currentContainer2Image),
  111. },
  112. },
  113. },
  114. },
  115. }
  116. var imageRegistry registry.Registry
  117. {
  118. current1 := makeImageInfo(currentContainer1Image, time.Now())
  119. new1 := makeImageInfo(newContainer1Image, time.Now().Add(1*time.Second))
  120. current2 := makeImageInfo(currentContainer2Image, time.Now())
  121. noTag2 := makeImageInfo(noTagContainer2Image, time.Now().Add(1*time.Second))
  122. imageRegistry = &registryMock.Registry{
  123. Images: []image.Info{
  124. current1,
  125. new1,
  126. current2,
  127. noTag2,
  128. },
  129. }
  130. }
  131. imageRepos, err := update.FetchImageRepos(imageRegistry, clusterContainers(workloads), logger)
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. changes := calculateChanges(logger, candidateWorkloads, workloads, imageRepos)
  136. if len := len(changes.Changes); len != 1 {
  137. t.Errorf("Expected exactly 1 change, got %d changes", len)
  138. } else if newImage := changes.Changes[0].ImageID.String(); newImage != newContainer1Image {
  139. t.Errorf("Expected changed image to be %s, got %s", newContainer1Image, newImage)
  140. }
  141. }
  142. func TestCalculateChanges_ZeroTimestamp(t *testing.T) {
  143. logger := log.NewNopLogger()
  144. resourceID := resource.MakeID(ns, "deployment", "application")
  145. candidateWorkloads := resources{
  146. resourceID: candidate{
  147. resourceID: resourceID,
  148. policies: policy.Set{
  149. policy.Automated: "true",
  150. policy.TagPrefix(container3): "semver:^1.0",
  151. },
  152. },
  153. }
  154. workloads := []cluster.Workload{
  155. cluster.Workload{
  156. ID: resourceID,
  157. Containers: cluster.ContainersOrExcuse{
  158. Containers: []resource.Container{
  159. {
  160. Name: container1,
  161. Image: mustParseImageRef(currentContainer1Image),
  162. },
  163. {
  164. Name: container2,
  165. Image: mustParseImageRef(currentContainer2Image),
  166. },
  167. {
  168. Name: container3,
  169. Image: mustParseImageRef(currentContainer3Image),
  170. },
  171. },
  172. },
  173. },
  174. }
  175. var imageRegistry registry.Registry
  176. {
  177. current1 := makeImageInfo(currentContainer1Image, time.Now())
  178. new1 := makeImageInfo(newContainer1Image, time.Now().Add(1*time.Second))
  179. zeroTimestampCurrent2 := image.Info{ID: mustParseImageRef(currentContainer2Image)}
  180. new2 := makeImageInfo(newContainer2Image, time.Now().Add(1*time.Second))
  181. current3 := makeImageInfo(currentContainer3Image, time.Now())
  182. zeroTimestampNew3 := image.Info{ID: mustParseImageRef(newContainer3Image)}
  183. imageRegistry = &registryMock.Registry{
  184. Images: []image.Info{
  185. current1,
  186. new1,
  187. zeroTimestampCurrent2,
  188. new2,
  189. current3,
  190. zeroTimestampNew3,
  191. },
  192. }
  193. }
  194. imageRepos, err := update.FetchImageRepos(imageRegistry, clusterContainers(workloads), logger)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. changes := calculateChanges(logger, candidateWorkloads, workloads, imageRepos)
  199. if len := len(changes.Changes); len != 2 {
  200. t.Fatalf("Expected exactly 2 changes, got %d changes: %v", len, changes.Changes)
  201. }
  202. if newImage := changes.Changes[0].ImageID.String(); newImage != newContainer1Image {
  203. t.Errorf("Expected changed image to be %s, got %s", newContainer1Image, newImage)
  204. }
  205. if newImage := changes.Changes[1].ImageID.String(); newImage != newContainer3Image {
  206. t.Errorf("Expected changed image to be %s, got %s", newContainer3Image, newImage)
  207. }
  208. }