Skip to content

Commit

Permalink
test(clusterconfig): add unit test for version.go and rename it to ga…
Browse files Browse the repository at this point in the history
…ther_cluster_version.go (#732)

* chore(clusterconfig): rename version.go to gather_cluster_version.go

* test(clusterconfig): adds unit test for gather_cluster_version

* test(clusterconfig): reduce dups for gather_cluster_version_test
  • Loading branch information
Ricardo Lüders authored Mar 23, 2023
1 parent 58ce689 commit 980c9eb
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
File renamed without changes.
107 changes: 107 additions & 0 deletions pkg/gatherers/clusterconfig/gather_cluster_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package clusterconfig

import (
"context"
"testing"
"time"

v1 "k8s.io/api/core/v1"

"github.com/openshift/insights-operator/pkg/utils/marshal"

configFake "github.com/openshift/client-go/config/clientset/versioned/fake"
"k8s.io/client-go/kubernetes/fake"

configv1 "github.com/openshift/api/config/v1"

"github.com/openshift/insights-operator/pkg/record"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Test_getClusterVersion(t *testing.T) {
lastTimestampEvent := metav1.Time{Time: time.Now().Add(2)}
clusterVersion := &configv1.ClusterVersion{
ObjectMeta: metav1.ObjectMeta{
Name: "version",
},
Spec: configv1.ClusterVersionSpec{
ClusterID: "cluster-id",
Channel: "stable-4.13",
},
}
pod := v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "version",
Namespace: "openshift-cluster-version",
},
Status: v1.PodStatus{
InitContainerStatuses: []v1.ContainerStatus{{RestartCount: 1}},
},
}
event := v1.Event{
ObjectMeta: metav1.ObjectMeta{
Namespace: "openshift-cluster-version",
},
Reason: "Unit test",
Message: "This is a unit test event",
LastTimestamp: lastTimestampEvent,
Count: 1,
}
compactEvents := eventListToCompactedEventList(&v1.EventList{
Items: []v1.Event{event},
})

tests := []struct {
name string
clusterVersionDefinition *configv1.ClusterVersion
pods *v1.PodList
events *v1.EventList
wantRecords []record.Record
wantErrCounts int
interval time.Duration
}{
{
name: "successful retrieve node version",
clusterVersionDefinition: clusterVersion,
pods: &v1.PodList{
Items: []v1.Pod{pod},
},
events: &v1.EventList{
Items: []v1.Event{event},
},
wantRecords: []record.Record{
{
Name: "config/version",
Item: record.ResourceMarshaller{
Resource: anonymizeClusterVersion(clusterVersion),
},
},
{
Name: "config/id",
Item: marshal.Raw{Str: string("cluster-id")},
},
{
Name: "config/pod/openshift-cluster-version/version",
Item: record.ResourceMarshaller{Resource: &pod},
},
{
Name: "events/openshift-cluster-version",
Item: record.JSONMarshaller{Object: &compactEvents},
},
},
wantErrCounts: 0,
interval: time.Second * 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
configClient := configFake.NewSimpleClientset(tt.clusterVersionDefinition).ConfigV1()
coreClient := fake.NewSimpleClientset(tt.pods, tt.events).CoreV1()

records, errs := getClusterVersion(context.Background(), configClient, coreClient, tt.interval)
assert.Len(t, errs, tt.wantErrCounts)
assert.Equal(t, tt.wantRecords, records)
})
}
}

0 comments on commit 980c9eb

Please sign in to comment.