Skip to content

Commit

Permalink
Increase code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: clyang82 <[email protected]>
  • Loading branch information
clyang82 committed Dec 4, 2024
1 parent 0a733c8 commit c0f05db
Showing 1 changed file with 263 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package protocol

import (
"encoding/json"
"strings"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"github.com/stolostron/multicluster-global-hub/operator/api/operator/v1alpha4"
constants "github.com/stolostron/multicluster-global-hub/operator/pkg/constants"
operatorconstants "github.com/stolostron/multicluster-global-hub/operator/pkg/constants"
"github.com/stolostron/multicluster-global-hub/pkg/utils"
)

Expand All @@ -17,8 +19,8 @@ func TestNewStrimziTransporter(t *testing.T) {
Name: "test-mgh",
Namespace: utils.GetDefaultNamespace(),
Annotations: map[string]string{
constants.CommunityCatalogSourceNameKey: "test",
constants.CommunityCatalogSourceNamespaceKey: "default",
operatorconstants.CommunityCatalogSourceNameKey: "test",
operatorconstants.CommunityCatalogSourceNamespaceKey: "default",
},
},
Spec: v1alpha4.MulticlusterGlobalHubSpec{
Expand Down Expand Up @@ -48,3 +50,261 @@ func TestNewStrimziTransporter(t *testing.T) {
t.Errorf("catalogSource name should be default, but %v", trans.subCatalogSourceNamespace)
}
}

func TestNewKafkaCluster(t *testing.T) {
tests := []struct {
name string
mgh *v1alpha4.MulticlusterGlobalHub
expectedKafkaCluster string
}{
{
name: "availabilityConfig is Basic",
mgh: &v1alpha4.MulticlusterGlobalHub{
ObjectMeta: metav1.ObjectMeta{
Name: "test-mgh",
Namespace: utils.GetDefaultNamespace(),
Annotations: map[string]string{
operatorconstants.CommunityCatalogSourceNameKey: "test",
operatorconstants.CommunityCatalogSourceNamespaceKey: "default",
},
},
Spec: v1alpha4.MulticlusterGlobalHubSpec{
AvailabilityConfig: v1alpha4.HABasic,
DataLayerSpec: v1alpha4.DataLayerSpec{
Postgres: v1alpha4.PostgresSpec{
Retention: "2y",
},
},
},
},
expectedKafkaCluster: `{
"metadata": {
"name": "kafka",
"namespace": "multicluster-global-hub",
"creationTimestamp": null,
"labels": {
"global-hub.open-cluster-management.io/managed-by": "global-hub"
},
"annotations": {
"strimzi.io/kraft": "enabled",
"strimzi.io/node-pools": "enabled"
}
},
"spec": {
"entityOperator": {
"topicOperator": {},
"userOperator": {}
},
"kafka": {
"authorization": {
"type": "simple"
},
"config": {
"default.replication.factor": 1,
"min.insync.replicas": 1,
"offsets.topic.replication.factor": 1,
"transaction.state.log.min.isr": 1,
"transaction.state.log.replication.factor": 1
},
"listeners": [
{
"authentication": {
"type": "tls"
},
"name": "tls",
"port": 9093,
"tls": true,
"type": "route"
}
],
"resources": {
"limits": {
"memory": "4Gi"
},
"requests": {
"cpu": "25m",
"memory": "128Mi"
}
},
"version": "3.8.0"
}
}
}`,
},
{
name: "availabilityConfig is High",
mgh: &v1alpha4.MulticlusterGlobalHub{
ObjectMeta: metav1.ObjectMeta{
Name: "test-mgh",
Namespace: utils.GetDefaultNamespace(),
Annotations: map[string]string{
operatorconstants.CommunityCatalogSourceNameKey: "test",
operatorconstants.CommunityCatalogSourceNamespaceKey: "default",
},
},
Spec: v1alpha4.MulticlusterGlobalHubSpec{
DataLayerSpec: v1alpha4.DataLayerSpec{
Postgres: v1alpha4.PostgresSpec{
Retention: "2y",
},
},
},
},
expectedKafkaCluster: `{
"metadata": {
"name": "kafka",
"namespace": "multicluster-global-hub",
"creationTimestamp": null,
"labels": {
"global-hub.open-cluster-management.io/managed-by": "global-hub"
},
"annotations": {
"strimzi.io/kraft": "enabled",
"strimzi.io/node-pools": "enabled"
}
},
"spec": {
"entityOperator": {
"topicOperator": {},
"userOperator": {}
},
"kafka": {
"authorization": {
"type": "simple"
},
"config": {
"default.replication.factor": 3,
"min.insync.replicas": 2,
"offsets.topic.replication.factor": 3,
"transaction.state.log.min.isr": 2,
"transaction.state.log.replication.factor": 3
},
"listeners": [
{
"authentication": {
"type": "tls"
},
"name": "tls",
"port": 9093,
"tls": true,
"type": "route"
}
],
"resources": {
"limits": {
"memory": "4Gi"
},
"requests": {
"cpu": "25m",
"memory": "128Mi"
}
},
"version": "3.8.0"
}
}
}`,
},

{
name: "availabilityConfig is Basic and expose via nodeport",
mgh: &v1alpha4.MulticlusterGlobalHub{
ObjectMeta: metav1.ObjectMeta{
Name: "test-mgh",
Namespace: utils.GetDefaultNamespace(),
Annotations: map[string]string{
operatorconstants.CommunityCatalogSourceNameKey: "test",
operatorconstants.CommunityCatalogSourceNamespaceKey: "default",
operatorconstants.KafkaUseNodeport: "",
operatorconstants.KinDClusterIPKey: "10.0.0.1",
},
},
Spec: v1alpha4.MulticlusterGlobalHubSpec{
DataLayerSpec: v1alpha4.DataLayerSpec{
Postgres: v1alpha4.PostgresSpec{
Retention: "2y",
},
},
},
},
expectedKafkaCluster: `{
"metadata": {
"name": "kafka",
"namespace": "multicluster-global-hub",
"creationTimestamp": null,
"labels": {
"global-hub.open-cluster-management.io/managed-by": "global-hub"
},
"annotations": {
"strimzi.io/kraft": "enabled",
"strimzi.io/node-pools": "enabled"
}
},
"spec": {
"entityOperator": {
"topicOperator": {},
"userOperator": {}
},
"kafka": {
"authorization": {
"type": "simple"
},
"config": {
"default.replication.factor": 3,
"min.insync.replicas": 2,
"offsets.topic.replication.factor": 3,
"transaction.state.log.min.isr": 2,
"transaction.state.log.replication.factor": 3
},
"listeners": [
{
"authentication": {
"type": "tls"
},
"configuration": {
"bootstrap": {
"nodePort": 30093
},
"brokers": [
{
"advertisedHost": "10.0.0.1",
"broker": 0
}
]
},
"name": "tls",
"port": 9093,
"tls": true,
"type": "nodeport"
}
],
"resources": {
"limits": {
"memory": "4Gi"
},
"requests": {
"cpu": "25m",
"memory": "128Mi"
}
},
"version": "3.8.0"
}
}
}`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
transporter := NewStrimziTransporter(nil, tt.mgh)
cluster := transporter.newKafkaCluster(tt.mgh)
clusterBytes, _ := json.Marshal(cluster)
// Replace spaces
s := strings.ReplaceAll(tt.expectedKafkaCluster, " ", "")
// Replace newlines
s = strings.ReplaceAll(s, "\n", "")
if string(clusterBytes) != s {
t.Errorf("want %v, but got %v", s, string(clusterBytes))
}
})
}
}

0 comments on commit c0f05db

Please sign in to comment.