Skip to content

Commit

Permalink
Adjust collector and platform tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bjee19 committed Mar 12, 2024
1 parent e6b99ba commit b7758e4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 35 deletions.
104 changes: 71 additions & 33 deletions internal/mode/static/telemetry/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,19 @@ var _ = Describe("Collector", Ordered, func() {
}
}

mergeListCallsWithBase := func(f listCallsFunc) listCallsFunc {
return func(
ctx context.Context,
object client.ObjectList,
option ...client.ListOption,
) error {
err := baseListCalls(ctx, object, option...)
Expect(err).ToNot(HaveOccurred())

return f(ctx, object, option...)
}
}

Describe("Normal case", func() {
When("collecting telemetry data", func() {
It("collects all fields", func() {
Expand Down Expand Up @@ -340,7 +353,26 @@ var _ = Describe("Collector", Ordered, func() {
})
})

Describe("clusterID collector", func() {
Describe("cluster information collector", func() {
When("collecting cluster platform", func() {
When("it encounters an error while collecting data", func() {
It("should error if the kubernetes client errored when getting the NamespaceList", func() {
expectedError := errors.New("failed to get NamespaceList")
k8sClientReader.ListCalls(mergeListCallsWithBase(
func(_ context.Context, object client.ObjectList, _ ...client.ListOption) error {
switch object.(type) {
case *v1.NamespaceList:
return expectedError
}
return nil
}))

_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedError))
})
})
})

When("collecting clusterID data", func() {
When("it encounters an error while collecting data", func() {
It("should error if the kubernetes client errored when getting the namespace", func() {
Expand All @@ -359,6 +391,34 @@ var _ = Describe("Collector", Ordered, func() {
})
})
})

When("collecting cluster version data", func() {
When("the kubelet version is missing", func() {
It("should be report 'unknown'", func() {
nodes := &v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Spec: v1.NodeSpec{
ProviderID: "k3s://ip-172-16-0-210",
},
},
},
}

k8sClientReader.ListCalls(createListCallsFunc(nodes))
expData.ClusterVersion = "unknown"
expData.ClusterPlatform = "k3s"

data, err := dataCollector.Collect(ctx)

Expect(err).To(BeNil())
Expect(expData).To(Equal(data))
})
})
})
})

Describe("node count collector", func() {
Expand All @@ -383,9 +443,17 @@ var _ = Describe("Collector", Ordered, func() {

Expect(err).To(MatchError(expectedError))
})

It("should error on kubernetes client api errors", func() {
expectedError := errors.New("there was an error getting NodeList")
k8sClientReader.ListReturns(expectedError)
expectedError := errors.New("failed to get NodeList")
k8sClientReader.ListCalls(
func(_ context.Context, object client.ObjectList, _ ...client.ListOption) error {
switch object.(type) {
case *v1.NodeList:
return expectedError
}
return nil
})

_, err := dataCollector.Collect(ctx)
Expect(err).To(MatchError(expectedError))
Expand All @@ -394,36 +462,6 @@ var _ = Describe("Collector", Ordered, func() {
})
})

Describe("cluster version collector", func() {
When("collecting cluster version data", func() {
When("the kubelet version is missing", func() {
It("should be report 'unknown'", func() {
nodes := &v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Spec: v1.NodeSpec{
ProviderID: "k3s://ip-172-16-0-210",
},
},
},
}

k8sClientReader.ListCalls(createListCallsFunc(nodes))
expData.ClusterVersion = "unknown"
expData.ClusterPlatform = "k3s"

data, err := dataCollector.Collect(ctx)

Expect(err).To(BeNil())
Expect(expData).To(Equal(data))
})
})
})
})

Describe("NGF resource count collector", func() {
var (
graph1 *graph.Graph
Expand Down
17 changes: 15 additions & 2 deletions internal/mode/static/telemetry/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestGetPlatform(t *testing.T) {
},
},
expectedPlatform: "rancher",
name: "rancher platform",
name: "rancher platform on k3s",
},
{
node: &v1.Node{
Expand All @@ -94,7 +94,20 @@ func TestGetPlatform(t *testing.T) {
},
namespaces: &v1.NamespaceList{},
expectedPlatform: "openshift",
name: "openshift platform",
name: "openshift platform on k3s",
},
{
node: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"node.openshift.io/os_id": "test"},
},
Spec: v1.NodeSpec{
ProviderID: "aws://test-data/us-central1-c/test-data",
},
},
namespaces: &v1.NamespaceList{},
expectedPlatform: "openshift",
name: "openshift platform on aws",
},
{
node: &v1.Node{
Expand Down

0 comments on commit b7758e4

Please sign in to comment.