agones.dev/agones@v1.53.0/pkg/apis/agones/v1/gameserverset_test.go (about)

     1  // Copyright 2018 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 v1
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	corev1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/util/validation"
    25  )
    26  
    27  func TestGameServerSetGameServer(t *testing.T) {
    28  	gsSet := GameServerSet{
    29  		ObjectMeta: metav1.ObjectMeta{
    30  			Name:      "test",
    31  			Namespace: "namespace",
    32  			UID:       "1234",
    33  			Labels: map[string]string{
    34  				FleetNameLabel: "fleetname",
    35  			},
    36  		},
    37  		Spec: GameServerSetSpec{
    38  			Replicas: 10,
    39  			Template: GameServerTemplateSpec{
    40  				Spec: GameServerSpec{
    41  					Ports: []GameServerPort{{ContainerPort: 1234}},
    42  					Template: corev1.PodTemplateSpec{
    43  						Spec: corev1.PodSpec{
    44  							Containers: []corev1.Container{{Name: "container", Image: "myimage"}},
    45  						},
    46  					},
    47  				},
    48  			},
    49  		},
    50  	}
    51  
    52  	gs := gsSet.GameServer()
    53  	assert.Equal(t, "", gs.ObjectMeta.Name)
    54  	assert.Equal(t, gsSet.ObjectMeta.Namespace, gs.ObjectMeta.Namespace)
    55  	assert.Equal(t, gsSet.ObjectMeta.Name+"-", gs.ObjectMeta.GenerateName)
    56  	assert.Equal(t, gsSet.ObjectMeta.Name, gs.ObjectMeta.Labels[GameServerSetGameServerLabel])
    57  	assert.Equal(t, gsSet.ObjectMeta.Labels[FleetNameLabel], gs.ObjectMeta.Labels[FleetNameLabel])
    58  
    59  	assert.Equal(t, gs.Spec, gsSet.Spec.Template.Spec)
    60  	assert.True(t, metav1.IsControlledBy(gs, &gsSet))
    61  }
    62  
    63  // TestGameServerSetValidateUpdate test GameServerSet Validate() and ValidateUpdate()
    64  func TestGameServerSetValidateUpdate(t *testing.T) {
    65  	gsSpec := defaultGameServer().Spec
    66  	gsSet := GameServerSet{
    67  		ObjectMeta: metav1.ObjectMeta{Name: "test"},
    68  		Spec: GameServerSetSpec{
    69  			Replicas: 10,
    70  			Template: GameServerTemplateSpec{
    71  				Spec: gsSpec,
    72  			},
    73  		},
    74  	}
    75  
    76  	errs := gsSet.ValidateUpdate(gsSet.DeepCopy())
    77  	assert.Empty(t, errs)
    78  
    79  	newGSS := gsSet.DeepCopy()
    80  	newGSS.Spec.Replicas = 5
    81  	errs = gsSet.ValidateUpdate(newGSS)
    82  	assert.Empty(t, errs)
    83  
    84  	newGSS.Spec.Template.Spec.Ports[0].ContainerPort = 321
    85  	errs = gsSet.ValidateUpdate(newGSS)
    86  	assert.Len(t, errs, 1)
    87  	assert.Equal(t, "spec.template", errs[0].Field)
    88  
    89  	newGSS = gsSet.DeepCopy()
    90  	longName := strings.Repeat("f", validation.LabelValueMaxLength+1)
    91  	newGSS.Name = longName
    92  	errs = newGSS.Validate(fakeAPIHooks{})
    93  	assert.Len(t, errs, 1)
    94  	assert.Equal(t, "metadata.name", errs[0].Field)
    95  
    96  	newGSS.Name = ""
    97  	newGSS.GenerateName = longName
    98  	errs = newGSS.Validate(fakeAPIHooks{})
    99  	assert.Len(t, errs, 0)
   100  
   101  	newGSS = gsSet.DeepCopy()
   102  	newGSS.Name = longName
   103  	errs = gsSet.ValidateUpdate(newGSS)
   104  	assert.Len(t, errs, 1)
   105  	assert.Equal(t, "metadata.name", errs[0].Field)
   106  
   107  	newGSS = gsSet.DeepCopy()
   108  	newGSS.Spec.Template.ObjectMeta.Labels = make(map[string]string)
   109  	newGSS.Spec.Template.ObjectMeta.Labels[longName] = ""
   110  	errs = newGSS.Validate(fakeAPIHooks{})
   111  	assert.Len(t, errs, 1)
   112  	assert.Equal(t, "spec.template.metadata.labels", errs[0].Field)
   113  
   114  	// Same validation applies to nested Labels which applies to GameServer pod
   115  	newGSS = gsSet.DeepCopy()
   116  	newGSS.Spec.Template.Spec.Template.ObjectMeta.Labels = make(map[string]string)
   117  	newGSS.Spec.Template.Spec.Template.ObjectMeta.Labels[longName] = ""
   118  	errs = newGSS.Validate(fakeAPIHooks{})
   119  	assert.Len(t, errs, 1)
   120  	assert.Equal(t, "spec.template.spec.template.metadata.labels", errs[0].Field)
   121  
   122  	// Similar Annotations validation check
   123  	newGSS = gsSet.DeepCopy()
   124  	newGSS.Spec.Template.ObjectMeta.Annotations = make(map[string]string)
   125  	newGSS.Spec.Template.ObjectMeta.Annotations[longName] = ""
   126  	errs = newGSS.Validate(fakeAPIHooks{})
   127  	assert.Len(t, errs, 1)
   128  	assert.Equal(t, "spec.template.metadata.annotations", errs[0].Field)
   129  
   130  	// Nested GS Spec Annotations
   131  	newGSS = gsSet.DeepCopy()
   132  	newGSS.Spec.Template.Spec.Template.ObjectMeta.Annotations = make(map[string]string)
   133  	newGSS.Spec.Template.Spec.Template.ObjectMeta.Annotations[longName] = ""
   134  	errs = newGSS.Validate(fakeAPIHooks{})
   135  	assert.Len(t, errs, 1)
   136  	assert.Equal(t, "spec.template.spec.template.metadata.annotations", errs[0].Field)
   137  
   138  	gsSet.Spec.Template.Spec.Template =
   139  		corev1.PodTemplateSpec{
   140  			Spec: corev1.PodSpec{
   141  				Containers: []corev1.Container{{Name: "container", Image: "myimage"}, {Name: "container2", Image: "myimage"}},
   142  			},
   143  		}
   144  
   145  	errs = gsSet.Validate(fakeAPIHooks{})
   146  	assert.Len(t, errs, 2)
   147  	assert.Equal(t, "spec.template.spec.container", errs[0].Field)
   148  }