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 golangci-lint warnings #31

Merged
merged 10 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 2 additions & 3 deletions gonvme.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type Tracer = tracer.Tracer

// NVMEinterface is the interface that provides the NVMe client functionality
type NVMEinterface interface {

// DiscoverNVMeTCPTargets discovers the targets exposed via a given portal
// returns an array of NVMeTCP Target instances
DiscoverNVMeTCPTargets(address string, login bool) ([]NVMeTarget, error)
Expand All @@ -46,10 +45,10 @@ type NVMEinterface interface {
// To use the system default file of "/etc/nvme/hostnqn", provide a filename of ""
GetInitiators(filename string) ([]string, error)

//NVMeTCPConnect connects into a specified NVMeTCP target
// NVMeTCPConnect connects into a specified NVMeTCP target
NVMeTCPConnect(target NVMeTarget, duplicateConnect bool) error

//NVMeFCConnect connects into a specified NVMeFC target
// NVMeFCConnect connects into a specified NVMeFC target
NVMeFCConnect(target NVMeTarget, duplicateConnect bool) error

// NVMeDisconnect disconnect from the specified NVMe target
Expand Down
47 changes: 20 additions & 27 deletions gonvme_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,18 @@ const (
MockNumberOfNamespaceDevices = "numberOfNamespaceDevices"
)

var (
// GONVMEMock is a struct controlling induced errors
GONVMEMock struct {
InduceDiscoveryError bool
InduceInitiatorError bool
InduceTCPLoginError bool
InduceFCLoginError bool
InduceLogoutError bool
InduceGetSessionsError bool
InducedNVMeDeviceAndNamespaceError bool
InducedNVMeNamespaceIDError bool
InducedNVMeDeviceDataError bool
}
)
// GONVMEMock is a struct controlling induced errors
var GONVMEMock struct {
InduceDiscoveryError bool
InduceInitiatorError bool
InduceTCPLoginError bool
InduceFCLoginError bool
InduceLogoutError bool
InduceGetSessionsError bool
InducedNVMeDeviceAndNamespaceError bool
InducedNVMeNamespaceIDError bool
InducedNVMeDeviceDataError bool
}

// MockNVMe provides a mock implementation of an NVMe client
type MockNVMe struct {
Expand All @@ -72,7 +70,7 @@ func getOptionAsInt(opts map[string]string, key string) int64 {
return v
}

func (nvme *MockNVMe) discoverNVMeTCPTargets(address string, login bool) ([]NVMeTarget, error) {
func (nvme *MockNVMe) discoverNVMeTCPTargets(address string, _ bool) ([]NVMeTarget, error) {
if GONVMEMock.InduceDiscoveryError {
return []NVMeTarget{}, errors.New("discoverTargets induced error")
}
Expand Down Expand Up @@ -104,7 +102,7 @@ func (nvme *MockNVMe) discoverNVMeTCPTargets(address string, login bool) ([]NVMe
return mockedTargets, nil
}

func (nvme *MockNVMe) discoverNVMeFCTargets(address string, login bool) ([]NVMeTarget, error) {
func (nvme *MockNVMe) discoverNVMeFCTargets(address string, _ bool) ([]NVMeTarget, error) {
if GONVMEMock.InduceDiscoveryError {
return []NVMeTarget{}, errors.New("discoverTargets induced error")
}
Expand Down Expand Up @@ -137,8 +135,7 @@ func (nvme *MockNVMe) discoverNVMeFCTargets(address string, login bool) ([]NVMeT
return mockedTargets, nil
}

func (nvme *MockNVMe) getInitiators(filename string) ([]string, error) {

func (nvme *MockNVMe) getInitiators(_ string) ([]string, error) {
if GONVMEMock.InduceInitiatorError {
return []string{}, errors.New("getInitiators induced error")
}
Expand All @@ -157,26 +154,23 @@ func (nvme *MockNVMe) getInitiators(filename string) ([]string, error) {
return mockedInitiators, nil
}

func (nvme *MockNVMe) nvmeTCPConnect(target NVMeTarget, duplicateConnect bool) error {

func (nvme *MockNVMe) nvmeTCPConnect(_ NVMeTarget, _ bool) error {
if GONVMEMock.InduceTCPLoginError {
return errors.New("NVMeTCP Login induced error")
}

return nil
}

func (nvme *MockNVMe) nvmeFCConnect(target NVMeTarget, duplicateConnect bool) error {

func (nvme *MockNVMe) nvmeFCConnect(_ NVMeTarget, _ bool) error {
if GONVMEMock.InduceFCLoginError {
return errors.New("NVMeFC Login induced error")
}

return nil
}

func (nvme *MockNVMe) nvmeDisconnect(target NVMeTarget) error {

func (nvme *MockNVMe) nvmeDisconnect(_ NVMeTarget) error {
if GONVMEMock.InduceLogoutError {
return errors.New("NVMe Logout induced error")
}
Expand All @@ -185,7 +179,7 @@ func (nvme *MockNVMe) nvmeDisconnect(target NVMeTarget) error {
}

// GetNVMeDeviceData returns the information (nguid and namespace) of an NVME device path
func (nvme *MockNVMe) GetNVMeDeviceData(path string) (string, string, error) {
func (nvme *MockNVMe) GetNVMeDeviceData(_ string) (string, string, error) {
if GONVMEMock.InducedNVMeDeviceDataError {
return "", "", errors.New("NVMe Namespace Data Induced Error")
}
Expand All @@ -197,7 +191,7 @@ func (nvme *MockNVMe) GetNVMeDeviceData(path string) (string, string, error) {
}

// ListNVMeNamespaceID returns the namespace IDs for each NVME device path
func (nvme *MockNVMe) ListNVMeNamespaceID(NVMeDeviceNamespace []DevicePathAndNamespace) (map[DevicePathAndNamespace][]string, error) {
func (nvme *MockNVMe) ListNVMeNamespaceID(_ []DevicePathAndNamespace) (map[DevicePathAndNamespace][]string, error) {
if GONVMEMock.InducedNVMeNamespaceIDError {
return map[DevicePathAndNamespace][]string{}, errors.New("listNamespaceID induced error")
}
Expand Down Expand Up @@ -247,7 +241,6 @@ func (nvme *MockNVMe) ListNVMeDeviceAndNamespace() ([]DevicePathAndNamespace, er
}

func (nvme *MockNVMe) getSessions() ([]NVMESession, error) {

if GONVMEMock.InduceGetSessionsError {
return []NVMESession{}, errors.New("getSessions induced error")
}
Expand Down
32 changes: 13 additions & 19 deletions gonvme_tcp_fc.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func (nvme *NVMe) buildNVMeCommand(cmd []string) []string {
}

func (nvme *NVMe) getFCHostInfo() ([]FCHBAInfo, error) {

match, err := filepath.Glob("/sys/class/fc_host/host*")
if err != nil {
log.Errorf("Error gathering fc hosts: %v", err)
Expand Down Expand Up @@ -130,7 +129,7 @@ func (nvme *NVMe) discoverNVMeTCPTargets(address string, login bool) ([]NVMeTarg
// nvme discovery is done via nvme cli
// nvme discover -t tcp -a <NVMe interface IP> -s <port>
exe := nvme.buildNVMeCommand([]string{NVMeCommand, "discover", "-t", "tcp", "-a", address, "-s", NVMePort})
cmd := exec.Command(exe[0], exe[1:]...)
cmd := exec.Command(exe[0], exe[1:]...) // #nosec G204

out, err := cmd.Output()
if err != nil {
Expand Down Expand Up @@ -271,7 +270,7 @@ func (nvme *NVMe) discoverNVMeFCTargets(targetAddress string, login bool) ([]NVM
// host_traddr = nn-<Initiator_WWNN>:pn-<Initiator_WWPN>
initiatorAddress := strings.Replace(fmt.Sprintf("nn-%s:pn-%s", FCHostInfo.NodeName, FCHostInfo.PortName), "\n", "", -1)
exe := nvme.buildNVMeCommand([]string{NVMeCommand, "discover", "-t", "fc", "-a", targetAddress, "-w", initiatorAddress})
cmd := exec.Command(exe[0], exe[1:]...)
cmd := exec.Command(exe[0], exe[1:]...) // #nosec G204

out, err = cmd.Output()
if err != nil {
Expand Down Expand Up @@ -398,7 +397,6 @@ func (nvme *NVMe) GetInitiators(filename string) ([]string, error) {
}

func (nvme *NVMe) getInitiators(filename string) ([]string, error) {

// a slice of filename, which might exist and define the nvme initiators
initiatorConfig := []string{}
nqns := []string{}
Expand Down Expand Up @@ -432,7 +430,6 @@ func (nvme *NVMe) getInitiators(filename string) ([]string, error) {
lines := strings.Split(string(out), "\n")

for _, line := range lines {

if line != "" {
nqns = append(nqns, line)
}
Expand Down Expand Up @@ -461,7 +458,7 @@ func (nvme *NVMe) nvmeTCPConnect(target NVMeTarget, duplicateConnect bool) error
} else {
exe = nvme.buildNVMeCommand([]string{NVMeCommand, "connect", "-t", "tcp", "-n", target.TargetNqn, "-a", target.Portal, "-s", NVMePort})
}
cmd := exec.Command(exe[0], exe[1:]...)
cmd := exec.Command(exe[0], exe[1:]...) // #nosec G204

var Output string
stderr, _ := cmd.StderrPipe()
Expand Down Expand Up @@ -530,7 +527,7 @@ func (nvme *NVMe) nvmeFCConnect(target NVMeTarget, duplicateConnect bool) error
} else {
exe = nvme.buildNVMeCommand([]string{NVMeCommand, "connect", "-t", "fc", "-a", target.Portal, "-w", target.HostAdr, "-n", target.TargetNqn})
}
cmd := exec.Command(exe[0], exe[1:]...)
cmd := exec.Command(exe[0], exe[1:]...) // #nosec G204
var Output string
stderr, _ := cmd.StderrPipe()
err := cmd.Start()
Expand Down Expand Up @@ -591,7 +588,7 @@ func (nvme *NVMe) nvmeDisconnect(target NVMeTarget) error {
// nvme disconnect is done via the nvme cli
// nvme disconnect -n <target NQN>
exe := nvme.buildNVMeCommand([]string{NVMeCommand, "disconnect", "-n", target.TargetNqn})
cmd := exec.Command(exe[0], exe[1:]...)
cmd := exec.Command(exe[0], exe[1:]...) // #nosec G204

_, err := cmd.Output()

Expand All @@ -606,7 +603,6 @@ func (nvme *NVMe) nvmeDisconnect(target NVMeTarget) error {

// ListNVMeDeviceAndNamespace returns the NVME Device Paths and Namespace of each of the NVME device
func (nvme *NVMe) ListNVMeDeviceAndNamespace() ([]DevicePathAndNamespace, error) {

/* ListNVMeDeviceAndNamespace Output
{/dev/nvme0n1 54}
{/dev/nvme0n2 55}
Expand Down Expand Up @@ -645,7 +641,7 @@ func (nvme *NVMe) ListNVMeDeviceAndNamespace() ([]DevicePathAndNamespace, error)
]
}
*/
cmd := exec.Command(exe[0], exe[1:]...)
cmd := exec.Command(exe[0], exe[1:]...) // #nosec G204

output, err := cmd.Output()
if err != nil {
Expand Down Expand Up @@ -694,7 +690,6 @@ func (nvme *NVMe) ListNVMeDeviceAndNamespace() ([]DevicePathAndNamespace, error)

// ListNVMeNamespaceID returns the namespace IDs for each NVME device path
func (nvme *NVMe) ListNVMeNamespaceID(NVMeDeviceAndNamespace []DevicePathAndNamespace) (map[DevicePathAndNamespace][]string, error) {

/* ListNVMeNamespaceID Output
{devicePath namespace} [namespaceId1 namespaceId2]
{/dev/nvme0n1 54} [0x36 0x37]
Expand All @@ -714,7 +709,7 @@ func (nvme *NVMe) ListNVMeNamespaceID(NVMeDeviceAndNamespace []DevicePathAndName
[ 0]:0x2401
[ 1]:0x2406
*/
cmd := exec.Command(exe[0], exe[1:]...)
cmd := exec.Command(exe[0], exe[1:]...) // #nosec G204
output, err := cmd.Output()
if err != nil {
continue
Expand Down Expand Up @@ -745,12 +740,11 @@ func (nvme *NVMe) ListNVMeNamespaceID(NVMeDeviceAndNamespace []DevicePathAndName

// GetNVMeDeviceData returns the information (nguid and namespace) of an NVME device path
func (nvme *NVMe) GetNVMeDeviceData(path string) (string, string, error) {

var nguid string
var namespace string

exe := nvme.buildNVMeCommand([]string{"nvme", "id-ns", path})
cmd := exec.Command(exe[0], exe[1:]...)
cmd := exec.Command(exe[0], exe[1:]...) // #nosec G204

/*
nvme id-ns /dev/nvme3n1 0x95
Expand Down Expand Up @@ -788,9 +782,9 @@ func (nvme *NVMe) GetNVMeDeviceData(path string) (string, string, error) {
lbaf 0 : ms:0 lbads:9 rp:0 (in use)
*/

output, error := cmd.Output()
if error != nil {
return "", "", error
output, err := cmd.Output()
if err != nil {
return "", "", err
}
str := string(output)
lines := strings.Split(str, "\n")
Expand All @@ -811,13 +805,13 @@ func (nvme *NVMe) GetNVMeDeviceData(path string) (string, string, error) {
return nguid, namespace, nil
}
}
return nguid, namespace, error
return nguid, namespace, err
}

// GetSessions queries information about NVMe sessions
func (nvme *NVMe) GetSessions() ([]NVMESession, error) {
exe := nvme.buildNVMeCommand([]string{"nvme", "list-subsys", "-o", "json"})
cmd := exec.Command(exe[0], exe[1:]...)
cmd := exec.Command(exe[0], exe[1:]...) // #nosec G204
output, err := cmd.Output()
if err != nil {
if isNoObjsExitCode(err) {
Expand Down
11 changes: 5 additions & 6 deletions gonvme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func TestLogoutLogoutTargets(t *testing.T) {

func TestGetInitiators(t *testing.T) {
reset()
var testdata = []struct {
testdata := []struct {
filename string
count int
}{
Expand All @@ -259,7 +259,6 @@ func TestGetInitiators(t *testing.T) {
t.Errorf("Expected %d initiators in %s, but got %d", tt.count, tt.filename, len(initiators))
}
}

}

func TestBuildNVMECommand(t *testing.T) {
Expand Down Expand Up @@ -335,7 +334,7 @@ func TestMockDiscoverNVMETCPTargets(t *testing.T) {
expected := 5
opts[MockNumberOfTCPTargets] = fmt.Sprintf("%d", expected)
c = NewMockNVMe(opts)
//c = mock
// c = mock
targets, err := c.DiscoverNVMeTCPTargets("1.1.1.1", true)
if err != nil {
t.Error(err.Error())
Expand All @@ -352,7 +351,7 @@ func TestMockDiscoverNVMEFCTargets(t *testing.T) {
expected := 5
opts[MockNumberOfFCTargets] = fmt.Sprintf("%d", expected)
c = NewMockNVMe(opts)
//c = mock
// c = mock
targets, err := c.DiscoverNVMeFCTargets("nn-0x11aaa111111a1a1a:pn-0x11aaa111111a1a1a", true)
if err != nil {
t.Error(err.Error())
Expand Down Expand Up @@ -606,7 +605,7 @@ func TestMockListNVMeDeviceAndNamespace(t *testing.T) {
expected := 5
opts[MockNumberOfNamespaceDevices] = fmt.Sprintf("%d", expected)
c = NewMockNVMe(opts)
//c = mock
// c = mock
targets, err := c.ListNVMeDeviceAndNamespace()
if err != nil {
t.Error(err.Error())
Expand Down Expand Up @@ -645,7 +644,7 @@ func TestMockListNVMeNamespaceID(t *testing.T) {
expected := 5
opts[MockNumberOfNamespaceDevices] = fmt.Sprintf("%d", expected)
c = NewMockNVMe(opts)
//c = mock
// c = mock
devices, _ := c.ListNVMeDeviceAndNamespace()
targets, err := c.ListNVMeNamespaceID(devices)
if err != nil {
Expand Down
34 changes: 17 additions & 17 deletions gonvme_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ package gonvme

// NVMeTarget defines an NVMe target
type NVMeTarget struct {
Portal string //traddr
TargetNqn string //subnqn
TrType string //trtype
AdrFam string //adrfam
SubType string //subtype
Treq string //treq
PortID string //portid
TrsvcID string //trsvcid
SecType string //sectype
TargetType string //trtype
HostAdr string //host_traddr
Portal string // traddr
TargetNqn string // subnqn
TrType string // trtype
AdrFam string // adrfam
SubType string // subtype
Treq string // treq
PortID string // portid
TrsvcID string // trsvcid
SecType string // sectype
TargetType string // trtype
HostAdr string // host_traddr
}

// NVMESessionState defines the NVMe connection state
Expand All @@ -44,18 +44,18 @@ const (
// NVMeTransportTypeFC - Placeholder for NVMe Transport type FC
NVMeTransportTypeFC = "fc"

//NVMESessionStateLive indicates the NVMe connection state as live
// NVMESessionStateLive indicates the NVMe connection state as live
NVMESessionStateLive NVMESessionState = "live"
//NVMESessionStateDeleting indicates the NVMe connection state as deleting
// NVMESessionStateDeleting indicates the NVMe connection state as deleting
NVMESessionStateDeleting NVMESessionState = "deleting"
//NVMESessionStateConnecting indicates the NVMe connection state as connecting
// NVMESessionStateConnecting indicates the NVMe connection state as connecting
NVMESessionStateConnecting NVMESessionState = "connecting"

//NVMETransportNameTCP indicates the NVMe protocol as tcp
// NVMETransportNameTCP indicates the NVMe protocol as tcp
NVMETransportNameTCP NVMETransportName = "tcp"
//NVMETransportNameFC indicates the NVMe protocol as fc
// NVMETransportNameFC indicates the NVMe protocol as fc
NVMETransportNameFC NVMETransportName = "fc"
//NVMETransportNameRDMA indicates the NVMe protocol as rdma
// NVMETransportNameRDMA indicates the NVMe protocol as rdma
NVMETransportNameRDMA NVMETransportName = "rdma"
)

Expand Down
Loading