[scheduler] avoid comparing function pointers in unit tests

PluginFactory is a function that returns a plugin. We have been
comparing these functions in unit tests and it has worked so far, but
starts to fail in gotip/master.

Note from the golang team:
```
Func values are incomparable. It is true that you could get the PC
through reflection but it is still not expected to be compared. PCs
can change due to inlining, wrappers, etc., depending on the
compiler's details, which is subject to change.
```

Signed-off-by: Davanum Srinivas <davanum@gmail.com>
This commit is contained in:
Davanum Srinivas 2021-05-25 09:21:08 -04:00
parent ae2df8aa37
commit 383ce85649
No known key found for this signature in database
GPG Key ID: 80D83A796103BF59

View File

@ -20,6 +20,8 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/google/uuid"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/scheduler/framework" "k8s.io/kubernetes/pkg/scheduler/framework"
) )
@ -76,7 +78,9 @@ func TestDecodeInto(t *testing.T) {
func isRegistryEqual(registryX, registryY Registry) bool { func isRegistryEqual(registryX, registryY Registry) bool {
for name, pluginFactory := range registryY { for name, pluginFactory := range registryY {
if val, ok := registryX[name]; ok { 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. // pluginFactory functions are not the same.
return false return false
} }
@ -96,19 +100,24 @@ func isRegistryEqual(registryX, registryY Registry) bool {
return true return true
} }
type mockNoopPlugin struct{} type mockNoopPlugin struct {
uuid string
}
func (p *mockNoopPlugin) Name() string { func (p *mockNoopPlugin) Name() string {
return "MockNoop" return p.uuid
} }
func NewMockNoopPluginFactory() PluginFactory { func NewMockNoopPluginFactory() PluginFactory {
uuid := uuid.New().String()
return func(_ runtime.Object, _ framework.Handle) (framework.Plugin, error) { return func(_ runtime.Object, _ framework.Handle) (framework.Plugin, error) {
return &mockNoopPlugin{}, nil return &mockNoopPlugin{uuid}, nil
} }
} }
func TestMerge(t *testing.T) { func TestMerge(t *testing.T) {
m1 := NewMockNoopPluginFactory()
m2 := NewMockNoopPluginFactory()
tests := []struct { tests := []struct {
name string name string
primaryRegistry Registry primaryRegistry Registry
@ -119,27 +128,27 @@ func TestMerge(t *testing.T) {
{ {
name: "valid Merge", name: "valid Merge",
primaryRegistry: Registry{ primaryRegistry: Registry{
"pluginFactory1": NewMockNoopPluginFactory(), "pluginFactory1": m1,
}, },
registryToMerge: Registry{ registryToMerge: Registry{
"pluginFactory2": NewMockNoopPluginFactory(), "pluginFactory2": m2,
}, },
expected: Registry{ expected: Registry{
"pluginFactory1": NewMockNoopPluginFactory(), "pluginFactory1": m1,
"pluginFactory2": NewMockNoopPluginFactory(), "pluginFactory2": m2,
}, },
shouldError: false, shouldError: false,
}, },
{ {
name: "Merge duplicate factories", name: "Merge duplicate factories",
primaryRegistry: Registry{ primaryRegistry: Registry{
"pluginFactory1": NewMockNoopPluginFactory(), "pluginFactory1": m1,
}, },
registryToMerge: Registry{ registryToMerge: Registry{
"pluginFactory1": NewMockNoopPluginFactory(), "pluginFactory1": m2,
}, },
expected: Registry{ expected: Registry{
"pluginFactory1": NewMockNoopPluginFactory(), "pluginFactory1": m1,
}, },
shouldError: true, shouldError: true,
}, },
@ -162,6 +171,8 @@ func TestMerge(t *testing.T) {
} }
func TestRegister(t *testing.T) { func TestRegister(t *testing.T) {
m1 := NewMockNoopPluginFactory()
m2 := NewMockNoopPluginFactory()
tests := []struct { tests := []struct {
name string name string
registry Registry registry Registry
@ -174,21 +185,21 @@ func TestRegister(t *testing.T) {
name: "valid Register", name: "valid Register",
registry: Registry{}, registry: Registry{},
nameToRegister: "pluginFactory1", nameToRegister: "pluginFactory1",
factoryToRegister: NewMockNoopPluginFactory(), factoryToRegister: m1,
expected: Registry{ expected: Registry{
"pluginFactory1": NewMockNoopPluginFactory(), "pluginFactory1": m1,
}, },
shouldError: false, shouldError: false,
}, },
{ {
name: "Register duplicate factories", name: "Register duplicate factories",
registry: Registry{ registry: Registry{
"pluginFactory1": NewMockNoopPluginFactory(), "pluginFactory1": m1,
}, },
nameToRegister: "pluginFactory1", nameToRegister: "pluginFactory1",
factoryToRegister: NewMockNoopPluginFactory(), factoryToRegister: m2,
expected: Registry{ expected: Registry{
"pluginFactory1": NewMockNoopPluginFactory(), "pluginFactory1": m1,
}, },
shouldError: true, shouldError: true,
}, },
@ -211,6 +222,8 @@ func TestRegister(t *testing.T) {
} }
func TestUnregister(t *testing.T) { func TestUnregister(t *testing.T) {
m1 := NewMockNoopPluginFactory()
m2 := NewMockNoopPluginFactory()
tests := []struct { tests := []struct {
name string name string
registry Registry registry Registry
@ -221,12 +234,12 @@ func TestUnregister(t *testing.T) {
{ {
name: "valid Unregister", name: "valid Unregister",
registry: Registry{ registry: Registry{
"pluginFactory1": NewMockNoopPluginFactory(), "pluginFactory1": m1,
"pluginFactory2": NewMockNoopPluginFactory(), "pluginFactory2": m2,
}, },
nameToUnregister: "pluginFactory1", nameToUnregister: "pluginFactory1",
expected: Registry{ expected: Registry{
"pluginFactory2": NewMockNoopPluginFactory(), "pluginFactory2": m2,
}, },
shouldError: false, shouldError: false,
}, },