agones.dev/agones@v1.53.0/pkg/cloudproduct/generic/generic_test.go (about)

     1  // Copyright 2023 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package generic
    16  
    17  import (
    18  	"testing"
    19  
    20  	"agones.dev/agones/pkg/apis"
    21  	"agones.dev/agones/pkg/apis/agones"
    22  	agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
    23  	"github.com/stretchr/testify/assert"
    24  	corev1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  )
    27  
    28  func TestGameServerPodAutoscalerAnnotations(t *testing.T) {
    29  	testCases := []struct {
    30  		description        string
    31  		scheduling         apis.SchedulingStrategy
    32  		setAnnotation      bool
    33  		expectedAnnotation string
    34  	}{
    35  		{
    36  			description:        "Packed",
    37  			scheduling:         apis.Packed,
    38  			expectedAnnotation: "false",
    39  		},
    40  		{
    41  			description:        "Distributed",
    42  			scheduling:         apis.Distributed,
    43  			expectedAnnotation: "false",
    44  		},
    45  		{
    46  			description:        "Packed with autoscaler annotation",
    47  			scheduling:         apis.Packed,
    48  			setAnnotation:      true,
    49  			expectedAnnotation: "true",
    50  		},
    51  		{
    52  			description:        "Distributed with autoscaler annotation",
    53  			scheduling:         apis.Distributed,
    54  			setAnnotation:      true,
    55  			expectedAnnotation: "true",
    56  		},
    57  	}
    58  
    59  	fixture := &agonesv1.GameServer{
    60  		ObjectMeta: metav1.ObjectMeta{Name: "logan"},
    61  		Spec:       agonesv1.GameServerSpec{Container: "sheep"},
    62  		Status:     agonesv1.GameServerStatus{Eviction: &agonesv1.Eviction{Safe: agonesv1.EvictionSafeNever}},
    63  	}
    64  	for _, tc := range testCases {
    65  		t.Run(tc.description, func(t *testing.T) {
    66  			gs := fixture.DeepCopy()
    67  			gs.Spec.Scheduling = tc.scheduling
    68  			if tc.setAnnotation {
    69  				gs.Spec.Template = corev1.PodTemplateSpec{ObjectMeta: metav1.ObjectMeta{
    70  					Annotations: map[string]string{agonesv1.PodSafeToEvictAnnotation: "true"},
    71  				}}
    72  			}
    73  			pod, err := gs.Pod(&generic{})
    74  			assert.Nil(t, err, "Pod should not return an error")
    75  			assert.Equal(t, gs.ObjectMeta.Name, pod.ObjectMeta.Name)
    76  			assert.Equal(t, gs.ObjectMeta.Namespace, pod.ObjectMeta.Namespace)
    77  			assert.Equal(t, agonesv1.GameServerLabelRole, pod.ObjectMeta.Labels[agonesv1.RoleLabel])
    78  			assert.Equal(t, "gameserver", pod.ObjectMeta.Labels[agones.GroupName+"/role"])
    79  			assert.Equal(t, gs.ObjectMeta.Name, pod.ObjectMeta.Labels[agonesv1.GameServerPodLabel])
    80  			assert.Equal(t, "sheep", pod.ObjectMeta.Annotations[agonesv1.GameServerContainerAnnotation])
    81  			assert.True(t, metav1.IsControlledBy(pod, gs))
    82  			assert.Equal(t, tc.expectedAnnotation, pod.ObjectMeta.Annotations[agonesv1.PodSafeToEvictAnnotation])
    83  		})
    84  	}
    85  }