|
| 1 | +package iscsi |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestDiscovery(t *testing.T) { |
| 6 | + execCommand = fakeExecCommand |
| 7 | + tests := map[string]struct { |
| 8 | + tgtPortal string |
| 9 | + iface string |
| 10 | + discoverySecret Secrets |
| 11 | + chapDiscovery bool |
| 12 | + wantErr bool |
| 13 | + mockedStdout string |
| 14 | + mockedExitStatus int |
| 15 | + }{ |
| 16 | + "DiscoverySuccess": { |
| 17 | + tgtPortal: "172.18.0.2:3260", |
| 18 | + iface: "default", |
| 19 | + chapDiscovery: false, |
| 20 | + mockedStdout: "172.18.0.2:3260,1 iqn.2016-09.com.openebs.jiva:store1\n", |
| 21 | + mockedExitStatus: 0, |
| 22 | + }, |
| 23 | + |
| 24 | + "ConnectionFailure": { |
| 25 | + tgtPortal: "172.18.0.2:3262", |
| 26 | + iface: "default", |
| 27 | + chapDiscovery: false, |
| 28 | + mockedStdout: `iscsiadm: cannot make connection to 172.18.0.2: Connection refused |
| 29 | +iscsiadm: cannot make connection to 172.18.0.2: Connection refused |
| 30 | +iscsiadm: connection login retries (reopen_max) 5 exceeded |
| 31 | +iscsiadm: Could not perform SendTargets discovery: encountered connection failure\n`, |
| 32 | + mockedExitStatus: 4, |
| 33 | + wantErr: true, |
| 34 | + }, |
| 35 | + |
| 36 | + "ChapEntrySuccess": { |
| 37 | + tgtPortal: "172.18.0.2:3260", |
| 38 | + iface: "default", |
| 39 | + chapDiscovery: true, |
| 40 | + discoverySecret: Secrets{ |
| 41 | + UserNameIn: "dummyuser", |
| 42 | + PasswordIn: "dummypass", |
| 43 | + }, |
| 44 | + mockedStdout: "172.18.0.2:3260,1 iqn.2016-09.com.openebs.jiva:store1\n", |
| 45 | + mockedExitStatus: 0, |
| 46 | + }, |
| 47 | + |
| 48 | + "ChapEntryFailure": { |
| 49 | + tgtPortal: "172.18.0.2:3260", |
| 50 | + iface: "default", |
| 51 | + discoverySecret: Secrets{ |
| 52 | + UserNameIn: "dummyuser", |
| 53 | + PasswordIn: "dummypass", |
| 54 | + }, |
| 55 | + chapDiscovery: true, |
| 56 | + mockedStdout: `iscsiadm: Login failed to authenticate with target |
| 57 | +iscsiadm: discovery login to 172.18.0.2 rejected: initiator error (02/01), non-retryable, giving up |
| 58 | +iscsiadm: Could not perform SendTargets discovery.\n`, |
| 59 | + mockedExitStatus: 4, |
| 60 | + wantErr: true, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + for name, tt := range tests { |
| 65 | + t.Run(name, func(t *testing.T) { |
| 66 | + mockedExitStatus = tt.mockedExitStatus |
| 67 | + mockedStdout = tt.mockedStdout |
| 68 | + err := Discovery(tt.tgtPortal, tt.iface, tt.discoverySecret, tt.chapDiscovery) |
| 69 | + if (err != nil) != tt.wantErr { |
| 70 | + t.Errorf("Discovery() error = %v, wantErr %v", err, tt.wantErr) |
| 71 | + return |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestCreateDBEntry(t *testing.T) { |
| 78 | + execCommand = fakeExecCommand |
| 79 | + tests := map[string]struct { |
| 80 | + tgtPortal string |
| 81 | + tgtIQN string |
| 82 | + iface string |
| 83 | + discoverySecret Secrets |
| 84 | + sessionSecret Secrets |
| 85 | + chapDiscovery bool |
| 86 | + wantErr bool |
| 87 | + mockedStdout string |
| 88 | + mockedExitStatus int |
| 89 | + }{ |
| 90 | + "CreateDBEntrySuccess": { |
| 91 | + tgtPortal: "192.168.1.107:3260", |
| 92 | + tgtIQN: "iqn.2010-10.org.openstack:volume-eb393993-73d0-4e39-9ef4-b5841e244ced", |
| 93 | + iface: "default", |
| 94 | + chapDiscovery: false, |
| 95 | + mockedStdout: nodeDB, |
| 96 | + mockedExitStatus: 0, |
| 97 | + }, |
| 98 | + "CreateDBEntryWithChapDiscoverySuccess": { |
| 99 | + tgtPortal: "192.168.1.107:3260", |
| 100 | + tgtIQN: "iqn.2010-10.org.openstack:volume-eb393993-73d0-4e39-9ef4-b5841e244ced", |
| 101 | + iface: "default", |
| 102 | + discoverySecret: Secrets{ |
| 103 | + UserNameIn: "dummyuser", |
| 104 | + PasswordIn: "dummypass", |
| 105 | + SecretsType: "chap", |
| 106 | + }, |
| 107 | + sessionSecret: Secrets{ |
| 108 | + UserNameIn: "dummyuser", |
| 109 | + PasswordIn: "dummypass", |
| 110 | + SecretsType: "chap", |
| 111 | + }, |
| 112 | + chapDiscovery: true, |
| 113 | + mockedStdout: nodeDB, |
| 114 | + mockedExitStatus: 0, |
| 115 | + }, |
| 116 | + "CreateDBEntryWithChapDiscoveryFailure": { |
| 117 | + tgtPortal: "172.18.0.2:3260", |
| 118 | + tgtIQN: "iqn.2016-09.com.openebs.jiva:store1", |
| 119 | + iface: "default", |
| 120 | + chapDiscovery: true, |
| 121 | + mockedStdout: "iscsiadm: No records found\n", |
| 122 | + mockedExitStatus: 21, |
| 123 | + wantErr: true, |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + for name, tt := range tests { |
| 128 | + t.Run(name, func(t *testing.T) { |
| 129 | + mockedExitStatus = tt.mockedExitStatus |
| 130 | + mockedStdout = tt.mockedStdout |
| 131 | + err := CreateDBEntry(tt.tgtIQN, tt.tgtPortal, tt.iface, tt.discoverySecret, tt.sessionSecret, tt.chapDiscovery) |
| 132 | + if (err != nil) != tt.wantErr { |
| 133 | + t.Errorf("CreateDBEntry() error = %v, wantErr %v", err, tt.wantErr) |
| 134 | + return |
| 135 | + } |
| 136 | + }) |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments