Skip to content

Commit

Permalink
add Unit Tests for controllers (#44)
Browse files Browse the repository at this point in the history
Signed-off-by: ChrisLiu <[email protected]>
  • Loading branch information
chrisliu1995 authored Mar 23, 2023
1 parent f585041 commit b1f3d94
Show file tree
Hide file tree
Showing 2 changed files with 783 additions and 0 deletions.
216 changes: 216 additions & 0 deletions pkg/controllers/gameserver/gameserver_manager_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
package gameserver

import (
kruiseV1alpha1 "github.com/openkruise/kruise-api/apps/v1alpha1"
kruiseV1beta1 "github.com/openkruise/kruise-api/apps/v1beta1"
gameKruiseV1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"testing"
)

var (
scheme = runtime.NewScheme()
)

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(gameKruiseV1alpha1.AddToScheme(scheme))
utilruntime.Must(kruiseV1beta1.AddToScheme(scheme))
utilruntime.Must(kruiseV1alpha1.AddToScheme(scheme))
}

func TestSyncServiceQualities(t *testing.T) {
up := intstr.FromInt(20)
dp := intstr.FromInt(10)
Expand Down Expand Up @@ -279,3 +297,201 @@ func TestSyncServiceQualities(t *testing.T) {
}
}
}

func TestSyncGsToPod(t *testing.T) {
up := intstr.FromInt(20)
dp := intstr.FromInt(10)
tests := []struct {
gs *gameKruiseV1alpha1.GameServer
pod *corev1.Pod
isUpdate bool
}{
{
gs: &gameKruiseV1alpha1.GameServer{
ObjectMeta: metav1.ObjectMeta{
Namespace: "xxx",
Name: "xxx-0",
Labels: map[string]string{
gameKruiseV1alpha1.GameServerOwnerGssKey: "xxx",
},
},
Spec: gameKruiseV1alpha1.GameServerSpec{
UpdatePriority: &up,
DeletionPriority: &dp,
OpsState: gameKruiseV1alpha1.WaitToDelete,
},
},
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: "xxx",
Name: "xxx-0",
},
Status: corev1.PodStatus{
Phase: corev1.PodPending,
},
},
isUpdate: true,
},

{
gs: &gameKruiseV1alpha1.GameServer{
ObjectMeta: metav1.ObjectMeta{
Namespace: "xxx",
Name: "xxx-0",
Labels: map[string]string{
gameKruiseV1alpha1.GameServerOwnerGssKey: "xxx",
},
},
Spec: gameKruiseV1alpha1.GameServerSpec{
UpdatePriority: &up,
DeletionPriority: &dp,
OpsState: gameKruiseV1alpha1.WaitToDelete,
},
Status: gameKruiseV1alpha1.GameServerStatus{
CurrentState: gameKruiseV1alpha1.Creating,
},
},
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: "xxx",
Name: "xxx-0",
Labels: map[string]string{
gameKruiseV1alpha1.GameServerOpsStateKey: string(gameKruiseV1alpha1.WaitToDelete),
gameKruiseV1alpha1.GameServerDeletePriorityKey: dp.String(),
gameKruiseV1alpha1.GameServerUpdatePriorityKey: up.String(),
gameKruiseV1alpha1.GameServerStateKey: string(gameKruiseV1alpha1.Creating),
},
},
Status: corev1.PodStatus{
Phase: corev1.PodPending,
},
},
isUpdate: false,
},
}

for _, test := range tests {
objs := []client.Object{test.gs, test.pod}
c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
manager := &GameServerManager{
client: c,
gameServer: test.gs,
pod: test.pod,
}

isUpdate, err := manager.SyncGsToPod()
if err != nil {
t.Error(err)
}

if isUpdate != test.isUpdate {
t.Errorf("expect isUpdate is %v ,but actually is %v", test.isUpdate, isUpdate)
}
}
}

