Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for service file in pkg/estimator/client #5427

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions pkg/estimator/client/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
Copyright 2024 The Karmada Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package client

import (
"context"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes/fake"
)

func TestResolveCluster(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about writing the test function using a table drive?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sure , we can look into it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@XiShanYongYe-Chang i have pushed the latest commits , we can process with review now !

tests := []struct {
name string
namespace string
id string
port int32
service *corev1.Service
expectError bool
expected string
}{
{
name: "Service not found",
namespace: "default",
id: "nonexistent",
port: 80,
service: nil,
expected: "nonexistent.default.svc.cluster.local:80",
},
{
name: "Unsupported service type",
namespace: "default",
id: "myservice",
port: 80,
service: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "myservice",
Namespace: "default",
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
},
},
expectError: true,
},
{
name: "ExternalName service with int target port",
namespace: "default",
id: "myservice",
port: 80,
service: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "myservice",
Namespace: "default",
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeExternalName,
ExternalName: "example.com",
Ports: []corev1.ServicePort{
{
Port: 80,
TargetPort: intstr.FromInt(8080),
},
},
},
},
expected: "example.com:8080",
},
{
name: "ExternalName service with non-int target port",
namespace: "default",
id: "myservice",
port: 80,
service: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "myservice",
Namespace: "default",
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeExternalName,
ExternalName: "example.com",
Ports: []corev1.ServicePort{
{
Port: 80,
TargetPort: intstr.FromString("http"),
},
},
},
},
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
clientset := fake.NewSimpleClientset()
if tt.service != nil {
_, err := clientset.CoreV1().Services(tt.namespace).Create(context.TODO(), tt.service, metav1.CreateOptions{})
if err != nil {
t.Fatalf("failed to create service: %v", err)
}
}

result, err := resolveCluster(clientset, tt.namespace, tt.id, tt.port)
if (err != nil) != tt.expectError {
t.Errorf("expected error: %v, got: %v", tt.expectError, err)
}
if result != tt.expected {
t.Errorf("expected: %s, got: %s", tt.expected, result)
}
})
}
}

func TestFindServicePort(t *testing.T) {
tests := []struct {
name string
service *corev1.Service
port int32
expectError bool
}{
{
name: "Port found",
service: &corev1.Service{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{Port: 80},
},
},
},
port: 80,
},
{
name: "Port not found",
service: &corev1.Service{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{Port: 8080},
},
},
},
port: 80,
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := findServicePort(tt.service, tt.port)
if (err != nil) != tt.expectError {
t.Errorf("expected error: %v, got: %v", tt.expectError, err)
}
})
}
}