@@ -376,15 +376,30 @@ func TestGameServerAllocationMetaDataPatch(t *testing.T) {
376376 t .Parallel ()
377377 ctx := context .Background ()
378378
379- gs := framework .DefaultGameServer (framework .Namespace )
380- gs .ObjectMeta .Labels = map [string ]string {"test" : t .Name ()}
379+ log := logrus .WithField ("test" , t .Name ())
380+ createAndAllocate := func (input * allocationv1.GameServerAllocation ) * allocationv1.GameServerAllocation {
381+ gs := framework .DefaultGameServer (framework .Namespace )
382+ gs .ObjectMeta .Labels = map [string ]string {"test" : t .Name ()}
383+ gs , err := framework .CreateGameServerAndWaitUntilReady (t , framework .Namespace , gs )
384+ require .NoError (t , err )
385+
386+ log .WithField ("gs" , gs .ObjectMeta .Name ).Info ("👍 created and ready" )
387+
388+ // poll, as it may take a moment for the allocation cache to be populated
389+ err = wait .PollImmediate (time .Second , 30 * time .Second , func () (bool , error ) {
390+ input , err = framework .AgonesClient .AllocationV1 ().GameServerAllocations (framework .Namespace ).Create (ctx , input , metav1.CreateOptions {})
391+ if err != nil {
392+ log .WithError (err ).Info ("Failed, trying again..." )
393+ return false , err
394+ }
381395
382- gs , err := framework .CreateGameServerAndWaitUntilReady (t , framework .Namespace , gs )
383- if ! assert .Nil (t , err ) {
384- assert .FailNow (t , "could not create GameServer" )
396+ return allocationv1 .GameServerAllocationAllocated == input .Status .State , nil
397+ })
398+ require .NoError (t , err )
399+ return input
385400 }
386- defer framework .AgonesClient .AgonesV1 ().GameServers (framework .Namespace ).Delete (ctx , gs .ObjectMeta .Name , metav1.DeleteOptions {}) // nolint: errcheck
387401
402+ // two standard labels
388403 gsa := & allocationv1.GameServerAllocation {ObjectMeta : metav1.ObjectMeta {GenerateName : "allocation-" },
389404 Spec : allocationv1.GameServerAllocationSpec {
390405 Selectors : []allocationv1.GameServerSelector {{LabelSelector : metav1.LabelSelector {MatchLabels : map [string ]string {"test" : t .Name ()}}}},
@@ -393,25 +408,29 @@ func TestGameServerAllocationMetaDataPatch(t *testing.T) {
393408 Annotations : map [string ]string {"dog" : "good" },
394409 },
395410 }}
411+ result := createAndAllocate (gsa )
412+ defer framework .AgonesClient .AgonesV1 ().GameServers (framework .Namespace ).Delete (ctx , result .Status .GameServerName , metav1.DeleteOptions {}) // nolint: errcheck
396413
397- err = wait .PollImmediate (time .Second , 30 * time .Second , func () (bool , error ) {
398- gsa , err = framework .AgonesClient .AllocationV1 ().GameServerAllocations (framework .Namespace ).Create (ctx , gsa .DeepCopy (), metav1.CreateOptions {})
414+ gs , err := framework .AgonesClient .AgonesV1 ().GameServers (framework .Namespace ).Get (ctx , result .Status .GameServerName , metav1.GetOptions {})
415+ require .NoError (t , err )
416+ assert .Equal (t , "blue" , gs .ObjectMeta .Labels ["red" ])
417+ assert .Equal (t , "good" , gs .ObjectMeta .Annotations ["dog" ])
399418
400- if err != nil {
401- return true , err
402- }
419+ // use special characters that are valid
420+ gsa .Spec .MetaPatch = allocationv1.MetaPatch {Labels : map [string ]string {"blue-frog.fred_thing" : "test" }}
421+ result = createAndAllocate (gsa )
422+ defer framework .AgonesClient .AgonesV1 ().GameServers (framework .Namespace ).Delete (ctx , result .Status .GameServerName , metav1.DeleteOptions {}) // nolint: errcheck
403423
404- return allocationv1 .GameServerAllocationAllocated == gsa .Status .State , nil
405- })
406- if err != nil {
407- assert .FailNow (t , err .Error ())
408- }
409-
410- gs , err = framework .AgonesClient .AgonesV1 ().GameServers (framework .Namespace ).Get (ctx , gsa .Status .GameServerName , metav1.GetOptions {})
411- if assert .Nil (t , err ) {
412- assert .Equal (t , "blue" , gs .ObjectMeta .Labels ["red" ])
413- assert .Equal (t , "good" , gs .ObjectMeta .Annotations ["dog" ])
414- }
424+ gs , err = framework .AgonesClient .AgonesV1 ().GameServers (framework .Namespace ).Get (ctx , result .Status .GameServerName , metav1.GetOptions {})
425+ require .NoError (t , err )
426+ assert .Equal (t , "test" , gs .ObjectMeta .Labels ["blue-frog.fred_thing" ])
427+
428+ // throw something invalid at it.
429+ gsa .Spec .MetaPatch = allocationv1.MetaPatch {Labels : map [string ]string {"$$$$$$$" : "test" }}
430+ result , err = framework .AgonesClient .AllocationV1 ().GameServerAllocations (framework .Namespace ).Create (ctx , gsa .DeepCopy (), metav1.CreateOptions {})
431+ log .WithField ("result" , result ).WithError (err ).Info ("Failed allocation" )
432+ require .Error (t , err )
433+ require .Contains (t , err .Error (), "GameServerAllocation is invalid" )
415434}
416435
417436func TestGameServerAllocationPreferredSelection (t * testing.T ) {
0 commit comments