-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize migrate constaints check and add unit tests
- Loading branch information
Showing
4 changed files
with
139 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package migrate | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
"go.szostok.io/version" | ||
) | ||
|
||
const ( | ||
botkubeMinVersionConstraint = ">= 1.0" | ||
) | ||
|
||
// BotkubeVersionConstraints returns Botkube version constraints as a string. | ||
func BotkubeVersionConstraints() string { | ||
botkubeMaxVersionConstraint := "" | ||
|
||
cliVer := version.Get().Version | ||
cliVersion, err := semver.NewVersion(cliVer) | ||
if err == nil { | ||
botkubeMaxVersionConstraint = fmt.Sprintf(", <= %s", cliVersion.String()) | ||
} | ||
|
||
return fmt.Sprintf("%s%s", botkubeMinVersionConstraint, botkubeMaxVersionConstraint) | ||
} | ||
|
||
// IsCompatible checks if Botkube version is compatible with the migrate command. | ||
func IsCompatible(botkubeVersionConstraintsStr string, botkubeVersionStr string) (bool, error) { | ||
constraint, err := semver.NewConstraint(botkubeVersionConstraintsStr) | ||
if err != nil { | ||
return false, fmt.Errorf("unable to parse Botkube semver version constraints: %w", err) | ||
} | ||
|
||
botkubeVersion, err := semver.NewVersion(botkubeVersionStr) | ||
if err != nil { | ||
return false, fmt.Errorf("unable to parse botkube version %s as semver: %w", botkubeVersion, err) | ||
} | ||
|
||
return constraint.Check(botkubeVersion), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package migrate | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIsCompatible(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
BotkubeVersionConstraintsStr string | ||
BotkubeVersionStr string | ||
ExpectedResult bool | ||
ExpectedErrMessage string | ||
}{ | ||
// TODO: The following test cases should pass, but they don't because of the bug in the `semver` library. | ||
// See: https://github.com/Masterminds/semver/issues/177 | ||
// The problem occurs only in the RC releases, so this isn't a huge issue for us. | ||
// | ||
//{ | ||
// Name: "RC within range", | ||
// BotkubeVersionStr: "1.8.0-rc.1", | ||
// BotkubeVersionConstraintsStr: ">= 1.0, <= 1.8.0-rc.1", | ||
// ExpectedResult: true, | ||
// ExpectedErrMessage: "", | ||
//}, | ||
//{ | ||
// Name: "RC within range 2", | ||
// BotkubeVersionStr: "1.8.0-rc.1", | ||
// BotkubeVersionConstraintsStr: ">= 1.0, <= 1.8.0", | ||
// ExpectedResult: true, | ||
// ExpectedErrMessage: "", | ||
//}, | ||
{ | ||
Name: "Final version out of range", | ||
BotkubeVersionStr: "1.8.0", | ||
BotkubeVersionConstraintsStr: ">= 1.0, <= 1.8.0-rc.1", | ||
ExpectedResult: false, | ||
ExpectedErrMessage: "", | ||
}, | ||
{ | ||
Name: "Final version within range", | ||
BotkubeVersionStr: "1.8.0", | ||
BotkubeVersionConstraintsStr: ">= 1.0, <= 1.8.0", | ||
ExpectedResult: true, | ||
ExpectedErrMessage: "", | ||
}, | ||
{ | ||
Name: "Lowest version within range", | ||
BotkubeVersionStr: "1.0.0", | ||
BotkubeVersionConstraintsStr: ">= 1.0, <= 1.8.0", | ||
ExpectedResult: true, | ||
ExpectedErrMessage: "", | ||
}, | ||
{ | ||
Name: "Older", | ||
BotkubeVersionStr: "1.7.0", | ||
BotkubeVersionConstraintsStr: ">= 1.0, <= 1.8.0", | ||
ExpectedResult: true, | ||
ExpectedErrMessage: "", | ||
}, | ||
{ | ||
Name: "Newer", | ||
BotkubeVersionStr: "1.9.0", | ||
BotkubeVersionConstraintsStr: ">= 1.0, <= 1.8.0", | ||
ExpectedResult: false, | ||
ExpectedErrMessage: "", | ||
}, | ||
{ | ||
Name: "Invalid constraint", | ||
BotkubeVersionStr: "1.8.0", | ||
BotkubeVersionConstraintsStr: ">= 1.0, <= dev", | ||
ExpectedResult: false, | ||
ExpectedErrMessage: "unable to parse Botkube semver version constraints: improper constraint: >= 1.0, <= dev", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
result, err := IsCompatible(tc.BotkubeVersionConstraintsStr, tc.BotkubeVersionStr) | ||
if tc.ExpectedErrMessage != "" { | ||
require.Error(t, err) | ||
assert.EqualError(t, err, tc.ExpectedErrMessage) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, tc.ExpectedResult, result) | ||
}) | ||
} | ||
} |