Skip to content

Commit 60e335a

Browse files
committed
oci-iscsi-config auto tests
1 parent 3d4ecd5 commit 60e335a

File tree

13 files changed

+467
-213
lines changed

13 files changed

+467
-213
lines changed

lib/oci_utils/impl/oci-iscsi-config-main.py

Lines changed: 209 additions & 161 deletions
Large diffs are not rendered by default.

lib/oci_utils/impl/sudo_utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ def call(cmd, log_output=True):
5959
_c = _prepare_command(cmd)
6060
try:
6161
if _logger.isEnabledFor(logging.DEBUG):
62-
_logger.debug('Executing [%s]' % ' '.join(_c))
62+
_logger.debug('Executing [%s]', ' '.join(_c))
6363
cp = subprocess.run(_c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
6464
if cp.returncode != 0 and log_output:
65-
_logger.debug("execution failed: ec=%s, output=[%s], stderr=[%s] " % (cp.returncode, cp.stdout, cp.stderr))
65+
_logger.debug("execution failed: ec=%s, output=[%s], stderr=[%s] ", cp.returncode, cp.stdout, cp.stderr)
6666
return cp.returncode
6767
except OSError:
6868
return 404
@@ -97,7 +97,7 @@ def call_output(cmd, log_output=True):
9797
if log_output:
9898
if _logger.isEnabledFor(logging.DEBUG):
9999
# pylint: disable=logging-not-lazy,logging-format-interpolation
100-
_logger.debug("Error execeuting {}: {}\n{}\n".format(_c, e.returncode, e.output))
100+
_logger.debug("Error executing {}: {}\n{}\n".format(_c, e.returncode, e.output.decode('utf-8')))
101101
return None
102102

103103

@@ -207,7 +207,7 @@ def write_to_file(path, content):
207207
stderr=subprocess.PIPE,
208208
stdin=subprocess.PIPE).communicate(content.encode())
209209
if err:
210-
_logger.debug("Error writing content to file: %s" % err)
210+
_logger.debug("Error writing content to file: %s", err)
211211
return 1
212212
return 0
213213

@@ -227,7 +227,7 @@ def create_file(path, mode=None):
227227
-------
228228
The return code fo the cat(1) command.
229229
"""
230-
_logger.debug("creating file : %s" % path)
230+
_logger.debug("creating file : %s", path)
231231
res = call([TOUCH_CMD, path])
232232
if res == 0 and mode is not None:
233233
res = set_file_mode(path, mode)
@@ -249,5 +249,5 @@ def set_file_mode(path, mode=None):
249249
-------
250250
The return code fo the chmod(1) command.
251251
"""
252-
_logger.debug("applying mode %s to file %s" % (mode, path))
252+
_logger.debug("applying mode %s to file %s", mode, path)
253253
return call([CHMOD_CMD, mode, path])

lib/oci_utils/impl/virt/virt_utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ def get_domains_name():
2828
All domains names as list of string.
2929
"""
3030
ret = []
31-
domains = sudo_utils.call_output(
32-
[VIRSH_CMD, 'list', '--name', '--all']).splitlines()
33-
34-
for d in domains:
35-
if len(d) > 0:
36-
ret.append(d)
31+
domains = sudo_utils.call_output([VIRSH_CMD, 'list', '--name', '--all'])
32+
if domains is not None:
33+
for d in domains.decode('utf-8').splitlines():
34+
if len(d) > 0:
35+
ret.append(d)
3736

3837
return ret
3938

lib/oci_utils/vnicutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def _is_intf_excluded(self, interface):
261261
Checks if interface name, VNIC ocid or ip addr is part of excluded items
262262
"""
263263

264-
for excl in self.vnic_info.get('exclude',()):
264+
for excl in self.vnic_info.get('exclude', ()):
265265
if excl in (interface['IFACE'], interface['VNIC'], interface['ADDR']):
266266
return True
267267
return False

tests/data/properties.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ oci-public-ip = /bin/oci-public-ip
66
oci-network-inspector = /bin/oci-network-inspector
77
oci-network-config = /bin/oci-network-config
88
oci-utils-config-helper = /usr/libexec/oci-utils-config-helper
9-
109
oci-image-migrate = /bin/oci-image-migrate
1110
oci-image-migrate-upload = /bin/oci-image-migrate-upload
1211
oci-image-migrate-import = /bin/oci-image-migrate-import
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[DEFAULT]
2+
volume-name = auto-test-volume
3+
volume-size = 51
4+
compartment-name = ImageDev
5+
waittime = 10

