agones.dev/agones@v1.53.0/test/e2e/allocator/pod_termination_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 allocator
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	pb "agones.dev/agones/pkg/allocation/go"
    23  	agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
    24  	helper "agones.dev/agones/test/e2e/allochelper"
    25  	e2e "agones.dev/agones/test/e2e/framework"
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  	"google.golang.org/grpc/status"
    29  	"k8s.io/apimachinery/pkg/labels"
    30  	"k8s.io/apimachinery/pkg/util/wait"
    31  
    32  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    33  )
    34  
    35  const (
    36  	retryInterval = 5 * time.Second
    37  	retryTimeout  = 45 * time.Second
    38  )
    39  
    40  func TestAllocatorAfterDeleteReplica(t *testing.T) {
    41  	ctx := context.Background()
    42  	logger := e2e.TestLogger(t)
    43  
    44  	// initialize gRPC client, which tests the connection
    45  	grpcClient, err := helper.GetAllocatorClient(ctx, t, framework)
    46  	require.NoError(t, err, "Could not initialize rpc client")
    47  
    48  	// poll and wait until all allocator pods are available
    49  	err = wait.PollUntilContextTimeout(context.Background(), retryInterval, retryTimeout, true, func(ctx context.Context) (done bool, err error) {
    50  		deployment, err := framework.KubeClient.AppsV1().Deployments("agones-system").Get(ctx, "agones-allocator", metav1.GetOptions{})
    51  		if err != nil {
    52  			return true, err
    53  		}
    54  		if deployment.Status.Replicas != deployment.Status.AvailableReplicas {
    55  			logger.Infof("Waiting for agones-allocator to stabilize: %d/%d replicas available", deployment.Status.AvailableReplicas, deployment.Status.ReadyReplicas)
    56  			return false, nil
    57  		}
    58  		return true, nil
    59  	})
    60  	require.NoError(t, err, "Failed to stabilize agones-allocator")
    61  
    62  	// create fleet
    63  	flt, err := helper.CreateFleet(ctx, framework.Namespace, framework)
    64  	if !assert.Nil(t, err) {
    65  		return
    66  	}
    67  	framework.AssertFleetCondition(t, flt, e2e.FleetReadyCount(flt.Spec.Replicas))
    68  
    69  	logger.Infof("=== agones-allocator available, gRPC client initialized ===")
    70  
    71  	// One probe into the test, delete all of the allocators except 1
    72  	go func() {
    73  		time.Sleep(retryInterval)
    74  
    75  		list, err := framework.KubeClient.CoreV1().Pods("agones-system").List(
    76  			ctx, metav1.ListOptions{LabelSelector: labels.Set{"multicluster.agones.dev/role": "allocator"}.String()})
    77  		if assert.NoError(t, err, "Could not list allocator pods") {
    78  			for _, pod := range list.Items[1:] {
    79  				logger.Infof("Deleting Pod %s", pod.ObjectMeta.Name)
    80  				err = helper.DeleteAgonesPod(ctx, pod.ObjectMeta.Name, "agones-system", framework)
    81  				assert.NoError(t, err, "Could not delete allocator pod")
    82  			}
    83  		}
    84  	}()
    85  
    86  	request := &pb.AllocationRequest{
    87  		Namespace:                    framework.Namespace,
    88  		PreferredGameServerSelectors: []*pb.GameServerSelector{{MatchLabels: map[string]string{agonesv1.FleetNameLabel: flt.ObjectMeta.Name}}},
    89  		Scheduling:                   pb.AllocationRequest_Packed,
    90  		Metadata:                     &pb.MetaPatch{Labels: map[string]string{"gslabel": "allocatedbytest"}},
    91  	}
    92  
    93  	// Wait and keep making calls till we know the draining time has passed
    94  	_ = wait.PollUntilContextTimeout(context.Background(), retryInterval, retryTimeout, true, func(_ context.Context) (bool, error) {
    95  		response, err := grpcClient.Allocate(context.Background(), request)
    96  		logger.Infof("err = %v (code = %v), response = %v", err, status.Code(err), response)
    97  		helper.ValidateAllocatorResponse(t, response)
    98  		require.NoError(t, err, "Failed grpc allocation request")
    99  		return false, nil
   100  	})
   101  }