File: check_test.go

package info (click to toggle)
docker.io 20.10.24%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates
  • size: 60,824 kB
  • sloc: sh: 5,621; makefile: 593; ansic: 179; python: 162; asm: 7
file content (52 lines) | stat: -rw-r--r-- 1,560 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package kubernetes

import (
	"testing"

	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
	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
	}{
		{"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},
	}

	for _, test := range tests {
		version, err := getAPIVersion(test.groups)
		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,
	}
}