tests/test_cache.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,14 @@ def test_get_newer(self):
8484
-------
8585
No return value.
8686
"""
87-
self.assertEqual(get_newer(self.file1, self.file2), self.file2,
88-
'expected self.file2 to be the newer')
89-
self.assertEqual(get_newer(self.file2, self.file1), self.file2,
90-
'expected self.file2 to be the newer')
91-
self.assertEqual(get_newer(self.file1, None), self.file1,
92-
'get_newer(filename, None) != filename')
93-
self.assertEqual(get_newer(None, self.file1), self.file1,
94-
'get_newer(None, filename) != filename')
95-
self.assertEqual(get_newer(self.file1, self.nofile), self.file1,
96-
'get_newer(filename, \'no existing file\') != filename')
97-
self.assertEqual(get_newer(self.nofile, self.file2), self.file2,
98-
'get_newer(\'no existing file\', filename, ) != filename')
99-
self.assertEqual(get_newer(self.nofile, None), None,
100-
'get_newer(\'no existing file\', None) != None')
101-
self.assertEqual(get_newer(None, None), None,
102-
'get_newer(None, None) != None')
87+
self.assertEqual(get_newer(self.file1, self.file2), self.file2, 'expected self.file2 to be the newer')
88+
self.assertEqual(get_newer(self.file2, self.file1), self.file2, 'expected self.file2 to be the newer')
89+
self.assertEqual(get_newer(self.file1, None), self.file1, 'get_newer(filename, None) != filename')
90+
self.assertEqual(get_newer(None, self.file1), self.file1, 'get_newer(None, filename) != filename')
91+
self.assertEqual(get_newer(self.file1, self.nofile), self.file1, 'get_newer(filename, \'no existing file\') != filename')
92+
self.assertEqual(get_newer(self.nofile, self.file2), self.file2, 'get_newer(\'no existing file\', filename, ) != filename')
93+
self.assertEqual(get_newer(self.nofile, None), None, 'get_newer(\'no existing file\', None) != None')
94+
self.assertEqual(get_newer(None, None), None, 'get_newer(None, None) != None')
10395

10496
def test_load_cache(self):
10597
"""
@@ -131,7 +123,7 @@ def test_load_cache(self):
131123

