Improved Test Coverage of pkg/controllers/context

Signed-off-by: Anuj Agrawal <anujagrawal380@gmail.com>
This commit is contained in:
Anuj Agrawal 2024-08-06 15:13:08 +05:30
parent 8b4e006f03
commit baff21a75a
1 changed files with 105 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/*
Copyright 2022 The Karmada Authors.
Copyright 2024 The Karmada Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -17,6 +17,8 @@ limitations under the License.
package context
import (
"errors"
"reflect"
"testing"
"k8s.io/apimachinery/pkg/util/sets"
@ -34,49 +36,63 @@ func TestContext_IsControllerEnabled(t *testing.T) {
name: "on by name",
controllerName: "bravo",
disabledByDefaultControllers: []string{"delta", "echo"},
controllers: []string{"alpha", "bravo", "-charlie"}, // --controllers=alpha,bravo,-charlie
controllers: []string{"alpha", "bravo", "-charlie"},
expected: true,
},
{
name: "off by name",
controllerName: "charlie",
disabledByDefaultControllers: []string{"delta", "echo"},
controllers: []string{"alpha", "bravo", "-charlie"}, // --controllers=alpha,bravo,-charlie
controllers: []string{"alpha", "bravo", "-charlie"},
expected: false,
},
{
name: "on by default",
controllerName: "alpha",
disabledByDefaultControllers: []string{"delta", "echo"},
controllers: []string{"*"}, // --controllers=*
controllers: []string{"*"},
expected: true,
},
{
name: "off by default",
controllerName: "delta",
disabledByDefaultControllers: []string{"delta", "echo"},
controllers: []string{"*"}, // --controllers=*
controllers: []string{"*"},
expected: false,
},
{
name: "on by star, not off by name",
controllerName: "alpha",
disabledByDefaultControllers: []string{"delta", "echo"},
controllers: []string{"*", "-charlie"}, // --controllers=*,-charlie
controllers: []string{"*", "-charlie"},
expected: true,
},
{
name: "off by name with star",
controllerName: "charlie",
disabledByDefaultControllers: []string{"delta", "echo"},
controllers: []string{"*", "-charlie"}, // --controllers=*,-charlie
controllers: []string{"*", "-charlie"},
expected: false,
},
{
name: "off by default implicit, no star",
controllerName: "foxtrot",
disabledByDefaultControllers: []string{"delta", "echo"},
controllers: []string{"alpha", "bravo", "-charlie"}, // --controllers=alpha,bravo,-charlie
controllers: []string{"alpha", "bravo", "-charlie"},
expected: false,
},
{
name: "on by star, not in disabled list",
controllerName: "foxtrot",
disabledByDefaultControllers: []string{"delta", "echo"},
controllers: []string{"*"},
expected: true,
},
{
name: "empty controllers list",
controllerName: "alpha",
disabledByDefaultControllers: []string{"delta", "echo"},
controllers: []string{},
expected: false,
},
}
@ -93,3 +109,84 @@ func TestContext_IsControllerEnabled(t *testing.T) {
})
}
}
func TestInitializers_ControllerNames(t *testing.T) {
initializers := Initializers{
"controller1": func(_ Context) (bool, error) { return true, nil },
"controller2": func(_ Context) (bool, error) { return true, nil },
"controller3": func(_ Context) (bool, error) { return true, nil },
}
expected := []string{"controller1", "controller2", "controller3"}
result := initializers.ControllerNames()
if !reflect.DeepEqual(sets.New(result...), sets.New(expected...)) {
t.Errorf("expected %v, but got %v", expected, result)
}
}
func TestInitializers_StartControllers(t *testing.T) {
tests := []struct {
name string
initializers Initializers
enabledControllers []string
disabledByDefaultControllers []string
expectedError bool
}{
{
name: "all controllers enabled and started successfully",
initializers: Initializers{
"controller1": func(_ Context) (bool, error) { return true, nil },
"controller2": func(_ Context) (bool, error) { return true, nil },
},
enabledControllers: []string{"*"},
disabledByDefaultControllers: []string{},
expectedError: false,
},
{
name: "some controllers disabled",
initializers: Initializers{
"controller1": func(_ Context) (bool, error) { return true, nil },
"controller2": func(_ Context) (bool, error) { return true, nil },
"controller3": func(_ Context) (bool, error) { return true, nil },
},
enabledControllers: []string{"controller1", "controller2"},
disabledByDefaultControllers: []string{"controller3"},
expectedError: false,
},
{
name: "controller returns error",
initializers: Initializers{
"controller1": func(_ Context) (bool, error) { return true, nil },
"controller2": func(_ Context) (bool, error) { return false, errors.New("test error") },
},
enabledControllers: []string{"*"},
disabledByDefaultControllers: []string{},
expectedError: true,
},
{
name: "controller not started",
initializers: Initializers{
"controller1": func(_ Context) (bool, error) { return true, nil },
"controller2": func(_ Context) (bool, error) { return false, nil },
},
enabledControllers: []string{"*"},
disabledByDefaultControllers: []string{},
expectedError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := Context{
Opts: Options{
Controllers: tt.enabledControllers,
},
}
err := tt.initializers.StartControllers(ctx, sets.New(tt.disabledByDefaultControllers...))
if (err != nil) != tt.expectedError {
t.Errorf("expected error: %v, but got: %v", tt.expectedError, err)
}
})
}
}