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

[Test] : Add Test Case ( k8sutils/statefulset.go & k8sutils/service.go ) #655

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.27.10
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
k8s.io/api v0.28.2
k8s.io/apimachinery v0.28.2
k8s.io/client-go v0.28.2
Expand Down Expand Up @@ -46,12 +47,12 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
Expand Down
230 changes: 230 additions & 0 deletions k8sutils/services_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package k8sutils

import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

func TestGenerateServiceDef(t *testing.T) {
tests := []struct {
name string
serviceMeta metav1.ObjectMeta
enableMetrics bool
headless bool
serviceType string
expected *corev1.Service
}{
{
name: "Test sentinel with ClusterIP service type",
serviceMeta: metav1.ObjectMeta{
Name: "test-service",
Labels: map[string]string{
"role": "sentinel",
},
},
enableMetrics: false,
headless: false,
serviceType: "ClusterIP",
expected: &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-service",
Labels: map[string]string{
"role": "sentinel",
},
OwnerReferences: []metav1.OwnerReference{
{},
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "sentinel-client",
Port: sentinelPort,
TargetPort: intstr.FromInt(int(sentinelPort)),
Protocol: corev1.ProtocolTCP,
},
},
Selector: map[string]string{"role": "sentinel"},
ClusterIP: "",
Type: corev1.ServiceTypeClusterIP,
},
},
},
{
name: "Test sentinel with headless service",
serviceMeta: metav1.ObjectMeta{
Name: "test-service",
Labels: map[string]string{
"role": "sentinel",
},
},
enableMetrics: false,
headless: true,
serviceType: "ClusterIP",
expected: &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-service",
Labels: map[string]string{
"role": "sentinel",
},
OwnerReferences: []metav1.OwnerReference{
{},
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "sentinel-client",
Port: sentinelPort,
TargetPort: intstr.FromInt(int(sentinelPort)),
Protocol: corev1.ProtocolTCP,
},
},
Selector: map[string]string{"role": "sentinel"},
ClusterIP: "None",
Type: corev1.ServiceTypeClusterIP,
},
},
},
{
name: "Test redis with ClusterIP service type",
serviceMeta: metav1.ObjectMeta{
Name: "test-redis-service",
Labels: map[string]string{
"role": "redis",
},
},
enableMetrics: false,
headless: false,
serviceType: "ClusterIP",
expected: &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-redis-service",
Labels: map[string]string{
"role": "redis",
},
OwnerReferences: []metav1.OwnerReference{
{},
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "redis-client",
Port: redisPort,
TargetPort: intstr.FromInt(int(redisPort)),
Protocol: corev1.ProtocolTCP,
},
},
Selector: map[string]string{"role": "redis"},
ClusterIP: "",
Type: corev1.ServiceTypeClusterIP,
},
},
},
{
name: "Test redis with headless service",
serviceMeta: metav1.ObjectMeta{
Name: "test-redis-headless-service",
Labels: map[string]string{
"role": "redis",
},
},
enableMetrics: false,
headless: true,
serviceType: "ClusterIP",
expected: &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-redis-headless-service",
Labels: map[string]string{
"role": "redis",
},
OwnerReferences: []metav1.OwnerReference{
{},
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "redis-client",
Port: redisPort,
TargetPort: intstr.FromInt(int(redisPort)),
Protocol: corev1.ProtocolTCP,
},
},
Selector: map[string]string{"role": "redis"},
ClusterIP: "None",
Type: corev1.ServiceTypeClusterIP,
},
},
},
{
name: "Test redis with ClusterIP service type and metrics enabled",
serviceMeta: metav1.ObjectMeta{
Name: "test-redis-metrics-service",
Labels: map[string]string{
"role": "redis",
},
},
enableMetrics: true,
headless: false,
serviceType: "ClusterIP",
expected: &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-redis-metrics-service",
Labels: map[string]string{
"role": "redis",
},
OwnerReferences: []metav1.OwnerReference{
{},
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "redis-client",
Port: redisPort,
TargetPort: intstr.FromInt(int(redisPort)),
Protocol: corev1.ProtocolTCP,
},
*enableMetricsPort(),
},
Selector: map[string]string{"role": "redis"},
ClusterIP: "",
Type: corev1.ServiceTypeClusterIP,
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := generateServiceDef(tt.serviceMeta, tt.enableMetrics, metav1.OwnerReference{}, tt.headless, tt.serviceType)
assert.Equal(t, tt.expected, actual)
})
}
}
Loading
Loading