def test_read_file_as_root(self): def fake_execute(*args, **kwargs): if args[1] == 'bad': raise processutils.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 get_iscsi_initiator(): """Get iscsi initiator name for this machine.""" # NOTE(vish) openiscsi stores initiator name in a file that # needs root permission to read. try: contents = utils.read_file_as_root('/etc/iscsi/initiatorname.iscsi') except exception.FileNotFound: return '' for l in contents.split('\n'): if l.startswith('InitiatorName='): return l[l.index('=') + 1:].strip() return ''
def read_file(self, path): LOG.debug("Read file path=%s", path) canonpath = self._canonical_path(path) return utils.read_file_as_root(canonpath)