Best Python code snippet using slash
test_tgt_driver.py
Source:test_tgt_driver.py
...113 return self.iscsi_target_prefix114 def test_iscsi_protocol(self):115 self.assertEqual(self.target.iscsi_protocol, 'iscsi')116 def test_get_target(self):117 def _fake_execute(*args, **kwargs):118 return self.fake_iscsi_scan, None119 self.stubs.Set(utils,120 'execute',121 _fake_execute)122 self.assertEqual('1',123 self.target._get_target('iqn.2010-10.org.openstack:'124 'volume-83c2e877-feed-46be-'125 '8435-77884fe55b45'))126 def test_verify_backing_lun(self):127 def _fake_execute(*args, **kwargs):128 return self.fake_iscsi_scan, None129 self.stubs.Set(utils,130 'execute',131 _fake_execute)132 self.assertTrue(self.target._verify_backing_lun(133 'iqn.2010-10.org.openstack:'134 'volume-83c2e877-feed-46be-'135 '8435-77884fe55b45', '1'))136 # Test the failure case137 bad_scan = self.fake_iscsi_scan.replace('LUN: 1', 'LUN: 3')138 def _fake_execute_bad_lun(*args, **kwargs):139 return bad_scan, None140 self.stubs.Set(utils,141 'execute',142 _fake_execute_bad_lun)143 self.assertFalse(self.target._verify_backing_lun(144 'iqn.2010-10.org.openstack:'145 'volume-83c2e877-feed-46be-'146 '8435-77884fe55b45', '1'))147 @mock.patch.object(time, 'sleep')148 @mock.patch.object(utils, 'execute')149 def test_recreate_backing_lun(self, mock_execute, mock_sleep):150 test_vol = 'iqn.2010-10.org.openstack:'\151 'volume-83c2e877-feed-46be-8435-77884fe55b45'152 mock_execute.return_value = ('out', 'err')153 self.target._recreate_backing_lun(test_vol, '1',154 self.testvol['name'],155 self.testvol_path)156 expected_command = ('tgtadm', '--lld', 'iscsi', '--op', 'new',157 '--mode', 'logicalunit', '--tid', '1',158 '--lun', '1', '-b',159 '/dev/stack-volumes-lvmdriver-1/'160 'volume-83c2e877-feed-46be-8435-77884fe55b45')161 mock_execute.assert_called_once_with(*expected_command,162 run_as_root=True)163 # Test the failure case164 mock_execute.side_effect = putils.ProcessExecutionError165 self.assertFalse(self.target._recreate_backing_lun(166 test_vol,167 '1',168 self.testvol['name'],169 self.testvol_path))170 def test_get_iscsi_target(self):171 ctxt = context.get_admin_context()172 expected = 0173 self.assertEqual(expected,174 self.target._get_iscsi_target(ctxt,175 self.testvol['id']))176 def test_get_target_and_lun(self):177 lun = 1178 iscsi_target = 0179 ctxt = context.get_admin_context()180 expected = (iscsi_target, lun)181 self.assertEqual(expected,182 self.target._get_target_and_lun(ctxt, self.testvol))183 def test_get_target_chap_auth(self):184 persist_file =\185 '<target iqn.2010-10.org.openstack:volume-83c2e877-feed-46be-8435-77884fe55b45>\n'\186 ' backing-store /dev/stack-volumes-lvmdriver-1/volume-83c2e877-feed-46be-8435-77884fe55b45\n'\187 ' driver iscsi\n'\188 ' incominguser otzLy2UYbYfnP4zXLG5z 234Zweo38VGBBvrpK9nt\n'\189 ' write-cache on\n'\190 '</target>'191 test_vol =\192 'iqn.2010-10.org.openstack:'\193 'volume-83c2e877-feed-46be-8435-77884fe55b45'194 with open(os.path.join(self.fake_volumes_dir,195 test_vol.split(':')[1]),196 'wb') as tmp_file:197 tmp_file.write(persist_file)198 ctxt = context.get_admin_context()199 expected = ('otzLy2UYbYfnP4zXLG5z', '234Zweo38VGBBvrpK9nt')200 self.assertEqual(expected,201 self.target._get_target_chap_auth(ctxt, test_vol))202 def test_get_target_chap_auth_negative(self):203 test_vol =\204 'iqn.2010-10.org.openstack:'\205 'volume-83c2e877-feed-46be-8435-77884fe55b45'206 with mock.patch('__builtin__.open') as mock_open:207 e = IOError()208 e.errno = 123209 mock_open.side_effect = e210 ctxt = context.get_admin_context()211 self.assertRaises(IOError,212 self.target._get_target_chap_auth,213 ctxt, test_vol)214 mock_open.side_effect = StandardError()215 self.assertRaises(StandardError,216 self.target._get_target_chap_auth,217 ctxt, test_vol)218 def test_create_iscsi_target(self):219 def _fake_execute(*args, **kwargs):220 return '', ''221 self.stubs.Set(utils,222 'execute',223 _fake_execute)224 self.stubs.Set(self.target,225 '_get_target',226 lambda x: 1)227 self.stubs.Set(self.target,228 '_verify_backing_lun',229 lambda x, y: True)230 test_vol = 'iqn.2010-10.org.openstack:'\231 'volume-83c2e877-feed-46be-8435-77884fe55b45'232 self.assertEqual(233 1,234 self.target.create_iscsi_target(235 test_vol,236 1,237 0,238 self.fake_volumes_dir))239 def test_create_iscsi_target_already_exists(self):240 def _fake_execute(*args, **kwargs):241 if 'update' in args:242 raise putils.ProcessExecutionError(243 exit_code=1,244 stdout='',245 stderr='target already exists',246 cmd='tgtad --lld iscsi --op show --mode target')247 else:248 return 'fake out', 'fake err'249 self.stubs.Set(utils,250 'execute',251 _fake_execute)252 self.stubs.Set(self.target,253 '_get_target',254 lambda x: 1)255 self.stubs.Set(self.target,256 '_verify_backing_lun',257 lambda x, y: True)258 test_vol = 'iqn.2010-10.org.openstack:'\259 'volume-83c2e877-feed-46be-8435-77884fe55b45'260 self.assertEqual(261 1,262 self.target.create_iscsi_target(263 test_vol,264 1,265 0,266 self.fake_volumes_dir))267 @mock.patch('os.path.isfile', return_value=True)268 @mock.patch('os.path.exists', return_value=True)269 @mock.patch.object(utils, 'execute')270 @mock.patch('os.unlink', return_value=None)271 def test_delete_target_not_found(self,272 mock_unlink,273 mock_exec,274 mock_pathexists,275 mock_isfile):276 def _fake_execute(*args, **kwargs):277 raise putils.ProcessExecutionError(278 exit_code=1,279 stdout='',280 stderr='can\'t find the target',281 cmd='tgt-admin --force --delete')282 def _fake_execute_wrong_message(*args, **kwargs):283 raise putils.ProcessExecutionError(284 exit_code=1,285 stdout='',286 stderr='this isnt the error your looking for',287 cmd='tgt-admin --force --delete')288 mock_exec.side_effect = _fake_execute289 test_vol_id = '83c2e877-feed-46be-8435-77884fe55b45'290 test_vol_name = 'volume-83c2e877-feed-46be-8435-77884fe55b45'291 with mock.patch.object(self.target, '_get_target', return_value=False):292 self.assertEqual(293 None,294 self.target.remove_iscsi_target(295 1,296 0,297 test_vol_id,298 test_vol_name))299 mock_exec.side_effect = _fake_execute_wrong_message300 self.assertRaises(exception.ISCSITargetRemoveFailed,301 self.target.remove_iscsi_target,302 1,303 0,304 test_vol_id,305 test_vol_name)306 @mock.patch('os.path.isfile', return_value=True)307 @mock.patch('os.path.exists', return_value=True)308 @mock.patch.object(utils, 'execute')309 @mock.patch('os.unlink', return_value=None)310 def test_delete_target_acl_not_found(self,311 mock_unlink,312 mock_exec,313 mock_pathexists,314 mock_isfile):315 def _fake_execute(*args, **kwargs):316 raise putils.ProcessExecutionError(317 exit_code=1,318 stdout='',319 stderr='this access control rule does not exist',320 cmd='tgt-admin --force --delete')321 def _fake_execute_wrong_message(*args, **kwargs):322 raise putils.ProcessExecutionError(323 exit_code=1,324 stdout='',325 stderr='this isnt the error your looking for',326 cmd='tgt-admin --force --delete')327 mock_exec.side_effect = _fake_execute328 test_vol_id = '83c2e877-feed-46be-8435-77884fe55b45'329 test_vol_name = 'volume-83c2e877-feed-46be-8435-77884fe55b45'330 with mock.patch.object(self.target, '_get_target', return_value=False):331 self.assertEqual(332 None,333 self.target.remove_iscsi_target(334 1,335 0,336 test_vol_id,337 test_vol_name))338 mock_exec.side_effect = _fake_execute_wrong_message339 self.assertRaises(exception.ISCSITargetRemoveFailed,340 self.target.remove_iscsi_target,341 1,342 0,343 test_vol_id,344 test_vol_name)345 @mock.patch.object(tgt.TgtAdm, '_get_iscsi_properties')346 def test_initialize_connection(self, mock_get_iscsi):347 connector = {'initiator': 'fake_init'}348 # Test the normal case349 mock_get_iscsi.return_value = 'foo bar'350 expected_return = {'driver_volume_type': 'iscsi',351 'data': 'foo bar'}352 self.assertEqual(expected_return,353 self.target.initialize_connection(self.testvol,354 connector))355 @mock.patch.object(utils, 'execute')356 @mock.patch.object(tgt.TgtAdm, '_get_target')357 @mock.patch.object(os.path, 'exists')358 @mock.patch.object(os.path, 'isfile')359 @mock.patch.object(os, 'unlink')360 def test_remove_iscsi_target(self,361 mock_unlink,362 mock_isfile,363 mock_path_exists,364 mock_get_target,365 mock_execute):366 test_vol = 'iqn.2010-10.org.openstack:'\367 'volume-83c2e877-feed-46be-8435-77884fe55b45'368 # Test the failure case: path does not exist369 mock_path_exists.return_value = None370 self.assertEqual(None,371 self.target.remove_iscsi_target(372 0,373 1,374 self.testvol['id'],375 self.testvol['name']))376 # Test the normal case377 mock_path_exists.return_value = True378 mock_isfile.return_value = True379 self.target.remove_iscsi_target(0,380 1,381 self.testvol['id'],382 self.testvol['name'])383 calls = [mock.call('tgt-admin', '--force', '--delete', test_vol,384 run_as_root=True),385 mock.call('tgt-admin', '--delete', test_vol,386 run_as_root=True)]387 mock_execute.assert_has_calls(calls)388 def test_create_export(self):389 def _fake_execute(*args, **kwargs):390 return '', ''391 self.stubs.Set(utils,392 'execute',393 _fake_execute)394 self.stubs.Set(self.target,395 '_get_target',396 lambda x: 1)397 self.stubs.Set(self.target,398 '_verify_backing_lun',399 lambda x, y: True)400 self.stubs.Set(self.target,401 '_get_target_chap_auth',402 lambda x, y: None)403 self.stubs.Set(vutils,...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!