mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-07-22 23:10:36 -04:00
versionsapi: canonicalize ref in version constructors
Signed-off-by: Paul Meyer <katexochen0@gmail.com>
This commit is contained in:
parent
d208251df1
commit
1d138f40a8
2 changed files with 130 additions and 4 deletions
|
@ -41,7 +41,7 @@ type Version struct {
|
||||||
// NewVersion creates a new Version object and validates it.
|
// NewVersion creates a new Version object and validates it.
|
||||||
func NewVersion(ref, stream, version string, kind VersionKind) (Version, error) {
|
func NewVersion(ref, stream, version string, kind VersionKind) (Version, error) {
|
||||||
ver := Version{
|
ver := Version{
|
||||||
ref: ref,
|
ref: CanonicalizeRef(ref),
|
||||||
stream: stream,
|
stream: stream,
|
||||||
version: version,
|
version: version,
|
||||||
kind: kind,
|
kind: kind,
|
||||||
|
@ -62,7 +62,7 @@ func NewVersionFromShortPath(shortPath string, kind VersionKind) (Version, error
|
||||||
}
|
}
|
||||||
|
|
||||||
ver := Version{
|
ver := Version{
|
||||||
ref: ref,
|
ref: ref, // Canonicalized by parseShortPath.
|
||||||
stream: stream,
|
stream: stream,
|
||||||
version: version,
|
version: version,
|
||||||
kind: kind,
|
kind: kind,
|
||||||
|
@ -331,7 +331,7 @@ func CanonicalizeRef(ref string) string {
|
||||||
canRef := notAZ09Regexp.ReplaceAllString(ref, "-")
|
canRef := notAZ09Regexp.ReplaceAllString(ref, "-")
|
||||||
|
|
||||||
if canRef == ReleaseRef {
|
if canRef == ReleaseRef {
|
||||||
return "" // No ref should be cannonicalized to the release ref.
|
return "" // No ref should be canonicalized to the release ref.
|
||||||
}
|
}
|
||||||
|
|
||||||
return canRef
|
return canRef
|
||||||
|
@ -401,7 +401,7 @@ func MeasurementURL(version Version) (measurementURL, signatureURL *url.URL, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
shortPathRegex = regexp.MustCompile(`^ref/([a-zA-Z0-9-]+)/stream/([a-zA-Z0-9-]+)/([a-zA-Z0-9.-]+)$`)
|
shortPathRegex = regexp.MustCompile(`^ref/([^/]+)/stream/([a-zA-Z0-9-]+)/([a-zA-Z0-9.-]+)$`)
|
||||||
shortPathReleaseRegex = regexp.MustCompile(`^stream/([a-zA-Z0-9-]+)/([a-zA-Z0-9.-]+)$`)
|
shortPathReleaseRegex = regexp.MustCompile(`^stream/([a-zA-Z0-9-]+)/([a-zA-Z0-9.-]+)$`)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -422,6 +422,7 @@ func parseShortPath(shortPath string) (ref, stream, version string, err error) {
|
||||||
if shortPathRegex.MatchString(shortPath) {
|
if shortPathRegex.MatchString(shortPath) {
|
||||||
matches := shortPathRegex.FindStringSubmatch(shortPath)
|
matches := shortPathRegex.FindStringSubmatch(shortPath)
|
||||||
ref := matches[1]
|
ref := matches[1]
|
||||||
|
ref = CanonicalizeRef(ref)
|
||||||
if err := ValidateRef(ref); err != nil {
|
if err := ValidateRef(ref); err != nil {
|
||||||
return "", "", "", err
|
return "", "", "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,111 @@ import (
|
||||||
"github.com/edgelesssys/constellation/v2/internal/constants"
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestNewVersion(t *testing.T) {
|
||||||
|
testCases := map[string]struct {
|
||||||
|
ref string
|
||||||
|
stream string
|
||||||
|
version string
|
||||||
|
kind VersionKind
|
||||||
|
wantVer Version
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
"stable release image": {
|
||||||
|
ref: ReleaseRef,
|
||||||
|
stream: "stable",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindImage,
|
||||||
|
wantVer: Version{
|
||||||
|
ref: ReleaseRef,
|
||||||
|
stream: "stable",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindImage,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"release debug image": {
|
||||||
|
ref: ReleaseRef,
|
||||||
|
stream: "debug",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindImage,
|
||||||
|
wantVer: Version{
|
||||||
|
ref: ReleaseRef,
|
||||||
|
stream: "debug",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindImage,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"stable release cli": {
|
||||||
|
ref: ReleaseRef,
|
||||||
|
stream: "stable",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindCLI,
|
||||||
|
wantVer: Version{
|
||||||
|
ref: ReleaseRef,
|
||||||
|
stream: "stable",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindCLI,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"release debug cli": {
|
||||||
|
ref: ReleaseRef,
|
||||||
|
stream: "debug",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindCLI,
|
||||||
|
wantVer: Version{
|
||||||
|
ref: ReleaseRef,
|
||||||
|
stream: "debug",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindCLI,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"unknown kind": {
|
||||||
|
ref: ReleaseRef,
|
||||||
|
stream: "debug",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindUnknown,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
"non-release ref as input": {
|
||||||
|
ref: "working-branch",
|
||||||
|
stream: "debug",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindImage,
|
||||||
|
wantVer: Version{
|
||||||
|
ref: "working-branch",
|
||||||
|
stream: "debug",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindImage,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"non-canonical ref as input": {
|
||||||
|
ref: "testing-1.23",
|
||||||
|
stream: "debug",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindImage,
|
||||||
|
wantVer: Version{
|
||||||
|
ref: "testing-1-23",
|
||||||
|
stream: "debug",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindImage,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, tc := range testCases {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
ver, err := NewVersion(tc.ref, tc.stream, tc.version, tc.kind)
|
||||||
|
if tc.wantErr {
|
||||||
|
assert.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Equal(tc.wantVer, ver)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewVersionFromShortPath(t *testing.T) {
|
func TestNewVersionFromShortPath(t *testing.T) {
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
path string
|
path string
|
||||||
|
@ -78,6 +183,26 @@ func TestNewVersionFromShortPath(t *testing.T) {
|
||||||
kind: VersionKindCLI,
|
kind: VersionKindCLI,
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
|
"non-release ref as input": {
|
||||||
|
path: "ref/working-branch/stream/debug/v9.9.9",
|
||||||
|
kind: VersionKindImage,
|
||||||
|
wantVer: Version{
|
||||||
|
ref: "working-branch",
|
||||||
|
stream: "debug",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindImage,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"non-canonical ref as input": {
|
||||||
|
path: "ref/testing-1.23/stream/debug/v9.9.9",
|
||||||
|
kind: VersionKindImage,
|
||||||
|
wantVer: Version{
|
||||||
|
ref: "testing-1-23",
|
||||||
|
stream: "debug",
|
||||||
|
version: "v9.9.9",
|
||||||
|
kind: VersionKindImage,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, tc := range testCases {
|
for name, tc := range testCases {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue