Skip to content

Commit

Permalink
Adds test for findGameServerForAllocation
Browse files Browse the repository at this point in the history
  • Loading branch information
igooch committed Apr 25, 2023
1 parent cb1e2a3 commit 4fb3162
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions pkg/gameserverallocations/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,119 @@ func TestFindGameServerForAllocationDistributed(t *testing.T) {
assert.FailNow(t, "We should get a different gameserver by now")

}

func TestFindGameServerForAllocationCountListActions(t *testing.T) {
t.Parallel()

ONE := int64(1)
FORTY := int64(40)
INCREMENT := "Increment"
READY := agonesv1.GameServerStateReady

gs1 := agonesv1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "gs2", Namespace: defaultNs, UID: "2"},
Status: agonesv1.GameServerStatus{NodeName: "node1", State: agonesv1.GameServerStateReady,
Lists: map[string]agonesv1.ListStatus{
"players": {
Values: []string{},
Capacity: 100,
},
},
Counters: map[string]agonesv1.CounterStatus{
"rooms": {
Count: 101,
Capacity: 1000,
}}}}

testScenarios := map[string]struct {
features string
gsa *allocationv1.GameServerAllocation
list []agonesv1.GameServer
listPointer []*agonesv1.GameServer
wantCounters map[string]agonesv1.CounterStatus
wantLists map[string]agonesv1.ListStatus
}{
"CounterActions increment and ListActions append and update capacity": {
features: fmt.Sprintf("%s=true&%s=true", runtime.FeatureCountsAndLists, runtime.FeatureStateAllocationFilter),
gsa: &allocationv1.GameServerAllocation{
ObjectMeta: metav1.ObjectMeta{Namespace: defaultNs},
Spec: allocationv1.GameServerAllocationSpec{
Selectors: []allocationv1.GameServerSelector{{
GameServerState: &READY,
}},
Scheduling: apis.Packed,
Counters: map[string]allocationv1.CounterAction{
"rooms": {
Action: &INCREMENT,
Amount: &ONE,
}},
Lists: map[string]allocationv1.ListAction{
"players": {
AddValues: &[]string{"x7un", "8inz"},
Capacity: &FORTY,
}}}},
list: []agonesv1.GameServer{gs1},
listPointer: []*agonesv1.GameServer{&gs1},
wantCounters: map[string]agonesv1.CounterStatus{
"rooms": {
Count: 102,
Capacity: 1000,
}},
wantLists: map[string]agonesv1.ListStatus{
"players": {
Values: []string{"x7un", "8inz"},
Capacity: 40,
}},
},
}

for test, testScenario := range testScenarios {
t.Run(test, func(t *testing.T) {
runtime.FeatureTestMutex.Lock()
defer runtime.FeatureTestMutex.Unlock()
// we always set the feature flag in all these tests, so always process it.
require.NoError(t, runtime.ParseFeatures(testScenario.features))

testScenario.gsa.ApplyDefaults()
_, ok := testScenario.gsa.Validate()
require.True(t, ok)

controller, m := newFakeController()
c := controller.allocator.allocationCache

m.AgonesClient.AddReactor("list", "gameservers", func(action k8stesting.Action) (bool, k8sruntime.Object, error) {
return true, &agonesv1.GameServerList{Items: testScenario.list}, nil
})

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

// This call initializes the cache
err := c.syncCache()
assert.Nil(t, err)

err = c.counter.Run(ctx, 0)
assert.Nil(t, err)

// This test assumes only one matching game server in the list (findGameServerForAllocation
// will always return the first match and does not remove it from the list).
foundGs, _, err := findGameServerForAllocation(testScenario.gsa, testScenario.listPointer)
assert.NoError(t, err)
changes := false
for counter, counterStatus := range testScenario.wantCounters {
if gsCounter, ok := foundGs.Status.Counters[counter]; ok {
changes = true
assert.Equal(t, counterStatus, gsCounter)
}
}
for list, listStatus := range testScenario.wantLists {
if gsList, ok := foundGs.Status.Lists[list]; ok {
changes = true
assert.Equal(t, listStatus, gsList)
}
}
if changes == false {
assert.Equal(t, testScenario.list[0], foundGs)
}
})
}
}

0 comments on commit 4fb3162

Please sign in to comment.