-
Notifications
You must be signed in to change notification settings - Fork 527
Expand file tree
/
Copy pathregister_test.go
More file actions
102 lines (86 loc) · 2.43 KB
/
register_test.go
File metadata and controls
102 lines (86 loc) · 2.43 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package v1
import (
"go/types"
"path"
"reflect"
"runtime"
"strings"
"testing"
"golang.org/x/tools/go/packages"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/utils/set"
"github.com/stretchr/testify/require"
"github.com/kubeovn/kube-ovn/pkg/apis/kubeovn"
)
func TestKind(t *testing.T) {
gk := Kind("foo")
require.Equal(t, schema.GroupKind{Group: kubeovn.GroupName, Kind: "foo"}, gk)
}
func TestResource(t *testing.T) {
gr := Resource("foo")
require.Equal(t, schema.GroupResource{Group: kubeovn.GroupName, Resource: "foo"}, gr)
}
func getPackagePath(t *testing.T) string {
t.Helper()
pc, _, _, _ := runtime.Caller(1)
funcName := runtime.FuncForPC(pc).Name()
base := path.Base(funcName)
index := strings.LastIndexByte(base, '.')
require.Greater(t, index, 0)
return path.Join(path.Dir(funcName), base[:index])
}
func TestAddResources(t *testing.T) {
pkgPath := getPackagePath(t)
config := &packages.Config{
Mode: packages.NeedTypes | packages.NeedTypesInfo,
}
pkgs, err := packages.Load(config, pkgPath)
require.NoError(t, err, "failed to load package %q", pkgPath)
require.Len(t, pkgs, 1, "expected exactly one package, got %d", len(pkgs))
scheme := k8sruntime.NewScheme()
err = addKnownTypes(scheme)
require.NoError(t, err)
addedTypes := set.New[string]()
for name, vt := range scheme.KnownTypes(SchemeGroupVersion) {
if vt.PkgPath() != pkgPath {
continue
}
require.Implements(t, (*k8sruntime.Object)(nil), reflect.New(vt).Interface())
addedTypes.Insert(name)
}
scope := pkgs[0].Types.Scope()
require.NotNil(t, scope)
typeMetaType := reflect.TypeFor[metav1.TypeMeta]()
objMetaType := reflect.TypeFor[metav1.ObjectMeta]()
listMetaType := reflect.TypeFor[metav1.ListMeta]()
for _, name := range scope.Names() {
obj := scope.Lookup(name)
if !obj.Exported() {
continue
}
st, ok := obj.Type().Underlying().(*types.Struct)
if !ok {
continue
}
var hasTypeMeta, hasObjMeta, hasListMeta bool
for i := range st.NumFields() {
v := st.Field(i)
if !v.Embedded() || !v.Exported() {
continue
}
switch v.Name() {
case typeMetaType.Name():
hasTypeMeta = true
case objMetaType.Name():
hasObjMeta = true
case listMetaType.Name():
hasListMeta = true
}
}
if hasTypeMeta && (hasObjMeta || hasListMeta) {
require.True(t, addedTypes.Has(name), "type %q not registered", name)
}
}
}