Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Unit Test Cases #120

Merged
merged 22 commits into from
May 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixes
Krunal-Thakkar committed May 20, 2024
commit 1f8eaa8439dbbe6e1aa93de46aaecedfe5cb2041
2 changes: 1 addition & 1 deletion api/api_logging.go
Original file line number Diff line number Diff line change
@@ -172,7 +172,7 @@ func dumpRequest(req *http.Request, body bool) ([]byte, error) {
reqURI = req.URL.RequestURI()
}

method := "GET"
method := http.MethodGet
if req.Method != "" {
method = req.Method
}
68 changes: 50 additions & 18 deletions deploy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// Copyright © 2023 - 2024 Dell Inc. or its subsidiaries. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package goscaleio

import (
@@ -19,7 +31,6 @@ import (
"strconv"
"strings"

"github.com/dell/goscaleio/api"
types "github.com/dell/goscaleio/types/v1"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
@@ -33,7 +44,6 @@ var (
// GatewayClient is client for Gateway server
type GatewayClient struct {
http *http.Client
api api.Client
host string
username string
password string
@@ -94,7 +104,7 @@ func NewGateway(host string, username, password string, insecure, useCerts bool)

body, _ := json.Marshal(bodyData)

req, err := http.NewRequest("POST", host+"/rest/auth/login", bytes.NewBuffer(body))
req, err := http.NewRequest(http.MethodPost, host+"/rest/auth/login", bytes.NewBuffer(body))
if err != nil {
return nil, err
}
@@ -117,7 +127,7 @@ func NewGateway(host string, username, password string, insecure, useCerts bool)
case resp == nil:
return nil, errNilReponse
case !(resp.StatusCode >= 200 && resp.StatusCode <= 299):
return nil, gc.api.ParseJSONError(resp)
return nil, ParseJSONError(resp)
}

bs, err := io.ReadAll(resp.Body)
@@ -149,7 +159,7 @@ func NewGateway(host string, username, password string, insecure, useCerts bool)

// GetVersion returns version
func (gc *GatewayClient) GetVersion() (string, error) {
req, httpError := http.NewRequest("GET", gc.host+"/api/version", nil)
req, httpError := http.NewRequest(http.MethodGet, gc.host+"/api/version", nil)
if httpError != nil {
return "", httpError
}
@@ -225,7 +235,7 @@ func (gc *GatewayClient) UploadPackages(filePaths []string) (*types.GatewayRespo
return &gatewayResponse, fileWriterError
}

req, httpError := http.NewRequest("POST", gc.host+"/im/types/installationPackages/instances/actions/uploadPackages", body)
req, httpError := http.NewRequest(http.MethodPost, gc.host+"/im/types/installationPackages/instances/actions/uploadPackages", body)
if httpError != nil {
return &gatewayResponse, httpError
}
@@ -295,7 +305,7 @@ func (gc *GatewayClient) ParseCSV(filePath string) (*types.GatewayResponse, erro
return &gatewayResponse, fileWriterError
}

req, httpError := http.NewRequest("POST", gc.host+"/im/types/Configuration/instances/actions/parseFromCSV", body)
req, httpError := http.NewRequest(http.MethodPost, gc.host+"/im/types/Configuration/instances/actions/parseFromCSV", body)
if httpError != nil {
return &gatewayResponse, httpError
}
@@ -354,7 +364,7 @@ func (gc *GatewayClient) ParseCSV(filePath string) (*types.GatewayResponse, erro
func (gc *GatewayClient) GetPackageDetails() ([]*types.PackageDetails, error) {
var packageParam []*types.PackageDetails

req, httpError := http.NewRequest("GET", gc.host+"/im/types/installationPackages/instances?onlyLatest=false&_search=false", nil)
req, httpError := http.NewRequest(http.MethodGet, gc.host+"/im/types/installationPackages/instances?onlyLatest=false&_search=false", nil)
if httpError != nil {
return packageParam, httpError
}
@@ -408,7 +418,7 @@ func (gc *GatewayClient) GetPackageDetails() ([]*types.PackageDetails, error) {
func (gc *GatewayClient) ValidateMDMDetails(mdmTopologyParam []byte) (*types.GatewayResponse, error) {
var gatewayResponse types.GatewayResponse

req, httpError := http.NewRequest("POST", gc.host+"/im/types/Configuration/instances", bytes.NewBuffer(mdmTopologyParam))
req, httpError := http.NewRequest(http.MethodPost, gc.host+"/im/types/Configuration/instances", bytes.NewBuffer(mdmTopologyParam))
if httpError != nil {
return &gatewayResponse, httpError
}
@@ -474,7 +484,7 @@ func (gc *GatewayClient) ValidateMDMDetails(mdmTopologyParam []byte) (*types.Gat
func (gc *GatewayClient) GetClusterDetails(mdmTopologyParam []byte, requireJSONOutput bool) (*types.GatewayResponse, error) {
var gatewayResponse types.GatewayResponse

req, httpError := http.NewRequest("POST", gc.host+"/im/types/Configuration/instances", bytes.NewBuffer(mdmTopologyParam))
req, httpError := http.NewRequest(http.MethodPost, gc.host+"/im/types/Configuration/instances", bytes.NewBuffer(mdmTopologyParam))
if httpError != nil {
return &gatewayResponse, httpError
}
@@ -639,7 +649,7 @@ func (gc *GatewayClient) BeginInstallation(jsonStr, mdmUsername, mdmPassword, li

u.RawQuery = q.Encode()

req, httpError := http.NewRequest("POST", u.String(), bytes.NewBuffer(finalJSON))
req, httpError := http.NewRequest(http.MethodPost, u.String(), bytes.NewBuffer(finalJSON))
if httpError != nil {
return &gatewayResponse, httpError
}
@@ -686,7 +696,7 @@ func (gc *GatewayClient) BeginInstallation(jsonStr, mdmUsername, mdmPassword, li
func (gc *GatewayClient) MoveToNextPhase() (*types.GatewayResponse, error) {
var gatewayResponse types.GatewayResponse

req, httpError := http.NewRequest("POST", gc.host+"/im/types/ProcessPhase/actions/moveToNextPhase", nil)
req, httpError := http.NewRequest(http.MethodPost, gc.host+"/im/types/ProcessPhase/actions/moveToNextPhase", nil)
if httpError != nil {
return &gatewayResponse, httpError
}
@@ -740,7 +750,7 @@ func (gc *GatewayClient) MoveToNextPhase() (*types.GatewayResponse, error) {
func (gc *GatewayClient) RetryPhase() (*types.GatewayResponse, error) {
var gatewayResponse types.GatewayResponse

req, httpError := http.NewRequest("POST", gc.host+"/im/types/Command/instances/actions/retry/", nil)
req, httpError := http.NewRequest(http.MethodPost, gc.host+"/im/types/Command/instances/actions/retry/", nil)
if httpError != nil {
return &gatewayResponse, httpError
}
@@ -794,7 +804,7 @@ func (gc *GatewayClient) RetryPhase() (*types.GatewayResponse, error) {
func (gc *GatewayClient) AbortOperation() (*types.GatewayResponse, error) {
var gatewayResponse types.GatewayResponse

req, httpError := http.NewRequest("POST", gc.host+"/im/types/Command/instances/actions/abort", nil)
req, httpError := http.NewRequest(http.MethodPost, gc.host+"/im/types/Command/instances/actions/abort", nil)
if httpError != nil {
return &gatewayResponse, httpError
}
@@ -848,7 +858,7 @@ func (gc *GatewayClient) AbortOperation() (*types.GatewayResponse, error) {
func (gc *GatewayClient) ClearQueueCommand() (*types.GatewayResponse, error) {
var gatewayResponse types.GatewayResponse

req, httpError := http.NewRequest("POST", gc.host+"/im/types/Command/instances/actions/clear", nil)
req, httpError := http.NewRequest(http.MethodPost, gc.host+"/im/types/Command/instances/actions/clear", nil)
if httpError != nil {
return &gatewayResponse, httpError
}
@@ -902,7 +912,7 @@ func (gc *GatewayClient) ClearQueueCommand() (*types.GatewayResponse, error) {
func (gc *GatewayClient) MoveToIdlePhase() (*types.GatewayResponse, error) {
var gatewayResponse types.GatewayResponse

req, httpError := http.NewRequest("POST", gc.host+"/im/types/ProcessPhase/actions/moveToIdlePhase", nil)
req, httpError := http.NewRequest(http.MethodPost, gc.host+"/im/types/ProcessPhase/actions/moveToIdlePhase", nil)
if httpError != nil {
return &gatewayResponse, httpError
}
@@ -956,7 +966,7 @@ func (gc *GatewayClient) MoveToIdlePhase() (*types.GatewayResponse, error) {
func (gc *GatewayClient) GetInQueueCommand() ([]types.MDMQueueCommandDetails, error) {
var mdmQueueCommandDetails []types.MDMQueueCommandDetails

req, httpError := http.NewRequest("GET", gc.host+"/im/types/Command/instances", nil)
req, httpError := http.NewRequest(http.MethodGet, gc.host+"/im/types/Command/instances", nil)
if httpError != nil {
return mdmQueueCommandDetails, httpError
}
@@ -1077,7 +1087,7 @@ func (gc *GatewayClient) UninstallCluster(jsonStr, mdmUsername, mdmPassword, lia

u, _ := url.Parse(gc.host + "/im/types/Configuration/actions/uninstall")

req, httpError := http.NewRequest("POST", u.String(), bytes.NewBuffer(finalJSON))
req, httpError := http.NewRequest(http.MethodPost, u.String(), bytes.NewBuffer(finalJSON))
if httpError != nil {
return &gatewayResponse, httpError
}
@@ -1238,3 +1248,25 @@ func writeConfig(config *CookieConfig) error {

return nil
}

func ParseJSONError(r *http.Response) error {
jsonError := &types.Error{}

// Starting in 4.0, response may be in html; so we cannot always use a json decoder
if strings.Contains(r.Header.Get("Content-Type"), "html") {
jsonError.HTTPStatusCode = r.StatusCode
jsonError.Message = r.Status
return jsonError
}

if err := json.NewDecoder(r.Body).Decode(jsonError); err != nil {
return err
}

jsonError.HTTPStatusCode = r.StatusCode
if jsonError.Message == "" {
jsonError.Message = r.Status
}

return jsonError
}
30 changes: 15 additions & 15 deletions deploy_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2021 - 2023 Dell Inc. or its subsidiaries. All Rights Reserved.
// Copyright © 2023 - 2024 Dell Inc. or its subsidiaries. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -25,14 +25,14 @@ import (
// TestNewGateway tests the NewGateway function.
func TestNewGateway(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.URL.Path == "/rest/auth/login" {
if r.Method == http.MethodPost && r.URL.Path == "/rest/auth/login" {

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{"access_token":"mock_access_token"}`)
return
}
if r.Method == "GET" && r.URL.Path == "/api/version" {
if r.Method == http.MethodGet && r.URL.Path == "/api/version" {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "4.0")
@@ -57,7 +57,7 @@ func TestNewGateway(t *testing.T) {
// TestGetVersion tests the GetVersion function.
func TestGetVersion(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && r.URL.Path == "/api/version" {
if r.Method == http.MethodGet && r.URL.Path == "/api/version" {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "4.0")
@@ -83,7 +83,7 @@ func TestGetVersion(t *testing.T) {
// TestUploadPackages tests the UploadPackages function.
func TestUploadPackages(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.URL.Path == "/im/types/installationPackages/instances/actions/uploadPackages" {
if r.Method == http.MethodPost && r.URL.Path == "/im/types/installationPackages/instances/actions/uploadPackages" {
w.WriteHeader(http.StatusOK)
return
}
@@ -108,7 +108,7 @@ func TestUploadPackages(t *testing.T) {

func TestParseCSV(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.URL.Path == "/im/types/Configuration/instances/actions/parseFromCSV" {
if r.Method == http.MethodPost && r.URL.Path == "/im/types/Configuration/instances/actions/parseFromCSV" {
w.WriteHeader(http.StatusOK)
return
}
@@ -164,7 +164,7 @@ func TestGetPackageDetails(t *testing.T) {
}]`

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && r.URL.Path == "/im/types/installationPackages/instances" {
if r.Method == http.MethodGet && r.URL.Path == "/im/types/installationPackages/instances" {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(responseJSON))
if err != nil {
@@ -222,7 +222,7 @@ 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 == "POST" && strings.HasPrefix(r.URL.Path, "/im/types/Configuration/actions/install") {
if r.Method == http.MethodPost && strings.HasPrefix(r.URL.Path, "/im/types/Configuration/actions/install") {
w.WriteHeader(http.StatusAccepted)
return
}
@@ -247,7 +247,7 @@ func TestBeginInstallation(t *testing.T) {

func TestMoveToNextPhase(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.URL.Path == "/im/types/ProcessPhase/actions/moveToNextPhase" {
if r.Method == http.MethodPost && r.URL.Path == "/im/types/ProcessPhase/actions/moveToNextPhase" {
w.WriteHeader(http.StatusOK)
return
}
@@ -269,7 +269,7 @@ func TestMoveToNextPhase(t *testing.T) {

func TestRetryPhase(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && strings.HasPrefix(r.URL.Path, "/im/types/Command/instances/actions/retry") {
if r.Method == http.MethodPost && strings.HasPrefix(r.URL.Path, "/im/types/Command/instances/actions/retry") {
w.WriteHeader(http.StatusOK)
return
}
@@ -291,7 +291,7 @@ func TestRetryPhase(t *testing.T) {

func TestAbortOperation(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.URL.Path == "/im/types/Command/instances/actions/abort" {
if r.Method == http.MethodPost && r.URL.Path == "/im/types/Command/instances/actions/abort" {
w.WriteHeader(http.StatusOK)
return
}
@@ -313,7 +313,7 @@ func TestAbortOperation(t *testing.T) {

func TestClearQueueCommand(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.URL.Path == "/im/types/Command/instances/actions/clear" {
if r.Method == http.MethodPost && r.URL.Path == "/im/types/Command/instances/actions/clear" {
w.WriteHeader(http.StatusOK)
return
}
@@ -335,7 +335,7 @@ func TestClearQueueCommand(t *testing.T) {

func TestMoveToIdlePhase(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.URL.Path == "/im/types/ProcessPhase/actions/moveToIdlePhase" {
if r.Method == http.MethodPost && r.URL.Path == "/im/types/ProcessPhase/actions/moveToIdlePhase" {
w.WriteHeader(http.StatusOK)
return
}
@@ -361,7 +361,7 @@ func TestCheckForCompletionQueueCommands(t *testing.T) {
}`

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && r.URL.Path == "/im/types/Command/instances" {
if r.Method == http.MethodGet && r.URL.Path == "/im/types/Command/instances" {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(responseJSON))
if err != nil {
@@ -388,7 +388,7 @@ func TestCheckForCompletionQueueCommands(t *testing.T) {

func TestUninstallCluster(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && strings.Contains(r.URL.Path, "/im/types/Configuration/actions/uninstall") {
if r.Method == http.MethodPost && strings.Contains(r.URL.Path, "/im/types/Configuration/actions/uninstall") {
w.WriteHeader(http.StatusAccepted)
return
}
10 changes: 5 additions & 5 deletions node.go
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ func (gc *GatewayClient) GetNodeByID(id string) (*types.NodeDetails, error) {
path := fmt.Sprintf("/Api/V1/ManagedDevice/%v", id)

var node types.NodeDetails
req, httpError := http.NewRequest("GET", gc.host+path, nil)
req, httpError := http.NewRequest(http.MethodGet, gc.host+path, nil)
if httpError != nil {
return nil, httpError
}
@@ -76,7 +76,7 @@ func (gc *GatewayClient) GetAllNodes() ([]types.NodeDetails, error) {
path := fmt.Sprintf("/Api/V1/ManagedDevice")

var nodes []types.NodeDetails
req, httpError := http.NewRequest("GET", gc.host+path, nil)
req, httpError := http.NewRequest(http.MethodGet, gc.host+path, nil)
if httpError != nil {
return nil, httpError
}
@@ -122,7 +122,7 @@ func (gc *GatewayClient) GetNodeByFilters(key string, value string) ([]types.Nod
path := fmt.Sprintf("/Api/V1/ManagedDevice?filter=eq,%v,%v", key, value)

var nodes []types.NodeDetails
req, httpError := http.NewRequest("GET", gc.host+path, nil)
req, httpError := http.NewRequest(http.MethodGet, gc.host+path, nil)
if httpError != nil {
return nil, httpError
}
@@ -171,7 +171,7 @@ func (gc *GatewayClient) GetNodePoolByID(id int) (*types.NodePoolDetails, error)
path := fmt.Sprintf("/Api/V1/nodepool/%v", id)

var nodePool types.NodePoolDetails
req, httpError := http.NewRequest("GET", gc.host+path, nil)
req, httpError := http.NewRequest(http.MethodGet, gc.host+path, nil)
if httpError != nil {
return nil, httpError
}
@@ -235,7 +235,7 @@ func (gc *GatewayClient) GetAllNodePools() (*types.NodePoolDetailsFilter, error)
path := fmt.Sprintf("/Api/V1/nodepool")

var nodePools types.NodePoolDetailsFilter
req, httpError := http.NewRequest("GET", gc.host+path, nil)
req, httpError := http.NewRequest(http.MethodGet, gc.host+path, nil)
if httpError != nil {
return nil, httpError
}
Loading