@@ -17,6 +17,8 @@ limitations under the License.
17
17
package cmd
18
18
19
19
import (
20
+ "errors"
21
+ "fmt"
20
22
"testing"
21
23
22
24
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
@@ -119,3 +121,82 @@ func TestGetArtifacts(t *testing.T) {
119
121
})
120
122
}
121
123
}
124
+
125
+ func Test_getBuildArtifactsAndSetTags (t * testing.T ) {
126
+ tests := []struct {
127
+ description string
128
+ artifacts []build.Artifact
129
+ expected []build.Artifact
130
+ defaultRepo string
131
+ shouldErr bool
132
+ }{
133
+ {
134
+ description : "no artifact without default-repo" ,
135
+ artifacts : nil ,
136
+ expected : []build.Artifact (nil ),
137
+ },
138
+ {
139
+ description : "single artifact without default-repo" ,
140
+ artifacts : []build.Artifact {{ImageName : "image" , Tag : "image:tag" }},
141
+ expected : []build.Artifact {{ImageName : "image" , Tag : "image:tag" }},
142
+ },
143
+ {
144
+ description : "multiple artifacts without default-repo" ,
145
+ artifacts : []build.Artifact {
146
+ {ImageName : "image1" , Tag : "image1:tag" },
147
+ {ImageName : "image1" , Tag : "image1:tag" },
148
+ },
149
+ expected : []build.Artifact {
150
+ {ImageName : "image1" , Tag : "image1:tag" },
151
+ {ImageName : "image1" , Tag : "image1:tag" },
152
+ },
153
+ },
154
+ {
155
+ description : "single artifact with default-repo" ,
156
+ artifacts : []build.Artifact {{ImageName : "image" , Tag : "image:tag" }},
157
+ expected : []build.Artifact {{ImageName : "image" , Tag : "example.com/test-repo/image:tag" }},
158
+ defaultRepo : "example.com/test-repo" ,
159
+ },
160
+ {
161
+ description : "multiple artifacts with default-repo" ,
162
+ artifacts : []build.Artifact {
163
+ {ImageName : "image1" , Tag : "image1:tag" },
164
+ {ImageName : "image1" , Tag : "image1:tag" },
165
+ },
166
+ expected : []build.Artifact {
167
+ {ImageName : "image1" , Tag : "example.com/test-repo/image1:tag" },
168
+ {ImageName : "image1" , Tag : "example.com/test-repo/image1:tag" },
169
+ },
170
+ defaultRepo : "example.com/test-repo" ,
171
+ },
172
+ {
173
+ description : "multiple artifacts with erring default-repo" ,
174
+ artifacts : []build.Artifact {
175
+ {ImageName : "image1" , Tag : "image1:tag" },
176
+ {ImageName : "image1" , Tag : "image1:tag" },
177
+ },
178
+ expected : []build.Artifact (nil ),
179
+ defaultRepo : "example.com/test-repo" ,
180
+ shouldErr : true ,
181
+ },
182
+ }
183
+ for _ , test := range tests {
184
+ testutil .Run (t , test .description , func (t * testutil.T ) {
185
+ artifacts , err := applyDefaultRepoToArtifacts (test .artifacts , func (s string ) (string , error ) {
186
+ if test .shouldErr {
187
+ // this seems counter-intuitive that we explicitly return an error when shouldErr is true,
188
+ // however this function is a callback, the test is ensuring the error from the callback is handled
189
+ // correctly
190
+ return "" , errors .New ("error" )
191
+ }
192
+
193
+ if test .defaultRepo == "" {
194
+ return s , nil
195
+ }
196
+
197
+ return fmt .Sprintf ("%s/%s" , test .defaultRepo , s ), nil
198
+ })
199
+ t .CheckErrorAndDeepEqual (test .shouldErr , err , test .expected , artifacts )
200
+ })
201
+ }
202
+ }
0 commit comments