Skip to content

Commit

Permalink
added test inside gke for passthrough label
Browse files Browse the repository at this point in the history
  • Loading branch information
vicentefb committed Apr 30, 2024
1 parent 7a007e9 commit e264796
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 49 deletions.
48 changes: 48 additions & 0 deletions pkg/cloudproduct/gke/gke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
package gke

import (
"fmt"
"testing"

"agones.dev/agones/pkg/apis"
agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
"agones.dev/agones/pkg/util/runtime"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -222,6 +224,52 @@ func TestPodSeccompUnconfined(t *testing.T) {
}
}

func TestSetPassthroughLabel(t *testing.T) {
for name, tc := range map[string]struct {
pod *corev1.Pod
wantPod *corev1.Pod
ports []agonesv1.GameServerPort
features string
}{
"gameserver with with Passthrough port policy adds label to pod": {
features: fmt.Sprintf("%s=true", runtime.FeatureAutopilotPassthroughPort),

pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
Labels: map[string]string{},
},
},
ports: []agonesv1.GameServerPort{
{
Name: "awesome-udp",
PortPolicy: agonesv1.Passthrough,
ContainerPort: 1234,
Protocol: corev1.ProtocolUDP,
},
},
wantPod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
Labels: map[string]string{
agonesv1.GameServerPortPolicyPodLabel: "autopilot-passthrough",
},
},
},
},
} {
t.Run(name, func(t *testing.T) {
runtime.FeatureTestMutex.Lock()
defer runtime.FeatureTestMutex.Unlock()
require.NoError(t, runtime.ParseFeatures(tc.features))
gs := (&autopilotPortAllocator{minPort: 7000, maxPort: 8000}).Allocate(&agonesv1.GameServer{Spec: agonesv1.GameServerSpec{Ports: tc.ports}})
pod := tc.pod.DeepCopy()
setPassthroughLabel(&gs.Spec, pod)
assert.Equal(t, tc.wantPod, pod)
})
}
}

func TestSetEvictionNoExtended(t *testing.T) {
emptyPodAnd := func(f func(*corev1.Pod)) *corev1.Pod {
pod := &corev1.Pod{
Expand Down
50 changes: 1 addition & 49 deletions pkg/gameservers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"agones.dev/agones/pkg/cloudproduct/generic"
agtesting "agones.dev/agones/pkg/testing"

agonesruntime "agones.dev/agones/pkg/util/runtime"
"agones.dev/agones/pkg/util/webhooks"
"github.com/heptiolabs/healthcheck"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -940,54 +939,6 @@ func TestControllerSyncGameServerCreatingState(t *testing.T) {
agtesting.AssertEventContains(t, m.FakeRecorder.Events, "Pod")
})

t.Run("Testing pod label is added to gameserver when using Passthrough PortPolicy", func(t *testing.T) {
features := fmt.Sprintf("%s=true", agonesruntime.FeatureAutopilotPassthroughPort)
agonesruntime.FeatureTestMutex.Lock()
defer agonesruntime.FeatureTestMutex.Unlock()
require.NoError(t, agonesruntime.ParseFeatures(features))
c, m := newFakeController()
fixture := &agonesv1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "default"},
Spec: newSingleContainerSpec(), Status: agonesv1.GameServerStatus{State: agonesv1.GameServerStateCreating}}
fixture.Spec.Ports[0].PortPolicy = agonesv1.Passthrough
fixture.Spec.Ports[0].Name = "udp-port"
fixture.Spec.Ports[0].HostPort = 7000
fixture.ApplyDefaults()
podCreated := false
gsUpdated := false

var pod *corev1.Pod
m.KubeClient.AddReactor("create", "pods", func(action k8stesting.Action) (bool, runtime.Object, error) {
podCreated = true
ca := action.(k8stesting.CreateAction)
pod = ca.GetObject().(*corev1.Pod)
assert.True(t, metav1.IsControlledBy(pod, fixture))
assert.Equal(t, "autopilot-passthrough", pod.ObjectMeta.Labels[agonesv1.GameServerPortPolicyPodLabel])
return true, pod, nil
})
m.AgonesClient.AddReactor("update", "gameservers", func(action k8stesting.Action) (bool, runtime.Object, error) {
gsUpdated = true
ua := action.(k8stesting.UpdateAction)
gs := ua.GetObject().(*agonesv1.GameServer)
assert.Equal(t, agonesv1.GameServerStateStarting, gs.Status.State)
assert.Len(t, gs.Spec.Ports, 1)
assert.Equal(t, "udp-port", gs.Spec.Ports[0].Name)
assert.Equal(t, corev1.ProtocolUDP, gs.Spec.Ports[0].Protocol)
return true, gs, nil
})

ctx, cancel := agtesting.StartInformers(m, c.gameServerSynced, c.podSynced)
defer cancel()

gs, err := c.syncGameServerCreatingState(ctx, fixture)

assert.NoError(t, err)
assert.True(t, podCreated, "Pod should have been created")

assert.Equal(t, agonesv1.GameServerStateStarting, gs.Status.State)
assert.True(t, gsUpdated, "GameServer should have been updated")
agtesting.AssertEventContains(t, m.FakeRecorder.Events, "Pod")
})

t.Run("Syncing from Created State, with no issues", func(t *testing.T) {
c, m := newFakeController()
fixture := newFixture()
Expand Down Expand Up @@ -1319,6 +1270,7 @@ func TestControllerCreateGameServerPod(t *testing.T) {
return true, pod, nil
})

fixture.Spec.Ports[0].PortPolicy = agonesv1.Passthrough
gs, err := c.createGameServerPod(context.Background(), fixture)
require.NoError(t, err)
assert.Equal(t, fixture.Status.State, gs.Status.State)
Expand Down

0 comments on commit e264796

Please sign in to comment.