132124
def test_write_cache(self):
133125
"""
134-
Test cach.write_cache().
126+
Test cache.write_cache().
135127
136128
Returns
137129
-------

tests/test_cli_iscsi_config.py

Lines changed: 212 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,63 @@
33
# at http://oss.oracle.com/licenses/upl.
44

55
import os
6+
import re
67
import subprocess
8+
import time
79
import unittest
10+
import uuid
811
from tools.oci_test_case import OciTestCase
12+
from tools.decorators import (skipUnlessOCI, skipUnlessRoot)
13+
14+
def _get_volume_data(volume_data):
15+
"""
16+
Formats the data list retrieved fr m show as a dictionary.
17+
18+
Parameters
19+
----------
20+
volume_data: list
21+
22+
Returns
23+
-------
24+
dict: dictionary
25+
{ display_name : {'ocid' : OCID, 'iqn': iqn} }
26+
"""
27+
singlespace = re.compile("\s*,\s*|\s+$")
28+
volume_data_dict = dict()
29+
ind = 0
30+
for y in volume_data:
31+
if y.startswith('Currently attached iSCSI'):
32+
break
33+
ind += 1
34+
while ind < len(volume_data) - 1:
35+
ind += 1
36+
v = volume_data[ind]
37+
if v.startswith('Target iqn'):
38+
iqn = singlespace.sub(' ', v.split(' ')[1]).strip()
39+
cnt = 0
40+
ind += 1
41+
for y in volume_data[ind:]:
42+
if 'Volume name' in y:
43+
display_name = singlespace.sub(' ', y.split(': ', 1)[1]).strip()
44+
cnt += 1
45+
elif 'Volume OCID' in y:
46+
ocid = singlespace.sub(' ', y.split(': ', 1)[1]).strip()
47+
cnt += 1
48+
if cnt == 2:
49+
volume_data_dict[display_name] = {'target': iqn, 'ocid': ocid}
50+
break
51+
else:
52+
ind += 1
53+
if v.startswith('Volume '):
54+
display_name = v.split(' ')[1]
55+
ind += 1
56+
for y in volume_data[ind:]:
57+
if 'OCID' in y:
58+
ocid = singlespace.sub(' ', y.split(': ', 1)[1]).strip()
59+
volume_data_dict[display_name] = {'target': None, 'ocid': ocid}
60+
break
61+
ind += 1
62+
return volume_data_dict
963

1064

1165
class TestCliOciIscsiConfig(OciTestCase):
@@ -25,11 +79,61 @@ def setUp(self):
2579
unittest.SkipTest
2680
If the ISCSI_CONFIG file does not exist.
2781
"""
28-
super(TestCliOciIscsiConfig, self).setUp()
82+
super().setUp()
2983
self.iscsi_config_path = self.properties.get_property('oci-iscsi-config-path')
3084
if not os.path.exists(self.iscsi_config_path):
31-
raise unittest.SkipTest("%s not present" %
32-
self.iscsi_config_path)
85+
raise unittest.SkipTest("%s not present" % self.iscsi_config_path)
86+
self.volume_name = uuid.uuid4().hex
87+
try:
88+
self.waittime = int(self.properties.get_property('waittime'))
89+
except Exception as e:
90+
self.waittime = 10
91+
try:
92+
self.volume_size = self.properties.get_property('volume-size')
93+
except Exception as e:
94+
self.volume_size = '60'
95+
try:
96+
self.compartment_name = self.properties.get_property('compartment-name')
97+
except Exception as e:
98+
self.compartment_name = 'ImageDev'
99+
100+
@staticmethod
101+
def _get_ocid(create_data, display_name):
102+
"""
103+
Find the ocid of a volume.
104+
105+
Parameters
106+
----------
107+
create_data: list
108+
Block volume data.
109+
display_name: str
110+
Display name of the volume.
111+
112+
Returns
113+
-------
114+
str: the ocid
115+
"""
116+
vol_dict = _get_volume_data(create_data)[display_name]
117+
return vol_dict['ocid']
118+
119+
@staticmethod
120+
def _get_iqn(create_data, display_name):
121+
"""
122+
Find the ocid of a volume.
123+
124+
Parameters
125+
----------
126+
create_data: list
127+
Block volume data.
128+
display_name: str
129+
Display name of the volume.
130+
131+
Returns
132+
-------
133+
str: the ocid
134+
"""
135+
vol_dict = _get_volume_data(create_data)[display_name]
136+
return vol_dict['target']
33137

34138
def test_display_help(self):
35139
"""
@@ -40,10 +144,16 @@ def test_display_help(self):
40144
No return value.
41145
"""
42146
try:
43-
_ = subprocess.check_output([self.iscsi_config_path,
44-
'--help'])
147+
_ = subprocess.check_output([self.iscsi_config_path, '--help'])
148+
_ = subprocess.check_output([self.iscsi_config_path, 'sync', '--help'])
149+
_ = subprocess.check_output([self.iscsi_config_path, 'usage', '--help'])
150+
_ = subprocess.check_output([self.iscsi_config_path, 'show', '--help'])
151+
_ = subprocess.check_output([self.iscsi_config_path, 'create', '--help'])
152+
_ = subprocess.check_output([self.iscsi_config_path, 'attach', '--help'])
153+
_ = subprocess.check_output([self.iscsi_config_path, 'detach', '--help'])
154+
_ = subprocess.check_output([self.iscsi_config_path, 'destroy', '--help'])
45155
except Exception as e:
46-
self.fail('Execution has failed: %s' % str(e))
156+
self.fail('oci-iscsi-config --help has failed: %s' % str(e))
47157

48158
def test_show_no_check(self):
49159
"""
@@ -54,10 +164,10 @@ def test_show_no_check(self):
54164
No return value.
55165
"""
56166
try:
57-
_ = subprocess.check_output([self.iscsi_config_path,
58-
'--show'])
167+
_ = subprocess.check_output([self.iscsi_config_path, '--show'])
168+
_ = subprocess.check_output([self.iscsi_config_path, 'show'])
59169
except Exception as e:
60-
self.fail('Execution has failed: %s' % str(e))
170+
self.fail('oci-iscsi-config show has failed: %s' % str(e))
61171

