You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: allocator_test.go
    path of file: ./repos/agones/test/e2e
    
    the code of the file until where you have to start completion: // Copyright 2019 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package e2e

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"testing"
	"time"

	pb "agones.dev/agones/pkg/allocation/go"
	agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
	multiclusterv1 "agones.dev/agones/pkg/apis/multicluster/v1"
	"agones.dev/agones/pkg/util/runtime"
	helper "agones.dev/agones/test/e2e/allochelper"
	e2e "agones.dev/agones/test/e2e/framework"
	"github.com/sirupsen/logrus"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/types/known/wrapperspb"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/util/uuid"
	"k8s.io/apimachinery/pkg/util/wait"
)

const (
	agonesSystemNamespace          = "agones-system"
	allocatorServiceName           = "agones-allocator"
	allocatorTLSName               = "allocator-tls"
	tlsCrtTag                      = "tls.crt"
	tlsKeyTag                      = "tls.key"
	allocatorReqURLFmt             = "%s:%d"
	allocatorClientSecretName      = "allocator-client.default"
	allocatorClientSecretNamespace = "default"
)

func TestAllocatorWithDeprecatedRequired(t *testing.T) {
	ctx := context.Background()

	ip, port := helper.GetAllocatorEndpoint(ctx, t, framework)
	requestURL := fmt.Sprintf(allocatorReqURLFmt, ip, port)
	tlsCA := helper.RefreshAllocatorTLSCerts(ctx, t, ip, framework)

	var flt *agonesv1.Fleet
	var err error
	if runtime.FeatureEnabled(runtime.FeaturePlayerAllocationFilter) {
		flt, err = helper.CreateFleetWithOpts(ctx, framework.Namespace, framework, func(f *agonesv1.Fleet) {
			f.Spec.Template.Spec.Players = &agonesv1.PlayersSpec{
				InitialCapacity: 10,
			}
		})
	} else {
		flt, err = helper.CreateFleet(ctx, framework.Namespace, framework)
	}
	assert.NoError(t, err)

	framework.AssertFleetCondition(t, flt, e2e.FleetReadyCount(flt.Spec.Replicas))
	request := &pb.AllocationRequest{
		Namespace:                    framework.Namespace,
		RequiredGameServerSelector:   &pb.GameServerSelector{MatchLabels: map[string]string{agonesv1.FleetNameLabel: flt.ObjectMeta.Name}},
		PreferredGameServerSelectors: []*pb.GameServerSelector{{MatchLabels: map[string]string{agonesv1.FleetNameLabel: flt.ObjectMeta.Name}}},
		Scheduling:                   pb.AllocationRequest_Packed,
		Metadata:                     &pb.MetaPatch{Labels: map[string]string{"gslabel": "allocatedbytest"}},
	}

	var response *pb.AllocationResponse
	// wait for the allocation system to come online
	err = wait.PollUntilContextTimeout(context.Background(), 2*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
		// create the grpc client each time, as we may end up looking at an old cert
		dialOpts, err := helper.CreateRemoteClusterDialOption(ctx, allocatorClientSe
    
func TestAllocatorWithSelectors(t *testing.T) {
	ctx := context.Background()

	ip, port := helper.GetAllocatorEndpoint(ctx, t, framework)
	requestURL := fmt.Sprintf(allocatorReqURLFmt, ip, port)
	tlsCA := helper.RefreshAllocatorTLSCerts(ctx, t, ip, framework)

	var flt *agonesv1.Fleet
	var err error
	if runtime.FeatureEnabled(runtime.FeaturePlayerAllocationFilter) {
		flt, err = helper.CreateFleetWithOpts(ctx, framework.Namespace, framework, func(f *agonesv1.Fleet) {
			f.Spec.Template.Spec.Players = &agonesv1.PlayersSpec{
				InitialCapacity: 10,
			}
		})
	} else {
		flt, err = helper.CreateFleet(ctx, framework.Namespace, framework)
	}
	assert.NoError(t, err)

	framework.AssertFleetCondition(t, flt, e2e.FleetReadyCount(flt.Spec.Replicas))
	request := &pb.AllocationRequest{
		Namespace:           framework.Namespace,
		GameServerSelectors: []*pb.GameServerSelector{{MatchLabels: map[string]string{agonesv1.FleetNameLabel: flt.ObjectMeta.Name}}},
		Scheduling:          pb.AllocationRequest_Packed,
		Metadata:            &pb.MetaPatch{Labels: map[string]string{"gslabel": "allocatedbytest", "blue-frog.fred_thing": "test.dog_fred-blue"}},
	}

	var response *pb.AllocationResponse
	// wait for the allocation system to come online
	err = wait.PollUntilContextTimeout(context.Background(), 2*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
		// create the grpc client each time, as we may end up looking at an old cert
		dialOpts, err := helper.CreateRemoteClusterDialOption(ctx, allocatorClientSecretNamespace, allocatorClientSecretName, tlsCA, framework)
		if err != nil {
			return false, err
		}

		conn, err := grpc.Dial(requestURL, dialOpts)
		if err != nil {
			logrus.WithError(err).Info("failing grpc.Dial")
			return false, nil
		}
		defer conn.Close() // nolint: errcheck

		grpcClient := pb.NewAllocationServiceClient(conn)
		response, err = grpcClient.Allocate(context.Background(), request)
		if err != nil {
			logrus.WithError(err).Info("failing Allocate request")
			return false, nil
		}
		helper.ValidateAllocatorResponse(t, response)

		// let's do a re-allocation
		if runtime.FeatureEnabled(runtime.FeaturePlayerAllocationFilter) {
			request.GameServerSelectors[0].GameServerState = pb.GameServerSelector_ALLOCATED
			allocatedResponse, err := grpcClient.Allocate(context.Background(), request)
			require.NoError(t, err)
			require.Equal(t, response.GameServerName, allocatedResponse.GameServerName)
			helper.ValidateAllocatorResponse(t, allocatedResponse)
			assert.Equal(t, flt.ObjectMeta.Name, allocatedResponse.Metadata.Labels[agonesv1.FleetNameLabel])

			// do a capacity based allocation
			logrus.Info("testing capacity allocation filter")
			request.GameServerSelectors[0].Players = &pb.PlayerSelector{
				MinAvailable: 5,
				MaxAvailable: 10,
			}
			allocatedResponse, err = grpcClient.Allocate(context.Background(), request)
			require.NoError(t, err)
			require.Equal(t, response.GameServerName, allocatedResponse.GameServerName)
			helper.ValidateAllocatorResponse(t, allocatedResponse)

			// do a capacity based allocation that should fail
			request.GameServerSelectors[0].GameServerState = pb.GameServerSelector_ALLOCATED
			request.GameServerSelectors[0].Players = &pb.PlayerSelector{MinAvailable: 99, MaxAvailable: 200}

			allocatedResponse, err = grpcClient.Allocate(context.Background(), request)
			assert.Nil(t, allocatedResponse)
			status, ok := status.FromError(err)
			require.True(t, ok)
			assert.Equal(t, codes.ResourceExhausted, status.Code())
		}

		return true, nil
	})

	assert.NoError(t, err)
}