|
| 1 | +package status |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 7 | + "k8s.io/apimachinery/pkg/runtime" |
| 8 | + "k8s.io/klog" |
| 9 | + |
| 10 | + configv1 "github.com/openshift/api/config/v1" |
| 11 | + configfake "github.com/openshift/client-go/config/clientset/versioned/fake" |
| 12 | + "github.com/openshift/insights-operator/pkg/config" |
| 13 | + "github.com/openshift/insights-operator/pkg/config/configobserver" |
| 14 | + "github.com/openshift/insights-operator/pkg/utils" |
| 15 | + kubeclientfake "k8s.io/client-go/kubernetes/fake" |
| 16 | +) |
| 17 | + |
| 18 | +func TestSaveInitialStart(t *testing.T) { |
| 19 | + |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + clusterOperator *configv1.ClusterOperator |
| 23 | + expErr error |
| 24 | + initialRun bool |
| 25 | + expectedSafeInitialStart bool |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "Non-initial run is has upload delayed", |
| 29 | + initialRun: false, |
| 30 | + expectedSafeInitialStart: false, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "Initial run with not existing Insights operator is not delayed", |
| 34 | + initialRun: true, |
| 35 | + clusterOperator: nil, |
| 36 | + expectedSafeInitialStart: true, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "Initial run with existing Insights operator which is degraded is delayed", |
| 40 | + initialRun: true, |
| 41 | + clusterOperator: &configv1.ClusterOperator{ |
| 42 | + ObjectMeta: metav1.ObjectMeta{ |
| 43 | + Name: "insights", |
| 44 | + }, |
| 45 | + Status: configv1.ClusterOperatorStatus{Conditions: []configv1.ClusterOperatorStatusCondition{ |
| 46 | + {Type: configv1.OperatorDegraded, Status: configv1.ConditionTrue}, |
| 47 | + }}, |
| 48 | + }, |
| 49 | + expectedSafeInitialStart: false, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "Initial run with existing Insights operator which is not degraded not delayed", |
| 53 | + initialRun: true, |
| 54 | + clusterOperator: &configv1.ClusterOperator{ |
| 55 | + ObjectMeta: metav1.ObjectMeta{ |
| 56 | + Name: "insights", |
| 57 | + }, |
| 58 | + Status: configv1.ClusterOperatorStatus{Conditions: []configv1.ClusterOperatorStatusCondition{ |
| 59 | + {Type: configv1.OperatorDegraded, Status: configv1.ConditionFalse}, |
| 60 | + }}, |
| 61 | + }, |
| 62 | + expectedSafeInitialStart: true, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + |
| 69 | + klog.SetOutput(utils.NewTestLog(t).Writer()) |
| 70 | + operators := []runtime.Object{} |
| 71 | + if tt.clusterOperator != nil { |
| 72 | + operators = append(operators, tt.clusterOperator) |
| 73 | + } |
| 74 | + kubeclientsetclient := kubeclientfake.NewSimpleClientset() |
| 75 | + |
| 76 | + client := configfake.NewSimpleClientset(operators...) |
| 77 | + ctrl := &Controller{name: "insights", client: client.ConfigV1(), configurator: configobserver.New(config.Controller{Report: true}, kubeclientsetclient)} |
| 78 | + |
| 79 | + err := ctrl.updateStatus(tt.initialRun) |
| 80 | + isSafe := ctrl.SafeInitialStart() |
| 81 | + if err != tt.expErr { |
| 82 | + t.Fatalf("updateStatus returned unexpected error: %s Expected %s", err, tt.expErr) |
| 83 | + } |
| 84 | + if isSafe != tt.expectedSafeInitialStart { |
| 85 | + t.Fatalf("unexpected SafeInitialStart was: %t Expected %t", isSafe, tt.expectedSafeInitialStart) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
0 commit comments