def _create_horcm_conf(self, horcmgr=_HORCMGR): inst = self.conf.hpxp_horcm_numbers[horcmgr] inst = int(inst) serial = self.conf.hpxp_storage_id filename = '/etc/horcm%s.conf' % inst port = _DEFAULT_PORT_BASE + inst found = False if not os.path.exists(filename): file_str = """ HORCM_MON #ip_address service poll(10ms) timeout(10ms) 127.0.0.1 %16d 6000 3000 HORCM_CMD """ % port else: file_str = cinder_utils.read_file_as_root(filename) if re.search(r'^\\\\.\\CMD-%s:/dev/sd$' % serial, file_str, re.M): found = True if not found: repl_str = r'\1\\\\.\\CMD-%s:/dev/sd\n' % serial file_str = CMD_PATTERN.sub(repl_str, file_str) result = utils.execute('tee', filename, process_input=file_str) if result[0]: msg = utils.output_log( 632, file=filename, ret=result[0], err=result[2]) raise exception.HPXPError(data=msg)
def _get_iscsi_initiator(self): """Get iscsi initiator name for this machine""" # NOTE openiscsi stores initiator name in a file that # needs root permission to read. contents = utils.read_file_as_root('/etc/iscsi/initiatorname.iscsi') for l in contents.split('\n'): if l.startswith('InitiatorName='): return l[l.index('=') + 1:].strip()
def test_read_file_as_root(self, mock_exec): out = mock.Mock() err = mock.Mock() mock_exec.return_value = (out, err) test_filepath = '/some/random/path' output = utils.read_file_as_root(test_filepath) mock_exec.assert_called_once_with('cat', test_filepath, run_as_root=True) self.assertEqual(out, output)
def test_read_file_as_root(self): def fake_execute(*args, **kwargs): if args[1] == "bad": raise putils.ProcessExecutionError return "fakecontents", None self.stubs.Set(utils, "execute", fake_execute) contents = utils.read_file_as_root("good") self.assertEqual(contents, "fakecontents") self.assertRaises(exception.FileNotFound, utils.read_file_as_root, "bad")
def test_read_file_as_root(self): def fake_execute(*args, **kwargs): if args[1] == 'bad': raise putils.ProcessExecutionError return 'fakecontents', None self.stubs.Set(utils, 'execute', fake_execute) contents = utils.read_file_as_root('good') self.assertEqual(contents, 'fakecontents') self.assertRaises(exception.FileNotFound, utils.read_file_as_root, 'bad')