Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 93c35af

Browse files
committed
add unit tests for tag check. move latest tag to constant
1 parent 2f1797a commit 93c35af

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

boilerplate/boilerplate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def file_passes(filename, refs, regexs):
8989
if p.search(d):
9090
return False
9191

92-
# Replace all occurrences of the regex "2017|2016|2015|2014" with "YEAR"
92+
# Replace all occurrences of the regex "2018|2017|2016|2015|2014" with "YEAR"
9393
p = regexs["date"]
9494
for i, d in enumerate(data):
9595
(data[i], found) = p.subn('YEAR', d)
@@ -149,8 +149,8 @@ def get_regexs():
149149
regexs = {}
150150
# Search for "YEAR" which exists in the boilerplate, but shouldn't in the real thing
151151
regexs["year"] = re.compile( 'YEAR' )
152-
# dates can be 2014, 2015, 2016 or 2017, company holder names can be anything
153-
regexs["date"] = re.compile( '(2014|2015|2016|2017)' )
152+
# dates can be 2014, 2015, 2016, 2017, or 2018, company holder names can be anything
153+
regexs["date"] = re.compile( '(2014|2015|2016|2017|2018)' )
154154
# strip // +build \n\n build constraints
155155
regexs["go_build_constraints"] = re.compile(r"^(// \+build.*\n)+\n", re.MULTILINE)
156156
# strip #!.* from shell scripts

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func getPrepperForImage(image string) (pkgutil.Prepper, error) {
136136

137137
// see if the image name has tag provided, if not add latest as tag
138138
if !pkgutil.HasTag(image) {
139-
image = image + ":latest"
139+
image = image + pkgutil.LatestTag
140140
}
141141

142142
if strings.HasPrefix(image, DaemonPrefix) {

pkg/util/image_prep_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func getImage(p Prepper) (Image, error) {
102102
var source string
103103
// see if the image name has tag provided, if not add latest as tag
104104
if !HasTag(p.GetSource()) {
105-
source = p.GetSource() + ":latest"
105+
source = p.GetSource() + LatestTag
106106
} else {
107107
source = p.GetSource()
108108
}

pkg/util/image_utils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"github.com/sirupsen/logrus"
2828
)
2929

30+
const LatestTag string = ":latest"
31+
3032
func GetImageLayers(pathToImage string) []string {
3133
layers := []string{}
3234
contents, err := ioutil.ReadDir(pathToImage)
@@ -71,6 +73,6 @@ func copyToFile(outfile string, r io.Reader) error {
7173

7274
// checks to see if an image string contains a tag.
7375
func HasTag(image string) bool {
74-
tagRegex := regexp.MustCompile(".*:[^/]*$")
76+
tagRegex := regexp.MustCompile(".*:[^/]+$")
7577
return tagRegex.MatchString(image)
7678
}

util/image_utils_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2018 Google, Inc. All rights reserved.
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 util
18+
19+
import (
20+
"testing"
21+
22+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
23+
)
24+
25+
func TestImageTags(t *testing.T) {
26+
tests := []struct {
27+
image string
28+
hasTag bool
29+
}{
30+
{
31+
image: "gcr.io/test_image/foo:latest",
32+
hasTag: true,
33+
},
34+
{
35+
image: "gcr.io/test_image/foo:",
36+
hasTag: false,
37+
},
38+
{
39+
image: "daemon://gcr.io/test_image/foo:test",
40+
hasTag: true,
41+
},
42+
{
43+
image: "remote://gcr.io/test_image_foo",
44+
hasTag: false,
45+
},
46+
}
47+
48+
for _, test := range tests {
49+
if pkgutil.HasTag(test.image) != test.hasTag {
50+
t.Errorf("Error checking tag on image %s", test.image)
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)