mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Merge pull request #102288 from dims/avoid-checking-func-pointers
[scheduler] avoid comparing function pointers in unit tests
This commit is contained in:
commit
10869586bc
@ -20,6 +20,8 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/scheduler/framework"
|
||||
)
|
||||
@ -76,7 +78,9 @@ func TestDecodeInto(t *testing.T) {
|
||||
func isRegistryEqual(registryX, registryY Registry) bool {
|
||||
for name, pluginFactory := range registryY {
|
||||
if val, ok := registryX[name]; ok {
|
||||
if reflect.ValueOf(pluginFactory).Pointer() != reflect.ValueOf(val).Pointer() {
|
||||
p1, _ := pluginFactory(nil, nil)
|
||||
p2, _ := val(nil, nil)
|
||||
if p1.Name() != p2.Name() {
|
||||
// pluginFactory functions are not the same.
|
||||
return false
|
||||
}
|
||||
@ -96,19 +100,24 @@ func isRegistryEqual(registryX, registryY Registry) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type mockNoopPlugin struct{}
|
||||
type mockNoopPlugin struct {
|
||||
uuid string
|
||||
}
|
||||
|
||||
func (p *mockNoopPlugin) Name() string {
|
||||
return "MockNoop"
|
||||
return p.uuid
|
||||
}
|
||||
|
||||
func NewMockNoopPluginFactory() PluginFactory {
|
||||
uuid := uuid.New().String()
|
||||
return func(_ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
|
||||
return &mockNoopPlugin{}, nil
|
||||
return &mockNoopPlugin{uuid}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestMerge(t *testing.T) {
|
||||
m1 := NewMockNoopPluginFactory()
|
||||
m2 := NewMockNoopPluginFactory()
|
||||
tests := []struct {
|
||||
name string
|
||||
primaryRegistry Registry
|
||||
@ -119,27 +128,27 @@ func TestMerge(t *testing.T) {
|
||||
{
|
||||
name: "valid Merge",
|
||||
primaryRegistry: Registry{
|
||||
"pluginFactory1": NewMockNoopPluginFactory(),
|
||||
"pluginFactory1": m1,
|
||||
},
|
||||
registryToMerge: Registry{
|
||||
"pluginFactory2": NewMockNoopPluginFactory(),
|
||||
"pluginFactory2": m2,
|
||||
},
|
||||
expected: Registry{
|
||||
"pluginFactory1": NewMockNoopPluginFactory(),
|
||||
"pluginFactory2": NewMockNoopPluginFactory(),
|
||||
"pluginFactory1": m1,
|
||||
"pluginFactory2": m2,
|
||||
},
|
||||
shouldError: false,
|
||||
},
|
||||
{
|
||||
name: "Merge duplicate factories",
|
||||
primaryRegistry: Registry{
|
||||
"pluginFactory1": NewMockNoopPluginFactory(),
|
||||
"pluginFactory1": m1,
|
||||
},
|
||||
registryToMerge: Registry{
|
||||
"pluginFactory1": NewMockNoopPluginFactory(),
|
||||
"pluginFactory1": m2,
|
||||
},
|
||||
expected: Registry{
|
||||
"pluginFactory1": NewMockNoopPluginFactory(),
|
||||
"pluginFactory1": m1,
|
||||
},
|
||||
shouldError: true,
|
||||
},
|
||||
@ -162,6 +171,8 @@ func TestMerge(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRegister(t *testing.T) {
|
||||
m1 := NewMockNoopPluginFactory()
|
||||
m2 := NewMockNoopPluginFactory()
|
||||
tests := []struct {
|
||||
name string
|
||||
registry Registry
|
||||
@ -174,21 +185,21 @@ func TestRegister(t *testing.T) {
|
||||
name: "valid Register",
|
||||
registry: Registry{},
|
||||
nameToRegister: "pluginFactory1",
|
||||
factoryToRegister: NewMockNoopPluginFactory(),
|
||||
factoryToRegister: m1,
|
||||
expected: Registry{
|
||||
"pluginFactory1": NewMockNoopPluginFactory(),
|
||||
"pluginFactory1": m1,
|
||||
},
|
||||
shouldError: false,
|
||||
},
|
||||
{
|
||||
name: "Register duplicate factories",
|
||||
registry: Registry{
|
||||
"pluginFactory1": NewMockNoopPluginFactory(),
|
||||
"pluginFactory1": m1,
|
||||
},
|
||||
nameToRegister: "pluginFactory1",
|
||||
factoryToRegister: NewMockNoopPluginFactory(),
|
||||
factoryToRegister: m2,
|
||||
expected: Registry{
|
||||
"pluginFactory1": NewMockNoopPluginFactory(),
|
||||
"pluginFactory1": m1,
|
||||
},
|
||||
shouldError: true,
|
||||
},
|
||||
@ -211,6 +222,8 @@ func TestRegister(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUnregister(t *testing.T) {
|
||||
m1 := NewMockNoopPluginFactory()
|
||||
m2 := NewMockNoopPluginFactory()
|
||||
tests := []struct {
|
||||
name string
|
||||
registry Registry
|
||||
@ -221,12 +234,12 @@ func TestUnregister(t *testing.T) {
|
||||
{
|
||||
name: "valid Unregister",
|
||||
registry: Registry{
|
||||
"pluginFactory1": NewMockNoopPluginFactory(),
|
||||
"pluginFactory2": NewMockNoopPluginFactory(),
|
||||
"pluginFactory1": m1,
|
||||
"pluginFactory2": m2,
|
||||
},
|
||||
nameToUnregister: "pluginFactory1",
|
||||
expected: Registry{
|
||||
"pluginFactory2": NewMockNoopPluginFactory(),
|
||||
"pluginFactory2": m2,
|
||||
},
|
||||
shouldError: false,
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user