agones.dev/agones@v1.54.0/pkg/cloudproduct/eviction/eviction_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 eviction
    16  
    17  import (
    18  	"testing"
    19  
    20  	agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
    21  	"github.com/stretchr/testify/assert"
    22  	corev1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  )
    25  
    26  func TestSetEviction(t *testing.T) {
    27  	emptyPodAnd := func(f func(*corev1.Pod)) *corev1.Pod {
    28  		pod := &corev1.Pod{
    29  			ObjectMeta: metav1.ObjectMeta{
    30  				Annotations: map[string]string{},
    31  				Labels:      map[string]string{},
    32  			},
    33  		}
    34  		f(pod)
    35  		return pod
    36  	}
    37  	for desc, tc := range map[string]struct {
    38  		eviction *agonesv1.Eviction
    39  		pod      *corev1.Pod
    40  		wantPod  *corev1.Pod
    41  	}{
    42  		"eviction: safe: Always, no incoming labels/annotations": {
    43  			eviction: &agonesv1.Eviction{Safe: agonesv1.EvictionSafeAlways},
    44  			pod:      emptyPodAnd(func(*corev1.Pod) {}),
    45  			wantPod: emptyPodAnd(func(pod *corev1.Pod) {
    46  				pod.ObjectMeta.Annotations[agonesv1.PodSafeToEvictAnnotation] = agonesv1.True
    47  				pod.ObjectMeta.Labels[agonesv1.SafeToEvictLabel] = agonesv1.True
    48  			}),
    49  		},
    50  		"eviction: safe: OnUpgrade, no incoming labels/annotations": {
    51  			eviction: &agonesv1.Eviction{Safe: agonesv1.EvictionSafeOnUpgrade},
    52  			pod:      emptyPodAnd(func(*corev1.Pod) {}),
    53  			wantPod: emptyPodAnd(func(pod *corev1.Pod) {
    54  				pod.ObjectMeta.Annotations[agonesv1.PodSafeToEvictAnnotation] = agonesv1.False
    55  				pod.ObjectMeta.Labels[agonesv1.SafeToEvictLabel] = agonesv1.True
    56  			}),
    57  		},
    58  		"eviction: safe: Never, no incoming labels/annotations": {
    59  			eviction: &agonesv1.Eviction{Safe: agonesv1.EvictionSafeNever},
    60  			pod:      emptyPodAnd(func(*corev1.Pod) {}),
    61  			wantPod: emptyPodAnd(func(pod *corev1.Pod) {
    62  				pod.ObjectMeta.Annotations[agonesv1.PodSafeToEvictAnnotation] = agonesv1.False
    63  				pod.ObjectMeta.Labels[agonesv1.SafeToEvictLabel] = agonesv1.False
    64  			}),
    65  		},
    66  		"eviction: safe: Always, incoming labels/annotations": {
    67  			eviction: &agonesv1.Eviction{Safe: agonesv1.EvictionSafeAlways},
    68  			pod: emptyPodAnd(func(pod *corev1.Pod) {
    69  				pod.ObjectMeta.Annotations[agonesv1.PodSafeToEvictAnnotation] = "just don't touch, ok?"
    70  				pod.ObjectMeta.Labels[agonesv1.SafeToEvictLabel] = "seriously, leave it"
    71  			}),
    72  			wantPod: emptyPodAnd(func(pod *corev1.Pod) {
    73  				pod.ObjectMeta.Annotations[agonesv1.PodSafeToEvictAnnotation] = "just don't touch, ok?"
    74  				pod.ObjectMeta.Labels[agonesv1.SafeToEvictLabel] = "seriously, leave it"
    75  			}),
    76  		},
    77  		"eviction: safe: OnUpgrade, incoming labels/annotations": {
    78  			eviction: &agonesv1.Eviction{Safe: agonesv1.EvictionSafeOnUpgrade},
    79  			pod: emptyPodAnd(func(pod *corev1.Pod) {
    80  				pod.ObjectMeta.Annotations[agonesv1.PodSafeToEvictAnnotation] = "better not touch"
    81  				pod.ObjectMeta.Labels[agonesv1.SafeToEvictLabel] = "not another one"
    82  			}),
    83  			wantPod: emptyPodAnd(func(pod *corev1.Pod) {
    84  				pod.ObjectMeta.Annotations[agonesv1.PodSafeToEvictAnnotation] = "better not touch"
    85  				pod.ObjectMeta.Labels[agonesv1.SafeToEvictLabel] = "not another one"
    86  			}),
    87  		},
    88  		"eviction: safe: Never, incoming labels/annotations": {
    89  			eviction: &agonesv1.Eviction{Safe: agonesv1.EvictionSafeNever},
    90  			pod: emptyPodAnd(func(pod *corev1.Pod) {
    91  				pod.ObjectMeta.Annotations[agonesv1.PodSafeToEvictAnnotation] = "a passthrough"
    92  				pod.ObjectMeta.Labels[agonesv1.SafeToEvictLabel] = "or is it passthru?"
    93  			}),
    94  			wantPod: emptyPodAnd(func(pod *corev1.Pod) {
    95  				pod.ObjectMeta.Annotations[agonesv1.PodSafeToEvictAnnotation] = "a passthrough"
    96  				pod.ObjectMeta.Labels[agonesv1.SafeToEvictLabel] = "or is it passthru?"
    97  			}),
    98  		},
    99  	} {
   100  		t.Run(desc, func(t *testing.T) {
   101  			assert.NoError(t, SetEviction(tc.eviction, tc.pod))
   102  			assert.Equal(t, tc.wantPod, tc.pod)
   103  		})
   104  	}
   105  }