Merge pull request #1022 from gy95/names

add unit test for functions of generating names
This commit is contained in:
karmada-bot 2021-11-26 09:24:04 +08:00 committed by GitHub
commit e14b2063b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 187 additions and 3 deletions

View File

@ -1,6 +1,14 @@
package names
import "testing"
import (
"fmt"
"hash/fnv"
"testing"
"k8s.io/apimachinery/pkg/util/rand"
hashutil "github.com/karmada-io/karmada/pkg/util/hash"
)
func TestGenerateExecutionSpaceName(t *testing.T) {
type args struct {
@ -82,12 +90,18 @@ func TestGenerateBindingReferenceKey(t *testing.T) {
name string
namespace string
}{
{name: "mytest-deployment",
{
name: "mytest-deployment",
namespace: "prod-xxx",
},
{name: "mytest-deployment",
{
name: "mytest-deployment",
namespace: "qa-xxx",
},
{
name: "mytest-deployment",
namespace: "",
},
}
result := map[string]struct{}{}
for _, tt := range tests {
@ -100,3 +114,173 @@ func TestGenerateBindingReferenceKey(t *testing.T) {
})
}
}
func TestGenerateBindingName(t *testing.T) {
tests := []struct {
testCase string
kind string
name string
expect string
}{
{
testCase: "uppercase kind",
kind: "Pods",
name: "pod",
expect: "pod-pods",
},
{
testCase: "uppercase name",
kind: "pods",
name: "Pod",
expect: "pod-pods",
},
{
testCase: "lowercase name",
kind: "pods",
name: "Pod",
expect: "pod-pods",
},
}
for _, test := range tests {
if result := GenerateBindingName(test.kind, test.name); result != test.expect {
t.Errorf("Test %s failed: expected %v, but got %v", test.testCase, test.expect, result)
}
}
}
func TestGenerateWorkName(t *testing.T) {
tests := []struct {
testCase string
kind string
name string
namespace string
workname string
}{
{
testCase: "empty namespace",
kind: "Pod",
name: "pod",
namespace: "",
workname: "pod-pod",
},
{
testCase: "non nil namespace",
kind: "pods",
name: "Pod",
namespace: "default",
workname: "default-pod-pods",
},
}
for _, test := range tests {
got := GenerateWorkName(test.kind, test.name, test.namespace)
hash := fnv.New32a()
hashutil.DeepHashObject(hash, test.workname)
if result := fmt.Sprintf("%s-%s", test.name, rand.SafeEncodeString(fmt.Sprint(hash.Sum32()))); result != got {
t.Errorf("Test %s failed: expected %v, but got %v", test.testCase, result, got)
}
}
}
func TestGenerateServiceAccountName(t *testing.T) {
tests := []struct {
name string
clusterName string
expected string
}{
{
name: "non empty clusterName",
clusterName: "cluster1",
expected: "karmada-cluster1",
},
}
for _, test := range tests {
got := GenerateServiceAccountName(test.clusterName)
if got != test.expected {
t.Errorf("Test %s failed: expected %v, but got %v", test.name, test.expected, got)
}
}
}
func TestGenerateRoleName(t *testing.T) {
tests := []struct {
name string
serviceAccountName string
expected string
}{
{
name: "non empty serviceAccountName",
serviceAccountName: "account",
expected: "karmada-controller-manager:account",
},
}
for _, test := range tests {
got := GenerateRoleName(test.serviceAccountName)
if got != test.expected {
t.Errorf("Test %s failed: expected %v, but got %v", test.name, test.expected, got)
}
}
}
func TestGenerateEndpointSliceName(t *testing.T) {
tests := []struct {
name string
endpointSliceName string
cluster string
expected string
}{
{
name: "",
endpointSliceName: "endpoint",
cluster: "cluster",
expected: "imported-cluster-endpoint",
},
}
for _, test := range tests {
got := GenerateEndpointSliceName(test.endpointSliceName, test.cluster)
if got != test.expected {
t.Errorf("Test %s failed: expected %v, but got %v", test.name, test.expected, got)
}
}
}
func TestGenerateDerivedServiceName(t *testing.T) {
tests := []struct {
name string
serviceName string
expected string
}{
{
name: "",
serviceName: "service",
expected: "derived-service",
},
}
for _, test := range tests {
got := GenerateDerivedServiceName(test.serviceName)
if got != test.expected {
t.Errorf("Test %s failed: expected %v, but got %v", test.name, test.expected, got)
}
}
}
func TestGenerateEstimatorServiceName(t *testing.T) {
tests := []struct {
name string
clusterName string
expected string
}{
{
name: "",
clusterName: "cluster",
expected: "karmada-scheduler-estimator-cluster",
},
}
for _, test := range tests {
got := GenerateEstimatorServiceName(test.clusterName)
if got != test.expected {
t.Errorf("Test %s failed: expected %v, but got %v", test.name, test.expected, got)
}
}
}