Skip to content

Commit

Permalink
added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
AnikaAgiwal2711 committed Oct 25, 2023
1 parent 32c3df0 commit b497ecf
Show file tree
Hide file tree
Showing 2 changed files with 269 additions and 4 deletions.
5 changes: 1 addition & 4 deletions fault_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
package goscaleio

import (
// "errors"
"fmt"
"net/http"
// "time"

types "github.com/dell/goscaleio/types/v1"
"net/http"
)

// CreateFaultSet creates a fault set
Expand Down
268 changes: 268 additions & 0 deletions fault_set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
// Copyright © 2023 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 (
"errors"
"math"
"net/http"
"net/http/httptest"
"testing"

types "github.com/dell/goscaleio/types/v1"
)

var (
ID string
errFs error
)

func TestCreateFaultSet(t *testing.T) {
type testCase struct {
fs *types.FaultSetParam
expected error
}
cases := []testCase{
{
fs: &types.FaultSetParam{
Name: "testFaultSet",
ProtectionDomainID: "202a046600000000",
},
expected: nil,
},
{
fs: &types.FaultSetParam{
Name: "testFaultSet",
ProtectionDomainID: "202a0466000000",
},
expected: errors.New("Invalid Protection Domain. Please try again with the correct ID or name."),
},
}
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
defer svr.Close()

for _, tc := range cases {
tc := tc
t.Run("", func(ts *testing.T) {
client, err := NewClientWithArgs(svr.URL, "", math.MaxInt64, true, false)
if err != nil {
t.Fatal(err)
}

p := NewProtectionDomain(client)

ID, errFs = p.CreateFaultSet(tc.fs)
if errFs != nil {
if tc.expected == nil {
t.Errorf("Creating fault set did not work as expected, \n\tgot: %s \n\twant: %v", errFs, tc.expected)
} else {
if errFs.Error() != tc.expected.Error() {
t.Errorf("Creating fault set did not work as expected, \n\tgot: %s \n\twant: %s", errFs, tc.expected)
}
}
}
})
}
}

func TestGetFaultByID(t *testing.T) {
type testCase struct {
id string
expected error
}
cases := []testCase{
{
id: ID,
expected: nil,
},
{
id: "1234",
expected: errors.New("Invalid Fault Set. Please try again with the correct ID or name."),
},
}
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
defer svr.Close()

for _, tc := range cases {
tc := tc
t.Run("", func(ts *testing.T) {
client, err := NewClientWithArgs(svr.URL, "", math.MaxInt64, true, false)
if err != nil {
t.Fatal(err)
}

s := System{
client: client,
}
_, err2 := s.GetFaultSetByID(tc.id)
if err2 != nil {
if tc.expected == nil {
t.Errorf("Fetching fault set did not work as expected, \n\tgot: %s \n\twant: %v", err2, tc.expected)
} else {
if err2.Error() != tc.expected.Error() {
t.Errorf("Fetching fault set did not work as expected, \n\tgot: %s \n\twant: %s", err2, tc.expected)
}
}
}
})
}
}

func TestModifyFaultSetName(t *testing.T) {
type testCase struct {
name string
id string
expected error
}
cases := []testCase{
{

id: ID,
name: "renameFaultSet",
expected: nil,
},
{
id: "1234",
name: "renameFaultSet",
expected: errors.New("Invalid Fault Set. Please try again with the correct ID or name."),
},
}
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
defer svr.Close()

for _, tc := range cases {
tc := tc
t.Run("", func(ts *testing.T) {
client, err := NewClientWithArgs(svr.URL, "", math.MaxInt64, true, false)
if err != nil {
t.Fatal(err)
}

p := ProtectionDomain{
client: client,
}
err2 := p.ModifyFaultSetName(tc.id, tc.name)
if err2 != nil {
if tc.expected == nil {
t.Errorf("Modifying fault set did not work as expected, \n\tgot: %s \n\twant: %v", err2, tc.expected)
} else {
if err2.Error() != tc.expected.Error() {
t.Errorf("Modifying fault set did not work as expected, \n\tgot: %s \n\twant: %s", err2, tc.expected)
}
}
}
})
}
}

func TestModifyFaultPerfProfile(t *testing.T) {
type testCase struct {
perfProfile string
id string
expected error
}
cases := []testCase{
{

id: ID,
perfProfile: "Compact",
expected: nil,
},
{

id: ID,
perfProfile: "HighPerformance",
expected: nil,
},
{

id: ID,
perfProfile: "Invalid",
expected: errors.New("perfProfile should get one of the following values: Compact, HighPerformance, but its value is Invalid."),
},
}
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
defer svr.Close()

for _, tc := range cases {
tc := tc
t.Run("", func(ts *testing.T) {
client, err := NewClientWithArgs(svr.URL, "", math.MaxInt64, true, false)
if err != nil {
t.Fatal(err)
}

p := ProtectionDomain{
client: client,
}
err2 := p.ModifyFaultSetPerfProfile(tc.id, tc.perfProfile)
if err2 != nil {
if tc.expected == nil {
t.Errorf("Modifying fault set did not work as expected, \n\tgot: %s \n\twant: %v", err2, tc.expected)
} else {
if err2.Error() != tc.expected.Error() {
t.Errorf("Modifying fault set did not work as expected, \n\tgot: %s \n\twant: %s", err2, tc.expected)
}
}
}
})
}
}

func TestDeleteFaultSet(t *testing.T) {
type testCase struct {
id string
expected error
}
cases := []testCase{
{
id: ID,
expected: nil,
},
{
id: "1234",
expected: errors.New("Invalid Fault Set. Please try again with the correct ID or name."),
},
}
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
defer svr.Close()

for _, tc := range cases {
tc := tc
t.Run("", func(ts *testing.T) {
client, err := NewClientWithArgs(svr.URL, "", math.MaxInt64, true, false)
if err != nil {
t.Fatal(err)
}

p := ProtectionDomain{
client: client,
}
err2 := p.DeleteFaultSet(tc.id)
if err2 != nil {
if tc.expected == nil {
t.Errorf("Removing fault set did not work as expected, \n\tgot: %s \n\twant: %v", err2, tc.expected)
} else {
if err2.Error() != tc.expected.Error() {
t.Errorf("Removing fault set did not work as expected, \n\tgot: %s \n\twant: %s", err2, tc.expected)
}
}
}
})
}
}

0 comments on commit b497ecf

Please sign in to comment.