Add parsing of image sha
Some checks reported errors
continuous-integration/drone/push Build was killed

Not yet using it in comparison
This commit is contained in:
IamTheFij 2021-09-15 09:48:13 -07:00
parent 2cfe9429b7
commit 17258861ba
2 changed files with 113 additions and 1 deletions

View File

@ -24,7 +24,7 @@ var (
maxPages = 10
// Regexp used to extract tag information
tagRegexp = regexp.MustCompile(`(.*):[vV]{0,1}([0-9.]+)(-(.*)){0,1}`)
tagRegexp = regexp.MustCompile(`(.*):[vV]{0,1}([0-9.]+)(-([a-zA-Z0-9_-]*)){0,1}(@([:0-9a-z]+)){0,1}`)
// version of tag checker
version = "dev"
)
@ -35,6 +35,7 @@ type ImageTag struct {
Registry string
Image string
TagDesc string
TagSha string
Version string
VersionParts []int
}
@ -109,6 +110,7 @@ func ParseImageTag(imageTag string) (ImageTag, error) {
Version: version,
VersionParts: versionParts,
TagDesc: results[4],
TagSha: results[6],
}, nil
}

110
main_test.go Normal file
View File

@ -0,0 +1,110 @@
package main
import (
"reflect"
"testing"
)
func TestTagParsing(t *testing.T) {
cases := []struct {
tag string
expected ImageTag
expectsError bool
}{
{
"image",
ImageTag{},
true,
},
{
"image:latest",
ImageTag{},
true,
},
{
"image:v1",
ImageTag{
ImageTag: "image:v1",
Image: "library/image",
Version: "1",
VersionParts: []int{1},
},
false,
},
{
"image:v1.0",
ImageTag{
ImageTag: "image:v1.0",
Image: "library/image",
Version: "1.0",
VersionParts: []int{1, 0},
},
false,
},
{
"image:v1.0.0",
ImageTag{
ImageTag: "image:v1.0.0",
Image: "library/image",
Version: "1.0.0",
VersionParts: []int{1, 0, 0},
},
false,
},
{
"image:v1.0.0-desc",
ImageTag{
ImageTag: "image:v1.0.0-desc",
Image: "library/image",
TagDesc: "desc",
Version: "1.0.0",
VersionParts: []int{1, 0, 0},
},
false,
},
{
"image:v1.0.0-desc@sha256:abc",
ImageTag{
ImageTag: "image:v1.0.0-desc@sha256:abc",
Image: "library/image",
TagDesc: "desc",
TagSha: "sha256:abc",
Version: "1.0.0",
VersionParts: []int{1, 0, 0},
},
false,
},
{
"image:v1.0.0@sha256:abc",
ImageTag{
ImageTag: "image:v1.0.0@sha256:abc",
Image: "library/image",
TagSha: "sha256:abc",
Version: "1.0.0",
VersionParts: []int{1, 0, 0},
},
false,
},
}
for _, c := range cases {
result, err := ParseImageTag(c.tag)
if c.expectsError {
if err == nil {
t.Errorf("ParseImageTag(%s): expected erro but didn't get one", c.tag)
}
continue
}
if err != nil {
t.Errorf("ParseImageTag(%s): unexpected error: %v", c.tag, err)
}
if !reflect.DeepEqual(result, c.expected) {
t.Errorf(
"ParseImageTag(%s): unexpected result. Actual: %+v Expected: %+v",
c.tag,
result,
c.expected,
)
}
}
}