Skip to content

Commit

Permalink
Use semver library to correctly compare agent versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ltsonov-cb committed Nov 8, 2023
1 parent 7302e05 commit b6bbcc5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
19 changes: 17 additions & 2 deletions cbcontainers/models/agent_version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models

import "golang.org/x/mod/semver"

type AgentVersion string

const (
Expand All @@ -12,12 +14,25 @@ func (v AgentVersion) IsLargerThan(version string) bool {
if v == AgentMinVersionNone || v == AgentVersionUnknown {
return false
}
return string(v) > version
return semver.Compare(normalizeToSemVer(string(v)), normalizeToSemVer(version)) > 0
}

func (v AgentVersion) IsLessThan(version string) bool {
if v == AgentMaxVersionLatest || v == AgentVersionUnknown {
return false
}
return string(v) < version
return semver.Compare(normalizeToSemVer(string(v)), normalizeToSemVer(version)) < 0
}

func normalizeToSemVer(v string) string {
if v == string(AgentMaxVersionLatest) || v == string(AgentVersionUnknown) || v == string(AgentMinVersionNone) {
return v
}

// semver requires a leading `v` at the front, so we make sure to have one
if v[0] != 'v' {
return "v" + v
}
// Note: v is not guaranteed to be a valid SemVer here, but it should be passable to the semver library
return v
}
2 changes: 2 additions & 0 deletions cbcontainers/models/operator_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestCheckCompatibilityCompatible(t *testing.T) {
{min: models.AgentVersion("0.0"), max: models.AgentVersion("2.8"), agent: "0.0"},
{min: models.AgentVersion("0.0"), max: models.AgentVersion("2.8"), agent: "1.0"},
{min: models.AgentVersion("0.0"), max: models.AgentVersion("2.8"), agent: "2.0"},
{min: models.AgentVersion("2.2.7"), max: models.AgentVersion("3.0"), agent: "2.10"},
}

testCheckCompatibility(t, testCases, true)
Expand All @@ -44,6 +45,7 @@ func TestCheckCompatibilityIncompatible(t *testing.T) {
{min: models.AgentVersion("2.7"), max: models.AgentVersion("2.8"), agent: "2.6.9"},
{min: models.AgentMinVersionNone, max: models.AgentVersion("2.8"), agent: "2.9"},
{min: models.AgentVersion("2.7"), max: models.AgentMaxVersionLatest, agent: "2.6"},
{min: models.AgentVersion("2.10"), max: models.AgentMaxVersionLatest, agent: "2.6"},
}

testCheckCompatibility(t, testCases, false)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/golang/mock v1.6.0
github.com/stretchr/testify v1.8.0
go.uber.org/zap v1.24.0
golang.org/x/mod v0.6.0
k8s.io/api v0.26.2
k8s.io/apimachinery v0.26.2
k8s.io/client-go v0.26.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I=
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down

0 comments on commit b6bbcc5

Please sign in to comment.