-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: zhangzujian <[email protected]>
- Loading branch information
1 parent
6016e30
commit 8efcf19
Showing
13 changed files
with
1,037 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package v1 | ||
|
||
import ( | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSetCondition(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
conditions Conditions | ||
ctype ConditionType | ||
status corev1.ConditionStatus | ||
reason string | ||
message string | ||
generation int64 | ||
expctedLen int | ||
}{ | ||
{ | ||
name: "add to nil conditions", | ||
conditions: nil, | ||
ctype: "Foo", | ||
status: corev1.ConditionTrue, | ||
reason: "insert", | ||
message: "foo", | ||
generation: 1, | ||
expctedLen: 1, | ||
}, | ||
{ | ||
name: "insert a new condition", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue}}, | ||
ctype: "Bar", | ||
status: corev1.ConditionTrue, | ||
reason: "insert", | ||
message: "bar", | ||
generation: 2, | ||
expctedLen: 2, | ||
}, | ||
{ | ||
name: "update an existing condition", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}}, | ||
ctype: "Foo", | ||
status: corev1.ConditionFalse, | ||
reason: "update", | ||
message: "bar", | ||
generation: 2, | ||
expctedLen: 1, | ||
}, | ||
{ | ||
name: "no op", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue, Reason: "noop", Message: "foo", ObservedGeneration: 1}}, | ||
ctype: "Foo", | ||
status: corev1.ConditionTrue, | ||
reason: "noop", | ||
message: "foo", | ||
generation: 1, | ||
expctedLen: 1, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.conditions.SetCondition(tt.ctype, tt.status, tt.reason, tt.message, 1) | ||
require.Len(t, tt.conditions, tt.expctedLen) | ||
}) | ||
} | ||
} | ||
|
||
func TestRemoveCondition(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
conditions Conditions | ||
ctype ConditionType | ||
expctedLen int | ||
}{ | ||
{ | ||
name: "remove from a nil conditions", | ||
conditions: nil, | ||
ctype: "Foo", | ||
expctedLen: 0, | ||
}, | ||
{ | ||
name: "remove from an empty conditions", | ||
conditions: Conditions{}, | ||
ctype: "Foo", | ||
expctedLen: 0, | ||
}, | ||
{ | ||
name: "remove an existing condition", | ||
conditions: Conditions{{ | ||
Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1, | ||
}, { | ||
Type: "Bar", Status: corev1.ConditionFalse, ObservedGeneration: 2, | ||
}}, | ||
ctype: "Foo", | ||
expctedLen: 1, | ||
}, | ||
{ | ||
name: "remove the only condition", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}}, | ||
ctype: "Foo", | ||
expctedLen: 0, | ||
}, | ||
{ | ||
name: "remove a non-existent condition", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}}, | ||
ctype: "Bar", | ||
expctedLen: 1, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.conditions.RemoveCondition(tt.ctype) | ||
require.Len(t, tt.conditions, tt.expctedLen) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetCondition(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
conditions Conditions | ||
ctype ConditionType | ||
expcted *Condition | ||
}{ | ||
{ | ||
name: "get from a nil conditions", | ||
conditions: nil, | ||
ctype: "Foo", | ||
expcted: nil, | ||
}, | ||
{ | ||
name: "get from an empty conditions", | ||
conditions: Conditions{}, | ||
ctype: "Foo", | ||
expcted: nil, | ||
}, | ||
{ | ||
name: "get an existing condition", | ||
conditions: Conditions{{ | ||
Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1, | ||
}, { | ||
Type: "Bar", Status: corev1.ConditionFalse, ObservedGeneration: 2, | ||
}}, | ||
ctype: "Foo", | ||
expcted: &Condition{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}, | ||
}, | ||
{ | ||
name: "get the only condition", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}}, | ||
ctype: "Foo", | ||
expcted: &Condition{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}, | ||
}, | ||
{ | ||
name: "get a non-existent condition", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}}, | ||
ctype: "Bar", | ||
expcted: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := tt.conditions.GetCondition(tt.ctype) | ||
require.Equal(t, tt.expcted, c) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 := 0; i < st.NumFields(); i++ { | ||
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.