github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/util_test.go (about) 1 /* 2 Copyright 2019 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package build 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph" 24 "github.com/GoogleContainerTools/skaffold/testutil" 25 ) 26 27 // TestMergeWithPreviousBuilds tests that artifacts are always kept in the same order 28 func TestMergeWithPreviousBuilds(t *testing.T) { 29 builds := MergeWithPreviousBuilds([]graph.Artifact{artifact("img1", "tag1_1"), artifact("img2", "tag2_1")}, nil) 30 testutil.CheckDeepEqual(t, "img1:tag1_1,img2:tag2_1", tags(builds)) 31 32 builds = MergeWithPreviousBuilds([]graph.Artifact{artifact("img1", "tag1_2")}, builds) 33 testutil.CheckDeepEqual(t, "img1:tag1_2,img2:tag2_1", tags(builds)) 34 35 builds = MergeWithPreviousBuilds([]graph.Artifact{artifact("img2", "tag2_2")}, builds) 36 testutil.CheckDeepEqual(t, "img1:tag1_2,img2:tag2_2", tags(builds)) 37 38 builds = MergeWithPreviousBuilds([]graph.Artifact{artifact("img1", "tag1_3"), artifact("img2", "tag2_3")}, builds) 39 testutil.CheckDeepEqual(t, "img1:tag1_3,img2:tag2_3", tags(builds)) 40 } 41 42 func TestTagWithDigest(t *testing.T) { 43 tag := TagWithDigest("some-tag", "sha256:abcd1234") 44 testutil.CheckDeepEqual(t, "some-tag@sha256:abcd1234", tag) 45 tag = TagWithDigest("some-tag@sha256:abcd1234", "sha256:abcd1234") 46 testutil.CheckDeepEqual(t, "some-tag@sha256:abcd1234", tag) 47 } 48 49 func artifact(image, tag string) graph.Artifact { 50 return graph.Artifact{ 51 ImageName: image, 52 Tag: tag, 53 } 54 } 55 56 func tags(artifacts []graph.Artifact) string { 57 var tags string 58 59 for i, a := range artifacts { 60 if i > 0 { 61 tags += "," 62 } 63 tags += fmt.Sprintf("%s:%s", a.ImageName, a.Tag) 64 } 65 66 return tags 67 }