mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	Adding in missing Registry unit tests.
This commit is contained in:
		@@ -32,7 +32,7 @@ func TestDecodeInto(t *testing.T) {
 | 
				
			|||||||
	tests := []struct {
 | 
						tests := []struct {
 | 
				
			||||||
		name            string
 | 
							name            string
 | 
				
			||||||
		schedulerConfig string
 | 
							schedulerConfig string
 | 
				
			||||||
		expeted         PluginFooConfig
 | 
							expected        PluginFooConfig
 | 
				
			||||||
	}{
 | 
						}{
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			name: "test decode for JSON config",
 | 
								name: "test decode for JSON config",
 | 
				
			||||||
@@ -57,7 +57,7 @@ func TestDecodeInto(t *testing.T) {
 | 
				
			|||||||
					}
 | 
										}
 | 
				
			||||||
				]
 | 
									]
 | 
				
			||||||
			}`,
 | 
								}`,
 | 
				
			||||||
			expeted: PluginFooConfig{
 | 
								expected: PluginFooConfig{
 | 
				
			||||||
				FooTest: "test decode",
 | 
									FooTest: "test decode",
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
@@ -74,25 +74,28 @@ pluginConfig:
 | 
				
			|||||||
  - name: foo
 | 
					  - name: foo
 | 
				
			||||||
    args:
 | 
					    args:
 | 
				
			||||||
      foo_test: "test decode"`,
 | 
					      foo_test: "test decode"`,
 | 
				
			||||||
			expeted: PluginFooConfig{
 | 
								expected: PluginFooConfig{
 | 
				
			||||||
				FooTest: "test decode",
 | 
									FooTest: "test decode",
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for i, test := range tests {
 | 
					
 | 
				
			||||||
		schedulerConf, err := loadConfig([]byte(test.schedulerConfig))
 | 
						for _, test := range tests {
 | 
				
			||||||
		if err != nil {
 | 
							t.Run(test.name, func(t *testing.T) {
 | 
				
			||||||
			t.Errorf("Test #%v(%s): failed to load scheduler config: %v", i, test.name, err)
 | 
								schedulerConf, err := loadConfig([]byte(test.schedulerConfig))
 | 
				
			||||||
		}
 | 
								if err != nil {
 | 
				
			||||||
		var pluginFooConf PluginFooConfig
 | 
									t.Errorf("loadConfig(): failed to load scheduler config: %v", err)
 | 
				
			||||||
		if err := DecodeInto(&schedulerConf.PluginConfig[0].Args, &pluginFooConf); err != nil {
 | 
								}
 | 
				
			||||||
			t.Errorf("Test #%v(%s): failed to decode args %+v: %v",
 | 
								var pluginFooConf PluginFooConfig
 | 
				
			||||||
				i, test.name, schedulerConf.PluginConfig[0].Args, err)
 | 
								if err := DecodeInto(&schedulerConf.PluginConfig[0].Args, &pluginFooConf); err != nil {
 | 
				
			||||||
		}
 | 
									t.Errorf("DecodeInto(): failed to decode args %+v: %v",
 | 
				
			||||||
		if !reflect.DeepEqual(pluginFooConf, test.expeted) {
 | 
										schedulerConf.PluginConfig[0].Args, err)
 | 
				
			||||||
			t.Errorf("Test #%v(%s): failed to decode plugin config, expected: %+v, got: %+v",
 | 
								}
 | 
				
			||||||
				i, test.name, test.expeted, pluginFooConf)
 | 
								if !reflect.DeepEqual(test.expected, pluginFooConf) {
 | 
				
			||||||
		}
 | 
									t.Errorf("DecodeInto(): failed to decode plugin config, expected: %+v, got: %+v",
 | 
				
			||||||
 | 
										test.expected, pluginFooConf)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -104,3 +107,187 @@ func loadConfig(data []byte) (*config.KubeSchedulerConfiguration, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	return configObj, nil
 | 
						return configObj, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// isRegistryEqual compares two registries for equality. This function is used in place of
 | 
				
			||||||
 | 
					// reflect.DeepEqual() and cmp() as they don't compare function values.
 | 
				
			||||||
 | 
					func isRegistryEqual(registryX, registryY Registry) bool {
 | 
				
			||||||
 | 
						for name, pluginFactory := range registryY {
 | 
				
			||||||
 | 
							if val, ok := registryX[name]; ok {
 | 
				
			||||||
 | 
								if reflect.ValueOf(pluginFactory) != reflect.ValueOf(val) {
 | 
				
			||||||
 | 
									// pluginFactory functions are not the same.
 | 
				
			||||||
 | 
									return false
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								// registryY contains an entry that is not present in registryX
 | 
				
			||||||
 | 
								return false
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for name := range registryX {
 | 
				
			||||||
 | 
							if _, ok := registryY[name]; !ok {
 | 
				
			||||||
 | 
								// registryX contains an entry that is not present in registryY
 | 
				
			||||||
 | 
								return false
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return true
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type mockNoopPlugin struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (p *mockNoopPlugin) Name() string {
 | 
				
			||||||
 | 
						return "MockNoop"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func NewMockNoopPluginFactory() PluginFactory {
 | 
				
			||||||
 | 
						return func(_ *runtime.Unknown, _ FrameworkHandle) (Plugin, error) {
 | 
				
			||||||
 | 
							return &mockNoopPlugin{}, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestMerge(t *testing.T) {
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							name            string
 | 
				
			||||||
 | 
							primaryRegistry Registry
 | 
				
			||||||
 | 
							registryToMerge Registry
 | 
				
			||||||
 | 
							expected        Registry
 | 
				
			||||||
 | 
							shouldError     bool
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name: "valid Merge",
 | 
				
			||||||
 | 
								primaryRegistry: Registry{
 | 
				
			||||||
 | 
									"pluginFactory1": NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								registryToMerge: Registry{
 | 
				
			||||||
 | 
									"pluginFactory2": NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								expected: Registry{
 | 
				
			||||||
 | 
									"pluginFactory1": NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
									"pluginFactory2": NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								shouldError: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name: "Merge duplicate factories",
 | 
				
			||||||
 | 
								primaryRegistry: Registry{
 | 
				
			||||||
 | 
									"pluginFactory1": NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								registryToMerge: Registry{
 | 
				
			||||||
 | 
									"pluginFactory1": NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								expected: Registry{
 | 
				
			||||||
 | 
									"pluginFactory1": NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								shouldError: true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, scenario := range tests {
 | 
				
			||||||
 | 
							t.Run(scenario.name, func(t *testing.T) {
 | 
				
			||||||
 | 
								err := scenario.primaryRegistry.Merge(scenario.registryToMerge)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if (err == nil) == scenario.shouldError {
 | 
				
			||||||
 | 
									t.Errorf("Merge() shouldError is: %v, however err is: %v.", scenario.shouldError, err)
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if !isRegistryEqual(scenario.expected, scenario.primaryRegistry) {
 | 
				
			||||||
 | 
									t.Errorf("Merge(). Expected %v. Got %v instead.", scenario.expected, scenario.primaryRegistry)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestRegister(t *testing.T) {
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							name              string
 | 
				
			||||||
 | 
							registry          Registry
 | 
				
			||||||
 | 
							nameToRegister    string
 | 
				
			||||||
 | 
							factoryToRegister PluginFactory
 | 
				
			||||||
 | 
							expected          Registry
 | 
				
			||||||
 | 
							shouldError       bool
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:              "valid Register",
 | 
				
			||||||
 | 
								registry:          Registry{},
 | 
				
			||||||
 | 
								nameToRegister:    "pluginFactory1",
 | 
				
			||||||
 | 
								factoryToRegister: NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
								expected: Registry{
 | 
				
			||||||
 | 
									"pluginFactory1": NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								shouldError: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name: "Register duplicate factories",
 | 
				
			||||||
 | 
								registry: Registry{
 | 
				
			||||||
 | 
									"pluginFactory1": NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								nameToRegister:    "pluginFactory1",
 | 
				
			||||||
 | 
								factoryToRegister: NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
								expected: Registry{
 | 
				
			||||||
 | 
									"pluginFactory1": NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								shouldError: true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, scenario := range tests {
 | 
				
			||||||
 | 
							t.Run(scenario.name, func(t *testing.T) {
 | 
				
			||||||
 | 
								err := scenario.registry.Register(scenario.nameToRegister, scenario.factoryToRegister)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if (err == nil) == scenario.shouldError {
 | 
				
			||||||
 | 
									t.Errorf("Register() shouldError is: %v however err is: %v.", scenario.shouldError, err)
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if !isRegistryEqual(scenario.expected, scenario.registry) {
 | 
				
			||||||
 | 
									t.Errorf("Register(). Expected %v. Got %v instead.", scenario.expected, scenario.registry)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestUnregister(t *testing.T) {
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							name             string
 | 
				
			||||||
 | 
							registry         Registry
 | 
				
			||||||
 | 
							nameToUnregister string
 | 
				
			||||||
 | 
							expected         Registry
 | 
				
			||||||
 | 
							shouldError      bool
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name: "valid Unregister",
 | 
				
			||||||
 | 
								registry: Registry{
 | 
				
			||||||
 | 
									"pluginFactory1": NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
									"pluginFactory2": NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								nameToUnregister: "pluginFactory1",
 | 
				
			||||||
 | 
								expected: Registry{
 | 
				
			||||||
 | 
									"pluginFactory2": NewMockNoopPluginFactory(),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								shouldError: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:             "Unregister non-existent plugin factory",
 | 
				
			||||||
 | 
								registry:         Registry{},
 | 
				
			||||||
 | 
								nameToUnregister: "pluginFactory1",
 | 
				
			||||||
 | 
								expected:         Registry{},
 | 
				
			||||||
 | 
								shouldError:      true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, scenario := range tests {
 | 
				
			||||||
 | 
							t.Run(scenario.name, func(t *testing.T) {
 | 
				
			||||||
 | 
								err := scenario.registry.Unregister(scenario.nameToUnregister)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if (err == nil) == scenario.shouldError {
 | 
				
			||||||
 | 
									t.Errorf("Unregister() shouldError is: %v however err is: %v.", scenario.shouldError, err)
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if !isRegistryEqual(scenario.expected, scenario.registry) {
 | 
				
			||||||
 | 
									t.Errorf("Unregister(). Expected %v. Got %v instead.", scenario.expected, scenario.registry)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user