forked from agones-dev/agones
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfleets_test.go
More file actions
119 lines (101 loc) · 3.82 KB
/
Copy pathfleets_test.go
File metadata and controls
119 lines (101 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright 2018 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fleets
import (
"sort"
"strconv"
"testing"
"agones.dev/agones/pkg/apis/stable/v1alpha1"
agtesting "agones.dev/agones/pkg/testing"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
k8stesting "k8s.io/client-go/testing"
)
func TestListGameServerSetsByFleetOwner(t *testing.T) {
t.Parallel()
f := &v1alpha1.Fleet{
ObjectMeta: metav1.ObjectMeta{
Name: "fleet-1",
Namespace: "default",
UID: "1234",
},
Spec: v1alpha1.FleetSpec{
Replicas: 5,
Template: v1alpha1.GameServerTemplateSpec{},
},
}
gsSet1 := f.GameServerSet()
gsSet1.ObjectMeta.Name = "gsSet1"
gsSet2 := f.GameServerSet()
gsSet2.ObjectMeta.Name = "gsSet2"
gsSet3 := f.GameServerSet()
gsSet3.ObjectMeta.Name = "gsSet3"
gsSet3.ObjectMeta.Labels = nil
gsSet4 := f.GameServerSet()
gsSet4.ObjectMeta.Name = "gsSet4"
gsSet4.ObjectMeta.OwnerReferences = nil
m := agtesting.NewMocks()
m.AgonesClient.AddReactor("list", "gameserversets", func(action k8stesting.Action) (bool, runtime.Object, error) {
return true, &v1alpha1.GameServerSetList{Items: []v1alpha1.GameServerSet{*gsSet1, *gsSet2, *gsSet3, *gsSet4}}, nil
})
gameServerSets := m.AgonesInformerFactory.Stable().V1alpha1().GameServerSets()
_, cancel := agtesting.StartInformers(m, gameServerSets.Informer().HasSynced)
defer cancel()
list, err := ListGameServerSetsByFleetOwner(gameServerSets.Lister(), f)
assert.Nil(t, err)
// sort of stable ordering
sort.SliceStable(list, func(i, j int) bool {
return list[i].ObjectMeta.Name < list[j].ObjectMeta.Name
})
assert.Equal(t, []*v1alpha1.GameServerSet{gsSet1, gsSet2}, list)
}
func TestListGameServersByFleetOwner(t *testing.T) {
t.Parallel()
f := defaultFixture()
gsSet := f.GameServerSet()
gsSet.ObjectMeta.Name = "gsSet1"
var gsList []v1alpha1.GameServer
for i := 1; i <= 3; i++ {
gs := gsSet.GameServer()
gs.ObjectMeta.Name = "gs" + strconv.Itoa(i)
gsList = append(gsList, *gs)
}
m := agtesting.NewMocks()
m.AgonesClient.AddReactor("list", "fleets", func(action k8stesting.Action) (bool, runtime.Object, error) {
return true, &v1alpha1.FleetList{Items: []v1alpha1.Fleet{*f}}, nil
})
m.AgonesClient.AddReactor("list", "gameserversets", func(action k8stesting.Action) (bool, runtime.Object, error) {
return true, &v1alpha1.GameServerSetList{Items: []v1alpha1.GameServerSet{*gsSet}}, nil
})
m.AgonesClient.AddReactor("list", "gameservers", func(action k8stesting.Action) (bool, runtime.Object, error) {
return true, &v1alpha1.GameServerList{Items: gsList}, nil
})
informer := m.AgonesInformerFactory.Stable().V1alpha1()
_, cancel := agtesting.StartInformers(m,
informer.Fleets().Informer().HasSynced, informer.GameServerSets().Informer().HasSynced, informer.GameServers().Informer().HasSynced)
defer cancel()
list, err := ListGameServersByFleetOwner(informer.GameServers().Lister(), informer.GameServerSets().Lister(), f)
assert.Nil(t, err)
assert.Len(t, list, len(gsList), "Retrieved list should be same size as original")
sort.SliceStable(list, func(i, j int) bool {
return list[i].ObjectMeta.Name < list[j].ObjectMeta.Name
})
// need to loop, because need to dereference
for i, gs := range gsList {
assert.Equal(t, gs, *list[i])
}
}