...
1
16
17 package reference
18
19 import (
20 "testing"
21
22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23 "k8s.io/apimachinery/pkg/runtime"
24 "k8s.io/apimachinery/pkg/runtime/schema"
25 )
26
27 type TestRuntimeObj struct {
28 metav1.TypeMeta
29 metav1.ObjectMeta
30 }
31
32 func (o *TestRuntimeObj) DeepCopyObject() runtime.Object {
33 panic("die")
34 }
35
36 func TestGetReferenceRefVersion(t *testing.T) {
37 tests := []struct {
38 name string
39 input *TestRuntimeObj
40 groupVersion schema.GroupVersion
41 expectedRefVersion string
42 }{
43 {
44 name: "v1 GV from scheme",
45 input: &TestRuntimeObj{
46 ObjectMeta: metav1.ObjectMeta{},
47 },
48 groupVersion: schema.GroupVersion{Group: "", Version: "v1"},
49 expectedRefVersion: "v1",
50 },
51 {
52 name: "foo.group/v3 GV from scheme",
53 input: &TestRuntimeObj{
54 ObjectMeta: metav1.ObjectMeta{},
55 },
56 groupVersion: schema.GroupVersion{Group: "foo.group", Version: "v3"},
57 expectedRefVersion: "foo.group/v3",
58 },
59 }
60
61 for _, test := range tests {
62 t.Run(test.name, func(t *testing.T) {
63 scheme := runtime.NewScheme()
64 scheme.AddKnownTypes(test.groupVersion, &TestRuntimeObj{})
65 ref, err := GetReference(scheme, test.input)
66 if err != nil {
67 t.Fatal(err)
68 }
69 if test.expectedRefVersion != ref.APIVersion {
70 t.Errorf("expected %q, got %q", test.expectedRefVersion, ref.APIVersion)
71 }
72 })
73 }
74 }
75
View as plain text