diff --git a/migration/migrate.go b/migration/migrate.go index 6f11dfe..ee5c5e0 100644 --- a/migration/migrate.go +++ b/migration/migrate.go @@ -6,6 +6,7 @@ package migration // helper functions that make this easier to implement. import ( + "errors" "fmt" "regexp" "sort" @@ -394,6 +395,16 @@ func Released(dockerImageSHA string) bool { return false } +// VersionFromSHA returns the version string matching the dockerImageSHA. +func VersionFromSHA(dockerImageSHA string) (string, error) { + for vStr, v := range Versions { + if v.dockerImageSHA == dockerImageSHA { + return vStr, nil + } + } + return "", errors.New("sha unsupported") +} + // ValidVersions returns a list of all versions defined func ValidVersions() []string { var vStrs []string diff --git a/migration/migrate_test.go b/migration/migrate_test.go index 0fc1332..1d9ad7a 100644 --- a/migration/migrate_test.go +++ b/migration/migrate_test.go @@ -1065,3 +1065,28 @@ func TestMatchOption(t *testing.T) { } } + +func TestVersionFromSHA(t *testing.T) { + testCases := []struct { + sha string + version string + shouldErr bool + }{ + { "2c8d61c46f484d881db43b34d13ca47a269336e576c81cf007ca740fa9ec0800", "1.6.7",false}, + { "blah", "",true}, + } + + for _, tc := range testCases { + ver, err := VersionFromSHA(tc.sha) + + if !tc.shouldErr && err != nil { + t.Fatalf("expected '%v' to not error.", tc.sha) + } + if tc.shouldErr && err == nil { + t.Fatalf("expected '%v' to error.", tc.sha) + } + if tc.version != ver { + t.Fatalf("expected '%v' to result in '%v', got '%v'.", tc.sha, tc.version, ver) + } + } +}