-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(clusterconfig): add unit test for version.go and rename it to ga…
…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
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
107 changes: 107 additions & 0 deletions
107
pkg/gatherers/clusterconfig/gather_cluster_version_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |