feat: update semver matching

This commit is contained in:
deviantony
2024-10-04 07:57:31 +00:00
parent b40d22dc74
commit ababd63d97
8 changed files with 63 additions and 12 deletions

View File

@@ -9,7 +9,7 @@ import (
"path/filepath"
"testing"
"github.com/Masterminds/semver"
"github.com/Masterminds/semver/v3"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/database/boltdb"
"github.com/portainer/portainer/api/database/models"

View File

@@ -7,7 +7,7 @@ import (
"github.com/pkg/errors"
portainer "github.com/portainer/portainer/api"
"github.com/Masterminds/semver"
"github.com/Masterminds/semver/v3"
"github.com/rs/zerolog/log"
)

View File

@@ -3,7 +3,7 @@ package migrator
import (
"errors"
"github.com/Masterminds/semver"
"github.com/Masterminds/semver/v3"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/database/models"
"github.com/portainer/portainer/api/dataservices/dockerhub"

View File

@@ -9,7 +9,7 @@ import (
dockerclient "github.com/portainer/portainer/api/docker/client"
"github.com/portainer/portainer/api/docker/images"
"github.com/Masterminds/semver"
"github.com/Masterminds/semver/v3"
"github.com/docker/docker/api/types"
dockercontainer "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"

View File

@@ -11,7 +11,7 @@ import (
httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/response"
"github.com/coreos/go-semver/semver"
"github.com/Masterminds/semver/v3"
"github.com/rs/zerolog/log"
"github.com/segmentio/encoding/json"
)
@@ -119,7 +119,7 @@ func HasNewerVersion(currentVersion, latestVersion string) bool {
return false
}
return currentVersionSemver.LessThan(*latestVersionSemver)
return currentVersionSemver.LessThan(latestVersionSemver)
}
// @id Version

View File

@@ -18,6 +18,60 @@ import (
"github.com/stretchr/testify/assert"
)
func TestHasNewerVersion(t *testing.T) {
// Test cases
tests := []struct {
name string
currentVersion string
latestVersion string
expected bool
}{
{
name: "current version is less than latest version",
currentVersion: "2.22.0",
latestVersion: "v2.22.1",
expected: true,
},
{
name: "current version is equal to latest version",
currentVersion: "2.22.0",
latestVersion: "v2.22.0",
expected: false,
},
{
name: "current version is greater than latest version",
currentVersion: "v2.22.2",
latestVersion: "v2.22.1",
expected: false,
},
{
name: "invalid current version",
currentVersion: "invalid",
latestVersion: "v2.22.0",
expected: false,
},
{
name: "invalid latest version",
currentVersion: "2.22.0",
latestVersion: "invalid",
expected: false,
},
{
name: "both versions are invalid",
currentVersion: "invalid",
latestVersion: "invalid",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := HasNewerVersion(tt.currentVersion, tt.latestVersion)
assert.Equal(t, tt.expected, result)
})
}
}
func Test_getSystemVersion(t *testing.T) {
is := assert.New(t)