Browse code

Increase the tests coverage for import-image command

Maciej Szulik authored on 2016/06/03 05:42:08
Showing 1 changed files
... ...
@@ -13,88 +13,151 @@ import (
13 13
 )
14 14
 
15 15
 func TestCreateImageImport(t *testing.T) {
16
-	testCases := []struct {
16
+	testCases := map[string]struct {
17 17
 		name     string
18
+		from     string
18 19
 		stream   *imageapi.ImageStream
19 20
 		all      bool
21
+		confirm  bool
20 22
 		insecure *bool
21 23
 		err      string
22
-		expected []imageapi.ImageImportSpec
24
+		expected *imageapi.ImageStreamImportSpec
23 25
 	}{
24
-		{
25
-			// 0: checking import's from when only .spec.dockerImageRepository is set, no status
26
+		"import from non-existing": {
27
+			name: "nonexisting",
28
+			err:  "pass --confirm to create and import",
29
+		},
30
+		"confirmed import from non-existing": {
31
+			name:    "nonexisting",
32
+			confirm: true,
33
+			expected: &imageapi.ImageStreamImportSpec{
34
+				Import: true,
35
+				Images: []imageapi.ImageImportSpec{{
36
+					From: kapi.ObjectReference{Kind: "DockerImage", Name: "nonexisting"},
37
+					To:   &kapi.LocalObjectReference{Name: "latest"},
38
+				}},
39
+			},
40
+		},
41
+		"confirmed import all from non-existing": {
42
+			name:    "nonexisting",
43
+			all:     true,
44
+			confirm: true,
45
+			expected: &imageapi.ImageStreamImportSpec{
46
+				Import: true,
47
+				Repository: &imageapi.RepositoryImportSpec{
48
+					From: kapi.ObjectReference{Kind: "DockerImage", Name: "nonexisting"},
49
+				},
50
+			},
51
+		},
52
+		"import from .spec.dockerImageRepository": {
26 53
 			name: "testis",
27 54
 			stream: &imageapi.ImageStream{
28
-				ObjectMeta: kapi.ObjectMeta{
29
-					Name:      "testis",
30
-					Namespace: "other",
55
+				ObjectMeta: kapi.ObjectMeta{Name: "testis", Namespace: "other"},
56
+				Spec: imageapi.ImageStreamSpec{
57
+					DockerImageRepository: "repo.com/somens/someimage",
58
+					Tags: make(map[string]imageapi.TagReference),
31 59
 				},
60
+			},
61
+			expected: &imageapi.ImageStreamImportSpec{
62
+				Import: true,
63
+				Images: []imageapi.ImageImportSpec{{
64
+					From: kapi.ObjectReference{Kind: "DockerImage", Name: "repo.com/somens/someimage"},
65
+					To:   &kapi.LocalObjectReference{Name: "latest"},
66
+				}},
67
+			},
68
+		},
69
+		"import all from .spec.dockerImageRepository": {
70
+			name: "testis",
71
+			all:  true,
72
+			stream: &imageapi.ImageStream{
73
+				ObjectMeta: kapi.ObjectMeta{Name: "testis", Namespace: "other"},
32 74
 				Spec: imageapi.ImageStreamSpec{
33 75
 					DockerImageRepository: "repo.com/somens/someimage",
34 76
 					Tags: make(map[string]imageapi.TagReference),
35 77
 				},
36 78
 			},
37
-			expected: []imageapi.ImageImportSpec{{
38
-				From: kapi.ObjectReference{
39
-					Kind: "DockerImage",
40
-					Name: "repo.com/somens/someimage",
79
+			expected: &imageapi.ImageStreamImportSpec{
80
+				Import: true,
81
+				Repository: &imageapi.RepositoryImportSpec{
82
+					From: kapi.ObjectReference{Kind: "DockerImage", Name: "repo.com/somens/someimage"},
41 83
 				},
42
-			}},
84
+			},
43 85
 		},
44
-		{
45
-			// 1: checking import's from when only .spec.dockerImageRepository is set, no status (with all flag set)
86
+		"import all from .spec.dockerImageRepository with different from": {
46 87
 			name: "testis",
88
+			from: "totally_different_spec",
89
+			all:  true,
90
+			err:  "different import spec",
47 91
 			stream: &imageapi.ImageStream{
48
-				ObjectMeta: kapi.ObjectMeta{
49
-					Name:      "testis",
50
-					Namespace: "other",
92
+				ObjectMeta: kapi.ObjectMeta{Name: "testis", Namespace: "other"},
93
+				Spec: imageapi.ImageStreamSpec{
94
+					DockerImageRepository: "repo.com/somens/someimage",
95
+					Tags: make(map[string]imageapi.TagReference),
51 96
 				},
97
+			},
98
+		},
99
+		"import all from .spec.dockerImageRepository with confirmed different from": {
100
+			name:    "testis",
101
+			from:    "totally/different/spec",
102
+			all:     true,
103
+			confirm: true,
104
+			stream: &imageapi.ImageStream{
105
+				ObjectMeta: kapi.ObjectMeta{Name: "testis", Namespace: "other"},
52 106
 				Spec: imageapi.ImageStreamSpec{
53 107
 					DockerImageRepository: "repo.com/somens/someimage",
54 108
 					Tags: make(map[string]imageapi.TagReference),
55 109
 				},
56 110
 			},
57
-			all: true,
58
-			expected: []imageapi.ImageImportSpec{{
59
-				From: kapi.ObjectReference{
60
-					Kind: "DockerImage",
61
-					Name: "repo.com/somens/someimage",
111
+			expected: &imageapi.ImageStreamImportSpec{
112
+				Import: true,
113
+				Repository: &imageapi.RepositoryImportSpec{
114
+					From: kapi.ObjectReference{Kind: "DockerImage", Name: "totally/different/spec"},
62 115
 				},
63
-			}},
116
+			},
64 117
 		},
65
-		{
66
-			// 2: with --all flag only .spec.dockerImageRepository is handled
118
+		"import all error for .spec.dockerImageRepository": {
67 119
 			name: "testis",
120
+			all:  true,
121
+			err:  "all is applicable only to images with spec.dockerImageRepository",
68 122
 			stream: &imageapi.ImageStream{
69
-				ObjectMeta: kapi.ObjectMeta{
70
-					Name:      "testis",
71
-					Namespace: "other",
72
-				},
123
+				ObjectMeta: kapi.ObjectMeta{Name: "testis", Namespace: "other"},
73 124
 				Spec: imageapi.ImageStreamSpec{
74 125
 					Tags: map[string]imageapi.TagReference{
75
-						"latest": {
76
-							From: &kapi.ObjectReference{Kind: "DockerImage", Name: "repo.com/somens/someimage:latest"},
77
-						},
126
+						"latest": {From: &kapi.ObjectReference{Kind: "DockerImage", Name: "repo.com/somens/someimage:latest"}},
78 127
 					},
79 128
 				},
80 129
 			},
81
-			all: true,
82
-			err: "all is applicable only to images with spec.dockerImageRepository",
83 130
 		},
84
-		{
85
-			// 3: empty image stream
131
+		"empty image stream": {
86 132
 			name: "testis",
133
+			err:  "image stream has not defined anything to import",
134
+			stream: &imageapi.ImageStream{
135
+				ObjectMeta: kapi.ObjectMeta{Name: "testis", Namespace: "other"},
136
+			},
137
+		},
138
+		"import latest tag": {
139
+			name: "testis:latest",
87 140
 			stream: &imageapi.ImageStream{
88 141
 				ObjectMeta: kapi.ObjectMeta{
89 142
 					Name:      "testis",
90 143
 					Namespace: "other",
91 144
 				},
145
+				Spec: imageapi.ImageStreamSpec{
146
+					Tags: map[string]imageapi.TagReference{
147
+						"latest": {From: &kapi.ObjectReference{Kind: "DockerImage", Name: "repo.com/somens/someimage:latest"}},
148
+					},
149
+				},
150
+			},
151
+			expected: &imageapi.ImageStreamImportSpec{
152
+				Import: true,
153
+				Images: []imageapi.ImageImportSpec{{
154
+					From: kapi.ObjectReference{Kind: "DockerImage", Name: "repo.com/somens/someimage:latest"},
155
+					To:   &kapi.LocalObjectReference{Name: "latest"},
156
+				}},
92 157
 			},
93
-			err: "image stream has not defined anything to import",
94 158
 		},
95
-		{
96
-			// 4: correct import of latest tag with tags specified in .spec.Tags
97
-			name: "testis:latest",
159
+		"import existing tag": {
160
+			name: "testis:existing",
98 161
 			stream: &imageapi.ImageStream{
99 162
 				ObjectMeta: kapi.ObjectMeta{
100 163
 					Name:      "testis",
... ...
@@ -102,22 +165,21 @@ func TestCreateImageImport(t *testing.T) {
102 102
 				},
103 103
 				Spec: imageapi.ImageStreamSpec{
104 104
 					Tags: map[string]imageapi.TagReference{
105
-						"latest": {
106
-							From: &kapi.ObjectReference{Kind: "DockerImage", Name: "repo.com/somens/someimage:latest"},
107
-						},
105
+						"existing": {From: &kapi.ObjectReference{Kind: "DockerImage", Name: "repo.com/somens/someimage:latest"}},
108 106
 					},
109 107
 				},
110 108
 			},
111
-			expected: []imageapi.ImageImportSpec{{
112
-				From: kapi.ObjectReference{
113
-					Kind: "DockerImage",
114
-					Name: "repo.com/somens/someimage:latest",
115
-				},
116
-			}},
109
+			expected: &imageapi.ImageStreamImportSpec{
110
+				Import: true,
111
+				Images: []imageapi.ImageImportSpec{{
112
+					From: kapi.ObjectReference{Kind: "DockerImage", Name: "repo.com/somens/someimage:latest"},
113
+					To:   &kapi.LocalObjectReference{Name: "existing"},
114
+				}},
115
+			},
117 116
 		},
118
-		{
119
-			// 5: import latest from image stream which has only tags specified and no latest
117
+		"import non-existing tag": {
120 118
 			name: "testis:latest",
119
+			err:  "does not exist on the image stream",
121 120
 			stream: &imageapi.ImageStream{
122 121
 				ObjectMeta: kapi.ObjectMeta{
123 122
 					Name:      "testis",
... ...
@@ -125,16 +187,12 @@ func TestCreateImageImport(t *testing.T) {
125 125
 				},
126 126
 				Spec: imageapi.ImageStreamSpec{
127 127
 					Tags: map[string]imageapi.TagReference{
128
-						"nonlatest": {
129
-							From: &kapi.ObjectReference{Kind: "DockerImage", Name: "repo.com/somens/someimage:latest"},
130
-						},
128
+						"nonlatest": {From: &kapi.ObjectReference{Kind: "DockerImage", Name: "repo.com/somens/someimage:latest"}},
131 129
 					},
132 130
 				},
133 131
 			},
134
-			err: "does not exist on the image stream",
135 132
 		},
136
-		{
137
-			// 6: insecure annotation should be applied to tags if exists
133
+		"use insecure annotation": {
138 134
 			name: "testis",
139 135
 			stream: &imageapi.ImageStream{
140 136
 				ObjectMeta: kapi.ObjectMeta{
... ...
@@ -147,17 +205,18 @@ func TestCreateImageImport(t *testing.T) {
147 147
 					Tags: make(map[string]imageapi.TagReference),
148 148
 				},
149 149
 			},
150
-			expected: []imageapi.ImageImportSpec{{
151
-				From: kapi.ObjectReference{
152
-					Kind: "DockerImage",
153
-					Name: "repo.com/somens/someimage",
154
-				},
155
-				ImportPolicy: imageapi.TagImportPolicy{Insecure: true},
156
-			}},
150
+			expected: &imageapi.ImageStreamImportSpec{
151
+				Import: true,
152
+				Images: []imageapi.ImageImportSpec{{
153
+					From:         kapi.ObjectReference{Kind: "DockerImage", Name: "repo.com/somens/someimage"},
154
+					To:           &kapi.LocalObjectReference{Name: "latest"},
155
+					ImportPolicy: imageapi.TagImportPolicy{Insecure: true},
156
+				}},
157
+			},
157 158
 		},
158
-		{
159
-			// 7: insecure annotation should be overridden by the flag
160
-			name: "testis",
159
+		"insecure flag overrides insecure annotation": {
160
+			name:     "testis",
161
+			insecure: newBool(false),
161 162
 			stream: &imageapi.ImageStream{
162 163
 				ObjectMeta: kapi.ObjectMeta{
163 164
 					Name:        "testis",
... ...
@@ -169,65 +228,55 @@ func TestCreateImageImport(t *testing.T) {
169 169
 					Tags: make(map[string]imageapi.TagReference),
170 170
 				},
171 171
 			},
172
-			insecure: newBool(false),
173
-			expected: []imageapi.ImageImportSpec{{
174
-				From: kapi.ObjectReference{
175
-					Kind: "DockerImage",
176
-					Name: "repo.com/somens/someimage",
177
-				},
178
-				ImportPolicy: imageapi.TagImportPolicy{Insecure: false},
179
-			}},
172
+			expected: &imageapi.ImageStreamImportSpec{
173
+				Import: true,
174
+				Images: []imageapi.ImageImportSpec{{
175
+					From:         kapi.ObjectReference{Kind: "DockerImage", Name: "repo.com/somens/someimage"},
176
+					To:           &kapi.LocalObjectReference{Name: "latest"},
177
+					ImportPolicy: imageapi.TagImportPolicy{Insecure: false},
178
+				}},
179
+			},
180 180
 		},
181 181
 	}
182 182
 
183
-	for idx, test := range testCases {
184
-		fake := testclient.NewSimpleFake(test.stream)
183
+	for name, test := range testCases {
184
+		var fake *testclient.Fake
185
+		if test.stream == nil {
186
+			fake = testclient.NewSimpleFake()
187
+		} else {
188
+			fake = testclient.NewSimpleFake(test.stream)
189
+		}
185 190
 		o := ImportImageOptions{
186
-			Target:    test.stream.Name,
187
-			All:       test.all,
188
-			Insecure:  test.insecure,
189
-			Namespace: test.stream.Namespace,
190
-			isClient:  fake.ImageStreams(test.stream.Namespace),
191
+			Target:   test.name,
192
+			From:     test.from,
193
+			All:      test.all,
194
+			Insecure: test.insecure,
195
+			Confirm:  test.confirm,
196
+			isClient: fake.ImageStreams(""),
191 197
 		}
192 198
 		// we need to run Validate, because it sets appropriate Name and Tag
193 199
 		if err := o.Validate(&cobra.Command{}); err != nil {
194
-			t.Errorf("(%d) unexpected error: %v", idx, err)
200
+			t.Errorf("%s: unexpected error: %v", name, err)
195 201
 		}
196 202
 
197 203
 		_, isi, err := o.createImageImport()
198 204
 		// check errors
199 205
 		if len(test.err) > 0 {
200 206
 			if err == nil || !strings.Contains(err.Error(), test.err) {
201
-				t.Errorf("(%d) unexpected error: expected %s, got %v", idx, test.err, err)
207
+				t.Errorf("%s: unexpected error: expected %s, got %v", name, test.err, err)
202 208
 			}
203 209
 			if isi != nil {
204
-				t.Errorf("(%d) unexpected import spec: expected nil, got %#v", idx, isi)
210
+				t.Errorf("%s: unexpected import spec: expected nil, got %#v", name, isi)
205 211
 			}
206 212
 			continue
207 213
 		}
208 214
 		if len(test.err) == 0 && err != nil {
209
-			t.Errorf("(%d) unexpected error: %v", idx, err)
215
+			t.Errorf("%s: unexpected error: %v", name, err)
210 216
 			continue
211 217
 		}
212 218
 		// check values
213
-		if test.all {
214
-			if !kapi.Semantic.DeepEqual(isi.Spec.Repository.From, test.expected[0].From) {
215
-				t.Errorf("(%d) unexpected import spec, expected %#v, got %#v", idx, test.expected[0].From, isi.Spec.Repository.From)
216
-			}
217
-		} else {
218
-			if len(isi.Spec.Images) != len(test.expected) {
219
-				t.Errorf("(%d) unexpected number of images, expected %d, got %d", idx, len(test.expected), len(isi.Spec.Images))
220
-			}
221
-			for i := 0; i < len(test.expected); i++ {
222
-				actual := isi.Spec.Images[i]
223
-				expected := test.expected[i]
224
-				if !kapi.Semantic.DeepEqual(actual.ImportPolicy, expected.ImportPolicy) {
225
-					t.Errorf("(%d) unexpected import[%d] policy, expected %v, got %v", idx, i, expected.ImportPolicy, actual.ImportPolicy)
226
-				}
227
-				if !kapi.Semantic.DeepEqual(actual.From, expected.From) {
228
-					t.Errorf("(%d) unexpected import[%d] from, expected %#v, got %#v", idx, i, expected.From, actual.From)
229
-				}
230
-			}
219
+		if !kapi.Semantic.DeepEqual(isi.Spec, *test.expected) {
220
+			t.Errorf("%s: unexpected import spec, expected %#v, got %#v", name, test.expected, isi.Spec)
231 221
 		}
232 222
 	}
233 223
 }