Browse Source

Test image map

Michael Bridgen 2 years ago
parent
commit
2d08ec5f55
1 changed files with 66 additions and 0 deletions
  1. 66
    0
      update/images_test.go

+ 66
- 0
update/images_test.go View File

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

Loading…
Cancel
Save