Skip to content

Commit

Permalink
fix: RHINENG-8490 fix unit tests issue
Browse files Browse the repository at this point in the history
  • Loading branch information
r14chandra committed Mar 4, 2024
1 parent 5187258 commit 6eea4b4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 78 deletions.
69 changes: 25 additions & 44 deletions infrastructure/persistence/cloudconnector/cloudconnector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 8 additions & 17 deletions infrastructure/persistence/dispatcher/dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
25 changes: 9 additions & 16 deletions infrastructure/persistence/inventory/inventory_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/http/v2/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 6eea4b4

Please sign in to comment.