diff --git a/pkg/sdkserver/sdkserver_test.go b/pkg/sdkserver/sdkserver_test.go index fb89ff0565..0d8743f37f 100644 --- a/pkg/sdkserver/sdkserver_test.go +++ b/pkg/sdkserver/sdkserver_test.go @@ -28,6 +28,7 @@ import ( agruntime "agones.dev/agones/pkg/util/runtime" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "golang.org/x/net/context" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -333,9 +334,7 @@ func TestSidecarUpdateState(t *testing.T) { t.Run(k, func(t *testing.T) { m := agtesting.NewMocks() sc, err := defaultSidecar(m) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) sc.gsState = agonesv1.GameServerStateReady updated := false @@ -375,9 +374,8 @@ func TestSidecarHealthLastUpdated(t *testing.T) { m := agtesting.NewMocks() sc, err := defaultSidecar(m) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) + sc.health = agonesv1.Health{Disabled: false} fc := clock.NewFakeClock(now) sc.clock = fc @@ -424,9 +422,7 @@ func TestSidecarUnhealthyMessage(t *testing.T) { m := agtesting.NewMocks() sc, err := NewSDKServer("test", "default", m.KubeClient, m.AgonesClient) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) m.AgonesClient.AddReactor("list", "gameservers", func(action k8stesting.Action) (bool, runtime.Object, error) { gs := agonesv1.GameServer{ @@ -472,9 +468,8 @@ func TestSidecarHealthy(t *testing.T) { m := agtesting.NewMocks() sc, err := defaultSidecar(m) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) + // manually set the values sc.health = agonesv1.Health{FailureThreshold: 1} sc.healthTimeout = 5 * time.Second @@ -573,9 +568,8 @@ func TestSidecarHealthy(t *testing.T) { func TestSidecarHTTPHealthCheck(t *testing.T) { m := agtesting.NewMocks() sc, err := NewSDKServer("test", "default", m.KubeClient, m.AgonesClient) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) + now := time.Now().Add(time.Hour).UTC() fc := clock.NewFakeClock(now) // now we control time - so slow machines won't fail anymore @@ -642,18 +636,14 @@ func TestSDKServerGetGameServer(t *testing.T) { defer close(stop) sc, err := defaultSidecar(m) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) sc.informerFactory.Start(stop) assert.True(t, cache.WaitForCacheSync(stop, sc.gameServerSynced)) sc.gsWaitForSync.Done() result, err := sc.GetGameServer(context.Background(), &sdk.Empty{}) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Equal(t, fixture.ObjectMeta.Name, result.ObjectMeta.Name) assert.Equal(t, fixture.ObjectMeta.Namespace, result.ObjectMeta.Namespace) assert.Equal(t, string(fixture.Status.State), result.Status.State) @@ -663,9 +653,7 @@ func TestSDKServerWatchGameServer(t *testing.T) { t.Parallel() m := agtesting.NewMocks() sc, err := defaultSidecar(m) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Empty(t, sc.connectedStreams) stream := newGameServerMockStream() @@ -701,14 +689,10 @@ func TestSDKServerWatchGameServerFeatureSDKWatchSendOnExecute(t *testing.T) { m.AgonesClient.AddWatchReactor("gameservers", k8stesting.DefaultWatchReactor(fakeWatch, nil)) err := agruntime.ParseFeatures(string(agruntime.FeatureSDKWatchSendOnExecute) + "=true") - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) sc, err := defaultSidecar(m) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Empty(t, sc.connectedStreams) stop := make(chan struct{}) @@ -762,9 +746,7 @@ func TestSDKServerSendGameServerUpdate(t *testing.T) { t.Parallel() m := agtesting.NewMocks() sc, err := defaultSidecar(m) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Empty(t, sc.connectedStreams) stop := make(chan struct{}) @@ -800,7 +782,7 @@ func TestSDKServerUpdateEventHandler(t *testing.T) { defer close(stop) sc, err := defaultSidecar(m) - assert.Nil(t, err) + require.Nil(t, err) sc.informerFactory.Start(stop) assert.True(t, cache.WaitForCacheSync(stop, sc.gameServerSynced)) @@ -860,9 +842,8 @@ func TestSDKServerReserveTimeoutOnRun(t *testing.T) { }) sc, err := defaultSidecar(m) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) + stop := make(chan struct{}) sc.informerFactory.Start(stop) assert.True(t, cache.WaitForCacheSync(stop, sc.gameServerSynced)) @@ -1012,18 +993,14 @@ func TestSDKServerPlayerCapacity(t *testing.T) { defer agruntime.FeatureTestMutex.Unlock() err := agruntime.ParseFeatures(string(agruntime.FeaturePlayerTracking) + "=true") - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) m := agtesting.NewMocks() stop := make(chan struct{}) defer close(stop) sc, err := defaultSidecar(m) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) m.AgonesClient.AddReactor("list", "gameservers", func(action k8stesting.Action) (bool, runtime.Object, error) { gs := agonesv1.GameServer{ @@ -1073,9 +1050,7 @@ func TestSDKServerPlayerCapacity(t *testing.T) { assert.NoError(t, err) count, err := sc.GetPlayerCapacity(context.Background(), &alpha.Empty{}) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Equal(t, int64(20), count.Count) // on an update, confirm that the update hits the K8s api @@ -1102,9 +1077,7 @@ func TestSDKServerPlayerConnectAndDisconnect(t *testing.T) { defer close(stop) sc, err := defaultSidecar(m) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) capacity := int64(3) m.AgonesClient.AddReactor("list", "gameservers", func(action k8stesting.Action) (bool, runtime.Object, error) { @@ -1151,21 +1124,15 @@ func TestSDKServerPlayerConnectAndDisconnect(t *testing.T) { assert.NoError(t, err) count, err := sc.GetPlayerCount(context.Background(), e) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Equal(t, int64(0), count.Count) list, err := sc.GetConnectedPlayers(context.Background(), e) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Empty(t, list.List) ok, err := sc.IsPlayerConnected(context.Background(), &alpha.PlayerID{PlayerID: "1"}) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.False(t, ok.Bool, "no player connected yet") // sdk value should always be correct, even if we send more than one update per second. @@ -1173,27 +1140,19 @@ func TestSDKServerPlayerConnectAndDisconnect(t *testing.T) { token := strconv.FormatInt(i, 10) id := &alpha.PlayerID{PlayerID: token} ok, err := sc.PlayerConnect(context.Background(), id) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.True(t, ok.Bool, "Player "+token+" should not yet be connected") ok, err = sc.IsPlayerConnected(context.Background(), id) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.True(t, ok.Bool, "Player "+token+" should be connected") } count, err = sc.GetPlayerCount(context.Background(), e) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Equal(t, capacity, count.Count) list, err = sc.GetConnectedPlayers(context.Background(), e) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Equal(t, []string{"0", "1", "2"}, list.List) // on an update, confirm that the update hits the K8s api, only once @@ -1224,27 +1183,19 @@ func TestSDKServerPlayerConnectAndDisconnect(t *testing.T) { token := strconv.FormatInt(i, 10) id := &alpha.PlayerID{PlayerID: token} ok, err := sc.PlayerDisconnect(context.Background(), id) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Truef(t, ok.Bool, "Player %s should be disconnected", token) ok, err = sc.IsPlayerConnected(context.Background(), id) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Falsef(t, ok.Bool, "Player %s should be connected", token) } count, err = sc.GetPlayerCount(context.Background(), e) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Equal(t, int64(1), count.Count) list, err = sc.GetConnectedPlayers(context.Background(), e) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Equal(t, []string{"2"}, list.List) // on an update, confirm that the update hits the K8s api, only once @@ -1266,43 +1217,31 @@ func TestSDKServerPlayerConnectAndDisconnect(t *testing.T) { // last player is still there ok, err = sc.IsPlayerConnected(context.Background(), &alpha.PlayerID{PlayerID: "2"}) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.True(t, ok.Bool, "Player 2 should be connected") // finally, check idempotency of connect and disconnect id := &alpha.PlayerID{PlayerID: "2"} // only one left behind ok, err = sc.PlayerConnect(context.Background(), id) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.False(t, ok.Bool, "Player 2 should already be connected") count, err = sc.GetPlayerCount(context.Background(), e) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Equal(t, int64(1), count.Count) // no longer there. id.PlayerID = "0" ok, err = sc.PlayerDisconnect(context.Background(), id) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.False(t, ok.Bool, "Player 2 should already be disconnected") count, err = sc.GetPlayerCount(context.Background(), e) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Equal(t, int64(1), count.Count) agtesting.AssertNoEvent(t, m.FakeRecorder.Events) list, err = sc.GetConnectedPlayers(context.Background(), e) - if err != nil { - assert.FailNow(t, err.Error()) - } + require.Nil(t, err) assert.Equal(t, []string{"2"}, list.List) }