3
3
# at http://oss.oracle.com/licenses/upl.
4
4
5
5
import os
6
+ import re
6
7
import subprocess
8
+ import time
7
9
import unittest
10
+ import uuid
8
11
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
9
63
10
64
11
65
class TestCliOciIscsiConfig (OciTestCase ):
@@ -25,11 +79,61 @@ def setUp(self):
25
79
unittest.SkipTest
26
80
If the ISCSI_CONFIG file does not exist.
27
81
"""
28
- super (TestCliOciIscsiConfig , self ).setUp ()
82
+ super ().setUp ()
29
83
self .iscsi_config_path = self .properties .get_property ('oci-iscsi-config-path' )
30
84
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' ]
33
137
34
138
def test_display_help (self ):
35
139
"""
@@ -40,10 +144,16 @@ def test_display_help(self):
40
144
No return value.
41
145
"""
42
146
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' ])
45
155
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 ))
47
157
48
158
def test_show_no_check (self ):
49
159
"""
@@ -54,10 +164,10 @@ def test_show_no_check(self):
54
164
No return value.
55
165
"""
56
166
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' ])
59
169
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 ))
61
171
62
172
def test_show_all_no_check (self ):
63
173
"""
@@ -68,7 +178,97 @@ def test_show_all_no_check(self):
68
178
No return value.
69
179
"""
70
180
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.')
73
272
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