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