|
| 1 | +package configobserver |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/openshift/api/config/v1alpha1" |
| 8 | + fakeConfigCli "github.com/openshift/client-go/config/clientset/versioned/fake" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | +) |
| 12 | + |
| 13 | +func TestInsightsDataGatherSync(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + insightsDatagatherToUpdated *v1alpha1.InsightsDataGather |
| 17 | + expectedGatherConfig *v1alpha1.GatherConfig |
| 18 | + expectedDisable bool |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "Obfuscation configured and some disabled gatherers", |
| 22 | + insightsDatagatherToUpdated: &v1alpha1.InsightsDataGather{ |
| 23 | + ObjectMeta: metav1.ObjectMeta{ |
| 24 | + Name: "cluster", |
| 25 | + }, |
| 26 | + Spec: v1alpha1.InsightsDataGatherSpec{ |
| 27 | + GatherConfig: v1alpha1.GatherConfig{ |
| 28 | + DataPolicy: v1alpha1.ObfuscateNetworking, |
| 29 | + DisabledGatherers: []string{"fooGather", "barGather"}, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + expectedGatherConfig: &v1alpha1.GatherConfig{ |
| 34 | + DataPolicy: v1alpha1.ObfuscateNetworking, |
| 35 | + DisabledGatherers: []string{"fooGather", "barGather"}, |
| 36 | + }, |
| 37 | + expectedDisable: false, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Gathering disabled and no obfuscation", |
| 41 | + insightsDatagatherToUpdated: &v1alpha1.InsightsDataGather{ |
| 42 | + ObjectMeta: metav1.ObjectMeta{ |
| 43 | + Name: "cluster", |
| 44 | + }, |
| 45 | + Spec: v1alpha1.InsightsDataGatherSpec{ |
| 46 | + GatherConfig: v1alpha1.GatherConfig{ |
| 47 | + DataPolicy: v1alpha1.NoPolicy, |
| 48 | + DisabledGatherers: []string{"ALL"}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + expectedGatherConfig: &v1alpha1.GatherConfig{ |
| 53 | + DataPolicy: v1alpha1.NoPolicy, |
| 54 | + DisabledGatherers: []string{"ALL"}, |
| 55 | + }, |
| 56 | + expectedDisable: true, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + insightDefaultConfig := &v1alpha1.InsightsDataGather{ |
| 63 | + ObjectMeta: metav1.ObjectMeta{ |
| 64 | + Name: "cluster", |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + client := fakeConfigCli.NewSimpleClientset(insightDefaultConfig) |
| 69 | + idgObserver := insightsDataGatherController{ |
| 70 | + gatherConfig: &insightDefaultConfig.Spec.GatherConfig, |
| 71 | + cli: client.ConfigV1alpha1(), |
| 72 | + } |
| 73 | + err := idgObserver.sync(context.Background(), nil) |
| 74 | + assert.NoError(t, err) |
| 75 | + |
| 76 | + assert.Equal(t, &insightDefaultConfig.Spec.GatherConfig, idgObserver.GatherConfig()) |
| 77 | + _, err = idgObserver.cli.InsightsDataGathers().Update(context.Background(), tt.insightsDatagatherToUpdated, metav1.UpdateOptions{}) |
| 78 | + assert.NoError(t, err) |
| 79 | + err = idgObserver.sync(context.Background(), nil) |
| 80 | + assert.NoError(t, err) |
| 81 | + assert.Equal(t, tt.expectedGatherConfig, idgObserver.GatherConfig()) |
| 82 | + assert.Equal(t, tt.expectedDisable, idgObserver.GatherDisabled()) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
0 commit comments