Compare commits

...

2 Commits

Author SHA1 Message Date
knoflook b3072cb5e2
test: pin compatiliby check tests 2021-11-01 10:48:47 +01:00
knoflook 7586abc3ca
feat: add pin compatibility check 2021-11-01 10:48:22 +01:00
3 changed files with 71 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
fmtcoverage.html

View File

@ -187,6 +187,30 @@ func (t Tag) IsCompatible(tag Tag) bool {
return true
}
// IsUpgradeCompatible chekcs if upTag is compatible with a pinned version tag.
// I.e. pinning to 22-fpm should return true if upTag is 22.2.0-fpm but not 22.2.0-alpine or 23.0.0-fpm
func (pin Tag) IsUpgradeCompatible(upTag Tag) bool {
if pin.Suffix != upTag.Suffix {
return false
}
if pin.Major != upTag.Major {
return false
}
if pin.MissingMinor {
return true
}
if pin.Minor != upTag.Minor {
return false
}
if pin.MissingPatch {
return true
}
if pin.Patch != upTag.Patch {
return false
}
return true
}
// UpgradeDelta returns a TagDelta object which is the difference between an old and new tag
// It can contain negative numbers if comparing with an older tag.
func (curTag Tag) UpgradeDelta(newTag Tag) (TagDelta, error) {

View File

@ -886,6 +886,52 @@ func TestDeltaString(t *testing.T) {
}
}
func TestIsUpgradeCompatible(t *testing.T) {
testsTrue := [][]string{
{"22-fpm", "22-fpm"},
{"22-fpm", "22.0-fpm"},
{"22-fpm", "22.0.0-fpm"},
{"22-fpm", "22.2.0-fpm"},
{"22-fpm", "22.0.0-fpm"},
{"22.2-fpm", "22.2-fpm"},
{"22.2-fpm", "22.2.0-fpm"},
{"22.2.2-fpm", "22.2.2-fpm"},
}
testsFalse := [][]string{
{"22-fpm", "22-alpine"},
{"22-fpm", "23-fpm"},
{"22-fpm", "21-fpm"},
{"22.2-fpm", "22.0.2-fpm"},
{"22.2.0-fpm", "22.2.2-fpm"},
}
for _, test := range testsTrue {
pin, err := tagcmp.Parse(test[0])
if err != nil {
t.Error(err)
}
upTag, err := tagcmp.Parse(test[1])
if err != nil {
t.Error(err)
}
if !pin.IsUpgradeCompatible(upTag) {
t.Errorf("pin %s should be upgradable to %s but returned false", test[0], test[1])
}
}
for _, test := range testsFalse {
pin, err := tagcmp.Parse(test[0])
if err != nil {
t.Error(err)
}
upTag, err := tagcmp.Parse(test[1])
if err != nil {
t.Error(err)
}
if pin.IsUpgradeCompatible(upTag) {
t.Errorf("pin %s should not be upgradable to %s but returned true", test[0], test[1])
}
}
}
func TestUpgradeDelta(t *testing.T) {
pairs := []struct {
t1 string