func TestSyncNetworkStatus(t *testing.T) {
fakeTime := metav1.Now()
portInternal := intstr.FromInt(80)
portExternal := intstr.FromInt(601)
tests := []struct {
gs *gameKruiseV1alpha1.GameServer
pod *corev1.Pod
gsNetworkStatus gameKruiseV1alpha1.NetworkStatus
}{
{
gs: &gameKruiseV1alpha1.GameServer{
Status: gameKruiseV1alpha1.GameServerStatus{
NetworkStatus: gameKruiseV1alpha1.NetworkStatus{},
},
},
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
gameKruiseV1alpha1.GameServerNetworkType: "xxx-type",
gameKruiseV1alpha1.GameServerNetworkConf: "[{\"name\":\"SlbIds\",\"value\":\"lb-2zev1w12n684h7ymjtpuo\"},{\"name\":\"PortProtocols\",\"value\":\"80\"},{\"name\":\"Fixed\",\"value\":\"true\"}]",
gameKruiseV1alpha1.GameServerNetworkDisabled: "false",
gameKruiseV1alpha1.GameServerNetworkStatus: "{\"internalAddresses\":[{\"ip\":\"172.16.1.132\",\"ports\":[{\"name\":\"80\",\"protocol\":\"TCP\",\"port\":80}],\"portRange\":{}}],\"externalAddresses\":[{\"ip\":\"47.99.47.99\",\"ports\":[{\"name\":\"80\",\"protocol\":\"TCP\",\"port\":601}],\"portRange\":{}}],\"currentNetworkState\":\"Ready\",\"createTime\":null,\"lastTransitionTime\":null}",
},
},
},
gsNetworkStatus: gameKruiseV1alpha1.NetworkStatus{
NetworkType: "xxx-type",
DesiredNetworkState: gameKruiseV1alpha1.NetworkReady,
CreateTime: fakeTime,
LastTransitionTime: fakeTime,
},
},

{
gs: &gameKruiseV1alpha1.GameServer{
Status: gameKruiseV1alpha1.GameServerStatus{
NetworkStatus: gameKruiseV1alpha1.NetworkStatus{
NetworkType: "xxx-type",
DesiredNetworkState: gameKruiseV1alpha1.NetworkReady,
CreateTime: fakeTime,
LastTransitionTime: fakeTime,
},
},
},
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
gameKruiseV1alpha1.GameServerNetworkType: "xxx-type",
gameKruiseV1alpha1.GameServerNetworkConf: "[{\"name\":\"SlbIds\",\"value\":\"lb-2zev1w12n684h7ymjtpuo\"},{\"name\":\"PortProtocols\",\"value\":\"80\"},{\"name\":\"Fixed\",\"value\":\"true\"}]",
gameKruiseV1alpha1.GameServerNetworkDisabled: "false",
gameKruiseV1alpha1.GameServerNetworkStatus: "{\"internalAddresses\":[{\"ip\":\"172.16.1.132\",\"ports\":[{\"name\":\"80\",\"protocol\":\"TCP\",\"port\":80}],\"portRange\":{}}],\"externalAddresses\":[{\"ip\":\"47.99.47.99\",\"ports\":[{\"name\":\"80\",\"protocol\":\"TCP\",\"port\":601}],\"portRange\":{}}],\"currentNetworkState\":\"Ready\",\"createTime\":null,\"lastTransitionTime\":null}",
},
},
},
gsNetworkStatus: gameKruiseV1alpha1.NetworkStatus{
NetworkType: "xxx-type",
CurrentNetworkState: gameKruiseV1alpha1.NetworkReady,
DesiredNetworkState: gameKruiseV1alpha1.NetworkReady,
InternalAddresses: []gameKruiseV1alpha1.NetworkAddress{
{
IP: "172.16.1.132",
Ports: []gameKruiseV1alpha1.NetworkPort{
{
Name: "80",
Protocol: "TCP",
Port: &portInternal,
},
},
},
},
ExternalAddresses: []gameKruiseV1alpha1.NetworkAddress{
{
IP: "47.99.47.99",
Ports: []gameKruiseV1alpha1.NetworkPort{
{
Name: "80",
Protocol: "TCP",
Port: &portExternal,
},
},
},
},
CreateTime: fakeTime,
LastTransitionTime: fakeTime,
},
},
}

for _, test := range tests {
objs := []client.Object{test.gs, test.pod}
c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
manager := &GameServerManager{
client: c,
gameServer: test.gs,
pod: test.pod,
}

actual := manager.syncNetworkStatus()
actual.CreateTime = fakeTime
actual.LastTransitionTime = fakeTime
if !reflect.DeepEqual(test.gsNetworkStatus, actual) {
t.Errorf("expect gsNetworkStatus is %v ,but actually is %v", test.gsNetworkStatus, actual)
}
}
}
Loading

0 comments on commit b1f3d94

Please sign in to comment.