Skip to content

Commit

Permalink
Move platform tests to unit test and adjust collector tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bjee19 committed Mar 12, 2024
1 parent d30166f commit eebc2c6
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 254 deletions.
260 changes: 6 additions & 254 deletions internal/mode/static/telemetry/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ var _ = Describe("Collector", Ordered, func() {
})
})

Describe("cluster information collector", func() {
Describe("node count collector", func() {
When("collecting node count data", func() {
It("collects correct data for one node", func() {
k8sClientReader.ListCalls(createListCallsFunc(nodeList))
Expand Down Expand Up @@ -371,261 +371,11 @@ var _ = Describe("Collector", Ordered, func() {
})
})
})
})

When("collecting cluster platform data", func() {
It("collects Kind platform", func() {
nodes := &v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Spec: v1.NodeSpec{
ProviderID: "kind://docker/kind/kind-control-plane",
},
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
KubeletVersion: "v1.29.2",
},
},
},
},
}

k8sClientReader.ListCalls(createListCallsFunc(nodes))
expData.ClusterVersion = "1.29.2"
expData.ClusterPlatform = "kind"

data, err := dataCollector.Collect(ctx)

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

It("collects GKE platform", func() {
nodes := &v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Spec: v1.NodeSpec{
ProviderID: "gce://test-data/us-central1-c/test-data",
},
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
KubeletVersion: "v1.29.2",
},
},
},
},
}

k8sClientReader.ListCalls(createListCallsFunc(nodes))
expData.ClusterVersion = "1.29.2"
expData.ClusterPlatform = "gke"

data, err := dataCollector.Collect(ctx)

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

It("collects AKS platform", func() {
nodes := &v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Spec: v1.NodeSpec{
ProviderID: "azure://test-data/us-central1-c/test-data",
},
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
KubeletVersion: "v1.29.2",
},
},
},
},
}

k8sClientReader.ListCalls(createListCallsFunc(nodes))
expData.ClusterVersion = "1.29.2"
expData.ClusterPlatform = "aks"

data, err := dataCollector.Collect(ctx)

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

It("collects EKS platform", func() {
nodes := &v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Spec: v1.NodeSpec{
ProviderID: "aws://test-data/us-central1-c/test-data",
},
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
KubeletVersion: "v1.29.2",
},
},
},
},
}

k8sClientReader.ListCalls(createListCallsFunc(nodes))
expData.ClusterVersion = "1.29.2"
expData.ClusterPlatform = "eks"

data, err := dataCollector.Collect(ctx)

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

It("collects Rancher platform", func() {
namespaceList := &v1.NamespaceList{
Items: []v1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "cattle-system",
},
},
},
}

k8sClientReader.ListCalls(createListCallsFunc(nodeList, namespaceList))
expData.ClusterVersion = "1.28.6"
expData.ClusterPlatform = "rancher"

data, err := dataCollector.Collect(ctx)

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

It("collects Openshift platform", func() {
nodes := &v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Labels: map[string]string{"node.openshift.io/os_id": "test"},
},
Spec: v1.NodeSpec{
ProviderID: "k3s://test-data/us-central1-c/test-data",
},
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
KubeletVersion: "v1.29.2",
},
},
},
},
}

k8sClientReader.ListCalls(createListCallsFunc(nodes))
expData.ClusterVersion = "1.29.2"
expData.ClusterPlatform = "openshift"

data, err := dataCollector.Collect(ctx)

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

When("platform is none of the above", func() {
It("marks the platform as 'other_' with whatever was in the providerID's providerName", func() {
nodes := &v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Spec: v1.NodeSpec{
ProviderID: "other-cloud-provider://test-here",
},
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
KubeletVersion: "v1.29.2",
},
},
},
},
}

k8sClientReader.ListCalls(createListCallsFunc(nodes))
expData.ClusterVersion = "1.29.2"
expData.ClusterPlatform = "other_other-cloud-provider"

