|
| 1 | +package builder |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "k8s.io/client-go/rest" |
| 8 | + mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager" |
| 9 | + |
| 10 | + pmtesting "github.com/platform-mesh/golang-commons/controller/testSupport" |
| 11 | + "github.com/platform-mesh/golang-commons/logger" |
| 12 | +) |
| 13 | + |
| 14 | +func TestNewBuilder_Defaults(t *testing.T) { |
| 15 | + log := &logger.Logger{} |
| 16 | + b := NewBuilder("op", "ctrl", nil, log) |
| 17 | + if b.operatorName != "op" { |
| 18 | + t.Errorf("expected operatorName 'op', got %s", b.operatorName) |
| 19 | + } |
| 20 | + if b.controllerName != "ctrl" { |
| 21 | + t.Errorf("expected controllerName 'ctrl', got %s", b.controllerName) |
| 22 | + } |
| 23 | + if b.withConditionManagement { |
| 24 | + t.Error("expected withConditionManagement to be false") |
| 25 | + } |
| 26 | + if b.withSpreadingReconciles { |
| 27 | + t.Error("expected withSpreadingReconciles to be false") |
| 28 | + } |
| 29 | + if b.withReadOnly { |
| 30 | + t.Error("expected withReadOnly to be false") |
| 31 | + } |
| 32 | + if b.log != log { |
| 33 | + t.Error("expected log to be set") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestBuilder_WithConditionManagement(t *testing.T) { |
| 38 | + b := NewBuilder("op", "ctrl", nil, &logger.Logger{}) |
| 39 | + b.WithConditionManagement() |
| 40 | + if !b.withConditionManagement { |
| 41 | + t.Error("WithConditionManagement should set withConditionManagement to true") |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestBuilder_WithSpreadingReconciles(t *testing.T) { |
| 46 | + b := NewBuilder("op", "ctrl", nil, &logger.Logger{}) |
| 47 | + b.WithSpreadingReconciles() |
| 48 | + if !b.withSpreadingReconciles { |
| 49 | + t.Error("WithSpreadingReconciles should set withSpreadingReconciles to true") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestBuilder_WithReadOnly(t *testing.T) { |
| 54 | + b := NewBuilder("op", "ctrl", nil, &logger.Logger{}) |
| 55 | + b.WithReadOnly() |
| 56 | + if !b.withReadOnly { |
| 57 | + t.Error("WithReadOnly should set withReadOnly to true") |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestControllerRuntimeBuilder(t *testing.T) { |
| 62 | + t.Run("Minimal setup", func(t *testing.T) { |
| 63 | + b := NewBuilder("op", "ctrl", nil, &logger.Logger{}) |
| 64 | + fakeClient := pmtesting.CreateFakeClient(t, &pmtesting.TestApiObject{}) |
| 65 | + lm := b.BuildControllerRuntime(fakeClient) |
| 66 | + assert.NotNil(t, lm) |
| 67 | + }) |
| 68 | + t.Run("All Options", func(t *testing.T) { |
| 69 | + b := NewBuilder("op", "ctrl", nil, &logger.Logger{}).WithConditionManagement().WithSpreadingReconciles() |
| 70 | + fakeClient := pmtesting.CreateFakeClient(t, &pmtesting.TestApiObject{}) |
| 71 | + lm := b.BuildControllerRuntime(fakeClient) |
| 72 | + assert.NotNil(t, lm) |
| 73 | + }) |
| 74 | + t.Run("ReadOnly", func(t *testing.T) { |
| 75 | + b := NewBuilder("op", "ctrl", nil, &logger.Logger{}).WithReadOnly() |
| 76 | + fakeClient := pmtesting.CreateFakeClient(t, &pmtesting.TestApiObject{}) |
| 77 | + lm := b.BuildControllerRuntime(fakeClient) |
| 78 | + assert.NotNil(t, lm) |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +func TestMulticontrollerRuntimeBuilder(t *testing.T) { |
| 83 | + t.Run("Minimal setup", func(t *testing.T) { |
| 84 | + b := NewBuilder("op", "ctrl", nil, &logger.Logger{}) |
| 85 | + cfg := &rest.Config{} |
| 86 | + provider := pmtesting.NewFakeProvider(cfg) |
| 87 | + mgr, err := mcmanager.New(cfg, provider, mcmanager.Options{}) |
| 88 | + assert.NoError(t, err) |
| 89 | + lm := b.BuildMultiCluster(mgr) |
| 90 | + assert.NotNil(t, lm) |
| 91 | + }) |
| 92 | + t.Run("All Options", func(t *testing.T) { |
| 93 | + b := NewBuilder("op", "ctrl", nil, &logger.Logger{}).WithConditionManagement().WithSpreadingReconciles() |
| 94 | + cfg := &rest.Config{} |
| 95 | + provider := pmtesting.NewFakeProvider(cfg) |
| 96 | + mgr, err := mcmanager.New(cfg, provider, mcmanager.Options{}) |
| 97 | + assert.NoError(t, err) |
| 98 | + lm := b.BuildMultiCluster(mgr) |
| 99 | + assert.NotNil(t, lm) |
| 100 | + }) |
| 101 | + t.Run("ReadOnly", func(t *testing.T) { |
| 102 | + b := NewBuilder("op", "ctrl", nil, &logger.Logger{}).WithReadOnly() |
| 103 | + cfg := &rest.Config{} |
| 104 | + provider := pmtesting.NewFakeProvider(cfg) |
| 105 | + mgr, err := mcmanager.New(cfg, provider, mcmanager.Options{}) |
| 106 | + assert.NoError(t, err) |
| 107 | + lm := b.BuildMultiCluster(mgr) |
| 108 | + assert.NotNil(t, lm) |
| 109 | + }) |
| 110 | +} |
0 commit comments