def _install(self): """Install ``fatdisk``.""" try: super(FatDisk, self)._install() return except NotImplementedError: # We have an alternative install method available for Linux, # below - but if not Linux, you're out of luck! if platform.system() != 'Linux': raise # Fatdisk installation requires make helpers['make'].install() # Fatdisk build requires clang or gcc or g++, # but COT doesn't care which one we have. helper_select(['clang', 'gcc', 'g++']) with self.download_and_expand_tgz( 'https://github.com/goblinhack/' 'fatdisk/archive/v1.0.0-beta.tar.gz') as directory: new_d = os.path.join(directory, 'fatdisk-1.0.0-beta') logger.info("Compiling 'fatdisk'") check_call(['./RUNME'], cwd=new_d) destdir = os.getenv('DESTDIR', '') prefix = os.getenv('PREFIX', '/usr/local') # os.path.join doesn't like absolute paths in the middle if destdir != '': prefix = prefix.lstrip(os.sep) destination = os.path.join(destdir, prefix, 'bin') logger.info("Compilation complete, installing to " + destination) self.mkdir(destination) self.copy_file(os.path.join(new_d, 'fatdisk'), destination)
def test_check_call_helpererror(self): """HelperError if executable fails and require_success is set.""" with self.assertRaises(HelperError) as catcher: check_call(["false"]) self.assertEqual(catcher.exception.errno, 1) check_call(["false"], require_success=False)
def _install(self): """Install ``vmdktool``.""" try: super(VMDKTool, self)._install() return except NotImplementedError: # We have an alternative install method available for Linux, # below - but if not Linux, you're out of luck! if platform.system() != 'Linux': raise # We don't have vmdktool in apt or yum yet, # but we can build it manually: # vmdktool requires make and zlib helpers['make'].install() # TODO: check for installed zlib? logger.notice("vmdktool requires 'zlib'... installing 'zlib'") if helpers['apt-get']: helpers['apt-get'].install_package('zlib1g-dev') elif helpers['yum']: helpers['yum'].install_package('zlib-devel') else: raise NotImplementedError("Not sure how to install 'zlib'") with self.download_and_expand_tgz( 'http://people.freebsd.org/~brian/vmdktool/vmdktool-1.4.tar.gz' ) as directory: new_d = os.path.join(directory, "vmdktool-1.4") logger.info("Compiling 'vmdktool'") # vmdktool is originally a BSD tool so it has some build # assumptions that aren't necessarily correct under Linux. # The easiest workaround is to override the CFLAGS to: # 1) add -D_GNU_SOURCE # 2) not treat all warnings as errors check_call(['make', 'CFLAGS="-D_GNU_SOURCE -g -O -pipe"'], cwd=new_d) destdir = os.getenv('DESTDIR', '') prefix = os.getenv('PREFIX', '/usr/local') args = ['make', 'install', 'PREFIX=' + prefix] if destdir != '': args.append('DESTDIR=' + destdir) # os.path.join doesn't like absolute paths in the middle prefix = prefix.lstrip(os.sep) logger.info("Compilation complete, installing to " + os.path.join(destdir, prefix)) # Make sure the relevant man and bin directories exist self.mkdir(os.path.join(destdir, prefix, 'man', 'man8')) self.mkdir(os.path.join(destdir, prefix, 'bin')) check_call(args, retry_with_sudo=True, cwd=new_d)
def test_check_call_permissions_needed(self, mock_check_call): """Test cases where sudo permission is needed.""" def raise_oserror(args, **_): """Raise an OSError unless using 'sudo'.""" if args[0] != 'sudo': raise OSError(13, 'permission denied') return mock_check_call.side_effect = raise_oserror # Without retry_on_sudo, we reraise the permissions error with self.assertRaises(OSError) as catcher: check_call(["false"]) self.assertEqual(catcher.exception.errno, 13) mock_check_call.assert_called_once_with(["false"]) # With retry_on_sudo, we retry. mock_check_call.reset_mock() check_call(["false"], retry_with_sudo=True) mock_check_call.assert_has_calls([ mock.call(['false']), mock.call(['sudo', 'false']), ]) # Now a variant - the subprocess call actually executed, but the # process exited with a non-zero exit code def raise_subprocess_error(args, **_): """Raise a CalledProcessError unless using 'sudo'.""" if args[0] != 'sudo': raise subprocess.CalledProcessError(1, " ".join(args)) return mock_check_call.reset_mock() mock_check_call.side_effect = raise_subprocess_error # Without retry_on_sudo, we reraise the permissions error with self.assertRaises(HelperError) as catcher: check_call(["false"]) self.assertEqual(catcher.exception.errno, 1) mock_check_call.assert_called_once_with(["false"]) # With retry_on_sudo, we retry. mock_check_call.reset_mock() check_call(["false"], retry_with_sudo=True) mock_check_call.assert_has_calls([ mock.call(['false']), mock.call(['sudo', 'false']), ])