data, err := dataCollector.Collect(ctx)

Expect(err).To(BeNil())
Expect(expData).To(Equal(data))
})
It("marks the platform as 'other' when providerID is empty", func() {
nodes := &v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
KubeletVersion: "v1.29.2",
},
},
},
},
}

k8sClientReader.ListCalls(createListCallsFunc(nodes))
expData.ClusterVersion = "1.29.2"
expData.ClusterPlatform = "other"

data, err := dataCollector.Collect(ctx)

Expect(err).To(BeNil())
Expect(expData).To(Equal(data))
})
It("marks the platform as 'other' when providerID is missing '://' separator", func() {
nodes := &v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Spec: v1.NodeSpec{
ProviderID: "other-cloud-provider",
},
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
KubeletVersion: "v1.29.2",
},
},
},
},
}

k8sClientReader.ListCalls(createListCallsFunc(nodes))
expData.ClusterVersion = "1.29.2"
expData.ClusterPlatform = "other"

data, err := dataCollector.Collect(ctx)

Expect(err).To(BeNil())
Expect(expData).To(Equal(data))
})
})
})
Describe("cluster version collector", func() {
When("collecting cluster version data", func() {
When("the kublet version is missing", func() {
When("the kubelet version is missing", func() {
It("should be report 'unknown'", func() {
nodes := &v1.NodeList{
Items: []v1.Node{
Expand All @@ -651,7 +401,9 @@ var _ = Describe("Collector", Ordered, func() {
})
})
})
})

Describe("clusterID collector", func() {
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 Down
108 changes: 108 additions & 0 deletions internal/mode/static/telemetry/platform_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package telemetry

import (
"testing"

. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestGetPlatform(t *testing.T) {
tests := []struct {
expectedPlatform string
name string
node v1.Node
namespaces v1.NamespaceList
}{
{
node: v1.Node{
Spec: v1.NodeSpec{
ProviderID: "kind://docker/kind/kind-control-plane",
},
},
namespaces: v1.NamespaceList{},
expectedPlatform: "kind",
name: "kind platform",
},
{
node: v1.Node{
Spec: v1.NodeSpec{
ProviderID: "k3s://ip-172-16-0-210",
},
},
namespaces: v1.NamespaceList{},
expectedPlatform: "k3s",
name: "k3s platform",
},
{
node: v1.Node{
Spec: v1.NodeSpec{
ProviderID: "gce://test-data/us-central1-c/test-data",
},
},
namespaces: v1.NamespaceList{},
expectedPlatform: "gke",
name: "gke platform",
},
{
node: v1.Node{
Spec: v1.NodeSpec{
ProviderID: "azure://test-data/us-central1-c/test-data",
},
},
namespaces: v1.NamespaceList{},
expectedPlatform: "aks",
name: "aks platform",
},
{
node: v1.Node{
Spec: v1.NodeSpec{
ProviderID: "aws://test-data/us-central1-c/test-data",
},
},
namespaces: v1.NamespaceList{},
expectedPlatform: "eks",
name: "eks platform",
},
{
node: v1.Node{
Spec: v1.NodeSpec{
ProviderID: "k3s://ip-172-16-0-210",
},
},
namespaces: v1.NamespaceList{
Items: []v1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "cattle-system",
},
},
},
},
expectedPlatform: "rancher",
name: "rancher platform",
},
{
node: v1.Node{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"node.openshift.io/os_id": "test"},
},
Spec: v1.NodeSpec{
ProviderID: "k3s://ip-172-16-0-210",
},
},
namespaces: v1.NamespaceList{},
expectedPlatform: "openshift",
name: "openshift platform",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
g := NewWithT(t)

platform := getPlatform(test.node, test.namespaces)
g.Expect(platform).To(Equal(test.expectedPlatform))
})
}
}

0 comments on commit eebc2c6

Please sign in to comment.