agones.dev/agones@v1.54.0/sdks/go/sdk_test.go (about)

     1  // Copyright 2017 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 sdk
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	"google.golang.org/grpc"
    25  	"google.golang.org/grpc/metadata"
    26  
    27  	"agones.dev/agones/pkg/sdk"
    28  )
    29  
    30  func TestSDK(t *testing.T) {
    31  	sm := &sdkMock{
    32  		hm: &healthMock{},
    33  	}
    34  	s := SDK{
    35  		ctx:    context.Background(),
    36  		client: sm,
    37  		health: sm.hm,
    38  	}
    39  
    40  	assert.False(t, sm.ready)
    41  	assert.False(t, sm.shutdown)
    42  	assert.False(t, sm.hm.healthy)
    43  
    44  	err := s.Ready()
    45  	assert.Nil(t, err)
    46  	assert.True(t, sm.ready)
    47  	assert.False(t, sm.shutdown)
    48  
    49  	err = s.Reserve(12 * time.Second)
    50  	assert.NoError(t, err)
    51  	assert.EqualValues(t, 12, sm.reserved.Seconds)
    52  
    53  	err = s.Health()
    54  	assert.Nil(t, err)
    55  	assert.True(t, sm.hm.healthy)
    56  
    57  	err = s.Allocate()
    58  	assert.NoError(t, err)
    59  	assert.True(t, sm.allocated)
    60  
    61  	err = s.Shutdown()
    62  	assert.Nil(t, err)
    63  	assert.True(t, sm.ready)
    64  	assert.True(t, sm.shutdown)
    65  
    66  	gs, err := s.GameServer()
    67  	assert.Nil(t, err)
    68  	assert.NotNil(t, gs)
    69  }
    70  
    71  func TestSDKWatchGameServer(t *testing.T) {
    72  	sm := &sdkMock{
    73  		wm: &watchMock{msgs: make(chan *sdk.GameServer, 5)},
    74  	}
    75  	s := SDK{
    76  		ctx:    context.Background(),
    77  		client: sm,
    78  	}
    79  
    80  	fixture := &sdk.GameServer{ObjectMeta: &sdk.GameServer_ObjectMeta{Name: "test-server"}}
    81  
    82  	updated := make(chan struct{}, 5)
    83  
    84  	err := s.WatchGameServer(func(gs *sdk.GameServer) {
    85  		assert.Equal(t, fixture.ObjectMeta.Name, gs.ObjectMeta.Name)
    86  		updated <- struct{}{}
    87  	})
    88  	assert.Nil(t, err)
    89  
    90  	sm.wm.msgs <- fixture
    91  
    92  	select {
    93  	case <-updated:
    94  	case <-time.After(5 * time.Second):
    95  		assert.FailNow(t, "update handler should have fired")
    96  	}
    97  }
    98  
    99  func TestSDKSetLabel(t *testing.T) {
   100  	t.Parallel()
   101  	sm := &sdkMock{
   102  		labels: map[string]string{},
   103  	}
   104  	s := SDK{
   105  		ctx:    context.Background(),
   106  		client: sm,
   107  	}
   108  
   109  	expected := "bar"
   110  	err := s.SetLabel("foo", expected)
   111  	assert.Nil(t, err)
   112  	assert.Equal(t, expected, sm.labels["agones.dev/sdk-foo"])
   113  }
   114  
   115  func TestSDKSetAnnotation(t *testing.T) {
   116  	t.Parallel()
   117  	sm := &sdkMock{
   118  		annotations: map[string]string{},
   119  	}
   120  	s := SDK{
   121  		ctx:    context.Background(),
   122  		client: sm,
   123  	}
   124  
   125  	expected := "bar"
   126  	err := s.SetAnnotation("foo", expected)
   127  	assert.Nil(t, err)
   128  	assert.Equal(t, expected, sm.annotations["agones.dev/sdk-foo"])
   129  }
   130  
   131  var _ sdk.SDKClient = &sdkMock{}
   132  var _ sdk.SDK_HealthClient = &healthMock{}
   133  var _ sdk.SDK_WatchGameServerClient = &watchMock{}
   134  
   135  type sdkMock struct {
   136  	ready       bool
   137  	shutdown    bool
   138  	allocated   bool
   139  	reserved    *sdk.Duration
   140  	hm          *healthMock
   141  	wm          *watchMock
   142  	labels      map[string]string
   143  	annotations map[string]string
   144  }
   145  
   146  func (m *sdkMock) SetLabel(_ context.Context, in *sdk.KeyValue, _ ...grpc.CallOption) (*sdk.Empty, error) {
   147  	m.labels["agones.dev/sdk-"+in.Key] = in.Value
   148  	return &sdk.Empty{}, nil
   149  }
   150  
   151  func (m *sdkMock) SetAnnotation(_ context.Context, in *sdk.KeyValue, _ ...grpc.CallOption) (*sdk.Empty, error) {
   152  	m.annotations["agones.dev/sdk-"+in.Key] = in.Value
   153  	return &sdk.Empty{}, nil
   154  }
   155  
   156  func (m *sdkMock) WatchGameServer(_ context.Context, _ *sdk.Empty, _ ...grpc.CallOption) (sdk.SDK_WatchGameServerClient, error) {
   157  	return m.wm, nil
   158  }
   159  
   160  func (m *sdkMock) GetGameServer(_ context.Context, _ *sdk.Empty, _ ...grpc.CallOption) (*sdk.GameServer, error) {
   161  	return &sdk.GameServer{}, nil
   162  }
   163  
   164  func (m *sdkMock) Ready(_ context.Context, e *sdk.Empty, _ ...grpc.CallOption) (*sdk.Empty, error) {
   165  	m.ready = true
   166  	return e, nil
   167  }
   168  
   169  func (m *sdkMock) Allocate(_ context.Context, e *sdk.Empty, _ ...grpc.CallOption) (*sdk.Empty, error) {
   170  	m.allocated = true
   171  	return e, nil
   172  }
   173  
   174  func (m *sdkMock) Shutdown(_ context.Context, e *sdk.Empty, _ ...grpc.CallOption) (*sdk.Empty, error) {
   175  	m.shutdown = true
   176  	return e, nil
   177  }
   178  
   179  func (m *sdkMock) Health(_ context.Context, _ ...grpc.CallOption) (sdk.SDK_HealthClient, error) {
   180  	return m.hm, nil
   181  }
   182  
   183  func (m *sdkMock) Reserve(_ context.Context, in *sdk.Duration, _ ...grpc.CallOption) (*sdk.Empty, error) {
   184  	m.reserved = in
   185  	return &sdk.Empty{}, nil
   186  }
   187  
   188  type healthMock struct {
   189  	healthy bool
   190  }
   191  
   192  func (h *healthMock) Send(*sdk.Empty) error {
   193  	h.healthy = true
   194  	return nil
   195  }
   196  
   197  func (h *healthMock) CloseAndRecv() (*sdk.Empty, error) {
   198  	panic("implement me")
   199  }
   200  
   201  func (h *healthMock) Header() (metadata.MD, error) {
   202  	panic("implement me")
   203  }
   204  
   205  func (h *healthMock) Trailer() metadata.MD {
   206  	panic("implement me")
   207  }
   208  
   209  func (h *healthMock) CloseSend() error {
   210  	panic("implement me")
   211  }
   212  
   213  func (h *healthMock) Context() context.Context {
   214  	panic("implement me")
   215  }
   216  
   217  func (h *healthMock) SendMsg(_ interface{}) error {
   218  	panic("implement me")
   219  }
   220  
   221  func (h *healthMock) RecvMsg(_ interface{}) error {
   222  	panic("implement me")
   223  }
   224  
   225  type watchMock struct {
   226  	msgs chan *sdk.GameServer
   227  }
   228  
   229  func (wm *watchMock) Recv() (*sdk.GameServer, error) {
   230  	return <-wm.msgs, nil
   231  }
   232  
   233  func (*watchMock) Header() (metadata.MD, error) {
   234  	panic("implement me")
   235  }
   236  
   237  func (*watchMock) Trailer() metadata.MD {
   238  	panic("implement me")
   239  }
   240  
   241  func (*watchMock) CloseSend() error {
   242  	panic("implement me")
   243  }
   244  
   245  func (*watchMock) Context() context.Context {
   246  	panic("implement me")
   247  }
   248  
   249  func (*watchMock) SendMsg(_ interface{}) error {
   250  	panic("implement me")
   251  }
   252  
   253  func (*watchMock) RecvMsg(_ interface{}) error {
   254  	panic("implement me")
   255  }