Skip to content

Commit

Permalink
Increase unit test coverage. (#65)
Browse files Browse the repository at this point in the history
donatwork authored Jan 22, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 86bd45e commit e68478e
Showing 7 changed files with 308 additions and 29 deletions.
20 changes: 6 additions & 14 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -8,23 +8,15 @@
# order is alphabetical for easier maintenance.
#
# Aaron Tye (atye)
# Adarsh Kumar Yadav (adarsh-dell)
# Akshay Saini (AkshaySainiDell)
# Alik Saring (alikdell)
# Boya Murthy (boyamurthy)
# Bahubali Jain (bpjain2004)
# Florian Coulombel (coulof)
# Utkarsh Dubey (delldubey)
# Francis Nijay (francis-nijay)
# Don Khan (donatwork)
# Harish P (harishp8889)
# Alexander Hoppe (hoppea2)
# Pooja Prasannakumar (kumarp20)
# Nida Taranum (nidtara)
# Prasanna Muthukumaraswamy (prablr79)
# Trevor Dawe (tdawe)
# Santhosh shekarappa (santhoshhs10)
# Yamunadevi Shanmugam (shanmydell)
# Sushma T S (tssushma)
# Jooseppi Luna (jooseppi-luna)
# Shayna Finocchiaro (shaynafinocchiaro)
# Luna Xu (xuluna)
# Trevor Dawe (tdawe)

# for all files:
* @boyamurthy @bpjain2004 @coulof @delldubey @francis-nijay @harishp8889 @hoppea2 @kumarp20 @nidtara @prablr79 @tdawe @santhoshhs10 @shanmydell @tssushma @alikdell @atye @shaynafinocchiaro @xuluna
* @atye @adarsh-dell @AkshaySainiDell @alikdell @bpjain2004 @donatwork @harishp8889 @jooseppi-luna @shaynafinocchiaro @tdawe
238 changes: 238 additions & 0 deletions gonvme_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
/*
* Copyright © 2025 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 gonvme

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewMockNVMe(t *testing.T) {
opts := map[string]string{
"numberOfInitiators": "1",
"numberOfTCPTargets": "2",
"numberOfFCTargets": "3",
"numberOfSession": "10",
"numberOfNamespaceDevices": "5",
}

nvme := NewMockNVMe(opts)
assert.NotNil(t, nvme)
assert.Equal(t, opts, nvme.options)
}

func TestMockedDiscoverNVMeTCPTargets(t *testing.T) {
nvme := NewMockNVMe(map[string]string{
MockNumberOfTCPTargets: "2",
})
GONVMEMock.InduceDiscoveryError = false
targets, err := nvme.DiscoverNVMeTCPTargets("1.1.1.1", false)

if len(targets) != 2 {
t.Errorf("Expected 2 targets, got %d", len(targets))
}

if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}

func TestMockedDiscoverNVMeTCPTargetsZero(t *testing.T) {
nvme := NewMockNVMe(map[string]string{
MockNumberOfTCPTargets: "0",
})
GONVMEMock.InduceDiscoveryError = false
targets, _ := nvme.DiscoverNVMeTCPTargets("1.1.1.1", false)

assert.Len(t, targets, 1)
}

func TestMockedDiscoverNVMeTCPTargetsError(t *testing.T) {
nvme := NewMockNVMe(map[string]string{
MockNumberOfTCPTargets: "2",
})
GONVMEMock.InduceDiscoveryError = true
_, err := nvme.DiscoverNVMeTCPTargets("1.1.1.1", false)
assert.NotNil(t, err)
}

func TestMockedDiscoverNVMeFCTargets(t *testing.T) {
nvme := NewMockNVMe(map[string]string{
MockNumberOfFCTargets: "2",
})
GONVMEMock.InduceDiscoveryError = false

targets, err := nvme.DiscoverNVMeFCTargets("nn-0x11aaa11111111a11:pn-0x11aaa11111111a11", false)

if len(targets) != 2 {
t.Errorf("Expected 2 targets, got %d", len(targets))
}

if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}

func TestMockedDiscoverNVMeFCTargetsZero(t *testing.T) {
nvme := NewMockNVMe(map[string]string{
MockNumberOfFCTargets: "0",
})
GONVMEMock.InduceDiscoveryError = false
targets, _ := nvme.DiscoverNVMeFCTargets("nn-0x11aaa11111111a11:pn-0x11aaa11111111a11", false)
assert.Len(t, targets, 1)
}

func TestMockedDiscoverNVMeFCTargetsError(t *testing.T) {
nvme := NewMockNVMe(map[string]string{
MockNumberOfFCTargets: "2",
})
GONVMEMock.InduceDiscoveryError = true
_, err := nvme.DiscoverNVMeFCTargets("nn-0x11aaa11111111a11:pn-0x11aaa11111111a11", false)
assert.NotNil(t, err)
}

func TestMockedGetInitiators(t *testing.T) {
nvme := NewMockNVMe(map[string]string{
MockNumberOfInitiators: "2",
})
initiators, err := nvme.GetInitiators("")
assert.Nil(t, err)
assert.Len(t, initiators, 2)
}

func TestMockedGetInitiatorsZero(t *testing.T) {
nvme := NewMockNVMe(map[string]string{
MockNumberOfInitiators: "0",
})
initiators, err := nvme.GetInitiators("")
assert.Nil(t, err)
assert.Len(t, initiators, 1)
}

func TestMockedGetInitiatorsError(t *testing.T) {
nvme := NewMockNVMe(map[string]string{
MockNumberOfInitiators: "2",
})
GONVMEMock.InduceInitiatorError = true
_, err := nvme.GetInitiators("")
assert.NotNil(t, err)
}

func TestMockedNVMeTCPConnect(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
err := nvme.NVMeTCPConnect(NVMeTarget{}, false)
assert.Nil(t, err)
}

func TestMockedNVMeTCPConnectError(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
GONVMEMock.InduceTCPLoginError = true
err := nvme.NVMeTCPConnect(NVMeTarget{}, false)
assert.NotNil(t, err)
}

func TestMockedNVMeFCConnect(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
err := nvme.NVMeFCConnect(NVMeTarget{}, false)
assert.Nil(t, err)
}

func TestMockedNVMeFCConnectError(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
GONVMEMock.InduceFCLoginError = true
err := nvme.NVMeFCConnect(NVMeTarget{}, false)
assert.NotNil(t, err)
}

func TestMockedNVMeDisconnect(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
err := nvme.NVMeDisconnect(NVMeTarget{})
assert.Nil(t, err)
}

func TestMockedNVMeDisconnectError(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
GONVMEMock.InduceLogoutError = true
err := nvme.NVMeDisconnect(NVMeTarget{})
assert.NotNil(t, err)
}

func TestMockedGetNVMeDeviceData(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
_, _, err := nvme.GetNVMeDeviceData("")
assert.Nil(t, err)
}

func TestMockedGetNVMeDeviceDataError(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
GONVMEMock.InducedNVMeDeviceDataError = true
_, _, err := nvme.GetNVMeDeviceData("")
assert.NotNil(t, err)
}

func TestMockedListNVMeNamespaceID(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
_, err := nvme.ListNVMeNamespaceID(nil)
assert.Nil(t, err)
}

func TestMockedListNVMeNamespaceIDError(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
GONVMEMock.InducedNVMeNamespaceIDError = true
_, err := nvme.ListNVMeNamespaceID(nil)
assert.NotNil(t, err)
}

func TestMockedListNVMeDeviceAndNamespace(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
_, err := nvme.ListNVMeDeviceAndNamespace()
assert.Nil(t, err)
}

func TestMockedListNVMeDeviceAndNamespaceError(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
GONVMEMock.InducedNVMeDeviceAndNamespaceError = true
_, err := nvme.ListNVMeDeviceAndNamespace()
assert.NotNil(t, err)
}

func TestMockedGetSessions(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
_, err := nvme.GetSessions()
assert.Nil(t, err)
}

func TestMockedGetSessionsError(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
GONVMEMock.InduceGetSessionsError = true
_, err := nvme.GetSessions()
assert.NotNil(t, err)
}

func TestMockedDeviceRescan(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
GONVMEMock.InduceGetSessionsError = false
err := nvme.DeviceRescan("")
assert.Nil(t, err)
}

func TestMockedDeviceRescanError(t *testing.T) {
nvme := NewMockNVMe(map[string]string{})
GONVMEMock.InduceGetSessionsError = true
err := nvme.DeviceRescan("")
assert.NotNil(t, err)
}
35 changes: 35 additions & 0 deletions gonvme_tcp_fc_test.go
Original file line number Diff line number Diff line change
@@ -660,6 +660,21 @@ func TestNVMeTCPConnect(t *testing.T) {
},
false,
},
{
"successfully connects duplicate",
NVMeTarget{
Portal: "1.1.1.1",
TargetNqn: "nqn.1988-11.com.mock:00:a1a1a1a111a1111A111A",
},
true,
func(_ string, _ ...string) command {
return &mockCommand{
startErr: nil,
waitErr: nil,
}
},
false,
},
{
"error connecting",
NVMeTarget{
@@ -730,6 +745,21 @@ func TestNVMeFCConnect(t *testing.T) {
},
false,
},
{
"successfully connects duplicate",
NVMeTarget{
Portal: "1.1.1.1",
TargetNqn: "nqn.1988-11.com.mock:00:a1a1a1a111a1111A111A",
},
true,
func(_ string, _ ...string) command {
return &mockCommand{
startErr: nil,
waitErr: nil,
}
},
false,
},
{
"error connecting",
NVMeTarget{
@@ -867,3 +897,8 @@ func TestDeviceRescan(t *testing.T) {
}
}
}

func TestIsNoObjsExitCode(t *testing.T) {
r := isNoObjsExitCode(nil)
assert.False(t, r)
}
12 changes: 6 additions & 6 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ import (
var logger Logger

func init() {
logger = &DummyLogger{}
logger = &ConsoleLogger{}
}

// SetLogger - set custom logger
@@ -40,21 +40,21 @@ type Logger interface {
Error(ctx context.Context, format string, args ...interface{})
}

// DummyLogger - placeholder for default logger
type DummyLogger struct{}
// ConsoleLogger - placeholder for default logger
type ConsoleLogger struct{}

// Info - log info using default logger
func (dl *DummyLogger) Info(_ context.Context, format string, args ...interface{}) {
func (dl *ConsoleLogger) Info(_ context.Context, format string, args ...interface{}) {
log.Print("INFO: " + fmt.Sprintf(format, args...))
}

// Debug - log debug using default logger
func (dl *DummyLogger) Debug(_ context.Context, format string, args ...interface{}) {
func (dl *ConsoleLogger) Debug(_ context.Context, format string, args ...interface{}) {
log.Print("DEBUG: " + fmt.Sprintf(format, args...))
}

// Error - log error using default logger
func (dl *DummyLogger) Error(_ context.Context, format string, args ...interface{}) {
func (dl *ConsoleLogger) Error(_ context.Context, format string, args ...interface{}) {
log.Print("ERROR: " + fmt.Sprintf(format, args...))
}

13 changes: 11 additions & 2 deletions internal/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright © 2024 Dell Inc. or its subsidiaries. All Rights Reserved.
* Copyright © 2024-2025 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.
@@ -21,6 +21,15 @@ import (
"testing"
)

func TestSetLogger(_ *testing.T) {
oldLogger := logger
defer func() {
logger = oldLogger
}()
logger = &ConsoleLogger{}
SetLogger(logger)
}

func TestLogger(t *testing.T) {
tests := []struct {
name string
@@ -45,7 +54,7 @@ func TestLogger(t *testing.T) {
defer func() {
logger = oldLogger
}()
logger = &DummyLogger{}
logger = &ConsoleLogger{}
tt.fn(context.Background(), "test")
})
}
Loading

0 comments on commit e68478e

Please sign in to comment.