Skip to content

Commit

Permalink
Merge pull request #41 from chrisohaver/verfromsha
Browse files Browse the repository at this point in the history
Add VersionFromSHA()
  • Loading branch information
chrisohaver authored Mar 17, 2020
2 parents 4b5f6e7 + 737fdb2 commit a81f14a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions migration/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package migration
// helper functions that make this easier to implement.

import (
"errors"
"fmt"
"regexp"
"sort"
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions migration/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit a81f14a

Please sign in to comment.