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 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package update
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/weaveworks/flux/image"
  6. )
  7. var (
  8. name = mustParseName("index.docker.io/weaveworks/helloworld").CanonicalName()
  9. infos = []image.Info{
  10. {ID: name.ToRef("v1"), CreatedAt: time.Now().Add(-time.Hour)},
  11. {ID: name.ToRef("v2"), CreatedAt: time.Now()},
  12. }
  13. )
  14. // TestDecanon checks that we return appropriate image names when
  15. // asked for images. The registry (cache) stores things with canonical
  16. // names (e.g., `index.docker.io/library/alpine`), but we ask
  17. // questions in terms of everyday names (e.g., `alpine`).
  18. func TestDecanon(t *testing.T) {
  19. m := ImageRepos{imageReposMap{
  20. name: infos,
  21. }}
  22. filteredImages := m.GetRepoImages(mustParseName("weaveworks/helloworld")).Filter("*")
  23. latest, ok := filteredImages.Latest()
  24. if !ok {
  25. t.Error("did not find latest image")
  26. } else if latest.ID.Name != mustParseName("weaveworks/helloworld") {
  27. t.Error("name did not match what was asked")
  28. }
  29. filteredImages = m.GetRepoImages(mustParseName("index.docker.io/weaveworks/helloworld")).Filter("*")
  30. latest, ok = filteredImages.Latest()
  31. if !ok {
  32. t.Error("did not find latest image")
  33. } else if latest.ID.Name != mustParseName("index.docker.io/weaveworks/helloworld") {
  34. t.Error("name did not match what was asked")
  35. }
  36. avail := m.GetRepoImages(mustParseName("weaveworks/helloworld"))
  37. if len(avail) != len(infos) {
  38. t.Errorf("expected %d available images, got %d", len(infos), len(avail))
  39. }
  40. for _, im := range avail {
  41. if im.ID.Name != mustParseName("weaveworks/helloworld") {
  42. t.Errorf("got image with name %q", im.ID.String())
  43. }
  44. }
  45. }
  46. func TestAvail(t *testing.T) {
  47. m := ImageRepos{imageReposMap{name: infos}}
  48. avail := m.GetRepoImages(mustParseName("weaveworks/goodbyeworld"))
  49. if len(avail) > 0 {
  50. t.Errorf("did not expect available images, but got %#v", avail)
  51. }
  52. }
  53. func mustParseName(im string) image.Name {
  54. ref, err := image.ParseRef(im)
  55. if err != nil {
  56. panic(err)
  57. }
  58. return ref.Name
  59. }