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