From af328c6c963851a9b1128c18fce5e5cb0dadfb40 Mon Sep 17 00:00:00 2001 From: Victor Yves Crispim Date: Wed, 22 Nov 2023 14:28:31 -0300 Subject: [PATCH] test: rewrite tests with testify --- go.mod | 1 + go.sum | 1 + internal/services/service_test.go | 203 +++++++++++++++--------------- 3 files changed, 103 insertions(+), 102 deletions(-) diff --git a/go.mod b/go.mod index 61b422a3e..ba3deef81 100644 --- a/go.mod +++ b/go.mod @@ -66,6 +66,7 @@ require ( github.com/shoenig/go-m1cpu v0.1.6 // indirect github.com/sirupsen/logrus v1.9.3 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/objx v0.5.0 // indirect github.com/supranational/blst v0.3.11 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect diff --git a/go.sum b/go.sum index d108c747d..d3a2830e1 100644 --- a/go.sum +++ b/go.sum @@ -238,6 +238,7 @@ github.com/status-im/keycard-go v0.2.0 h1:QDLFswOQu1r5jsycloeQh3bVU8n/NatHHaZobt github.com/status-im/keycard-go v0.2.0/go.mod h1:wlp8ZLbsmrF6g6WjugPAx+IzoLrkdf9+mHxBEeo3Hbg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.1.5-0.20170601210322-f6abca593680/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= diff --git a/internal/services/service_test.go b/internal/services/service_test.go index 41eb92756..065c94ffd 100644 --- a/internal/services/service_test.go +++ b/internal/services/service_test.go @@ -5,6 +5,7 @@ package services import ( "context" + "fmt" "os" "os/exec" "path/filepath" @@ -12,132 +13,130 @@ import ( "time" "github.com/cartesi/rollups-node/internal/logger" + "github.com/stretchr/testify/suite" ) -const ( - servicePort = "44444" - serviceAdress = "0.0.0.0:" + servicePort -) - -var tmpDir string +type ServiceTestSuite struct { + suite.Suite + tmpDir string + servicePort int +} -func setup() { +func (s *ServiceTestSuite) SetupSuite() { logger.Init("warning", false) - buildFakeService() + s.buildFakeService() + s.servicePort = 55555 +} + +func (s *ServiceTestSuite) TearDownSuite() { + err := os.RemoveAll(s.tmpDir) + if err != nil { + panic(err) + } +} + +func (s *ServiceTestSuite) SetupTest() { + s.servicePort++ + serviceAdress := "0.0.0.0:" + fmt.Sprint(s.servicePort) + os.Setenv("SERVICE_ADDRESS", serviceAdress) +} + +// Service should stop when context is cancelled +func (s *ServiceTestSuite) TestServiceStops() { + service := Service{ + name: "fake-service", + binaryName: "fake-service", + healthcheckPort: fmt.Sprint(s.servicePort), + } + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + // start service in goroutine + result := make(chan error) + go func() { + result <- service.Start(ctx) + }() + + time.Sleep(100 * time.Millisecond) + + // shutdown + cancel() + err := <-result + s.Nil(err, "service exited for the wrong reason: %v", err) } -func tearDown() { - deleteTempDir() +// Service should stop if timeout is reached and it isn't ready yet +func (s *ServiceTestSuite) TestServiceTimeout() { + service := Service{ + name: "fake-service", + binaryName: "fake-service", + healthcheckPort: "0000", // wrong port + } + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + // start service in goroutine + result := make(chan error, 1) + go func() { + result <- service.Start(ctx) + }() + + // expect timeout because of wrong port + err := service.Ready(ctx, 500*time.Millisecond) + s.NotNil(err, "expected service to timeout") + + // shutdown + cancel() + s.Nil(<-result, "service exited for the wrong reason: %v", err) } -func TestService(t *testing.T) { - setup() - - t.Run("it stops when the context is cancelled", func(t *testing.T) { - service := Service{ - name: "fake-service", - binaryName: "fake-service", - healthcheckPort: servicePort, - } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - // start service in goroutine - result := make(chan error) - go func() { - result <- service.Start(ctx) - }() - - time.Sleep(100 * time.Millisecond) - - // shutdown - cancel() - if err := <-result; err != nil { - t.Errorf("service exited for the wrong reason: %v", err) - } - }) - - t.Run("it stops when timeout is reached and it isn't ready yet", func(t *testing.T) { - service := Service{ - name: "fake-service", - binaryName: "fake-service", - healthcheckPort: "0000", // wrong port - } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - // start service in goroutine - result := make(chan error, 1) - go func() { - result <- service.Start(ctx) - }() - - // expect timeout because of wrong port - if err := service.Ready(ctx, 500*time.Millisecond); err == nil { - t.Errorf("expected service to timeout") - } - - // shutdown - cancel() - if err := <-result; err != nil { - t.Errorf("service exited for the wrong reason: %v", err) - } - }) - - t.Run("it becomes ready soon after being started", func(t *testing.T) { - service := Service{ - name: "fake-service", - binaryName: "fake-service", - healthcheckPort: servicePort, - } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - // start service in goroutine - result := make(chan error) - go func() { - result <- service.Start(ctx) - }() - - // wait for service to be ready - if err := service.Ready(ctx, 500*time.Millisecond); err != nil { - t.Errorf("service timed out") - } - - // shutdown - cancel() - if err := <-result; err != nil { - t.Errorf("service exited for the wrong reason: %v", err) - } - }) - - tearDown() +// Service should be ready soon after starting +func (s *ServiceTestSuite) TestServiceReady() { + service := Service{ + name: "fake-service", + binaryName: "fake-service", + healthcheckPort: fmt.Sprint(s.servicePort), + } + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + // start service in goroutine + result := make(chan error) + go func() { + result <- service.Start(ctx) + }() + + // wait for service to be ready + err := service.Ready(ctx, 1*time.Second) + s.Nil(err, "service timed out") + + // shutdown + cancel() + s.Nil(<-result, "service exited for the wrong reason: %v", err) } // Builds the fake-service binary and adds it to PATH -func buildFakeService() { - temporaryDir, err := os.MkdirTemp("", "") +func (s *ServiceTestSuite) buildFakeService() { + tempDir, err := os.MkdirTemp("", "") if err != nil { panic(err) } - tmpDir = temporaryDir + s.tmpDir = tempDir cmd := exec.Command( "go", "build", "-o", - filepath.Join(tmpDir, "fake-service"), + filepath.Join(s.tmpDir, "fake-service"), "fakeservice/main.go", ) if err := cmd.Run(); err != nil { panic(err) } - os.Setenv("PATH", os.Getenv("PATH")+":"+tmpDir) + + os.Setenv("PATH", os.Getenv("PATH")+":"+s.tmpDir) } -func deleteTempDir() { - err := os.RemoveAll(tmpDir) - if err != nil { - panic(err) - } +func TestServiceSuite(t *testing.T) { + suite.Run(t, new(ServiceTestSuite)) }