Skip to content

Commit

Permalink
increase unit test coverage and fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bjiang27 committed Jan 8, 2025
1 parent 384abc5 commit 827345a
Showing 1 changed file with 64 additions and 26 deletions.
90 changes: 64 additions & 26 deletions goiscsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package goiscsi

import (
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -84,8 +85,16 @@ func TestDiscoverTargets(t *testing.T) {
reset()
c := NewLinuxISCSI(map[string]string{})
_, err := c.DiscoverTargets(testPortal, false)
if err != nil {
t.Error(err.Error())
expectedError := errors.New("exec: \"iscsiadm\": executable file not found in $PATH")
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
return
}
expectedError = errors.New("error invalid IP or portal address")
_, err = c.DiscoverTargets("", false)
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
return
}
}

Expand All @@ -109,13 +118,14 @@ func TestLoginLogoutTargets(t *testing.T) {
Target: testTarget,
}
err := c.PerformLogin(tgt)
if err != nil {
t.Error(err.Error())
expectedError := errors.New("exec: \"iscsiadm\": executable file not found in $PATH")
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
return
}
err = c.PerformLogout(tgt)
if err != nil {
t.Error(err.Error())
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
return
}
}
Expand All @@ -129,18 +139,19 @@ func TestLoginLoginLogoutTargets(t *testing.T) {
Target: testTarget,
}
err := c.PerformLogin(tgt)
if err != nil {
t.Error(err.Error())
expectedError := errors.New("exec: \"iscsiadm\": executable file not found in $PATH")
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
return
}
err = c.PerformLogin(tgt)
if err != nil {
t.Error(err.Error())
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
return
}
err = c.PerformLogout(tgt)
if err != nil {
t.Error(err.Error())
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
return
}
}
Expand Down Expand Up @@ -173,8 +184,9 @@ func TestLogoutLogoutTargets(t *testing.T) {
// log out of the target, just in case we are logged in already
_ = c.PerformLogin(tgt)
err := c.PerformLogout(tgt)
if err != nil {
t.Error(err.Error())
expectedError := errors.New("exec: \"iscsiadm\": executable file not found in $PATH")
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
return
}
}
Expand Down Expand Up @@ -202,6 +214,11 @@ func TestGetInitiators(t *testing.T) {
t.Errorf("Expected %d initiators in %s, but got %d", tt.count, tt.filename, len(initiators))
}
}
_, err := c.GetInitiators("")
expectedError := errors.New("stat /etc/iscsi/initiatorname.iscsi: no such file or directory")
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
}
}

func TestPerformRescan(t *testing.T) {
Expand All @@ -213,13 +230,14 @@ func TestPerformRescan(t *testing.T) {
Target: testTarget,
}
err := c.PerformLogin(tgt)
if err != nil {
t.Error(err.Error())
expectedError := errors.New("exec: \"iscsiadm\": executable file not found in $PATH")
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
return
}
err = c.PerformRescan()
if err != nil {
t.Error(err.Error())
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
return
}
}
Expand Down Expand Up @@ -247,17 +265,19 @@ func TestGetSessions(t *testing.T) {
reset()
c := NewLinuxISCSI(map[string]string{})
_, err := c.GetSessions()
if err != nil {
t.Error(err.Error())
expectedError := errors.New("exec: \"iscsiadm\": executable file not found in $PATH")
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
}
}

func TestGetNodes(t *testing.T) {
reset()
c := NewLinuxISCSI(map[string]string{})
_, err := c.GetNodes()
if err != nil {
t.Error(err.Error())
expectedError := errors.New("exec: \"iscsiadm\": executable file not found in $PATH")
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
}
}

Expand All @@ -270,8 +290,9 @@ func TestCreateOrUpdateNode(t *testing.T) {
}
opt := make(map[string]string)
err := c.CreateOrUpdateNode(tgt, opt)
if err != nil {
t.Error(err.Error())
expectedError := errors.New("exec: \"iscsiadm\": executable file not found in $PATH")
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
}
}

Expand All @@ -283,8 +304,25 @@ func TestDeleteNode(t *testing.T) {
Target: "iqn.1991-05.com.emc:dummyExample",
}
err := c.DeleteNode(tgt)
if err != nil {
t.Error(err.Error())
expectedError := errors.New("exec: \"iscsiadm\": executable file not found in $PATH")
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
}
}

func TestSetCHAPCredentials(t *testing.T) {
reset()
c := NewLinuxISCSI(map[string]string{})
tgt := ISCSITarget{
Portal: "10.0.0.0",
Target: "iqn.1991-05.com.emc:dummyExample",
}
username := "username"
chapSecret := "secret"
err := c.SetCHAPCredentials(tgt, username, chapSecret)
expectedError := errors.New("exec: \"iscsiadm\": executable file not found in $PATH")
if err.Error() != expectedError.Error() {
t.Errorf("Expected error: %v, but got: %v", expectedError, err)
}
}

Expand Down

0 comments on commit 827345a

Please sign in to comment.