2022-12-29 11:23:23 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package versionsapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestImageInfoJSONPath(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
info ImageInfo
|
|
|
|
wantPath string
|
|
|
|
}{
|
|
|
|
"image info": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
},
|
2023-01-04 11:07:16 -05:00
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/test-ref/stream/nightly/v1.0.0/image/info.json",
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
"image info release": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: ReleaseRef,
|
|
|
|
Stream: "stable",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
},
|
2023-01-04 11:07:16 -05:00
|
|
|
wantPath: constants.CDNAPIPrefix + "/ref/-/stream/stable/v1.0.0/image/info.json",
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert.Equal(t, tc.wantPath, tc.info.JSONPath())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImageInfoURL(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
info ImageInfo
|
|
|
|
wantURL string
|
|
|
|
wantPath string
|
|
|
|
}{
|
|
|
|
"image info": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
},
|
2023-01-04 11:07:16 -05:00
|
|
|
wantURL: constants.CDNRepositoryURL + "/" + constants.CDNAPIPrefix + "/ref/test-ref/stream/nightly/v1.0.0/image/info.json",
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
"image info release": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: ReleaseRef,
|
|
|
|
Stream: "stable",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
},
|
2023-01-04 11:07:16 -05:00
|
|
|
wantURL: constants.CDNRepositoryURL + "/" + constants.CDNAPIPrefix + "/ref/-/stream/stable/v1.0.0/image/info.json",
|
2022-12-29 11:23:23 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
url, err := tc.info.URL()
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.wantURL, url)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImageInfoValidate(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
info ImageInfo
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"valid image info": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
AWS: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
GCP: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
Azure: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
QEMU: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"invalid ref": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
AWS: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
GCP: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
Azure: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
QEMU: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid stream": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
AWS: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
GCP: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
Azure: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
QEMU: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid version": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "",
|
|
|
|
AWS: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
GCP: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
Azure: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
QEMU: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-02-27 12:19:52 -05:00
|
|
|
"no provider": {
|
2022-12-29 11:23:23 -05:00
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"multiple errors": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "",
|
|
|
|
Stream: "",
|
|
|
|
Version: "",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
err := tc.info.Validate()
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImageInfoValidateRequest(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
info ImageInfo
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"valid image info": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"invalid ref": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid stream": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid version": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid gcp": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
GCP: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid azure": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
Azure: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid aws": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
AWS: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid qemu": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
QEMU: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-02-27 12:19:52 -05:00
|
|
|
"invalid openstack": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
|
|
|
Version: "v1.0.0",
|
|
|
|
OpenStack: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-12-29 11:23:23 -05:00
|
|
|
"multiple errors": {
|
|
|
|
info: ImageInfo{
|
|
|
|
Ref: "",
|
|
|
|
Stream: "",
|
|
|
|
Version: "",
|
|
|
|
AWS: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
GCP: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
Azure: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
QEMU: map[string]string{"key": "value", "key2": "value2"},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
err := tc.info.ValidateRequest()
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|