Checks current running containers for newer tags according to semver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

132 lines
2.7 KiB

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,
},
{
"user/image:v1",
ImageTag{
ImageTag: "user/image:v1",
Image: "user/image",
Version: "1",
VersionParts: []int{1},
},
false,
},
{
"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:123abc",
ImageTag{
ImageTag: "image:v1.0.0-desc@sha256:123abc",
Image: "library/image",
TagDesc: "desc",
TagSha: "sha256:123abc",
Version: "1.0.0",
VersionParts: []int{1, 0, 0},
},
false,
},
{
"image:v1.0.0@sha256:123abc",
ImageTag{
ImageTag: "image:v1.0.0@sha256:123abc",
Image: "library/image",
TagSha: "sha256:123abc",
Version: "1.0.0",
VersionParts: []int{1, 0, 0},
},
false,
},
// Real world example that had caused issues
{
"matrixdotorg/synapse:v1.41.0@sha256:75f2b3c35c047693f4f4334d4ceb97951de1ba12bd3c6182c8a2ee624d7c5ab7",
ImageTag{
ImageTag: "matrixdotorg/synapse:v1.41.0@sha256:75f2b3c35c047693f4f4334d4ceb97951de1ba12bd3c6182c8a2ee624d7c5ab7",
Image: "matrixdotorg/synapse",
TagSha: "sha256:75f2b3c35c047693f4f4334d4ceb97951de1ba12bd3c6182c8a2ee624d7c5ab7",
Version: "1.41.0",
VersionParts: []int{1, 41, 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,
)
}
}
}