agones.dev/agones@v1.53.0/pkg/sdkserver/helper_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 sdkserver
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  	"net/http"
    21  	"testing"
    22  	"time"
    23  
    24  	"agones.dev/agones/pkg/sdk"
    25  	"github.com/sirupsen/logrus"
    26  	"github.com/stretchr/testify/assert"
    27  	netcontext "golang.org/x/net/context"
    28  	"google.golang.org/grpc/metadata"
    29  	"k8s.io/apimachinery/pkg/util/wait"
    30  )
    31  
    32  func testHTTPHealth(t *testing.T, url string, expectedResponse string, expectedStatus int) {
    33  	// do a poll, because this code could run before the health check becomes live
    34  	err := wait.PollUntilContextTimeout(context.Background(), time.Second, 20*time.Second, true, func(_ context.Context) (done bool, err error) {
    35  		resp, err := http.Get(url)
    36  		if err != nil {
    37  			logrus.WithError(err).Error("Error connecting to ", url)
    38  			return false, nil
    39  		}
    40  
    41  		assert.NotNil(t, resp)
    42  		if resp != nil {
    43  			defer resp.Body.Close() // nolint: errcheck
    44  			body, err := io.ReadAll(resp.Body)
    45  			assert.Nil(t, err, "(%s) read response error should be nil: %v", url, err)
    46  			assert.Equal(t, expectedStatus, resp.StatusCode, "url: %s", url)
    47  			assert.Equal(t, []byte(expectedResponse), body, "(%s) response body should be '%s'", url, expectedResponse)
    48  		}
    49  
    50  		return true, nil
    51  	})
    52  	assert.Nil(t, err, "Timeout on %s health check, %v", url, err)
    53  }
    54  
    55  // emptyMockStream is the mock of the SDK_HealthServer for streaming
    56  type emptyMockStream struct {
    57  	msgs chan *sdk.Empty
    58  }
    59  
    60  func newEmptyMockStream() *emptyMockStream {
    61  	return &emptyMockStream{msgs: make(chan *sdk.Empty)}
    62  }
    63  
    64  func (m *emptyMockStream) SendAndClose(*sdk.Empty) error {
    65  	return nil
    66  }
    67  
    68  func (m *emptyMockStream) Recv() (*sdk.Empty, error) {
    69  	empty, ok := <-m.msgs
    70  	if ok {
    71  		return empty, nil
    72  	}
    73  	return empty, io.EOF
    74  }
    75  
    76  func (m *emptyMockStream) SetHeader(metadata.MD) error {
    77  	panic("implement me")
    78  }
    79  
    80  func (m *emptyMockStream) SendHeader(metadata.MD) error {
    81  	panic("implement me")
    82  }
    83  
    84  func (m *emptyMockStream) SetTrailer(metadata.MD) {
    85  	panic("implement me")
    86  }
    87  
    88  func (m *emptyMockStream) Context() context.Context {
    89  	panic("implement me")
    90  }
    91  
    92  func (m *emptyMockStream) SendMsg(_ interface{}) error {
    93  	panic("implement me")
    94  }
    95  
    96  func (m *emptyMockStream) RecvMsg(_ interface{}) error {
    97  	panic("implement me")
    98  }
    99  
   100  type gameServerMockStream struct {
   101  	ctx  context.Context
   102  	msgs chan *sdk.GameServer
   103  }
   104  
   105  // newGameServerMockStream implements SDK_WatchGameServerServer for testing
   106  func newGameServerMockStream() *gameServerMockStream {
   107  	return &gameServerMockStream{
   108  		ctx:  context.Background(),
   109  		msgs: make(chan *sdk.GameServer, 10),
   110  	}
   111  }
   112  
   113  func (m *gameServerMockStream) Send(gs *sdk.GameServer) error {
   114  	m.msgs <- gs
   115  	return nil
   116  }
   117  
   118  func (*gameServerMockStream) SetHeader(metadata.MD) error {
   119  	panic("implement me")
   120  }
   121  
   122  func (*gameServerMockStream) SendHeader(metadata.MD) error {
   123  	panic("implement me")
   124  }
   125  
   126  func (*gameServerMockStream) SetTrailer(metadata.MD) {
   127  	panic("implement me")
   128  }
   129  
   130  func (m *gameServerMockStream) Context() netcontext.Context {
   131  	return m.ctx
   132  }
   133  
   134  func (*gameServerMockStream) SendMsg(_ interface{}) error {
   135  	panic("implement me")
   136  }
   137  
   138  func (*gameServerMockStream) RecvMsg(_ interface{}) error {
   139  	panic("implement me")
   140  }