-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add version library #12
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,14 @@ | ||
package version | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrInvalidVersion = errors.New("invalid version string") | ||
ErrIncomparableVersion = errors.New("incomparable: dev version") | ||
|
||
ErrInvalidHarvesterVersion = errors.New("invalid harvester version") | ||
ErrMinUpgradeRequirement = errors.New("current version does not meet minimum upgrade requirement") | ||
ErrDowngrade = errors.New("downgrading is prohibited") | ||
ErrDevUpgrade = errors.New("upgrading from dev versions to non-dev versions is prohibited") | ||
ErrPrereleaseCrossVersionUpgrade = errors.New("cross-version upgrades from/to any prerelease version are prohibited") | ||
) |
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,96 @@ | ||
package version | ||
|
||
type HarvesterUpgradeVersion struct { | ||
currentVersion *HarvesterVersion | ||
upgradeVersion *HarvesterVersion | ||
minUpgradableVersion *HarvesterVersion | ||
} | ||
|
||
func NewHarvesterUpgradeVersion(cv, uv, mv *HarvesterVersion) *HarvesterUpgradeVersion { | ||
return &HarvesterUpgradeVersion{ | ||
currentVersion: cv, | ||
upgradeVersion: uv, | ||
minUpgradableVersion: mv, | ||
} | ||
} | ||
|
||
func (u *HarvesterUpgradeVersion) IsUpgrade() error { | ||
if u.currentVersion == nil || u.upgradeVersion == nil { | ||
return ErrInvalidHarvesterVersion | ||
} | ||
|
||
isDowngrade, err := u.currentVersion.IsNewer(u.upgradeVersion) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if isDowngrade { | ||
return ErrDowngrade | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// IsUpgradable checks that whether the current version satisfies the minimum upgrade requirement. | ||
func (u *HarvesterUpgradeVersion) IsUpgradable() error { | ||
// If minUpgradableVersion is nil means there's no restriction. | ||
if u.minUpgradableVersion == nil { | ||
return nil | ||
} | ||
|
||
isOlderThanMinUpgradableVersion, err := u.currentVersion.IsOlder(u.minUpgradableVersion) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if isOlderThanMinUpgradableVersion { | ||
return ErrMinUpgradeRequirement | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (u *HarvesterUpgradeVersion) CheckUpgradeEligibility(strictMode bool) error { | ||
// Upgrading to dev versions is always allowed | ||
if u.upgradeVersion.isDev { | ||
return nil | ||
} | ||
|
||
// Upgrading from dev versions is restricted if strict mode is enabled | ||
if u.currentVersion.isDev { | ||
if strictMode { | ||
return ErrDevUpgrade | ||
} | ||
return nil | ||
} | ||
|
||
// Same-version upgrade is always allowed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we prohibit this in the current upgrade code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I know, no. Why the ask? |
||
isSameVersion, err := u.currentVersion.IsEqual(u.upgradeVersion) | ||
if err != nil { | ||
return err | ||
} | ||
if isSameVersion { | ||
return nil | ||
} | ||
|
||
// Check if it's effectively a downgrade | ||
if err := u.IsUpgrade(); err != nil { | ||
return err | ||
} | ||
|
||
// Check the minimum upgradable version | ||
if err := u.IsUpgradable(); err != nil { | ||
return err | ||
} | ||
|
||
// Check if it's a prerelease cross-version upgrade | ||
if u.currentVersion.isPrerelease { | ||
currentStableVersion := u.currentVersion.GetStableVersion() | ||
upgradeStableVersion := u.upgradeVersion.GetStableVersion() | ||
if isSameStableVersion, _ := currentStableVersion.IsEqual(upgradeStableVersion); !isSameStableVersion { | ||
return ErrPrereleaseCrossVersionUpgrade | ||
} | ||
} | ||
|
||
return 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,147 @@ | ||
package version | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHarvesterUpgradeVersion_IsUpgrade(t *testing.T) { | ||
var testCases = []struct { | ||
name string | ||
currentVersion string | ||
upgradeVersion string | ||
expectedErr error | ||
}{ | ||
{"upgrade from a newer release version to an older one", "v1.3.0", "v1.2.1", ErrDowngrade}, | ||
{"upgrade from an older release version to a newer one", "v1.2.2", "v1.3.0", nil}, | ||
|
||
{"upgrade from a newer prerelease version to an older one", "v1.3.0-rc1", "v1.2.1-rc1", ErrDowngrade}, | ||
{"upgrade from a newer prerelease version to an older one", "v1.2.1-rc2", "v1.2.1-rc1", ErrDowngrade}, | ||
{"upgrade from an older prerelease version to a newer one", "v1.2.1-rc1", "v1.3.0-rc1", nil}, | ||
{"upgrade from an older prerelease version to a newer one", "v1.2.1-rc1", "v1.2.1-rc2", nil}, | ||
|
||
{"upgrade among two dev versions", "11223344", "aabbccdd", ErrIncomparableVersion}, | ||
|
||
{"upgrade from a newer prerelease version to an older release version", "v1.2.2-rc2", "v1.2.1", ErrDowngrade}, | ||
{"upgrade from an older prerelease version to a newer release version", "v1.2.1-rc2", "v1.2.2", nil}, | ||
{"upgrade from a newer release version to an older prerelease version", "v1.2.2", "v1.2.2-rc2", ErrDowngrade}, | ||
{"upgrade from a newer release version to an older prerelease version", "v1.2.2", "v1.2.1-rc2", ErrDowngrade}, | ||
{"upgrade from an older release version to a newer prerelease version", "v1.2.1", "v1.2.2-rc1", nil}, | ||
|
||
{"upgrade from a release version to a dev version", "v1.2.1", "aabbccdd", ErrIncomparableVersion}, | ||
{"upgrade from a dev version to a release version", "aabbccdd", "v1.2.1", ErrIncomparableVersion}, | ||
{"upgrade from a prerelease version to a dev version", "v1.2.2-rc1", "aabbccdd", ErrIncomparableVersion}, | ||
{"upgrade from a dev version to a prerelease version", "aabbccdd", "v1.2.2-rc1", ErrIncomparableVersion}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
cv, _ := NewHarvesterVersion(tc.currentVersion) | ||
uv, _ := NewHarvesterVersion(tc.upgradeVersion) | ||
|
||
huv := NewHarvesterUpgradeVersion(cv, uv, nil) | ||
actualErr := huv.IsUpgrade() | ||
if tc.expectedErr != nil { | ||
assert.Equal(t, tc.expectedErr, actualErr, tc.name) | ||
} else { | ||
assert.Nil(t, actualErr, tc.name) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestHarvesterUpgradeVersion_IsUpgradable(t *testing.T) { | ||
var testCases = []struct { | ||
name string | ||
currentVersion string | ||
minUpgradableVersion string | ||
expectedErr error | ||
}{ | ||
{"upgrade from a release version above the minimal requirement", "v1.2.1", "v1.2.0", nil}, | ||
{"upgrade from a release version lower than the minimal requirement", "v1.2.1", "v1.2.2", ErrMinUpgradeRequirement}, | ||
{"upgrade from the exact same release version of the minimal requirement", "v1.2.1", "v1.2.1", nil}, | ||
|
||
{"upgrade from a prerelease version lower than the minimal requirement", "v1.2.1-rc1", "v1.2.1", ErrMinUpgradeRequirement}, | ||
{"upgrade from a prerelease version above the minimal requirement (rc minUpgradableVersion)", "v1.2.1-rc2", "v1.2.1-rc1", nil}, | ||
{"upgrade from a prerelease version lower than the minimal requirement (rc minUpgradableVersion)", "v1.2.1-rc1", "v1.2.1-rc2", ErrMinUpgradeRequirement}, | ||
{"upgrade from a prerelease version lower than the minimal requirement (rc minUpgradableVersion)", "v1.2.0-rc1", "v1.2.1-rc2", ErrMinUpgradeRequirement}, | ||
{"upgrade from the exact same prerelease version of the minimal requirement (rc minUpgradableVersion)", "v1.2.1-rc1", "v1.2.1-rc1", nil}, | ||
|
||
{"upgrade from dev versions", "11223344", "v1.2.1", ErrIncomparableVersion}, | ||
{"upgrade from dev versions", "aabbccdd", "v1.2.1", ErrIncomparableVersion}, | ||
|
||
{"upgrade from a release version without any minimum requirement specified", "v1.2.1", "", nil}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
cv, _ := NewHarvesterVersion(tc.currentVersion) | ||
mv, _ := NewHarvesterVersion(tc.minUpgradableVersion) | ||
|
||
huv := NewHarvesterUpgradeVersion(cv, nil, mv) | ||
brandboat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
actualErr := huv.IsUpgradable() | ||
if tc.expectedErr != nil { | ||
assert.Equal(t, tc.expectedErr, actualErr, tc.name) | ||
} else { | ||
assert.Nil(t, actualErr, tc.name) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestHarvesterUpgradeVersion_CheckUpgradeEligibility(t *testing.T) { | ||
var testCases = []struct { | ||
name string | ||
currentVersion string | ||
upgradeVersion string | ||
minUpgradableVersion string | ||
strictMode bool | ||
expectedErr error | ||
}{ | ||
{"upgrade to same release version", "v1.2.1", "v1.2.1", "v1.1.2", true, nil}, | ||
{"upgrade from an old release version same as the minimal requirement of a new release version", "v1.1.2", "v1.2.1", "v1.1.2", true, nil}, | ||
{"upgrade from an old release version above the minimal requirement of a new release version", "v1.2.0", "v1.2.1", "v1.1.2", true, nil}, | ||
{"upgrade from an old release version below the minimal requirement of a new release version", "v1.1.1", "v1.2.1", "v1.1.2", true, ErrMinUpgradeRequirement}, | ||
{"upgrade from a new release version above the minimal requirement of an old release version", "v1.2.1", "v1.2.0", "v1.1.2", true, ErrDowngrade}, | ||
{"upgrade from an old release version same the minimal requirement of a new prerelease version", "v1.1.2", "v1.2.1-rc1", "v1.1.2", true, nil}, | ||
{"upgrade from an old release version above the minimal requirement of a new prerelease version", "v1.2.0", "v1.2.1-rc1", "v1.1.2", true, nil}, | ||
{"upgrade from an old release version below the minimal requirement of a new prerelease version", "v1.1.1", "v1.2.1-rc1", "v1.1.2", true, ErrMinUpgradeRequirement}, | ||
{"upgrade from a new release version above the minimal requirement of an old prerelease version", "v1.2.1", "v1.2.1-rc1", "v1.1.2", true, ErrDowngrade}, | ||
{"upgrade from a release version to a dev version", "v1.2.1", "v1.2-ab12cd34", "", true, nil}, | ||
|
||
{"upgrade to same prerelease version", "v1.2.2-rc1", "v1.2.2-rc1", "v1.2.1", true, nil}, | ||
{"upgrade from an old prerelease version below the minimal requirement of a new release version", "v1.1.2-rc1", "v1.2.1", "v1.1.2", true, ErrMinUpgradeRequirement}, | ||
{"upgrade from an old prerelease version above the minimal requirement of a new release version", "v1.2.0-rc1", "v1.2.1", "v1.1.2", true, ErrPrereleaseCrossVersionUpgrade}, | ||
{"upgrade from an old prerelease version below the minimal requirement of a new release version", "v1.1.1-rc1", "v1.2.1", "v1.1.2", true, ErrMinUpgradeRequirement}, | ||
{"upgrade from an old prerelease version above the minimal requirement of a new prerelease version", "v1.2.2-rc1", "v1.2.2-rc2", "v1.2.1", true, nil}, | ||
{"upgrade from an old prerelease version below the minimal requirement of a new prerelease version", "v1.2.1-rc1", "v1.2.2-rc2", "v1.2.1", true, ErrMinUpgradeRequirement}, | ||
{"upgrade from an old prerelease version above the minimal requirement of a new prerelease version", "v1.2.0-rc1", "v1.2.1-rc2", "v1.1.2", true, ErrPrereleaseCrossVersionUpgrade}, | ||
{"upgrade from a prerelease version to a dev version", "v1.2.2-rc1", "v1.2-ab12cd34", "", true, nil}, | ||
|
||
{"upgrade to same dev version", "v1.2-ab12cd34", "v1.2-ab12cd34", "", true, nil}, | ||
{"upgrade among two dev versions", "v1.2-ab12cd34", "v1.2-1234567", "", true, nil}, | ||
{"upgrade from a dev version to a release version (strict mode)", "v1.2-ab12cd34", "v1.2.1", "v1.1.2", true, ErrDevUpgrade}, | ||
{"upgrade from a dev version to a release version (loose mode)", "v1.2-ab12cd34", "v1.2.1", "v1.1.2", false, nil}, | ||
{"upgrade from a dev version to a prerelease version (strict mode)", "v1.2-ab12cd34", "v1.2.2-rc1", "v1.2.1", true, ErrDevUpgrade}, | ||
{"upgrade from a dev version to a prerelease version (loose mode)", "v1.2-ab12cd34", "v1.2.2-rc1", "v1.2.1", false, nil}, | ||
|
||
{"upgrade from an old release version to a new release version without any minimum requirement specified", "v1.2.1", "v1.3.0", "", true, nil}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
cv, _ := NewHarvesterVersion(tc.currentVersion) | ||
uv, _ := NewHarvesterVersion(tc.upgradeVersion) | ||
mv, _ := NewHarvesterVersion(tc.minUpgradableVersion) | ||
|
||
huv := NewHarvesterUpgradeVersion(cv, uv, mv) | ||
actualErr := huv.CheckUpgradeEligibility(tc.strictMode) | ||
if tc.expectedErr != nil { | ||
assert.Equal(t, tc.expectedErr, actualErr, tc.name) | ||
} else { | ||
assert.Nil(t, actualErr, tc.name) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If "strictMode=true", we can still upgrade from one dev version to another dev version. It's a bit weird to me due to the word "strict", but if we are doing this for specific purpose, please ignore this comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is open to discussion. From a developer perspective, I tend to allow this kind of upgrade path (dev-to-dev upgrades) by default for the convenience of both developers and QA—actually, the part that leverages this library never bothers setting the strict mode off. It just checks the skip-check annotation and skip through the entire eligibility check, making the mode seem redundant. cc @bk201
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After discussion, the
strict-mode
for upgrade will be introduced as a new Harvester setting. I'll create a new ticket to track the progress. Thanks!