From 0724268a2840473998061458db5656aa86e4def0 Mon Sep 17 00:00:00 2001 From: Surya Gupta <109594002+suryagupta4@users.noreply.github.com> Date: Fri, 15 Dec 2023 14:03:04 +0530 Subject: [PATCH] Fix golangci-lint warnings (#31) Co-authored-by: Yamunadevi N Shanmugam <82038610+shanmydell@users.noreply.github.com> --- gonvme.go | 5 ++--- gonvme_mock.go | 47 +++++++++++++++++---------------------- gonvme_tcp_fc.go | 32 +++++++++++--------------- gonvme_test.go | 11 +++++---- gonvme_types.go | 34 ++++++++++++++-------------- internal/logger/logger.go | 6 ++--- internal/tracer/tracer.go | 2 +- 7 files changed, 61 insertions(+), 76 deletions(-) diff --git a/gonvme.go b/gonvme.go index 0897f49..70a682f 100644 --- a/gonvme.go +++ b/gonvme.go @@ -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) @@ -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 diff --git a/gonvme_mock.go b/gonvme_mock.go index 8a2dd85..0a332ac 100644 --- a/gonvme_mock.go +++ b/gonvme_mock.go @@ -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 { @@ -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") } @@ -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") } @@ -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") } @@ -157,8 +154,7 @@ 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") } @@ -166,8 +162,7 @@ func (nvme *MockNVMe) nvmeTCPConnect(target NVMeTarget, duplicateConnect bool) e 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") } @@ -175,8 +170,7 @@ func (nvme *MockNVMe) nvmeFCConnect(target NVMeTarget, duplicateConnect bool) er 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") } @@ -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") } @@ -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") } @@ -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") } diff --git a/gonvme_tcp_fc.go b/gonvme_tcp_fc.go index 51fb20d..9ed31e7 100644 --- a/gonvme_tcp_fc.go +++ b/gonvme_tcp_fc.go @@ -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) @@ -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 -s 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 { @@ -271,7 +270,7 @@ func (nvme *NVMe) discoverNVMeFCTargets(targetAddress string, login bool) ([]NVM // host_traddr = nn-:pn- 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 { @@ -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{} @@ -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) } @@ -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() @@ -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() @@ -591,7 +588,7 @@ func (nvme *NVMe) nvmeDisconnect(target NVMeTarget) error { // nvme disconnect is done via the nvme cli // nvme disconnect -n 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() @@ -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} @@ -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 { @@ -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] @@ -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 @@ -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 @@ -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") @@ -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) { diff --git a/gonvme_test.go b/gonvme_test.go index 0324d70..4970127 100644 --- a/gonvme_test.go +++ b/gonvme_test.go @@ -239,7 +239,7 @@ func TestLogoutLogoutTargets(t *testing.T) { func TestGetInitiators(t *testing.T) { reset() - var testdata = []struct { + testdata := []struct { filename string count int }{ @@ -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) { @@ -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()) @@ -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()) @@ -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()) @@ -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 { diff --git a/gonvme_types.go b/gonvme_types.go index 698d736..aabe52f 100644 --- a/gonvme_types.go +++ b/gonvme_types.go @@ -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 @@ -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" ) diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 77ff869..3e9db55 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -44,17 +44,17 @@ type Logger interface { type DummyLogger struct{} // Info - log info using default logger -func (dl *DummyLogger) Info(ctx context.Context, format string, args ...interface{}) { +func (dl *DummyLogger) Info(_ context.Context, format string, args ...interface{}) { log.Print("INFO: " + fmt.Sprintf(format, args...)) } // Debug - log debug using default logger -func (dl *DummyLogger) Debug(ctx context.Context, format string, args ...interface{}) { +func (dl *DummyLogger) Debug(_ context.Context, format string, args ...interface{}) { log.Print("DEBUG: " + fmt.Sprintf(format, args...)) } // Error - log error using default logger -func (dl *DummyLogger) Error(ctx context.Context, format string, args ...interface{}) { +func (dl *DummyLogger) Error(_ context.Context, format string, args ...interface{}) { log.Print("ERROR: " + fmt.Sprintf(format, args...)) } diff --git a/internal/tracer/tracer.go b/internal/tracer/tracer.go index adc6e8f..ece4177 100644 --- a/internal/tracer/tracer.go +++ b/internal/tracer/tracer.go @@ -49,7 +49,7 @@ func TraceFuncCall(ctx context.Context, funcName string) func() { type DummyTracer struct{} // Trace - default trace -func (dl *DummyTracer) Trace(ctx context.Context, format string, args ...interface{}) { +func (dl *DummyTracer) Trace(_ context.Context, format string, args ...interface{}) { fmt.Printf(format+"\n", args...) }