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

Commit

Permalink
add unit tests for tag check. move latest tag to constant
Browse files Browse the repository at this point in the history
  • Loading branch information
nkubala committed Mar 14, 2018
1 parent 2f1797a commit 93c35af
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
6 changes: 3 additions & 3 deletions boilerplate/boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def file_passes(filename, refs, regexs):
if p.search(d):
return False

# Replace all occurrences of the regex "2017|2016|2015|2014" with "YEAR"
# Replace all occurrences of the regex "2018|2017|2016|2015|2014" with "YEAR"
p = regexs["date"]
for i, d in enumerate(data):
(data[i], found) = p.subn('YEAR', d)
Expand Down Expand Up @@ -149,8 +149,8 @@ def get_regexs():
regexs = {}
# Search for "YEAR" which exists in the boilerplate, but shouldn't in the real thing
regexs["year"] = re.compile( 'YEAR' )
# dates can be 2014, 2015, 2016 or 2017, company holder names can be anything
regexs["date"] = re.compile( '(2014|2015|2016|2017)' )
# dates can be 2014, 2015, 2016, 2017, or 2018, company holder names can be anything
regexs["date"] = re.compile( '(2014|2015|2016|2017|2018)' )
# strip // +build \n\n build constraints
regexs["go_build_constraints"] = re.compile(r"^(// \+build.*\n)+\n", re.MULTILINE)
# strip #!.* from shell scripts
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func getPrepperForImage(image string) (pkgutil.Prepper, error) {

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

if strings.HasPrefix(image, DaemonPrefix) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/image_prep_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func getImage(p Prepper) (Image, error) {
var source string
// see if the image name has tag provided, if not add latest as tag
if !HasTag(p.GetSource()) {
source = p.GetSource() + ":latest"
source = p.GetSource() + LatestTag
} else {
source = p.GetSource()
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/util/image_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/sirupsen/logrus"
)

const LatestTag string = ":latest"

func GetImageLayers(pathToImage string) []string {
layers := []string{}
contents, err := ioutil.ReadDir(pathToImage)
Expand Down Expand Up @@ -71,6 +73,6 @@ func copyToFile(outfile string, r io.Reader) error {

// checks to see if an image string contains a tag.
func HasTag(image string) bool {
tagRegex := regexp.MustCompile(".*:[^/]*$")
tagRegex := regexp.MustCompile(".*:[^/]+$")
return tagRegex.MatchString(image)
}
53 changes: 53 additions & 0 deletions util/image_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2018 Google, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"testing"

pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
)

func TestImageTags(t *testing.T) {
tests := []struct {
image string
hasTag bool
}{
{
image: "gcr.io/test_image/foo:latest",
hasTag: true,
},
{
image: "gcr.io/test_image/foo:",
hasTag: false,
},
{
image: "daemon://gcr.io/test_image/foo:test",
hasTag: true,
},
{
image: "remote://gcr.io/test_image_foo",
hasTag: false,
},
}

for _, test := range tests {
if pkgutil.HasTag(test.image) != test.hasTag {
t.Errorf("Error checking tag on image %s", test.image)
}
}
}

0 comments on commit 93c35af

Please sign in to comment.