Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit test for the InsightsDataGather Observer #813

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions pkg/config/configobserver/insighgtsdatagather_observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ import (
type InsightsDataGatherObserver interface {
factory.Controller
GatherConfig() *v1alpha1.GatherConfig
GatherDataPolicy() *v1alpha1.DataPolicy
GatherDisabled() bool
}

type insightsDataGatherController struct {
factory.Controller
lock sync.Mutex
configV1Alpha1Cli *configCliv1alpha1.ConfigV1alpha1Client
gatherConfig *v1alpha1.GatherConfig
lock sync.Mutex
cli configCliv1alpha1.ConfigV1alpha1Interface
gatherConfig *v1alpha1.GatherConfig
}

func NewInsightsDataGatherObserver(kubeConfig *rest.Config,
Expand All @@ -39,10 +38,10 @@ func NewInsightsDataGatherObserver(kubeConfig *rest.Config,
return nil, err
}
c := &insightsDataGatherController{
configV1Alpha1Cli: configV1Alpha1Cli,
cli: configV1Alpha1Cli,
}

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

func (i *insightsDataGatherController) sync(ctx context.Context, _ factory.SyncContext) error {
insightDataGatherConf, err := i.configV1Alpha1Cli.InsightsDataGathers().Get(ctx, "cluster", metav1.GetOptions{})
insightDataGatherConf, err := i.cli.InsightsDataGathers().Get(ctx, "cluster", metav1.GetOptions{})
if err != nil {
return err
}
Expand All @@ -83,10 +82,3 @@ func (i *insightsDataGatherController) GatherDisabled() bool {
}
return false
}

// GatherDataPolicy provides DataPolicy attribute value defined in the API
func (i *insightsDataGatherController) GatherDataPolicy() *v1alpha1.DataPolicy {
i.lock.Lock()
defer i.lock.Unlock()
return &i.gatherConfig.DataPolicy
}
85 changes: 85 additions & 0 deletions pkg/config/configobserver/insightsdatagather_observer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package configobserver

import (
"context"
"testing"

"github.com/openshift/api/config/v1alpha1"
fakeConfigCli "github.com/openshift/client-go/config/clientset/versioned/fake"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestInsightsDataGatherSync(t *testing.T) {
tests := []struct {
name string
insightsDatagatherToUpdated *v1alpha1.InsightsDataGather
expectedGatherConfig *v1alpha1.GatherConfig
expectedDisable bool
}{
{
name: "Obfuscation configured and some disabled gatherers",
insightsDatagatherToUpdated: &v1alpha1.InsightsDataGather{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
},
Spec: v1alpha1.InsightsDataGatherSpec{
GatherConfig: v1alpha1.GatherConfig{
DataPolicy: v1alpha1.ObfuscateNetworking,
DisabledGatherers: []string{"fooGather", "barGather"},
},
},
},
expectedGatherConfig: &v1alpha1.GatherConfig{
DataPolicy: v1alpha1.ObfuscateNetworking,
DisabledGatherers: []string{"fooGather", "barGather"},
},
expectedDisable: false,
},
{
name: "Gathering disabled and no obfuscation",
insightsDatagatherToUpdated: &v1alpha1.InsightsDataGather{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
},
Spec: v1alpha1.InsightsDataGatherSpec{
GatherConfig: v1alpha1.GatherConfig{
DataPolicy: v1alpha1.NoPolicy,
DisabledGatherers: []string{"ALL"},
},
},
},
expectedGatherConfig: &v1alpha1.GatherConfig{
DataPolicy: v1alpha1.NoPolicy,
DisabledGatherers: []string{"ALL"},
},
expectedDisable: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
insightDefaultConfig := &v1alpha1.InsightsDataGather{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
},
}

client := fakeConfigCli.NewSimpleClientset(insightDefaultConfig)
idgObserver := insightsDataGatherController{
gatherConfig: &insightDefaultConfig.Spec.GatherConfig,
cli: client.ConfigV1alpha1(),
}
err := idgObserver.sync(context.Background(), nil)
assert.NoError(t, err)

assert.Equal(t, &insightDefaultConfig.Spec.GatherConfig, idgObserver.GatherConfig())
_, err = idgObserver.cli.InsightsDataGathers().Update(context.Background(), tt.insightsDatagatherToUpdated, metav1.UpdateOptions{})
assert.NoError(t, err)
err = idgObserver.sync(context.Background(), nil)
assert.NoError(t, err)
assert.Equal(t, tt.expectedGatherConfig, idgObserver.GatherConfig())
assert.Equal(t, tt.expectedDisable, idgObserver.GatherDisabled())
})
}
}