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.

warming_test.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // +build integration
  2. package registry
  3. import (
  4. "encoding/json"
  5. "github.com/go-kit/kit/log"
  6. "github.com/weaveworks/flux"
  7. "os"
  8. "sync"
  9. "testing"
  10. "time"
  11. )
  12. func TestWarmer_CacheNewRepo(t *testing.T) {
  13. mc := Setup(t)
  14. defer mc.Stop()
  15. dc := NewMockClient(
  16. func(repository Repository, tag string) (flux.Image, error) {
  17. return img, nil
  18. },
  19. func(repository Repository) ([]string, error) {
  20. return []string{"tag1"}, nil
  21. },
  22. )
  23. w := Warmer{
  24. Logger: log.NewLogfmtLogger(os.Stderr),
  25. ClientFactory: &mockRemoteFactory{c: dc},
  26. Creds: NoCredentials(),
  27. Expiry: time.Hour,
  28. Client: mc,
  29. }
  30. shutdown := make(chan struct{})
  31. repo := make(chan Repository)
  32. shutdownWg := &sync.WaitGroup{}
  33. shutdownWg.Add(1)
  34. go w.Loop(shutdown, shutdownWg, repo)
  35. r, _ := ParseRepository("test/repo")
  36. repo <- r
  37. shutdown <- struct{}{}
  38. shutdownWg.Wait()
  39. // Test that tags were written
  40. key := tagKey("", r.String())
  41. item, err := mc.Get(key)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. var tags []string
  46. err = json.Unmarshal(item.Value, &tags)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. if len(tags) != 1 {
  51. t.Fatalf("Expected 1 history item, got %v", tags)
  52. }
  53. expectedTag := "tag1"
  54. if tags[0] != expectedTag {
  55. t.Fatalf("Expected history item: %v, got %v", expectedTag, tags[0])
  56. }
  57. // Test that manifest was written
  58. key = manifestKey("", r.String(), "tag1")
  59. item, err = mc.Get(key)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. var i flux.Image
  64. err = json.Unmarshal(item.Value, &i)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. if i.ID.String() != img.ID.String() {
  69. t.Fatalf("Expected %s, got %s", img.ID.String(), i.ID.String())
  70. }
  71. }
  72. func TestQueue_Usage(t *testing.T) {
  73. queue := NewQueue(
  74. func() []Repository {
  75. r, _ := ParseRepository("test/image")
  76. return []Repository{r}
  77. },
  78. log.NewLogfmtLogger(os.Stderr),
  79. 1*time.Millisecond,
  80. )
  81. shutdown := make(chan struct{})
  82. shutdownWg := &sync.WaitGroup{}
  83. shutdownWg.Add(1)
  84. go queue.Loop(shutdown, shutdownWg)
  85. defer func() {
  86. shutdown <- struct{}{}
  87. shutdownWg.Wait()
  88. }()
  89. time.Sleep(10 * time.Millisecond)
  90. if len(queue.Queue()) == 0 {
  91. t.Fatal("Should have randomly added containers to queue")
  92. }
  93. }
  94. func TestQueue_NoContainers(t *testing.T) {
  95. queue := NewQueue(
  96. func() []Repository {
  97. return []Repository{}
  98. },
  99. log.NewLogfmtLogger(os.Stderr),
  100. 1*time.Millisecond,
  101. )
  102. shutdown := make(chan struct{})
  103. shutdownWg := &sync.WaitGroup{}
  104. shutdownWg.Add(1)
  105. go queue.Loop(shutdown, shutdownWg)
  106. defer func() {
  107. shutdown <- struct{}{}
  108. shutdownWg.Wait()
  109. }()
  110. time.Sleep(10 * time.Millisecond)
  111. if len(queue.Queue()) != 0 {
  112. t.Fatal("There were no containers, so there should be no repositories in the queue")
  113. }
  114. }