Skip to content

Commit

Permalink
Add compare versions to astro upgrade (#357)
Browse files Browse the repository at this point in the history
* Add compare versions to astro upgrade

* Apply suggestions from code review

Co-authored-by: Andrii Soldatenko <andrii.soldatenko@gmail.com>

* Fix err. Revert compareVersions to lowercase.

* Update version/validate_compatibility.go

Co-authored-by: Andrii Soldatenko <andrii.soldatenko@gmail.com>

Co-authored-by: Andrii Soldatenko <andrii.soldatenko@gmail.com>
  • Loading branch information
Adam Vandover and andriisoldatenko committed Sep 2, 2020
1 parent 0f3463c commit ccf5619
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
35 changes: 18 additions & 17 deletions version/validate_compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,30 @@ func ValidateCompatibility(client *houston.Client, out io.Writer, cliVer string,
return nil
}

func compareVersions(serverVer string, cliVer string, out io.Writer) error {
semVerServer, serverErr := parseVersion(serverVer)
if serverErr != nil {
return serverErr
// compareVersions print warning message if astro-cli has a variation in the minor version. Errors if major version is behind.
func compareVersions(compareVer string, currentVer string, out io.Writer) error {
semCompareVer, err := parseVersion(compareVer)
if err != nil {
return err
}

semVerCli, cliErr := parseVersion(cliVer)
if cliErr != nil {
return cliErr
semCurrVer, err := parseVersion(currentVer)
if err != nil {
return err
}

cliMajor := semVerCli.Major()
cliMinor := semVerCli.Minor()
currMajor := semCurrVer.Major()
currMinor := semCurrVer.Minor()

serverMajor := semVerServer.Major()
serverMinor := semVerServer.Minor()
compareMajor := semCompareVer.Major()
compareMinor := semCompareVer.Minor()

if cliMajor < serverMajor {
return errors.Errorf(messages.ERROR_NEW_MAJOR_VERSION, cliVer, serverVer)
} else if cliMinor < serverMinor {
fmt.Fprintf(out, messages.WARNING_NEW_MINOR_VERSION, cliVer, serverVer)
} else if cliMinor > serverMinor {
fmt.Fprintf(out, messages.WARNING_DOWNGRADE_VERSION, cliVer, serverVer)
if currMajor < compareMajor {
return errors.Errorf(messages.ERROR_NEW_MAJOR_VERSION, currentVer, compareVer)
} else if currMinor < compareMinor {
fmt.Fprintf(out, messages.WARNING_NEW_MINOR_VERSION, currentVer, compareVer)
} else if currMinor > compareMinor {
fmt.Fprintf(out, messages.WARNING_DOWNGRADE_VERSION, currentVer, compareVer)
}

return nil
Expand Down
8 changes: 1 addition & 7 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,7 @@ func CheckForUpdate(client *houston.Client, ghc *github.Client, out io.Writer) e
fmt.Fprintf(out, messages.CLI_LATEST_VERSION_DATE+"\n", latestTag, latestPub)

printServerVersion(client, out)

if latestTag > currentTag {
fmt.Fprintln(out, messages.CLI_UPGRADE_PROMPT)
fmt.Fprintln(out, messages.CLI_INSTALL_CMD)
} else {
fmt.Fprintln(out, messages.CLI_RUNNING_LATEST)
}
compareVersions(latestTag, currentTag, out)

return nil
}
Expand Down
31 changes: 30 additions & 1 deletion version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,36 @@ func TestCheckForUpdateVersionMatch(t *testing.T) {
githubClient := github.NewGithubClient(gitHubClient)
output := new(strings.Builder)
CheckForUpdate(houstonClient, githubClient, output)
expected := "Astro CLI Version: v0.15.0 (2020.06.01)\nAstro CLI Latest: v0.15.0 (2020.06.01)\nAstro Server Version: 0.13.0\nYou are running the latest version.\n"
expected := "Astro CLI Version: v0.15.0 (2020.06.01)\nAstro CLI Latest: v0.15.0 (2020.06.01)\nAstro Server Version: 0.13.0\n"
actual := output.String()

assert.Equal(t, expected, actual)
}

func TestPrintServerVersion(t *testing.T) {
testUtil.InitTestConfig()
okResponse := `{
"data": {
"appConfig": {
"version": "0.18.0",
"baseDomain": "local.astronomer.io",
"smtpConfigured": true,
"manualReleaseNames": false
}
}
}`
client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(okResponse)),
Header: make(http.Header),
}
})
houstonClient := houston.NewHoustonClient(client)

output := new(strings.Builder)
printServerVersion(houstonClient, output)
expected := "Astro Server Version: 0.18.0\n"
actual := output.String()

assert.Equal(t, expected, actual)
Expand Down

0 comments on commit ccf5619

Please sign in to comment.