|
| 1 | +package operator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + opv1 "github.com/openshift/api/operator/v1" |
| 9 | + "github.com/openshift/csi-operator/pkg/operator/config" |
| 10 | + "github.com/openshift/library-go/pkg/operator/v1helpers" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | +) |
| 13 | + |
| 14 | +func TestValidatePreCondition(t *testing.T) { |
| 15 | + testProvider := "test-driver.csi.k8s.io" |
| 16 | + |
| 17 | + cases := []struct { |
| 18 | + name string |
| 19 | + cfg *config.OperatorControllerConfig |
| 20 | + expectSyncError bool |
| 21 | + expectControllersRunning bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "Precondition valid without error", |
| 25 | + cfg: &config.OperatorControllerConfig{ |
| 26 | + Precondition: func() (bool, error) { return true, nil }, |
| 27 | + }, |
| 28 | + expectSyncError: false, |
| 29 | + expectControllersRunning: true, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Precondition valid with error from internal operation", |
| 33 | + cfg: &config.OperatorControllerConfig{ |
| 34 | + Precondition: func() (bool, error) { return true, errors.New("") }, |
| 35 | + }, |
| 36 | + expectSyncError: true, |
| 37 | + expectControllersRunning: true, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Precondition not valid without error", |
| 41 | + cfg: &config.OperatorControllerConfig{ |
| 42 | + Precondition: func() (bool, error) { return false, nil }, |
| 43 | + }, |
| 44 | + expectSyncError: false, |
| 45 | + expectControllersRunning: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "Precondition not valid with error from internal operation", |
| 49 | + cfg: &config.OperatorControllerConfig{ |
| 50 | + Precondition: func() (bool, error) { return false, errors.New("") }, |
| 51 | + }, |
| 52 | + expectSyncError: false, |
| 53 | + expectControllersRunning: false, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + for _, tc := range cases { |
| 58 | + t.Run(tc.name, func(t *testing.T) { |
| 59 | + operator := &FakeOperator{ |
| 60 | + ObjectMeta: metav1.ObjectMeta{Name: testProvider}, |
| 61 | + Spec: opv1.OperatorSpec{ManagementState: opv1.Managed}, |
| 62 | + } |
| 63 | + c := &Controller{ |
| 64 | + operatorClient: v1helpers.NewFakeOperatorClientWithObjectMeta(&operator.ObjectMeta, &operator.Spec, &operator.Status, nil), |
| 65 | + operatorControllerConfig: tc.cfg, |
| 66 | + } |
| 67 | + ctx := context.Background() |
| 68 | + err := c.sync(ctx, nil) |
| 69 | + if c.controllersRunning != tc.expectControllersRunning { |
| 70 | + t.Fatalf("Unexpected CSI controller running state: %v", c.controllersRunning) |
| 71 | + } |
| 72 | + if err != nil && !tc.expectSyncError { |
| 73 | + t.Fatalf("Did not expect an error when syncing") |
| 74 | + } |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
0 commit comments