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.

cache_test.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // +build integration
  2. package registry
  3. import (
  4. "flag"
  5. "os"
  6. "strings"
  7. "testing"
  8. "time"
  9. "encoding/json"
  10. "github.com/bradfitz/gomemcache/memcache"
  11. "github.com/go-kit/kit/log"
  12. registryMemcache "github.com/weaveworks/flux/registry/memcache"
  13. )
  14. var (
  15. memcachedIPs = flag.String("memcached-ips", "127.0.0.1:11211", "space-separated host:port values for memcached to connect to")
  16. )
  17. type stoppableMemcacheClient struct {
  18. *memcache.Client
  19. }
  20. func (s *stoppableMemcacheClient) Stop() {}
  21. // Setup sets up stuff for testing
  22. func Setup(t *testing.T) registryMemcache.MemcacheClient {
  23. mc := memcache.New(strings.Fields(*memcachedIPs)...)
  24. if err := mc.FlushAll(); err != nil {
  25. t.Fatal(err)
  26. }
  27. return &stoppableMemcacheClient{mc}
  28. }
  29. func TestCache_Manifests(t *testing.T) {
  30. mc := Setup(t)
  31. defer mc.Stop()
  32. creds := NoCredentials()
  33. c := NewCache(
  34. creds,
  35. mc,
  36. 20*time.Minute,
  37. log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)),
  38. )
  39. r, _ := ParseRepository("index.docker.io/weaveworks/foorepo")
  40. img := r.ToImage("tag1")
  41. val, _ := json.Marshal(img)
  42. key := manifestKey(creds.credsFor(r.Host()).username, r.String(), img.ID.Tag)
  43. if err := mc.Set(&memcache.Item{
  44. Key: key,
  45. Value: val,
  46. Expiration: int32(time.Hour.Seconds()),
  47. }); err != nil {
  48. t.Fatal(err)
  49. }
  50. // It should fetch stuff
  51. response, err := c.Manifest(r, img.ID.Tag)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. if response.ID.String() == "" {
  56. t.Fatal("Should have returned image")
  57. }
  58. r2, _ := ParseRepository("index.docker.io/weaveworks/another")
  59. // It should miss if not in cache
  60. _, err = c.Manifest(r2, "tag1")
  61. if err != memcache.ErrCacheMiss {
  62. t.Fatal("Expected cache miss")
  63. }
  64. }
  65. func TestCache_Tags(t *testing.T) {
  66. mc := Setup(t)
  67. creds := NoCredentials()
  68. c := NewCache(
  69. creds,
  70. mc,
  71. 20*time.Minute,
  72. log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)),
  73. )
  74. r, _ := ParseRepository("index.docker.io/weaveworks/foorepo")
  75. val, _ := json.Marshal([]string{"tag1", "tag2"})
  76. key := tagKey(creds.credsFor(r.Host()).username, r.String())
  77. if err := mc.Set(&memcache.Item{
  78. Key: key,
  79. Value: val,
  80. Expiration: int32(time.Hour.Seconds()),
  81. }); err != nil {
  82. t.Fatal(err)
  83. }
  84. // It should fetch stuff
  85. response, err := c.Tags(r)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. if len(response) != 2 {
  90. t.Fatalf("Expected 2 tags item, got %v", response)
  91. }
  92. expected := "tag1"
  93. if response[0] != expected {
  94. t.Fatalf("Expected history item: %v, got %v", expected, response[0])
  95. }
  96. r2, _ := ParseRepository("index.docker.io/weaveworks/anotherrepo")
  97. // It should miss if not in cache
  98. _, err = c.Tags(r2)
  99. if err != memcache.ErrCacheMiss {
  100. t.Fatal("Expected cache miss")
  101. }
  102. }