Skip to content

Commit 2a9532a

Browse files
committed
add unit test for the InsightsDataGather Observer
1 parent e81d991 commit 2a9532a

File tree

2 files changed

+91
-14
lines changed

2 files changed

+91
-14
lines changed

pkg/config/configobserver/insighgtsdatagather_observer.go

+6-14
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ import (
1919
type InsightsDataGatherObserver interface {
2020
factory.Controller
2121
GatherConfig() *v1alpha1.GatherConfig
22-
GatherDataPolicy() *v1alpha1.DataPolicy
2322
GatherDisabled() bool
2423
}
2524

2625
type insightsDataGatherController struct {
2726
factory.Controller
28-
lock sync.Mutex
29-
configV1Alpha1Cli *configCliv1alpha1.ConfigV1alpha1Client
30-
gatherConfig *v1alpha1.GatherConfig
27+
lock sync.Mutex
28+
cli configCliv1alpha1.ConfigV1alpha1Interface
29+
gatherConfig *v1alpha1.GatherConfig
3130
}
3231

3332
func NewInsightsDataGatherObserver(kubeConfig *rest.Config,
@@ -39,10 +38,10 @@ func NewInsightsDataGatherObserver(kubeConfig *rest.Config,
3938
return nil, err
4039
}
4140
c := &insightsDataGatherController{
42-
configV1Alpha1Cli: configV1Alpha1Cli,
41+
cli: configV1Alpha1Cli,
4342
}
4443

45-
insightDataGatherConf, err := c.configV1Alpha1Cli.InsightsDataGathers().Get(context.Background(), "cluster", metav1.GetOptions{})
44+
insightDataGatherConf, err := c.cli.InsightsDataGathers().Get(context.Background(), "cluster", metav1.GetOptions{})
4645
if err != nil {
4746
klog.Errorf("Cannot read API gathering configuration: %v", err)
4847
}
@@ -57,7 +56,7 @@ func NewInsightsDataGatherObserver(kubeConfig *rest.Config,
5756
}
5857

5958
func (i *insightsDataGatherController) sync(ctx context.Context, _ factory.SyncContext) error {
60-
insightDataGatherConf, err := i.configV1Alpha1Cli.InsightsDataGathers().Get(ctx, "cluster", metav1.GetOptions{})
59+
insightDataGatherConf, err := i.cli.InsightsDataGathers().Get(ctx, "cluster", metav1.GetOptions{})
6160
if err != nil {
6261
return err
6362
}
@@ -83,10 +82,3 @@ func (i *insightsDataGatherController) GatherDisabled() bool {
8382
}
8483
return false
8584
}
86-
87-
// GatherDataPolicy provides DataPolicy attribute value defined in the API
88-
func (i *insightsDataGatherController) GatherDataPolicy() *v1alpha1.DataPolicy {
89-
i.lock.Lock()
90-
defer i.lock.Unlock()
91-
return &i.gatherConfig.DataPolicy
92-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)