From 6eea4b47fef94bc0c934d25c8b0e323f6492b16f Mon Sep 17 00:00:00 2001 From: Rohini Chandra <61837065+r14chandra@users.noreply.github.com> Date: Mon, 4 Mar 2024 11:16:28 +0530 Subject: [PATCH] fix: RHINENG-8490 fix unit tests issue --- .../cloudconnector/cloudconnector_test.go | 69 +++++++------------ .../persistence/dispatcher/dispatcher_test.go | 25 +++---- .../inventory/inventory_repository_test.go | 25 +++---- internal/http/v2/handlers_test.go | 2 +- 4 files changed, 43 insertions(+), 78 deletions(-) diff --git a/infrastructure/persistence/cloudconnector/cloudconnector_test.go b/infrastructure/persistence/cloudconnector/cloudconnector_test.go index 33e95b7e..56efd44d 100644 --- a/infrastructure/persistence/cloudconnector/cloudconnector_test.go +++ b/infrastructure/persistence/cloudconnector/cloudconnector_test.go @@ -5,12 +5,8 @@ import ( "config-manager/internal/http/staticmux" "config-manager/internal/url" "context" - "fmt" - "log" - "math/rand" - "net/http" + "net/http/httptest" "testing" - "time" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" @@ -52,23 +48,18 @@ func TestGetConnections(t *testing.T) { for _, test := range tests { t.Run(test.description, func(t *testing.T) { - rand.Seed(time.Now().UnixNano()) - port := uint32(rand.Int31n(65535-55535) + 55535) + mux := staticmux.StaticMux{} + responseBody := test.input.response + headers := map[string][]string{"Content-Type": {"application/json"}} + mux.AddResponse("/connection/"+test.input.accountID, 200, responseBody, headers) + + server := httptest.NewServer(&mux) + defer server.Close() - config.DefaultConfig.CloudConnectorHost.Value = url.MustParse(fmt.Sprintf("http://localhost:%v", port)) + config.DefaultConfig.CloudConnectorHost.Value = url.MustParse(server.URL) config.DefaultConfig.CloudConnectorClientID = "test" config.DefaultConfig.CloudConnectorPSK = "test" - mux := staticmux.StaticMux{} - mux.AddResponse("/connection/"+test.input.accountID, 200, test.input.response, map[string][]string{"Content-Type": {"application/json"}}) - server := http.Server{Addr: config.DefaultConfig.CloudConnectorHost.Value.Host, Handler: &mux} - defer server.Close() - go func() { - if err := server.ListenAndServe(); err != nil { - log.Print(err) - } - }() - connector, err := NewCloudConnectorClient() if err != nil { t.Fatal(err) @@ -129,23 +120,18 @@ func TestSendMessage(t *testing.T) { for _, test := range tests { t.Run(test.description, func(t *testing.T) { - rand.Seed(time.Now().UnixNano()) - port := uint32(rand.Int31n(65535-55535) + 55535) + mux := staticmux.StaticMux{} + responseBody := test.input.response + headers := map[string][]string{"Content-Type": {"application/json"}} + mux.AddResponse("/message", 201, responseBody, headers) - config.DefaultConfig.CloudConnectorHost.Value = url.MustParse(fmt.Sprintf("http://localhost:%v", port)) + server := httptest.NewServer(&mux) + defer server.Close() + + config.DefaultConfig.CloudConnectorHost.Value = url.MustParse(server.URL) config.DefaultConfig.CloudConnectorClientID = "test" config.DefaultConfig.CloudConnectorPSK = "test" - mux := staticmux.StaticMux{} - mux.AddResponse("/message", 201, test.input.response, map[string][]string{"Content-Type": {"application/json"}}) - server := http.Server{Addr: config.DefaultConfig.CloudConnectorHost.Value.Host, Handler: &mux} - defer server.Close() - go func() { - if err := server.ListenAndServe(); err != nil { - log.Print(err) - } - }() - connector, err := NewCloudConnectorClient() if err != nil { t.Fatal(err) @@ -207,23 +193,18 @@ func TestGetConnectionStatus(t *testing.T) { for _, test := range tests { t.Run(test.description, func(t *testing.T) { - rand.Seed(time.Now().UnixNano()) - port := uint32(rand.Int31n(65535-55535) + 55535) + mux := staticmux.StaticMux{} + responseBody := test.input.response + headers := map[string][]string{"Content-Type": {"application/json"}} + mux.AddResponse("/connection/status", 200, responseBody, headers) + + server := httptest.NewServer(&mux) + defer server.Close() - config.DefaultConfig.CloudConnectorHost.Value = url.MustParse(fmt.Sprintf("http://localhost:%v", port)) + config.DefaultConfig.CloudConnectorHost.Value = url.MustParse(server.URL) config.DefaultConfig.CloudConnectorClientID = "test" config.DefaultConfig.CloudConnectorPSK = "test" - mux := staticmux.StaticMux{} - mux.AddResponse("/connection/status", 200, test.input.response, map[string][]string{"Content-Type": {"application/json"}}) - server := http.Server{Addr: config.DefaultConfig.CloudConnectorHost.Value.Host, Handler: &mux} - defer server.Close() - go func() { - if err := server.ListenAndServe(); err != nil { - log.Print(err) - } - }() - connector, err := NewCloudConnectorClient() if err != nil { t.Fatal(err) diff --git a/infrastructure/persistence/dispatcher/dispatcher_test.go b/infrastructure/persistence/dispatcher/dispatcher_test.go index 6c7e9075..e08742d5 100644 --- a/infrastructure/persistence/dispatcher/dispatcher_test.go +++ b/infrastructure/persistence/dispatcher/dispatcher_test.go @@ -5,12 +5,8 @@ import ( "config-manager/internal/http/staticmux" "config-manager/internal/url" "context" - "fmt" - "log" - "math/rand" - "net/http" + "net/http/httptest" "testing" - "time" "github.com/google/go-cmp/cmp" ) @@ -108,20 +104,15 @@ func TestDispatch(t *testing.T) { for _, test := range tests { t.Run(test.description, func(t *testing.T) { - rand.Seed(time.Now().UnixNano()) - port := uint32(rand.Int31n(65535-55535) + 55535) - - config.DefaultConfig.DispatcherHost.Value = url.MustParse(fmt.Sprintf("http://localhost:%v", port)) - mux := staticmux.StaticMux{} - mux.AddResponse("/internal/dispatch", 207, test.input.response, map[string][]string{"Content-Type": {"application/json"}}) - server := http.Server{Addr: config.DefaultConfig.DispatcherHost.Value.Host, Handler: &mux} + responseBody := test.input.response + headers := map[string][]string{"Content-Type": {"application/json"}} + mux.AddResponse("/internal/dispatch", 207, responseBody, headers) + + server := httptest.NewServer(&mux) defer server.Close() - go func() { - if err := server.ListenAndServe(); err != nil { - log.Print(err) - } - }() + + config.DefaultConfig.DispatcherHost.Value = url.MustParse(server.URL) got, err := NewDispatcherClient().Dispatch(context.Background(), test.input.runs) if err != nil { diff --git a/infrastructure/persistence/inventory/inventory_repository_test.go b/infrastructure/persistence/inventory/inventory_repository_test.go index bd6537ad..1f3bb40e 100644 --- a/infrastructure/persistence/inventory/inventory_repository_test.go +++ b/infrastructure/persistence/inventory/inventory_repository_test.go @@ -6,12 +6,9 @@ import ( "config-manager/internal/http/staticmux" "config-manager/internal/url" "context" - "fmt" - "log" - "math/rand" - "net/http" + + "net/http/httptest" "testing" - "time" "github.com/google/go-cmp/cmp" ) @@ -74,20 +71,16 @@ func TestGetInventoryClients(t *testing.T) { for _, test := range tests { t.Run(test.description, func(t *testing.T) { - rand.Seed(time.Now().UnixNano()) - port := uint32(rand.Int31n(65535-55535) + 55535) - - config.DefaultConfig.InventoryHost.Value = url.MustParse(fmt.Sprintf("http://localhost:%v", port)) mux := staticmux.StaticMux{} - mux.AddResponse("/api/inventory/v1/hosts", 200, test.input.response, map[string][]string{"Content-Type": {"application/json"}}) - server := http.Server{Addr: config.DefaultConfig.InventoryHost.Value.Host, Handler: &mux} + responseBody := test.input.response + headers := map[string][]string{"Content-Type": {"application/json"}} + mux.AddResponse("/api/inventory/v1/hosts", 200, responseBody, headers) + + server := httptest.NewServer(&mux) defer server.Close() - go func() { - if err := server.ListenAndServe(); err != nil { - log.Print(err) - } - }() + + config.DefaultConfig.InventoryHost.Value = url.MustParse(server.URL) got, err := NewInventoryClient().GetInventoryClients(context.Background(), test.input.page) if err != nil { diff --git a/internal/http/v2/handlers_test.go b/internal/http/v2/handlers_test.go index 9584d54d..d27dba1e 100644 --- a/internal/http/v2/handlers_test.go +++ b/internal/http/v2/handlers_test.go @@ -46,7 +46,7 @@ func TestMain(m *testing.M) { port = uint32(rand.Int31n(10000-9876) + 9876) DSN = fmt.Sprintf("host=localhost port=%v user=postgres password=postgres dbname=postgres sslmode=disable", port) - runtimedir, err := os.MkdirTemp("", "config-manager-internal-http-v1.") + runtimedir, err := os.MkdirTemp("", "config-manager-internal-http-v2.") if err != nil { log.Fatalf("cannot make temp dir: %v", err) }