2022-11-29 05:39:07 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-12-01 04:26:35 -05:00
|
|
|
package versionsapi
|
2022-11-29 05:39:07 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/goleak"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidate(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-12-01 04:26:35 -05:00
|
|
|
listFunc func() *List
|
|
|
|
overrideFunc func(list *List)
|
2022-11-29 05:39:07 -05:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"valid major list": {
|
|
|
|
listFunc: majorList,
|
|
|
|
},
|
|
|
|
"valid minor list": {
|
|
|
|
listFunc: minorList,
|
|
|
|
},
|
|
|
|
"invalid stream": {
|
|
|
|
listFunc: majorList,
|
2022-12-01 04:26:35 -05:00
|
|
|
overrideFunc: func(list *List) {
|
2022-11-29 05:39:07 -05:00
|
|
|
list.Stream = "invalid"
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid granularity": {
|
|
|
|
listFunc: majorList,
|
2022-12-01 04:26:35 -05:00
|
|
|
overrideFunc: func(list *List) {
|
2022-11-29 05:39:07 -05:00
|
|
|
list.Granularity = "invalid"
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid kind": {
|
|
|
|
listFunc: majorList,
|
2022-12-01 04:26:35 -05:00
|
|
|
overrideFunc: func(list *List) {
|
2022-11-29 05:39:07 -05:00
|
|
|
list.Kind = "invalid"
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"base ver is not semantic version": {
|
|
|
|
listFunc: majorList,
|
2022-12-01 04:26:35 -05:00
|
|
|
overrideFunc: func(list *List) {
|
2022-11-29 05:39:07 -05:00
|
|
|
list.Base = "invalid"
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"base ver does not reflect major granularity": {
|
|
|
|
listFunc: majorList,
|
2022-12-01 04:26:35 -05:00
|
|
|
overrideFunc: func(list *List) {
|
2022-11-29 05:39:07 -05:00
|
|
|
list.Base = "v1.0"
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"base ver does not reflect minor granularity": {
|
|
|
|
listFunc: minorList,
|
2022-12-01 04:26:35 -05:00
|
|
|
overrideFunc: func(list *List) {
|
2022-11-29 05:39:07 -05:00
|
|
|
list.Base = "v1"
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"version in list is not semantic version": {
|
|
|
|
listFunc: majorList,
|
2022-12-01 04:26:35 -05:00
|
|
|
overrideFunc: func(list *List) {
|
2022-11-29 05:39:07 -05:00
|
|
|
list.Versions[0] = "invalid"
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"version in list is not sub version of base": {
|
|
|
|
listFunc: majorList,
|
2022-12-01 04:26:35 -05:00
|
|
|
overrideFunc: func(list *List) {
|
2022-11-29 05:39:07 -05:00
|
|
|
list.Versions[0] = "v2.1"
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
list := tc.listFunc()
|
|
|
|
if tc.overrideFunc != nil {
|
|
|
|
tc.overrideFunc(list)
|
|
|
|
}
|
2022-11-29 10:58:22 -05:00
|
|
|
err := list.Validate()
|
2022-11-29 05:39:07 -05:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestList(t *testing.T) {
|
|
|
|
majorListJSON, err := json.Marshal(majorList())
|
|
|
|
require.NoError(t, err)
|
|
|
|
minorListJSON, err := json.Marshal(minorList())
|
|
|
|
require.NoError(t, err)
|
|
|
|
inconsistentList := majorList()
|
|
|
|
inconsistentList.Base = "v2"
|
|
|
|
inconsistentListJSON, err := json.Marshal(inconsistentList)
|
|
|
|
require.NoError(t, err)
|
|
|
|
client := newTestClient(func(req *http.Request) *http.Response {
|
|
|
|
switch req.URL.Path {
|
2022-12-05 09:15:03 -05:00
|
|
|
case "/constellation/v1/ref/test-ref/stream/nightly/versions/major/v1/image.json":
|
2022-11-29 05:39:07 -05:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: io.NopCloser(bytes.NewBuffer(majorListJSON)),
|
|
|
|
Header: make(http.Header),
|
|
|
|
}
|
2022-12-05 09:15:03 -05:00
|
|
|
case "/constellation/v1/ref/test-ref/stream/nightly/versions/minor/v1.1/image.json":
|
2022-11-29 05:39:07 -05:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: io.NopCloser(bytes.NewBuffer(minorListJSON)),
|
|
|
|
Header: make(http.Header),
|
|
|
|
}
|
2022-12-05 09:15:03 -05:00
|
|
|
case "/constellation/v1/ref/test-ref/stream/nightly/versions/major/v1/500.json": // 500 error
|
2022-11-29 05:39:07 -05:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
Body: io.NopCloser(bytes.NewBufferString("Server Error.")),
|
|
|
|
Header: make(http.Header),
|
|
|
|
}
|
2022-12-05 09:15:03 -05:00
|
|
|
case "/constellation/v1/ref/test-ref/stream/nightly/versions/major/v1/nojson.json": // invalid format
|
2022-11-29 05:39:07 -05:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: io.NopCloser(bytes.NewBufferString("not json")),
|
|
|
|
Header: make(http.Header),
|
|
|
|
}
|
2022-12-05 09:15:03 -05:00
|
|
|
case "/constellation/v1/ref/test-ref/stream/nightly/versions/major/v2/image.json": // inconsistent list
|
2022-11-29 05:39:07 -05:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: io.NopCloser(bytes.NewBuffer(inconsistentListJSON)),
|
|
|
|
Header: make(http.Header),
|
|
|
|
}
|
2022-12-05 09:15:03 -05:00
|
|
|
case "/constellation/v1/ref/test-ref/stream/nightly/versions/major/v3/image.json": // does not match requested version
|
2022-11-29 05:39:07 -05:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: io.NopCloser(bytes.NewBuffer(minorListJSON)),
|
|
|
|
Header: make(http.Header),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
Body: io.NopCloser(bytes.NewBufferString("Not found.")),
|
|
|
|
Header: make(http.Header),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-12-05 09:15:03 -05:00
|
|
|
ref, stream, granularity, base, kind string
|
|
|
|
overrideFile string
|
|
|
|
wantList List
|
|
|
|
wantErr bool
|
2022-11-29 05:39:07 -05:00
|
|
|
}{
|
|
|
|
"major list fetched remotely": {
|
|
|
|
wantList: *majorList(),
|
|
|
|
},
|
|
|
|
"minor list fetched remotely": {
|
|
|
|
granularity: "minor",
|
|
|
|
base: "v1.1",
|
|
|
|
wantList: *minorList(),
|
|
|
|
},
|
|
|
|
"list does not exist": {
|
|
|
|
stream: "unknown",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"unexpected error code": {
|
|
|
|
kind: "500",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid json returned": {
|
|
|
|
kind: "nojson",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid list returned": {
|
|
|
|
base: "v2",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"response does not match request": {
|
|
|
|
base: "v3",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-12-05 09:15:03 -05:00
|
|
|
ref := "test-ref"
|
|
|
|
stream := "nightly"
|
2022-11-29 05:39:07 -05:00
|
|
|
granularity := "major"
|
|
|
|
base := "v1"
|
|
|
|
kind := "image"
|
|
|
|
if tc.stream != "" {
|
|
|
|
stream = tc.stream
|
|
|
|
}
|
|
|
|
if tc.granularity != "" {
|
|
|
|
granularity = tc.granularity
|
|
|
|
}
|
|
|
|
if tc.base != "" {
|
|
|
|
base = tc.base
|
|
|
|
}
|
|
|
|
if tc.kind != "" {
|
|
|
|
kind = tc.kind
|
|
|
|
}
|
|
|
|
|
2022-12-01 04:26:35 -05:00
|
|
|
fetcher := &Fetcher{
|
2022-11-29 05:39:07 -05:00
|
|
|
httpc: client,
|
|
|
|
}
|
2022-12-05 09:15:03 -05:00
|
|
|
list, err := fetcher.list(context.Background(), ref, stream, granularity, base, kind)
|
2022-11-29 05:39:07 -05:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tc.wantList, *list)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// roundTripFunc .
|
|
|
|
type roundTripFunc func(req *http.Request) *http.Response
|
|
|
|
|
|
|
|
// RoundTrip .
|
|
|
|
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
return f(req), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTestClient returns *http.Client with Transport replaced to avoid making real calls.
|
|
|
|
func newTestClient(fn roundTripFunc) *http.Client {
|
|
|
|
return &http.Client{
|
|
|
|
Transport: fn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 04:26:35 -05:00
|
|
|
func majorList() *List {
|
|
|
|
return &List{
|
2022-12-05 09:15:03 -05:00
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
2022-11-29 05:39:07 -05:00
|
|
|
Granularity: "major",
|
|
|
|
Base: "v1",
|
|
|
|
Kind: "image",
|
|
|
|
Versions: []string{
|
|
|
|
"v1.0", "v1.1", "v1.2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 04:26:35 -05:00
|
|
|
func minorList() *List {
|
|
|
|
return &List{
|
2022-12-05 09:15:03 -05:00
|
|
|
Ref: "test-ref",
|
|
|
|
Stream: "nightly",
|
2022-11-29 05:39:07 -05:00
|
|
|
Granularity: "minor",
|
|
|
|
Base: "v1.1",
|
|
|
|
Kind: "image",
|
|
|
|
Versions: []string{
|
|
|
|
"v1.1.0", "v1.1.1", "v1.1.2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-12-02 10:59:09 -05:00
|
|
|
|
2022-12-05 09:15:03 -05:00
|
|
|
func TestIsValidRef(t *testing.T) {
|
2022-12-02 10:59:09 -05:00
|
|
|
testCases := map[string]bool{
|
2022-12-05 09:15:03 -05:00
|
|
|
"feat/foo": false,
|
|
|
|
"feat-foo": true,
|
|
|
|
"feat$foo": false,
|
|
|
|
"3234": true,
|
|
|
|
"feat foo": false,
|
|
|
|
"refs-heads-feat-foo": false,
|
|
|
|
"": false,
|
2022-12-02 10:59:09 -05:00
|
|
|
}
|
|
|
|
|
2022-12-05 09:15:03 -05:00
|
|
|
for ref, want := range testCases {
|
|
|
|
t.Run(ref, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
assert.Equal(want, IsValidRef(ref))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
|
|
|
|
for ref, want := range testCases {
|
|
|
|
t.Run(ref, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
assert.Equal(want, CanonicalRef(ref))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsValidStream(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
branch string
|
|
|
|
stream string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{branch: "-", stream: "stable", want: true},
|
|
|
|
{branch: "-", stream: "debug", want: true},
|
|
|
|
{branch: "-", stream: "nightly", want: false},
|
|
|
|
{branch: "-", stream: "console", want: true},
|
|
|
|
{branch: "main", stream: "stable", want: false},
|
|
|
|
{branch: "main", stream: "debug", want: true},
|
|
|
|
{branch: "main", stream: "nightly", want: true},
|
|
|
|
{branch: "main", stream: "console", want: true},
|
|
|
|
{branch: "foo-branch", stream: "nightly", want: true},
|
|
|
|
{branch: "foo-branch", stream: "console", want: true},
|
|
|
|
{branch: "foo-branch", stream: "debug", want: true},
|
|
|
|
{branch: "foo-branch", stream: "stable", want: false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.branch+"+"+tc.stream, func(t *testing.T) {
|
2022-12-02 10:59:09 -05:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-12-05 09:15:03 -05:00
|
|
|
assert.Equal(tc.want, IsValidStream(tc.branch, tc.stream))
|
2022-12-02 10:59:09 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|