Skip to content

Commit

Permalink
Merge pull request #724 from rxy0210/e2e
Browse files Browse the repository at this point in the history
test: add testcase for clustertree util
  • Loading branch information
duanmengkk authored Sep 28, 2024
2 parents 3b459ca + a9b7500 commit 880287b
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 0 deletions.
63 changes: 63 additions & 0 deletions pkg/clustertree/cluster-manager/utils/leaf_model_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package utils

import (
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"

kosmosv1alpha1 "github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1"
)

func TestClassificationHandler(t *testing.T) {
// 创建一个假的 Kubernetes 客户端
rootClientset := fake.NewSimpleClientset()
leafClientset := fake.NewSimpleClientset()

clusterParty := &kosmosv1alpha1.Cluster{
Spec: kosmosv1alpha1.ClusterSpec{
ClusterTreeOptions: &kosmosv1alpha1.ClusterTreeOptions{
Enable: true,
LeafModels: []kosmosv1alpha1.LeafModel{
{
NodeSelector: kosmosv1alpha1.NodeSelector{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"app": "test"},
},
},
},
},
},
},
}

handlerParty := NewLeafModelHandler(clusterParty, rootClientset, leafClientset)

modeParty := handlerParty.GetLeafMode()
if modeParty != Party {
t.Errorf("expected modeParty to remain 3, got %v", modeParty)
}

if !clusterParty.Spec.ClusterTreeOptions.Enable {
t.Errorf("expected Enable to remain true, got %v", clusterParty.Spec.ClusterTreeOptions.Enable)
}

clusterALL := &kosmosv1alpha1.Cluster{
Spec: kosmosv1alpha1.ClusterSpec{
ClusterTreeOptions: &kosmosv1alpha1.ClusterTreeOptions{
Enable: false,
},
},
}

handlerALL := NewLeafModelHandler(clusterALL, rootClientset, leafClientset)

modeALL := handlerALL.GetLeafMode()
if modeALL != ALL {
t.Errorf("expected GetLeafNodes to remain 1, got %v", modeALL)
}

if !clusterParty.Spec.ClusterTreeOptions.Enable {
t.Errorf("expected Enable to remain false, got %v", clusterParty.Spec.ClusterTreeOptions.Enable)
}
}
37 changes: 37 additions & 0 deletions pkg/clustertree/cluster-manager/utils/rootcluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package utils

import (
"testing"

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

kosmosv1alpha1 "github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1"
)

func TestIsRootCluster(t *testing.T) {
// Create a mock cluster with the root annotation
cluster := &kosmosv1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
Annotations: map[string]string{
RootClusterAnnotationKey: RootClusterAnnotationValue,
},
},
}

if !IsRootCluster(cluster) {
t.Errorf("expected IsRootCluster to remain true, got %v", "false")
}

// Test for non-root cluster
nonRootCluster := &kosmosv1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "non-root-cluster",
Annotations: map[string]string{},
},
}

if IsRootCluster(nonRootCluster) {
t.Errorf("Expected the cluster to not be identified as a root cluster")
}
}
165 changes: 165 additions & 0 deletions pkg/kosmosctl/util/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package util

import (
"testing"
)

func TestGenerateDeployment(t *testing.T) {
template := `apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deployment
spec:
replicas: 2
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
containers:
- name: test-container
image: nginx
`

obj := struct{}{} // 可以根据需要定义实际对象

deployment, err := GenerateDeployment(template, obj)

if err != nil {
t.Errorf("expected no error")
}

if deployment == nil {
t.Errorf("expected deployment is not nil")
return
}

if deployment.Name != "test-deployment" {
t.Errorf("expected deployment name is %v", "test-deployment")
}
}

func TestGenerateDaemonSet(t *testing.T) {
template := `apiVersion: apps/v1
kind: DaemonSet
metadata:
name: test-daemonset
spec:
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
containers:
- name: test-container
image: nginx
`

obj := struct{}{} // 可以根据需要定义实际对象

daemonSet, err := GenerateDaemonSet(template, obj)

if err != nil {
t.Errorf("expected no error")
}

if daemonSet == nil {
t.Errorf("expected daemonSet is not nil")
return
}

if daemonSet.Name != "test-daemonset" {
t.Errorf("expected daemonSet name is %v", "test-daemonset")
}
}

func TestGenerateServiceAccount(t *testing.T) {
template := `apiVersion: v1
kind: ServiceAccount
metadata:
name: test-serviceaccount
`

obj := struct{}{} // 可以根据需要定义实际对象

serviceAccount, err := GenerateServiceAccount(template, obj)
if err != nil {
t.Errorf("expected no error")
}

if serviceAccount == nil {
t.Errorf("expected serviceAccount is not nil")
return
}

if serviceAccount.Name != "test-serviceaccount" {
t.Errorf("expected serviceAccount name is %v", "test-serviceaccount")
}
}

func TestGenerateClusterRole(t *testing.T) {
template := `apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: test-clusterrole
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
`

obj := struct{}{} // 可以根据需要定义实际对象

clusterRole, err := GenerateClusterRole(template, obj)

if err != nil {
t.Errorf("expected no error")
}

if clusterRole == nil {
t.Errorf("expected clusterRole is not nil")
return
}

if clusterRole.Name != "test-clusterrole" {
t.Errorf("expected clusterRole name is %v", "test-clusterrole")
}
}

func TestGenerateClusterRoleBinding(t *testing.T) {
template := `apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: test-clusterrolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: test-clusterrole
subjects:
- kind: ServiceAccount
name: default
namespace: default
`

obj := struct{}{} // 可以根据需要定义实际对象

clusterRoleBinding, err := GenerateClusterRoleBinding(template, obj)
if err != nil {
t.Errorf("expected no error")
}

if clusterRoleBinding == nil {
t.Errorf("expected clusterrolebinding is not nil")
return
}

if clusterRoleBinding.Name != "test-clusterrolebinding" {
t.Errorf("expected clusterrolebinding name is %v", "test-clusterrolebinding")
}
}

0 comments on commit 880287b

Please sign in to comment.