62172
def test_show_all_no_check(self):
63173
"""
@@ -68,7 +178,97 @@ def test_show_all_no_check(self):
68178
No return value.
69179
"""
70180
try:
71-
_ = subprocess.check_output([self.iscsi_config_path,
72-
'--show', '--all'])
181+
_ = subprocess.check_output([self.iscsi_config_path, '--show', '--all'])
182+
_ = subprocess.check_output([self.iscsi_config_path, 'show', '--all'])
183+
except Exception as e:
184+
self.fail('oci-iscsi-config show --all has failed: %s' % str(e))
185+
186+
@skipUnlessOCI()
187+
@skipUnlessRoot()
188+
def test_create_destroy(self):
189+
"""
190+
Test block volume creation and destruction
191+
192+
Returns
193+
-------
194+
No return value.
195+
"""
196+
try:
197+
create_volume_data = subprocess.check_output([self.iscsi_config_path,
198+
'create',
199+
'--size', self.volume_size,
200+
'--volume-name', self.volume_name,
201+
'--show']).decode('utf-8').splitlines()
202+
# print('\nvolume created: %s' % create_volume_data)
203+
time.sleep(self.waittime)
204+
new_ocid = self._get_ocid(create_volume_data, self.volume_name)
205+
destroy_volume_data = subprocess.check_output([self.iscsi_config_path,
206+
'destroy',
207+
'--ocids', new_ocid,
208+
'--yes',
209+
'--show']).decode('utf-8').splitlines()
210+
# print('\nvolume %s destroyed: %s' % (self.volume_name, destroy_volume_data))
211+
except Exception as e:
212+
self.fail('oci-iscsi-config create/destroy has failed: %s' % str(e))
213+
214+
@skipUnlessOCI()
215+
@skipUnlessRoot()
216+
def test_attach_detach(self):
217+
"""
218+
Test block device attach and detach.
219+
220+
Returns
221+
-------
222+
No return value
223+
"""
224+
try:
225+
create_volume_data = subprocess.check_output([self.iscsi_config_path,
226+
'create',
227+
'--size', self.volume_size,
228+
'--volume-name', self.volume_name,
229+
'--show']).decode('utf-8').splitlines()
230+
# print('\nvolume created: %s' % create_volume_data)
231+
time.sleep(self.waittime)
232+
new_ocid = self._get_ocid(create_volume_data, self.volume_name)
233+
attach_volume_data = subprocess.check_output([self.iscsi_config_path,
234+
'attach',
235+
'--iqns', new_ocid,
236+
'--show']).decode('utf-8').splitlines()
237+
attach_volume_data_show = subprocess.check_output([self.iscsi_config_path,
238+
'show']).decode('utf-8').splitlines()
239+
# print('\nvolume attached: %s' % attach_volume_data_show)
240+
time.sleep(self.waittime)
241+
new_iqn = self._get_iqn(attach_volume_data_show, self.volume_name)
242+
detach_volume_data = subprocess.check_output([self.iscsi_config_path,
243+
'detach',
244+
'--iqns', new_iqn]).decode('utf-8').splitlines()
245+
# print('\nvolume detached: %s' % detach_volume_data)
246+
time.sleep(self.waittime)
247+
destroy_volume_data = subprocess.check_output([self.iscsi_config_path,
248+
'destroy',
249+
'--ocids', new_ocid,
250+
'--yes',
251+
'--show']).decode('utf-8').splitlines()
252+
# print('\nvolume %s destroyed: %s' % (self.volume_name, destroy_volume_data))
253+
except Exception as e:
254+
self.fail('oci-iscsi-config attach/detach has failed: %s' % str(e))
255+
256+
@skipUnlessOCI()
257+
@skipUnlessRoot()
258+
def test_sync(self):
259+
"""
260+
Test volume attach.
261+
262+
Returns
263+
-------
264+
No return value.
265+
"""
266+
try:
267+
sync_data = subprocess.check_output([self.iscsi_config_path,
268+
'sync',
269+
'--apply',
270+
'--yes']).decode('utf-8').splitlines()
271+
# print('\nvolumes synced.')
73272
except Exception as e:
74-
self.fail('Execution has failed: %s' % str(e))
273+
self.fail('oci-iscsi-config sync has failed: %s' % str(e))
274+

0 commit comments

Comments
 (0)