From b6a031bed27c8d026fd3959af4537effce4049bc Mon Sep 17 00:00:00 2001 From: Trevor Dawe Date: Tue, 17 Dec 2024 22:47:18 +0000 Subject: [PATCH 1/3] Update tests for deploy --- deploy_test.go | 277 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 257 insertions(+), 20 deletions(-) diff --git a/deploy_test.go b/deploy_test.go index 66e6d76..99f7962 100644 --- a/deploy_test.go +++ b/deploy_test.go @@ -13,12 +13,17 @@ package goscaleio import ( + "encoding/json" + "errors" "fmt" + "io/ioutil" "net/http" "net/http/httptest" + "reflect" "strings" "testing" + types "github.com/dell/goscaleio/types/v1" "github.com/stretchr/testify/assert" ) @@ -252,28 +257,48 @@ func TestDeletePackage(t *testing.T) { } func TestBeginInstallation(t *testing.T) { - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Method == http.MethodPost && strings.HasPrefix(r.URL.Path, "/im/types/Configuration/actions/install") { - w.WriteHeader(http.StatusAccepted) - return - } - http.NotFound(w, r) - })) - - defer server.Close() - - gc := &GatewayClient{ - http: &http.Client{}, - host: server.URL, - username: "test_username", - password: "test_password", + tests := map[string]struct { + server *httptest.Server + version string + }{ + "success with version 4.0": { + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost && strings.HasPrefix(r.URL.Path, "/im/types/Configuration/actions/install") { + w.WriteHeader(http.StatusAccepted) + return + } + http.NotFound(w, r) + })), + version: "4.0", + }, + "success with version < 4.0": { + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost && strings.HasPrefix(r.URL.Path, "/im/types/Configuration/actions/install") { + w.WriteHeader(http.StatusAccepted) + return + } + http.NotFound(w, r) + })), + version: "3.6", + }, } + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + defer tt.server.Close() + + gc := &GatewayClient{ + http: &http.Client{}, + host: tt.server.URL, + username: "test_username", + password: "test_password", + version: tt.version, + } - _, err := gc.BeginInstallation("", "mdm_user", "mdm_password", "lia_password", true, true, true, false) - assert.Error(t, err) - - expectedErrorMsg := "unexpected end of JSON input" - assert.EqualError(t, err, expectedErrorMsg) + resp, err := gc.BeginInstallation("{}", "mdm_user", "mdm_password", "lia_password", true, true, true, false) + assert.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) + }) + } } func TestMoveToNextPhase(t *testing.T) { @@ -313,6 +338,7 @@ func TestRetryPhase(t *testing.T) { host: server.URL, username: "test_username", password: "test_password", + version: "4.0", } gatewayResponse, err := gc.RetryPhase() @@ -409,6 +435,7 @@ func TestCheckForCompletionQueueCommands(t *testing.T) { host: server.URL, username: "test_username", password: "test_password", + version: "4.0", } gatewayResponse, err := gc.CheckForCompletionQueueCommands("Query") @@ -498,3 +525,213 @@ func TestRenewInstallationCookie(t *testing.T) { err := gc.RenewInstallationCookie(5) assert.NoError(t, err) } + +func TestValidateMDMDetails(t *testing.T) { + tests := map[string]struct { + mdmTopologyParam []byte + server *httptest.Server + expectedResponse *types.GatewayResponse + version string + expectedErr error + }{ + "success with version 4.0": { + mdmTopologyParam: []byte(`{"mdmUser": "admin", "mdmPassword": "password", "mdmIps": ["192.168.0.1"]}`), + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + resp := types.MDMTopologyDetails{ + SdcIps: []string{"10.0.0.1", "10.0.0.2"}, + } + + data, err := json.Marshal(resp) + if err != nil { + t.Fatal(err) + } + w.WriteHeader(http.StatusOK) + w.Write(data) + })), + expectedResponse: &types.GatewayResponse{ + StatusCode: 200, + Data: "10.0.0.1,10.0.0.2", + }, + version: "4.0", + expectedErr: nil, + }, + "success with version < 4.0": { + mdmTopologyParam: []byte(`{"mdmUser": "admin", "mdmPassword": "password", "mdmIps": ["192.168.0.1"]}`), + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + resp := types.MDMTopologyDetails{ + SdcIps: []string{"10.0.0.1", "10.0.0.2"}, + } + + data, err := json.Marshal(resp) + if err != nil { + t.Fatal(err) + } + w.WriteHeader(http.StatusOK) + w.Write(data) + })), + expectedResponse: &types.GatewayResponse{ + StatusCode: 200, + Data: "10.0.0.1,10.0.0.2", + }, + version: "3.6", + expectedErr: nil, + }, + "error primary mdm ip": { + mdmTopologyParam: []byte(`{"mdmUser": "admin", "mdmPassword": "password", "mdmIps": ["192.168.0.2"]}`), + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + })), + + expectedErr: errors.New("Wrong Primary MDM IP, Please provide valid Primary MDM IP"), + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + defer tt.server.Close() + + gc := &GatewayClient{ + http: &http.Client{}, + host: tt.server.URL, + username: "test_username", + password: "test_password", + version: tt.version, + } + + res, err := gc.ValidateMDMDetails(tt.mdmTopologyParam) + + if tt.expectedErr != nil { + assert.EqualError(t, err, tt.expectedErr.Error()) + } else { + assert.NoError(t, err) + assert.NotNil(t, res) + assert.Equal(t, tt.expectedResponse, res) + } + }) + } +} + +func TestGetClusterDetails(t *testing.T) { + tests := map[string]struct { + mdmTopologyParam []byte + requireJSONOutput bool + server *httptest.Server + version string + expectedErr error + expectedStatusCode int + expectedResponse *types.GatewayResponse + }{ + "success with version 4.0": { + mdmTopologyParam: []byte(`{"mdmIps": ["192.168.0.1"]}`), + requireJSONOutput: false, + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + resp := types.MDMTopologyDetails{ + SdcIps: []string{"10.0.0.1", "10.0.0.2"}, + } + data, err := json.Marshal(resp) + if err != nil { + t.Fatal(err) + } + w.WriteHeader(http.StatusOK) + w.Write(data) + })), + version: "4.0", + expectedErr: nil, + expectedStatusCode: http.StatusOK, + expectedResponse: &types.GatewayResponse{ + StatusCode: 200, + ClusterDetails: types.MDMTopologyDetails{ + SdcIps: []string{"10.0.0.1", "10.0.0.2"}, + }, + }, + }, + "error getting cluster details": { + mdmTopologyParam: []byte(`{"invalid": "data"}`), + requireJSONOutput: false, + expectedErr: errors.New("Error Getting Cluster Details"), + expectedStatusCode: http.StatusBadRequest, + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // no response + })), + expectedResponse: &types.GatewayResponse{ + StatusCode: 200, + ClusterDetails: types.MDMTopologyDetails{}, + }, + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + defer tt.server.Close() + + gc := &GatewayClient{ + http: &http.Client{}, + host: tt.server.URL, + username: "test_username", + password: "test_password", + version: tt.version, + } + + res, err := gc.GetClusterDetails(tt.mdmTopologyParam, tt.requireJSONOutput) + + if tt.expectedErr != nil { + assert.EqualError(t, err, tt.expectedErr.Error()) + } else { + assert.NoError(t, err) + assert.NotNil(t, res) + assert.Equal(t, tt.expectedStatusCode, res.StatusCode) + assert.Equal(t, tt.expectedResponse, res) + } + }) + } +} + +func TestParseJSONError(t *testing.T) { + tests := map[string]struct { + name string + response *http.Response + expectedErr error + }{ + "JSON response": { + response: &http.Response{ + StatusCode: http.StatusBadRequest, + Header: http.Header{"Content-Type": []string{"application/json"}}, + Body: ioutil.NopCloser(strings.NewReader(`{"message":"Bad Request"}`)), + }, + expectedErr: &types.Error{ + HTTPStatusCode: http.StatusBadRequest, + Message: "Bad Request", + }, + }, + "HTML response": { + response: &http.Response{ + StatusCode: http.StatusBadRequest, + Status: "Bad Request", + Header: http.Header{"Content-Type": []string{"text/html"}}, + Body: ioutil.NopCloser(strings.NewReader("Bad Request")), + }, + expectedErr: &types.Error{ + HTTPStatusCode: http.StatusBadRequest, + Message: "Bad Request", + }, + }, + "No content type": { + response: &http.Response{ + StatusCode: http.StatusBadRequest, + Body: ioutil.NopCloser(strings.NewReader(`{"message":"Bad Request"}`)), + }, + expectedErr: &types.Error{ + HTTPStatusCode: http.StatusBadRequest, + Message: "Bad Request", + }, + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + err := ParseJSONError(tt.response) + if !reflect.DeepEqual(err, tt.expectedErr) { + t.Errorf("Expected error %v, got %v", tt.expectedErr, err) + } + }) + } +} From 1e031d4ceb5ec8ca433186e912fcf469196d7aa5 Mon Sep 17 00:00:00 2001 From: Trevor Dawe Date: Tue, 17 Dec 2024 23:07:05 +0000 Subject: [PATCH 2/3] Fix linting --- deploy_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/deploy_test.go b/deploy_test.go index 99f7962..d837f2f 100644 --- a/deploy_test.go +++ b/deploy_test.go @@ -536,7 +536,7 @@ func TestValidateMDMDetails(t *testing.T) { }{ "success with version 4.0": { mdmTopologyParam: []byte(`{"mdmUser": "admin", "mdmPassword": "password", "mdmIps": ["192.168.0.1"]}`), - server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { resp := types.MDMTopologyDetails{ SdcIps: []string{"10.0.0.1", "10.0.0.2"}, } @@ -557,7 +557,7 @@ func TestValidateMDMDetails(t *testing.T) { }, "success with version < 4.0": { mdmTopologyParam: []byte(`{"mdmUser": "admin", "mdmPassword": "password", "mdmIps": ["192.168.0.1"]}`), - server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { resp := types.MDMTopologyDetails{ SdcIps: []string{"10.0.0.1", "10.0.0.2"}, } @@ -578,7 +578,7 @@ func TestValidateMDMDetails(t *testing.T) { }, "error primary mdm ip": { mdmTopologyParam: []byte(`{"mdmUser": "admin", "mdmPassword": "password", "mdmIps": ["192.168.0.2"]}`), - server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server: httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { })), expectedErr: errors.New("Wrong Primary MDM IP, Please provide valid Primary MDM IP"), @@ -623,7 +623,7 @@ func TestGetClusterDetails(t *testing.T) { "success with version 4.0": { mdmTopologyParam: []byte(`{"mdmIps": ["192.168.0.1"]}`), requireJSONOutput: false, - server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { resp := types.MDMTopologyDetails{ SdcIps: []string{"10.0.0.1", "10.0.0.2"}, } @@ -649,7 +649,7 @@ func TestGetClusterDetails(t *testing.T) { requireJSONOutput: false, expectedErr: errors.New("Error Getting Cluster Details"), expectedStatusCode: http.StatusBadRequest, - server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server: httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { // no response })), expectedResponse: &types.GatewayResponse{ From fa52fdc8237dace992426a8a819b2b4ab4c3d9f8 Mon Sep 17 00:00:00 2001 From: Trevor Dawe Date: Wed, 18 Dec 2024 12:48:35 +0000 Subject: [PATCH 3/3] Update deploy tests --- deploy_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/deploy_test.go b/deploy_test.go index d837f2f..2732b26 100644 --- a/deploy_test.go +++ b/deploy_test.go @@ -218,6 +218,7 @@ func TestGetPackageDetails(t *testing.T) { host: server.URL, username: "test_username", password: "test_password", + version: "4.0", } packageDetails, err := gc.GetPackageDetails() @@ -459,6 +460,7 @@ func TestUninstallCluster(t *testing.T) { host: server.URL, username: "test_username", password: "test_password", + version: "4.0", } jsonStr := `{