From c9c2536f301590e266dbf40a05673c2514b51c90 Mon Sep 17 00:00:00 2001 From: Joel Rebello Date: Tue, 25 Jun 2024 15:10:52 +0200 Subject: [PATCH] tests: fix intermittent test failures with NATS server The tests would fail intermittently locally and in the CI because of the NATS server store directory being the same across various packages. This sets up a temp store directory that is cleaned up by the testing package for us. --- FAIL: TestCreateReadUpdate (0.00s) nats_test.go:116: Error Trace: /home/runner/work/conditionorc/conditionorc/internal/store/nats_test.go:116 Error: Received unexpected error: nats: error opening msg block file ["/tmp/nats/jetstream/$G/streams/KV_active-conditions/msgs/1.blk"]: open /tmp/nats/jetstream/$G/streams/KV_active-conditions/msgs/1.blk: no such file or directory Test: TestCreateReadUpdate --- FAIL: TestMultipleConditionUpdate (0.00s) --- FAIL: TestMultipleConditionUpdate/create_multiple_sanity_checks (0.00s) nats_test.go:175: Error Trace: /home/runner/work/conditionorc/conditionorc/internal/store/nats_test.go:175 Error: Received unexpected error: nats: error opening msg block file ["/tmp/nats/jetstream/$G/streams/KV_active-conditions/msgs/1.blk"]: open /tmp/nats/jetstream/$G/streams/KV_active-conditions/msgs/1.blk: no such file or directory --- internal/orchestrator/updates_test.go | 23 ++++++++++------------- internal/status/kv_test.go | 2 ++ internal/store/nats_test.go | 23 ++++++++++------------- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/internal/orchestrator/updates_test.go b/internal/orchestrator/updates_test.go index 5b12b1cf..1dd6e0ab 100644 --- a/internal/orchestrator/updates_test.go +++ b/internal/orchestrator/updates_test.go @@ -44,10 +44,14 @@ func init() { logrus.SetFormatter(&logrus.JSONFormatter{}) } -func startJetStreamServer() *server.Server { +func startJetStreamServer(t *testing.T) *server.Server { + t.Helper() + opts := srvtest.DefaultTestOptions opts.Port = -1 opts.JetStream = true + opts.StoreDir = t.TempDir() + return srvtest.RunServer(&opts) } @@ -63,17 +67,9 @@ func jetStreamContext(s *server.Server) (*nats.Conn, nats.JetStreamContext) { return nc, js } -func shutdownJetStream(s *server.Server) { - var sd string - if config := s.JetStreamConfig(); config != nil { - sd = config.StoreDir - } +func shutdownJetStream(t *testing.T, s *server.Server) { + t.Helper() s.Shutdown() - if sd != "" { - if err := os.RemoveAll(sd); err != nil { - logger.Fatalf("Unable to remove storage %q: %v", sd, err) - } - } s.WaitForShutdown() } @@ -81,8 +77,9 @@ func shutdownJetStream(s *server.Server) { func TestMain(m *testing.M) { logger = logrus.New() - srv := startJetStreamServer() - defer shutdownJetStream(srv) + t := &testing.T{} + srv := startJetStreamServer(t) + defer shutdownJetStream(t, srv) nc, js = jetStreamContext(srv) // nc is closed on evJS.Close(), js needs no cleanup evJS = events.NewJetstreamFromConn(nc) defer evJS.Close() diff --git a/internal/status/kv_test.go b/internal/status/kv_test.go index 18fa2b8d..48ed08ca 100644 --- a/internal/status/kv_test.go +++ b/internal/status/kv_test.go @@ -25,6 +25,8 @@ func startJetStreamServer(t *testing.T) *server.Server { opts := srvtest.DefaultTestOptions opts.Port = -1 opts.JetStream = true + opts.StoreDir = t.TempDir() + return srvtest.RunServer(&opts) } diff --git a/internal/store/nats_test.go b/internal/store/nats_test.go index 0ae999c6..6c205a29 100644 --- a/internal/store/nats_test.go +++ b/internal/store/nats_test.go @@ -30,10 +30,14 @@ func init() { logrus.SetFormatter(&logrus.JSONFormatter{}) } -func startJetStreamServer() *server.Server { +func startJetStreamServer(t *testing.T) *server.Server { + t.Helper() + opts := srvtest.DefaultTestOptions opts.Port = -1 opts.JetStream = true + opts.StoreDir = t.TempDir() + return srvtest.RunServer(&opts) } @@ -49,17 +53,9 @@ func jetStreamContext(s *server.Server) (*nats.Conn, nats.JetStreamContext) { return nc, js } -func shutdownJetStream(s *server.Server) { - var sd string - if config := s.JetStreamConfig(); config != nil { - sd = config.StoreDir - } +func shutdownJetStream(t *testing.T, s *server.Server) { + t.Helper() s.Shutdown() - if sd != "" { - if err := os.RemoveAll(sd); err != nil { - logger.Fatalf("Unable to remove storage %q: %v", sd, err) - } - } s.WaitForShutdown() } @@ -67,8 +63,9 @@ func shutdownJetStream(s *server.Server) { func TestMain(m *testing.M) { logger = logrus.New() - srv := startJetStreamServer() - defer shutdownJetStream(srv) + t := &testing.T{} + srv := startJetStreamServer(t) + defer shutdownJetStream(t, srv) nc, js = jetStreamContext(srv) // nc is closed on evJS.Close(), js needs no cleanup evJS = events.NewJetstreamFromConn(nc) defer evJS.Close()