2019-01-16 06:56:37 -05:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2019-01-16 06:56:37 -05:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetStackAPIVersion(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
description string
|
|
|
|
groups *metav1.APIGroupList
|
|
|
|
err bool
|
|
|
|
expectedStack StackVersion
|
|
|
|
}{
|
2020-10-02 08:19:34 -04:00
|
|
|
{"no stack api", makeGroups(), true, ""},
|
|
|
|
{"v1beta1", makeGroups(groupVersion{"compose.docker.com", []string{"v1beta1"}}), false, StackAPIV1Beta1},
|
|
|
|
{"v1beta2", makeGroups(groupVersion{"compose.docker.com", []string{"v1beta2"}}), false, StackAPIV1Beta2},
|
|
|
|
{"most recent has precedence", makeGroups(groupVersion{"compose.docker.com", []string{"v1beta1", "v1beta2"}}), false, StackAPIV1Beta2},
|
|
|
|
{"most recent has precedence", makeGroups(groupVersion{"compose.docker.com", []string{"v1beta1", "v1beta2", "v1alpha3"}}), false, StackAPIV1Alpha3},
|
2019-01-16 06:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2020-10-02 08:19:34 -04:00
|
|
|
version, err := getAPIVersion(test.groups)
|
2019-01-16 06:56:37 -05:00
|
|
|
if test.err {
|
|
|
|
assert.ErrorContains(t, err, "")
|
|
|
|
} else {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
assert.Check(t, is.Equal(test.expectedStack, version))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type groupVersion struct {
|
|
|
|
name string
|
|
|
|
versions []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeGroups(versions ...groupVersion) *metav1.APIGroupList {
|
|
|
|
groups := make([]metav1.APIGroup, len(versions))
|
|
|
|
for i := range versions {
|
|
|
|
groups[i].Name = versions[i].name
|
|
|
|
for _, v := range versions[i].versions {
|
|
|
|
groups[i].Versions = append(groups[i].Versions, metav1.GroupVersionForDiscovery{Version: v})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &metav1.APIGroupList{
|
|
|
|
Groups: groups,
|
|
|
|
}
|
|
|
|
}
|