2022-12-29 11:23:23 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-06-07 10:16:32 -04:00
|
|
|
package versionsapi
|
2022-12-29 11:23:23 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-22 08:59:28 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-02-03 05:05:42 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
2022-12-29 11:23:23 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewVersionFromShortPath(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
path string
|
|
|
|
kind VersionKind
|
|
|
|
wantVer Version
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"stable release image": {
|
|
|
|
path: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
|
|
|
wantVer: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"release debug image": {
|
|
|
|
path: "stream/debug/v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
|
|
|
wantVer: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"stable release cli": {
|
|
|
|
path: "v9.9.9",
|
|
|
|
kind: VersionKindCLI,
|
|
|
|
wantVer: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindCLI,
|
2023-02-24 06:00:04 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"release debug cli": {
|
|
|
|
path: "stream/debug/v9.9.9",
|
|
|
|
kind: VersionKindCLI,
|
|
|
|
wantVer: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindCLI,
|
2023-02-24 06:00:04 -05:00
|
|
|
},
|
|
|
|
},
|
2022-12-29 11:23:23 -05:00
|
|
|
"unknown kind": {
|
|
|
|
path: "v9.9.9",
|
|
|
|
kind: VersionKindUnknown,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"invalid path image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
path: "va.b.c",
|
|
|
|
kind: VersionKindImage,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"invalid path cli": {
|
|
|
|
path: "va.b.c",
|
|
|
|
kind: VersionKindCLI,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-12-29 11:23:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
ver, err := NewVersionFromShortPath(tc.path, tc.kind)
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.wantVer, ver)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionShortPath(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
ver Version
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
"stable release image": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
want: "v9.9.9",
|
|
|
|
},
|
|
|
|
"release debug image": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
want: "stream/debug/v9.9.9",
|
|
|
|
},
|
|
|
|
"branch image": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: "foo",
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
want: "ref/foo/stream/debug/v9.9.9",
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"stable release cli": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindCLI,
|
2023-02-24 06:00:04 -05:00
|
|
|
},
|
|
|
|
want: "v9.9.9",
|
|
|
|
},
|
|
|
|
"release debug cli": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindCLI,
|
2023-02-24 06:00:04 -05:00
|
|
|
},
|
|
|
|
want: "stream/debug/v9.9.9",
|
|
|
|
},
|
|
|
|
"branch cli": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: "foo",
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindCLI,
|
2023-02-24 06:00:04 -05:00
|
|
|
},
|
|
|
|
want: "ref/foo/stream/debug/v9.9.9",
|
|
|
|
},
|
2022-12-29 11:23:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
got := tc.ver.ShortPath()
|
|
|
|
assert.Equal(tc.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionValidate(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
ver Version
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2023-02-24 06:00:04 -05:00
|
|
|
"valid image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"invalid ref image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: "foo/bar",
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"invalid stream image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "foo/bar",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"invalid version image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9/foo",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"valid cli": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindCLI,
|
2023-02-24 06:00:04 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"invalid ref cli": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: "foo/bar",
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindCLI,
|
2023-02-24 06:00:04 -05:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid stream cli": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "foo/bar",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindCLI,
|
2023-02-24 06:00:04 -05:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid version cli": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9/foo",
|
|
|
|
kind: VersionKindCLI,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
err := tc.ver.Validate()
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionMajor(t *testing.T) {
|
|
|
|
testCases := map[string]string{
|
|
|
|
"v9.9.9": "v9",
|
|
|
|
"v9.6.9-foo": "v9",
|
|
|
|
"v7.9.9": "v7",
|
|
|
|
}
|
|
|
|
|
|
|
|
for version, major := range testCases {
|
|
|
|
t.Run(version, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2023-08-01 10:48:13 -04:00
|
|
|
ver := Version{version: version}
|
2022-12-29 11:23:23 -05:00
|
|
|
assert.Equal(major, ver.Major())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionMajorMinor(t *testing.T) {
|
|
|
|
testCases := map[string]string{
|
|
|
|
"v9.9.9": "v9.9",
|
|
|
|
"v9.6.9-foo": "v9.6",
|
|
|
|
"v7.9.9": "v7.9",
|
|
|
|
}
|
|
|
|
|
|
|
|
for version, major := range testCases {
|
|
|
|
t.Run(version, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2023-08-01 10:48:13 -04:00
|
|
|
ver := Version{version: version}
|
2022-12-29 11:23:23 -05:00
|
|
|
assert.Equal(major, ver.MajorMinor())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionWithGranularity(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
ver string
|
|
|
|
gran Granularity
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
ver: "v9.9.9",
|
|
|
|
gran: GranularityMajor,
|
|
|
|
want: "v9",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ver: "v9.9.9",
|
|
|
|
gran: GranularityMinor,
|
|
|
|
want: "v9.9",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ver: "v9.9.9",
|
|
|
|
gran: GranularityPatch,
|
|
|
|
want: "v9.9.9",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ver: "v9.9.9-foo",
|
|
|
|
gran: GranularityMajor,
|
|
|
|
want: "v9",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ver: "v9.9.9-foo",
|
|
|
|
gran: GranularityPatch,
|
|
|
|
want: "v9.9.9-foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ver: "v9.9.9-foo",
|
|
|
|
gran: GranularityUnknown,
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.ver, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2023-08-01 10:48:13 -04:00
|
|
|
ver := Version{version: tc.ver}
|
2022-12-29 11:23:23 -05:00
|
|
|
assert.Equal(tc.want, ver.WithGranularity(tc.gran))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionListPathURL(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
ver Version
|
|
|
|
gran Granularity
|
|
|
|
wantPath string
|
|
|
|
wantURL string
|
|
|
|
}{
|
2023-02-24 06:00:04 -05:00
|
|
|
"release image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
gran: GranularityMajor,
|
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/" + ReleaseRef + "/stream/stable/versions/major/v9/image.json",
|
|
|
|
wantURL: constants.CDNRepositoryURL + "/" + constants.CDNAPIPrefix + "/ref/" + ReleaseRef + "/stream/stable/versions/major/v9/image.json",
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"release with minor image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
gran: GranularityMinor,
|
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/" + ReleaseRef + "/stream/stable/versions/minor/v9.9/image.json",
|
|
|
|
wantURL: constants.CDNRepositoryURL + "/" + constants.CDNAPIPrefix + "/ref/" + ReleaseRef + "/stream/stable/versions/minor/v9.9/image.json",
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"release with patch image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
gran: GranularityPatch,
|
|
|
|
wantPath: "",
|
|
|
|
wantURL: "",
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"release with unknown image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
gran: GranularityUnknown,
|
|
|
|
wantPath: "",
|
|
|
|
wantURL: "",
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"release debug stream image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
gran: GranularityMajor,
|
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/" + ReleaseRef + "/stream/debug/versions/major/v9/image.json",
|
|
|
|
wantURL: constants.CDNRepositoryURL + "/" + constants.CDNAPIPrefix + "/ref/" + ReleaseRef + "/stream/debug/versions/major/v9/image.json",
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"release debug stream with minor image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
gran: GranularityMinor,
|
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/" + ReleaseRef + "/stream/debug/versions/minor/v9.9/image.json",
|
|
|
|
wantURL: constants.CDNRepositoryURL + "/" + constants.CDNAPIPrefix + "/ref/" + ReleaseRef + "/stream/debug/versions/minor/v9.9/image.json",
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"branch ref image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: "foo",
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
gran: GranularityMajor,
|
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/foo/stream/debug/versions/major/v9/image.json",
|
|
|
|
wantURL: constants.CDNRepositoryURL + "/" + constants.CDNAPIPrefix + "/ref/foo/stream/debug/versions/major/v9/image.json",
|
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"branch ref with minor image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: "foo",
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
gran: GranularityMinor,
|
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/foo/stream/debug/versions/minor/v9.9/image.json",
|
|
|
|
wantURL: constants.CDNRepositoryURL + "/" + constants.CDNAPIPrefix + "/ref/foo/stream/debug/versions/minor/v9.9/image.json",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(fmt.Sprintf("%s url", name), func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
url := tc.ver.ListURL(tc.gran)
|
|
|
|
assert.Equal(tc.wantURL, url)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("%s path", name), func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
path := tc.ver.ListPath(tc.gran)
|
|
|
|
assert.Equal(tc.wantPath, path)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 05:05:42 -05:00
|
|
|
func TestVersionArtifactURL(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
ver Version
|
|
|
|
csp cloudprovider.Provider
|
|
|
|
wantMeasurementURL string
|
|
|
|
wantSignatureURL string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"nightly-feature": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: "feat-some-feature",
|
|
|
|
stream: "nightly",
|
|
|
|
version: "v2.6.0-pre.0.20230217095603-193dd48ca19f",
|
|
|
|
kind: VersionKindImage,
|
2023-02-03 05:05:42 -05:00
|
|
|
},
|
|
|
|
csp: cloudprovider.GCP,
|
2023-05-22 08:59:28 -04:00
|
|
|
wantMeasurementURL: constants.CDNRepositoryURL + "/" + constants.CDNAPIPrefixV2 + "/ref/feat-some-feature/stream/nightly/v2.6.0-pre.0.20230217095603-193dd48ca19f/image/measurements.json",
|
|
|
|
wantSignatureURL: constants.CDNRepositoryURL + "/" + constants.CDNAPIPrefixV2 + "/ref/feat-some-feature/stream/nightly/v2.6.0-pre.0.20230217095603-193dd48ca19f/image/measurements.json.sig",
|
2023-02-03 05:05:42 -05:00
|
|
|
},
|
|
|
|
"fail for wrong kind": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
kind: VersionKindCLI,
|
2023-02-03 05:05:42 -05:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2023-05-22 08:59:28 -04:00
|
|
|
measurementURL, signatureURL, err := MeasurementURL(tc.ver)
|
2023-02-03 05:05:42 -05:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.wantMeasurementURL, measurementURL.String())
|
|
|
|
assert.Equal(tc.wantSignatureURL, signatureURL.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-05 09:52:57 -05:00
|
|
|
func TestVersionArtifactPathURL(t *testing.T) {
|
2022-12-29 11:23:23 -05:00
|
|
|
testCases := map[string]struct {
|
|
|
|
ver Version
|
|
|
|
wantPath string
|
|
|
|
}{
|
2023-02-24 06:00:04 -05:00
|
|
|
"release image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
2023-01-04 11:07:16 -05:00
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/" + ReleaseRef + "/stream/stable/v9.9.9",
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"release debug stream image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
2023-01-04 11:07:16 -05:00
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/" + ReleaseRef + "/stream/debug/v9.9.9",
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"branch ref image": {
|
2022-12-29 11:23:23 -05:00
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: "foo",
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindImage,
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
2023-01-04 11:07:16 -05:00
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/foo/stream/debug/v9.9.9",
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
2023-02-24 06:00:04 -05:00
|
|
|
"release cli": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindCLI,
|
2023-02-24 06:00:04 -05:00
|
|
|
},
|
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/" + ReleaseRef + "/stream/stable/v9.9.9",
|
|
|
|
},
|
|
|
|
"release debug stream cli": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindCLI,
|
2023-02-24 06:00:04 -05:00
|
|
|
},
|
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/" + ReleaseRef + "/stream/debug/v9.9.9",
|
|
|
|
},
|
|
|
|
"branch ref cli": {
|
|
|
|
ver: Version{
|
2023-08-01 10:48:13 -04:00
|
|
|
ref: "foo",
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
kind: VersionKindCLI,
|
2023-02-24 06:00:04 -05:00
|
|
|
},
|
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/foo/stream/debug/v9.9.9",
|
|
|
|
},
|
2022-12-29 11:23:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2023-05-25 08:33:57 -04:00
|
|
|
path := tc.ver.ArtifactPath(APIV1)
|
2022-12-29 11:23:23 -05:00
|
|
|
assert.Equal(tc.wantPath, path)
|
2023-05-25 08:33:57 -04:00
|
|
|
url := tc.ver.ArtifactsURL(APIV1)
|
2023-01-05 09:52:57 -05:00
|
|
|
assert.Equal(constants.CDNRepositoryURL+"/"+tc.wantPath, url)
|
2022-12-29 11:23:23 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionKindUnMarshalJson(t *testing.T) {
|
|
|
|
testCases := map[string]VersionKind{
|
|
|
|
`"image"`: VersionKindImage,
|
2023-02-24 06:00:04 -05:00
|
|
|
`"cli"`: VersionKindCLI,
|
2022-12-29 11:23:23 -05:00
|
|
|
`"unknown"`: VersionKindUnknown,
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, kind := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
data, err := kind.MarshalJSON()
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(name, string(data))
|
|
|
|
|
|
|
|
var gotKind VersionKind
|
|
|
|
err = gotKind.UnmarshalJSON([]byte(name))
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(kind, gotKind)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionKindFromString(t *testing.T) {
|
|
|
|
testCases := map[string]VersionKind{
|
|
|
|
"image": VersionKindImage,
|
2023-02-24 06:00:04 -05:00
|
|
|
"cli": VersionKindCLI,
|
2022-12-29 11:23:23 -05:00
|
|
|
"unknown": VersionKindUnknown,
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, kind := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
s := kind.String()
|
|
|
|
assert.Equal(name, s)
|
|
|
|
|
|
|
|
k := VersionKindFromString(name)
|
|
|
|
assert.Equal(kind, k)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGranularityUnMarschalJSON(t *testing.T) {
|
|
|
|
testCases := map[string]Granularity{
|
|
|
|
`"major"`: GranularityMajor,
|
|
|
|
`"minor"`: GranularityMinor,
|
|
|
|
`"patch"`: GranularityPatch,
|
|
|
|
`"unknown"`: GranularityUnknown,
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, gran := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
data, err := gran.MarshalJSON()
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(name, string(data))
|
|
|
|
|
|
|
|
var gotGran Granularity
|
|
|
|
err = gotGran.UnmarshalJSON([]byte(name))
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(gran, gotGran)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGranularityFromString(t *testing.T) {
|
|
|
|
testCases := map[string]Granularity{
|
|
|
|
"major": GranularityMajor,
|
|
|
|
"minor": GranularityMinor,
|
|
|
|
"patch": GranularityPatch,
|
|
|
|
"unknown": GranularityUnknown,
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, gran := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
s := gran.String()
|
|
|
|
assert.Equal(name, s)
|
|
|
|
|
|
|
|
g := GranularityFromString(name)
|
|
|
|
assert.Equal(gran, g)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCanonicalRef(t *testing.T) {
|
|
|
|
testCases := map[string]string{
|
|
|
|
"feat/foo": "feat-foo",
|
|
|
|
"feat-foo": "feat-foo",
|
|
|
|
"feat$foo": "feat-foo",
|
|
|
|
"3234": "3234",
|
|
|
|
"feat foo": "feat-foo",
|
|
|
|
"/../": "----",
|
|
|
|
ReleaseRef: ReleaseRef,
|
|
|
|
".": "",
|
|
|
|
}
|
|
|
|
|
|
|
|
for ref, want := range testCases {
|
|
|
|
t.Run(ref, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
assert.Equal(want, CanonicalizeRef(ref))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateRef(t *testing.T) {
|
|
|
|
testCases := map[string]bool{
|
|
|
|
"feat/foo": true,
|
|
|
|
"feat-foo": false,
|
|
|
|
"feat$foo": true,
|
|
|
|
"3234": false,
|
|
|
|
"feat foo": true,
|
|
|
|
"refs-heads-feat-foo": true,
|
|
|
|
"": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for ref, wantErr := range testCases {
|
|
|
|
t.Run(ref, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
err := ValidateRef(ref)
|
|
|
|
if !wantErr {
|
|
|
|
assert.NoError(err)
|
|
|
|
} else {
|
|
|
|
assert.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateStream(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
branch string
|
|
|
|
stream string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{branch: "-", stream: "stable", wantErr: false},
|
|
|
|
{branch: "-", stream: "debug", wantErr: false},
|
|
|
|
{branch: "-", stream: "nightly", wantErr: true},
|
|
|
|
{branch: "-", stream: "console", wantErr: false},
|
|
|
|
{branch: "main", stream: "stable", wantErr: true},
|
|
|
|
{branch: "main", stream: "debug", wantErr: false},
|
|
|
|
{branch: "main", stream: "nightly", wantErr: false},
|
|
|
|
{branch: "main", stream: "console", wantErr: false},
|
|
|
|
{branch: "foo-branch", stream: "nightly", wantErr: false},
|
|
|
|
{branch: "foo-branch", stream: "console", wantErr: false},
|
|
|
|
{branch: "foo-branch", stream: "debug", wantErr: false},
|
|
|
|
{branch: "foo-branch", stream: "stable", wantErr: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.branch+"+"+tc.stream, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
err := ValidateStream(tc.branch, tc.stream)
|
|
|
|
if !tc.wantErr {
|
|
|
|
assert.NoError(err)
|
|
|
|
} else {
|
|
|
|
assert.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShortPath(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
ref string
|
|
|
|
stream string
|
|
|
|
version string
|
|
|
|
}{
|
|
|
|
"v9.9.9": {
|
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
},
|
|
|
|
"v9.9.9-foo": {
|
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9-foo",
|
|
|
|
},
|
|
|
|
"stream/debug/v9.9.9": {
|
|
|
|
ref: ReleaseRef,
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
},
|
|
|
|
"ref/foo/stream/debug/v9.9.9": {
|
|
|
|
ref: "foo",
|
|
|
|
stream: "debug",
|
|
|
|
version: "v9.9.9",
|
|
|
|
},
|
2023-01-05 10:55:57 -05:00
|
|
|
"ref/foo/stream/stable/v9.9.9": {
|
|
|
|
ref: "foo",
|
|
|
|
stream: "stable",
|
|
|
|
version: "v9.9.9",
|
|
|
|
},
|
2022-12-29 11:23:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
path := shortPath(tc.ref, tc.stream, tc.version)
|
|
|
|
assert.Equal(name, path)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseShortPath(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
wantRef string
|
|
|
|
wantStream string
|
|
|
|
wantVersion string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"v9.9.9": {
|
|
|
|
wantRef: ReleaseRef,
|
|
|
|
wantStream: "stable",
|
|
|
|
wantVersion: "v9.9.9",
|
|
|
|
},
|
|
|
|
"v9.9.9-foo": {
|
|
|
|
wantRef: ReleaseRef,
|
|
|
|
wantStream: "stable",
|
|
|
|
wantVersion: "v9.9.9-foo",
|
|
|
|
},
|
|
|
|
"stream/debug/v9.9.9": {
|
|
|
|
wantRef: ReleaseRef,
|
|
|
|
wantStream: "debug",
|
|
|
|
wantVersion: "v9.9.9",
|
|
|
|
},
|
|
|
|
"ref/foo/stream/debug/v9.9.9": {
|
|
|
|
wantRef: "foo",
|
|
|
|
wantStream: "debug",
|
|
|
|
wantVersion: "v9.9.9",
|
|
|
|
},
|
|
|
|
"v9.9.9-foo/bar": {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"ref/foo/stream/debug/va.b.9": {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"stream/debug/va.b.9": {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"ref/foo/stream/bar/v9.9.9": {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"stream/bar/v9.9.9": {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"ref/refs-heads-bar/stream/debug/v9.9.9": {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
ref, stream, version, err := parseShortPath(name)
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.wantRef, ref)
|
|
|
|
assert.Equal(tc.wantStream, stream)
|
|
|
|
assert.Equal(tc.wantVersion, version)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|