diff --git a/MODULE.bazel b/MODULE.bazel index 508e6a94..b37ac221 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -31,6 +31,7 @@ go_deps.from_file(go_mod = "//:go.mod") use_repo( go_deps, "com_github_gogo_protobuf", + "com_github_stretchr_testify", "com_github_uber_go_tally_v4", "org_golang_google_grpc", "org_golang_google_protobuf", diff --git a/gateway/controller/BUILD.bazel b/gateway/controller/BUILD.bazel index 80fe6899..d625087d 100644 --- a/gateway/controller/BUILD.bazel +++ b/gateway/controller/BUILD.bazel @@ -16,5 +16,11 @@ go_test( name = "controller_test", srcs = ["ping_test.go", "land_test.go"], embed = [":controller"], - deps = ["//gateway/protopb"], + deps = [ + "//gateway/protopb", + "@com_github_stretchr_testify//assert", + "@com_github_stretchr_testify//require", + "@com_github_uber_go_tally_v4//:tally", + "@org_uber_go_zap//:zap", + ], ) diff --git a/gateway/controller/land_test.go b/gateway/controller/land_test.go index efb5f13c..233c8b26 100644 --- a/gateway/controller/land_test.go +++ b/gateway/controller/land_test.go @@ -4,14 +4,13 @@ import ( "context" "testing" + "github.com/stretchr/testify/require" pb "github.com/uber/submitqueue/gateway/protopb" ) func TestNewLandController(t *testing.T) { controller := NewLandController(nil, nil) - if controller == nil { - t.Fatal("NewLandController() returned nil") - } + require.NotNil(t, controller) } func TestLand_ReturnsSqid(t *testing.T) { @@ -24,11 +23,6 @@ func TestLand_ReturnsSqid(t *testing.T) { } resp, err := controller.Land(ctx, req) - if err != nil { - t.Fatalf("Land() returned error: %v", err) - } - - if resp.Sqid == "" { - t.Fatal("Expected sqid to be set, got empty string") - } + require.NoError(t, err) + require.NotEmpty(t, resp.Sqid) } diff --git a/gateway/controller/ping_test.go b/gateway/controller/ping_test.go index 5ff713bd..0a3d3774 100644 --- a/gateway/controller/ping_test.go +++ b/gateway/controller/ping_test.go @@ -5,14 +5,14 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" pb "github.com/uber/submitqueue/gateway/protopb" ) func TestNewPingController(t *testing.T) { controller := NewPingController(nil, nil) - if controller == nil { - t.Fatal("NewPingController() returned nil") - } + require.NotNil(t, controller) } func TestPing_DefaultMessage(t *testing.T) { @@ -22,13 +22,8 @@ func TestPing_DefaultMessage(t *testing.T) { req := &pb.PingRequest{} resp, err := controller.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - if resp.Message != "pong" { - t.Errorf("Expected message 'pong', got '%s'", resp.Message) - } + require.NoError(t, err) + assert.Equal(t, "pong", resp.Message) } func TestPing_CustomMessage(t *testing.T) { @@ -50,13 +45,8 @@ func TestPing_CustomMessage(t *testing.T) { req := &pb.PingRequest{Message: tc.input} resp, err := controller.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - if resp.Message != tc.expected { - t.Errorf("Expected message '%s', got '%s'", tc.expected, resp.Message) - } + require.NoError(t, err) + assert.Equal(t, tc.expected, resp.Message) }) } } @@ -68,13 +58,8 @@ func TestPing_ServiceName(t *testing.T) { req := &pb.PingRequest{} resp, err := controller.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - if resp.ServiceName != "gateway" { - t.Errorf("Expected service name 'gateway', got '%s'", resp.ServiceName) - } + require.NoError(t, err) + assert.Equal(t, "gateway", resp.ServiceName) } func TestPing_Timestamp(t *testing.T) { @@ -86,13 +71,9 @@ func TestPing_Timestamp(t *testing.T) { resp, err := controller.Ping(ctx, req) after := time.Now().Unix() - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - if resp.Timestamp < before || resp.Timestamp > after { - t.Errorf("Timestamp %d is not within expected range [%d, %d]", resp.Timestamp, before, after) - } + require.NoError(t, err) + assert.GreaterOrEqual(t, resp.Timestamp, before) + assert.LessOrEqual(t, resp.Timestamp, after) } func TestPing_Hostname(t *testing.T) { @@ -102,13 +83,6 @@ func TestPing_Hostname(t *testing.T) { req := &pb.PingRequest{} resp, err := controller.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - // Hostname should be set (non-empty string) - // We don't check the exact value as it depends on the environment - if resp.Hostname == "" { - t.Error("Expected hostname to be set, got empty string") - } + require.NoError(t, err) + assert.NotEmpty(t, resp.Hostname) } diff --git a/gateway/integration_tests/BUILD.bazel b/gateway/integration_tests/BUILD.bazel index af1a53d6..774c8243 100644 --- a/gateway/integration_tests/BUILD.bazel +++ b/gateway/integration_tests/BUILD.bazel @@ -11,6 +11,8 @@ go_test( ], deps = [ "//gateway/protopb", + "@com_github_stretchr_testify//assert", + "@com_github_stretchr_testify//require", "@org_golang_google_grpc//:grpc", "@org_golang_google_grpc//credentials/insecure", ], diff --git a/gateway/integration_tests/ping_test.go b/gateway/integration_tests/ping_test.go index 862cbf40..bfa0517a 100644 --- a/gateway/integration_tests/ping_test.go +++ b/gateway/integration_tests/ping_test.go @@ -7,6 +7,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" pb "github.com/uber/submitqueue/gateway/protopb" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" @@ -24,9 +26,7 @@ func TestPingAPI(t *testing.T) { // Wait for server to be ready conn, err := waitForServer(t, addr, serverReadyTimeout) - if err != nil { - t.Fatalf("Gateway server not ready: %v", err) - } + require.NoError(t, err, "Gateway server not ready") defer conn.Close() client := pb.NewSubmitQueueGatewayClient(conn) @@ -39,23 +39,13 @@ func TestPingAPI(t *testing.T) { } resp, err := client.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping failed: %v", err) - } + require.NoError(t, err, "Ping failed") // Validate response - if resp.Message == "" { - t.Error("Response message is empty") - } - if resp.ServiceName != "gateway" { - t.Errorf("Expected service name 'gateway', got '%s'", resp.ServiceName) - } - if resp.Timestamp == 0 { - t.Error("Timestamp is zero") - } - if resp.Hostname == "" { - t.Error("Hostname is empty") - } + assert.NotEmpty(t, resp.Message, "Response message should not be empty") + assert.Equal(t, "gateway", resp.ServiceName) + assert.NotZero(t, resp.Timestamp, "Timestamp should not be zero") + assert.NotEmpty(t, resp.Hostname, "Hostname should not be empty") t.Logf("Gateway Ping test passed:") t.Logf(" Message: %s", resp.Message) diff --git a/go.mod b/go.mod index 0b1b830f..5353e768 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.24.5 require ( github.com/gogo/protobuf v1.3.2 + github.com/stretchr/testify v1.9.0 github.com/uber-go/tally/v4 v4.1.17 go.uber.org/fx v1.22.0 go.uber.org/yarpc v1.81.0 @@ -16,12 +17,14 @@ require ( github.com/BurntSushi/toml v1.2.1 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/gogo/googleapis v1.4.1 // indirect github.com/gogo/status v1.1.0 // indirect github.com/golang/mock v1.7.0-rc.1 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.11.1 // indirect github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.26.0 // indirect @@ -43,5 +46,6 @@ require ( golang.org/x/text v0.23.0 // indirect golang.org/x/tools v0.22.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20241230172942-26aa7a208def // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect honnef.co/go/tools v0.4.3 // indirect ) diff --git a/go.sum b/go.sum index 3f4ace50..3a7f8b15 100644 --- a/go.sum +++ b/go.sum @@ -82,6 +82,8 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI= github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= @@ -91,6 +93,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -283,6 +287,8 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/integration_tests/BUILD.bazel b/integration_tests/BUILD.bazel index 323e1053..b9ddda0b 100644 --- a/integration_tests/BUILD.bazel +++ b/integration_tests/BUILD.bazel @@ -16,6 +16,8 @@ go_test( "//gateway/protopb", "//orchestrator/protopb", "//speculator/protopb", + "@com_github_stretchr_testify//assert", + "@com_github_stretchr_testify//require", "@org_golang_google_grpc//:grpc", "@org_golang_google_grpc//credentials/insecure", ], diff --git a/integration_tests/ping_test.go b/integration_tests/ping_test.go index 7d4d4577..c346dfc0 100644 --- a/integration_tests/ping_test.go +++ b/integration_tests/ping_test.go @@ -7,6 +7,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" gatewaypb "github.com/uber/submitqueue/gateway/protopb" orchestratorpb "github.com/uber/submitqueue/orchestrator/protopb" speculatorpb "github.com/uber/submitqueue/speculator/protopb" @@ -27,9 +29,7 @@ func TestPingForAllServices(t *testing.T) { t.Run("Gateway", func(t *testing.T) { addr := getEnvOrDefault("GATEWAY_ADDR", "localhost:8081") conn, err := waitForServer(t, addr, serverReadyTimeout) - if err != nil { - t.Fatalf("Gateway server not ready: %v", err) - } + require.NoError(t, err, "Gateway server not ready") defer conn.Close() client := gatewaypb.NewSubmitQueueGatewayClient(conn) @@ -37,12 +37,8 @@ func TestPingForAllServices(t *testing.T) { defer cancel() resp, err := client.Ping(ctx, &gatewaypb.PingRequest{Message: "e2e test"}) - if err != nil { - t.Fatalf("Gateway Ping failed: %v", err) - } - if resp.ServiceName != "gateway" { - t.Errorf("Expected service name 'gateway', got '%s'", resp.ServiceName) - } + require.NoError(t, err, "Gateway Ping failed") + assert.Equal(t, "gateway", resp.ServiceName) t.Logf("Gateway is healthy: %s", resp.Message) }) @@ -50,9 +46,7 @@ func TestPingForAllServices(t *testing.T) { t.Run("Orchestrator", func(t *testing.T) { addr := getEnvOrDefault("ORCHESTRATOR_ADDR", "localhost:8082") conn, err := waitForServer(t, addr, serverReadyTimeout) - if err != nil { - t.Fatalf("Orchestrator server not ready: %v", err) - } + require.NoError(t, err, "Orchestrator server not ready") defer conn.Close() client := orchestratorpb.NewSubmitQueueOrchestratorClient(conn) @@ -60,12 +54,8 @@ func TestPingForAllServices(t *testing.T) { defer cancel() resp, err := client.Ping(ctx, &orchestratorpb.PingRequest{Message: "e2e test"}) - if err != nil { - t.Fatalf("Orchestrator Ping failed: %v", err) - } - if resp.ServiceName != "orchestrator" { - t.Errorf("Expected service name 'orchestrator', got '%s'", resp.ServiceName) - } + require.NoError(t, err, "Orchestrator Ping failed") + assert.Equal(t, "orchestrator", resp.ServiceName) t.Logf("Orchestrator is healthy: %s", resp.Message) }) @@ -73,9 +63,7 @@ func TestPingForAllServices(t *testing.T) { t.Run("Speculator", func(t *testing.T) { addr := getEnvOrDefault("SPECULATOR_ADDR", "localhost:8083") conn, err := waitForServer(t, addr, serverReadyTimeout) - if err != nil { - t.Fatalf("Speculator server not ready: %v", err) - } + require.NoError(t, err, "Speculator server not ready") defer conn.Close() client := speculatorpb.NewSubmitQueueSpeculatorClient(conn) @@ -83,12 +71,8 @@ func TestPingForAllServices(t *testing.T) { defer cancel() resp, err := client.Ping(ctx, &speculatorpb.PingRequest{Message: "e2e test"}) - if err != nil { - t.Fatalf("Speculator Ping failed: %v", err) - } - if resp.ServiceName != "speculator" { - t.Errorf("Expected service name 'speculator', got '%s'", resp.ServiceName) - } + require.NoError(t, err, "Speculator Ping failed") + assert.Equal(t, "speculator", resp.ServiceName) t.Logf("Speculator is healthy: %s", resp.Message) }) diff --git a/orchestrator/controller/BUILD.bazel b/orchestrator/controller/BUILD.bazel index 3b8e495a..93f8daad 100644 --- a/orchestrator/controller/BUILD.bazel +++ b/orchestrator/controller/BUILD.bazel @@ -16,5 +16,11 @@ go_test( name = "controller_test", srcs = ["ping_test.go"], embed = [":controller"], - deps = ["//orchestrator/protopb"], + deps = [ + "//orchestrator/protopb", + "@com_github_stretchr_testify//assert", + "@com_github_stretchr_testify//require", + "@com_github_uber_go_tally_v4//:tally", + "@org_uber_go_zap//:zap", + ], ) diff --git a/orchestrator/controller/ping_test.go b/orchestrator/controller/ping_test.go index 381dad05..26dc3506 100644 --- a/orchestrator/controller/ping_test.go +++ b/orchestrator/controller/ping_test.go @@ -5,14 +5,14 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" pb "github.com/uber/submitqueue/orchestrator/protopb" ) func TestNewPingController(t *testing.T) { controller := NewPingController(nil, nil) - if controller == nil { - t.Fatal("NewPingController() returned nil") - } + require.NotNil(t, controller) } func TestPing_DefaultMessage(t *testing.T) { @@ -22,13 +22,8 @@ func TestPing_DefaultMessage(t *testing.T) { req := &pb.PingRequest{} resp, err := controller.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - if resp.Message != "pong" { - t.Errorf("Expected message 'pong', got '%s'", resp.Message) - } + require.NoError(t, err) + assert.Equal(t, "pong", resp.Message) } func TestPing_CustomMessage(t *testing.T) { @@ -50,13 +45,8 @@ func TestPing_CustomMessage(t *testing.T) { req := &pb.PingRequest{Message: tc.input} resp, err := controller.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - if resp.Message != tc.expected { - t.Errorf("Expected message '%s', got '%s'", tc.expected, resp.Message) - } + require.NoError(t, err) + assert.Equal(t, tc.expected, resp.Message) }) } } @@ -68,13 +58,8 @@ func TestPing_ServiceName(t *testing.T) { req := &pb.PingRequest{} resp, err := controller.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - if resp.ServiceName != "orchestrator" { - t.Errorf("Expected service name 'orchestrator', got '%s'", resp.ServiceName) - } + require.NoError(t, err) + assert.Equal(t, "orchestrator", resp.ServiceName) } func TestPing_Timestamp(t *testing.T) { @@ -86,13 +71,9 @@ func TestPing_Timestamp(t *testing.T) { resp, err := controller.Ping(ctx, req) after := time.Now().Unix() - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - if resp.Timestamp < before || resp.Timestamp > after { - t.Errorf("Timestamp %d is not within expected range [%d, %d]", resp.Timestamp, before, after) - } + require.NoError(t, err) + assert.GreaterOrEqual(t, resp.Timestamp, before) + assert.LessOrEqual(t, resp.Timestamp, after) } func TestPing_Hostname(t *testing.T) { @@ -102,13 +83,6 @@ func TestPing_Hostname(t *testing.T) { req := &pb.PingRequest{} resp, err := controller.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - // Hostname should be set (non-empty string) - // We don't check the exact value as it depends on the environment - if resp.Hostname == "" { - t.Error("Expected hostname to be set, got empty string") - } + require.NoError(t, err) + assert.NotEmpty(t, resp.Hostname) } diff --git a/orchestrator/integration_tests/BUILD.bazel b/orchestrator/integration_tests/BUILD.bazel index 84a71b3f..0114fda2 100644 --- a/orchestrator/integration_tests/BUILD.bazel +++ b/orchestrator/integration_tests/BUILD.bazel @@ -11,6 +11,8 @@ go_test( ], deps = [ "//orchestrator/protopb", + "@com_github_stretchr_testify//assert", + "@com_github_stretchr_testify//require", "@org_golang_google_grpc//:grpc", "@org_golang_google_grpc//credentials/insecure", ], diff --git a/orchestrator/integration_tests/ping_test.go b/orchestrator/integration_tests/ping_test.go index 91bd5e82..19126c93 100644 --- a/orchestrator/integration_tests/ping_test.go +++ b/orchestrator/integration_tests/ping_test.go @@ -7,6 +7,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" pb "github.com/uber/submitqueue/orchestrator/protopb" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" @@ -24,9 +26,7 @@ func TestPingAPI(t *testing.T) { // Wait for server to be ready conn, err := waitForServer(t, addr, serverReadyTimeout) - if err != nil { - t.Fatalf("Orchestrator server not ready: %v", err) - } + require.NoError(t, err, "Orchestrator server not ready") defer conn.Close() client := pb.NewSubmitQueueOrchestratorClient(conn) @@ -39,23 +39,13 @@ func TestPingAPI(t *testing.T) { } resp, err := client.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping failed: %v", err) - } + require.NoError(t, err, "Ping failed") // Validate response - if resp.Message == "" { - t.Error("Response message is empty") - } - if resp.ServiceName != "orchestrator" { - t.Errorf("Expected service name 'orchestrator', got '%s'", resp.ServiceName) - } - if resp.Timestamp == 0 { - t.Error("Timestamp is zero") - } - if resp.Hostname == "" { - t.Error("Hostname is empty") - } + assert.NotEmpty(t, resp.Message, "Response message should not be empty") + assert.Equal(t, "orchestrator", resp.ServiceName) + assert.NotZero(t, resp.Timestamp, "Timestamp should not be zero") + assert.NotEmpty(t, resp.Hostname, "Hostname should not be empty") t.Logf("Orchestrator Ping test passed:") t.Logf(" Message: %s", resp.Message) diff --git a/speculator/controller/BUILD.bazel b/speculator/controller/BUILD.bazel index dfd8fa81..1884867b 100644 --- a/speculator/controller/BUILD.bazel +++ b/speculator/controller/BUILD.bazel @@ -16,5 +16,11 @@ go_test( name = "controller_test", srcs = ["ping_test.go"], embed = [":controller"], - deps = ["//speculator/protopb"], + deps = [ + "//speculator/protopb", + "@com_github_stretchr_testify//assert", + "@com_github_stretchr_testify//require", + "@com_github_uber_go_tally_v4//:tally", + "@org_uber_go_zap//:zap", + ], ) diff --git a/speculator/controller/ping_test.go b/speculator/controller/ping_test.go index b0f60f06..c2a23f85 100644 --- a/speculator/controller/ping_test.go +++ b/speculator/controller/ping_test.go @@ -5,14 +5,14 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" pb "github.com/uber/submitqueue/speculator/protopb" ) func TestNewPingController(t *testing.T) { controller := NewPingController(nil, nil) - if controller == nil { - t.Fatal("NewPingController() returned nil") - } + require.NotNil(t, controller) } func TestPing_DefaultMessage(t *testing.T) { @@ -22,13 +22,8 @@ func TestPing_DefaultMessage(t *testing.T) { req := &pb.PingRequest{} resp, err := controller.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - if resp.Message != "pong" { - t.Errorf("Expected message 'pong', got '%s'", resp.Message) - } + require.NoError(t, err) + assert.Equal(t, "pong", resp.Message) } func TestPing_CustomMessage(t *testing.T) { @@ -50,13 +45,8 @@ func TestPing_CustomMessage(t *testing.T) { req := &pb.PingRequest{Message: tc.input} resp, err := controller.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - if resp.Message != tc.expected { - t.Errorf("Expected message '%s', got '%s'", tc.expected, resp.Message) - } + require.NoError(t, err) + assert.Equal(t, tc.expected, resp.Message) }) } } @@ -68,13 +58,8 @@ func TestPing_ServiceName(t *testing.T) { req := &pb.PingRequest{} resp, err := controller.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - if resp.ServiceName != "speculator" { - t.Errorf("Expected service name 'speculator', got '%s'", resp.ServiceName) - } + require.NoError(t, err) + assert.Equal(t, "speculator", resp.ServiceName) } func TestPing_Timestamp(t *testing.T) { @@ -86,13 +71,9 @@ func TestPing_Timestamp(t *testing.T) { resp, err := controller.Ping(ctx, req) after := time.Now().Unix() - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - if resp.Timestamp < before || resp.Timestamp > after { - t.Errorf("Timestamp %d is not within expected range [%d, %d]", resp.Timestamp, before, after) - } + require.NoError(t, err) + assert.GreaterOrEqual(t, resp.Timestamp, before) + assert.LessOrEqual(t, resp.Timestamp, after) } func TestPing_Hostname(t *testing.T) { @@ -102,13 +83,6 @@ func TestPing_Hostname(t *testing.T) { req := &pb.PingRequest{} resp, err := controller.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping() returned error: %v", err) - } - - // Hostname should be set (non-empty string) - // We don't check the exact value as it depends on the environment - if resp.Hostname == "" { - t.Error("Expected hostname to be set, got empty string") - } + require.NoError(t, err) + assert.NotEmpty(t, resp.Hostname) } diff --git a/speculator/integration_tests/BUILD.bazel b/speculator/integration_tests/BUILD.bazel index 7fbe31ab..e7d5d4b3 100644 --- a/speculator/integration_tests/BUILD.bazel +++ b/speculator/integration_tests/BUILD.bazel @@ -11,6 +11,8 @@ go_test( ], deps = [ "//speculator/protopb", + "@com_github_stretchr_testify//assert", + "@com_github_stretchr_testify//require", "@org_golang_google_grpc//:grpc", "@org_golang_google_grpc//credentials/insecure", ], diff --git a/speculator/integration_tests/ping_test.go b/speculator/integration_tests/ping_test.go index 00e0fa44..3e550826 100644 --- a/speculator/integration_tests/ping_test.go +++ b/speculator/integration_tests/ping_test.go @@ -7,6 +7,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" pb "github.com/uber/submitqueue/speculator/protopb" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" @@ -24,9 +26,7 @@ func TestPingAPI(t *testing.T) { // Wait for server to be ready conn, err := waitForServer(t, addr, serverReadyTimeout) - if err != nil { - t.Fatalf("Speculator server not ready: %v", err) - } + require.NoError(t, err, "Speculator server not ready") defer conn.Close() client := pb.NewSubmitQueueSpeculatorClient(conn) @@ -39,23 +39,13 @@ func TestPingAPI(t *testing.T) { } resp, err := client.Ping(ctx, req) - if err != nil { - t.Fatalf("Ping failed: %v", err) - } + require.NoError(t, err, "Ping failed") // Validate response - if resp.Message == "" { - t.Error("Response message is empty") - } - if resp.ServiceName != "speculator" { - t.Errorf("Expected service name 'speculator', got '%s'", resp.ServiceName) - } - if resp.Timestamp == 0 { - t.Error("Timestamp is zero") - } - if resp.Hostname == "" { - t.Error("Hostname is empty") - } + assert.NotEmpty(t, resp.Message, "Response message should not be empty") + assert.Equal(t, "speculator", resp.ServiceName) + assert.NotZero(t, resp.Timestamp, "Timestamp should not be zero") + assert.NotEmpty(t, resp.Hostname, "Hostname should not be empty") t.Logf("Speculator Ping test passed:") t.Logf(" Message: %s", resp.Message)