Skip to content

Commit

Permalink
Merge pull request #5310 from anujagrawal699/improveTestCoverage-pkg/…
Browse files Browse the repository at this point in the history
…controllers/context

Improve test coverage of pkg/controllers/context
  • Loading branch information
karmada-bot authored Aug 9, 2024
2 parents 052b06e + 4632376 commit 6e9136d
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions pkg/controllers/context/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package context

import (
"errors"
"reflect"
"testing"

"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -58,6 +60,13 @@ func TestContext_IsControllerEnabled(t *testing.T) {
controllers: []string{"*"}, // --controllers=*
expected: false,
},
{
name: "on by star, not in disabled list",
controllerName: "foxtrot",
disabledByDefaultControllers: []string{"delta", "echo"},
controllers: []string{"*"}, // --controllers=*
expected: true,
},
{
name: "on by star, not off by name",
controllerName: "alpha",
Expand All @@ -79,6 +88,13 @@ func TestContext_IsControllerEnabled(t *testing.T) {
controllers: []string{"alpha", "bravo", "-charlie"}, // --controllers=alpha,bravo,-charlie
expected: false,
},
{
name: "empty controllers list",
controllerName: "alpha",
disabledByDefaultControllers: []string{"delta", "echo"},
controllers: []string{}, // No controllers
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -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)
}
})
}
}

0 comments on commit 6e9136d

Please sign in to comment.