cli: use Semver type to represent microservice versions (#2125)

Previously we used strings to pass microservice versions. This invited
bugs due to missing input validation.
This commit is contained in:
Otto Bittner 2023-07-25 14:20:25 +02:00 committed by GitHub
parent 2d3999440d
commit 1d5a8283e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
35 changed files with 612 additions and 318 deletions

View file

@ -9,26 +9,27 @@ package config
import (
"testing"
"github.com/edgelesssys/constellation/v2/internal/semver"
"github.com/stretchr/testify/assert"
)
// TestValidateVersionCompatibilityHelper checks that basic version and image short paths are correctly validated.
func TestValidateVersionCompatibilityHelper(t *testing.T) {
testCases := map[string]struct {
cli string
cli semver.Semver
target string
wantError bool
}{
"full version works": {
cli: "0.1.0",
cli: semver.NewFromInt(0, 1, 0, ""),
target: "v0.0.0",
},
"short path works": {
cli: "0.1.0",
cli: semver.NewFromInt(0, 1, 0, ""),
target: "ref/main/stream/debug/v0.0.0-pre.0.20230109121528-d24fac00f018",
},
"minor version difference > 1": {
cli: "0.0.0",
cli: semver.NewFromInt(0, 0, 0, ""),
target: "ref/main/stream/debug/v0.2.0-pre.0.20230109121528-d24fac00f018",
wantError: true,
},
@ -38,7 +39,43 @@ func TestValidateVersionCompatibilityHelper(t *testing.T) {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
err := validateVersionCompatibilityHelper(tc.cli, "image", tc.target)
err := validateImageCompatibilityHelper(tc.cli, "image", tc.target)
if tc.wantError {
assert.Error(err)
return
}
assert.NoError(err)
})
}
}
func TestValidateMicroserviceVersion(t *testing.T) {
testCases := map[string]struct {
cli semver.Semver
services semver.Semver
wantError bool
}{
"success": {
cli: semver.NewFromInt(0, 1, 0, ""),
services: semver.NewFromInt(0, 0, 0, ""),
},
"minor version difference > 1": {
cli: semver.NewFromInt(0, 0, 0, ""),
services: semver.NewFromInt(0, 2, 0, "pre.0.20230109121528-d24fac00f018"),
wantError: true,
},
"major version difference": {
cli: semver.NewFromInt(0, 0, 0, ""),
services: semver.NewFromInt(1, 0, 0, ""),
wantError: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
err := validateMicroserviceVersion(tc.cli, tc.services)
if tc.wantError {
assert.Error(err)
return