def test_exclude_input(): resolver = FileResolver(['--in', 'README.md', '--out', 'samples'], pathlib.Path(), input_filters=[FileMatcher('samples/*', include=False)]) assert resolver.command_args == ['--in', 'README.md', '--out', 'samples'] assert resolver.detect_upload_files() == [pathlib.Path('README.md'), pathlib.Path('samples')] resolver = FileResolver(['--in', 'README.md', '--out', 'samples'], pathlib.Path(), input_filters=[FileMatcher('samples/source-b.txt', include=True)]) assert resolver.command_args == ['--in', 'README.md', '--out', 'samples'] assert resolver.detect_upload_files() == [pathlib.Path('samples/source-b.txt')]
def __run(self, args: List[str]) -> CommandLog: """Run native tool in container with given arguments""" # resolve files to upload resolver = FileResolver(args, pathlib.Path.cwd(), do_resolve=not self.input_tar, output_dirs=self.output_dirs, input_filters=self.input_filters) upload_files = {} cmd_args = resolver.resolve_upload_files(upload_files) for h_file, a_name in upload_files.items(): self.logger.debug(f"{h_file.as_posix()} -> {a_name}") self.logger.debug("args: %s", ' '.join(quote_args(cmd_args))) in_files = [] log = self.__create_container(upload_files, in_files, cmd_args) try: log = self.__container_exec(self.container, log, write_stdout=(self.output_tar != '-')) log.in_files.extend(in_files) if log.exit_code == 0: # download results log = self.__download_results(self.container, log) except KeyboardInterrupt: self.logger.info( "Keyboard Interrupt detected, download results anyway.") log = self.__download_results(self.container, log) finally: self.logger.debug("stopping and removing the container") try: # Required when interrupting with Ctrl+C self.container.kill() except docker.errors.APIError: self.logger.debug( "Container was not running anymore. Can't kill.") if self.create_image: self.logger.info( "Creating new image from the produced container.") new_image = self.container.commit() # print(new_image.id) self.logger.info( f"Use it with following id. Shorter version can be used.") self.logger.info(f"id: {new_image.id}") self.logger.info( f"e.g. run 'cincan shell {new_image.short_id}' to open shell." ) # We have to remove container manually, can't use auto_remove parameter earlier. (need output files) self.container.remove() # if we created the image, lets also remove it (intended for testing) if not self.loaded_image: self.logger.info(f"removing the docker image {self.get_id()}") try: self.remove_image() except docker.errors.APIError as e: self.logger.warning(e) work_dir = pathlib.Path().cwd() self.upload_files = sorted( [f.as_posix() for f in list(upload_files.keys())]) self.download_files = sorted([ f.path.relative_to(work_dir).as_posix() for f in filter( lambda f: not f.path.as_posix().startswith('/dev/'), log.out_files) ]) return log
def test_with_existing_directory(): assert pathlib.Path('tests/').is_dir() # pre-requisite for testing # Issue #11 - Cincan-command eats uphill '/' from command resolver = FileResolver(['-o', 'tests/%n'], pathlib.Path()) assert resolver.command_args == ['-o', 'tests/%n'] assert len(resolver.detect_upload_files()) > 2 resolver = FileResolver(['-o', 'no_tests/%n'], pathlib.Path()) assert resolver.command_args == ['-o', 'no_tests/%n'] assert len(resolver.detect_upload_files()) == 0 resolver = FileResolver(['-o', 'tests&%&'], pathlib.Path()) assert resolver.command_args == ['-o', 'tests&%&'] assert len(resolver.detect_upload_files()) > 2 resolver = FileResolver(['-o', 'tests///%n'], pathlib.Path()) assert resolver.command_args == ['-o', 'tests///%n'] assert len(resolver.detect_upload_files()) > 2 resolver = FileResolver(['-o', '/'], pathlib.Path()) assert resolver.command_args == ['-o', '/'] assert len(resolver.detect_upload_files()) == 0 resolver = FileResolver(['-o', '//'], pathlib.Path()) assert resolver.command_args == ['-o', '//'] assert len(resolver.detect_upload_files()) == 0 # Issue #26 - space character doesn't work in file name resolver = FileResolver(["-i", "'tests/foo: story of foo-bar.pdf'"], pathlib.Path()) assert resolver.host_files == [pathlib.Path("tests/foo: story of foo-bar.pdf")]
def test_create_target_directories(): resolver = FileResolver(['--out', 'samples/output'], pathlib.Path()) assert resolver.command_args == ['--out', 'samples/output'] assert resolver.detect_upload_files() == [pathlib.Path('samples')]
def test_upload_target_directories(): resolver = FileResolver(['--out', 'samples/sub'], pathlib.Path()) assert resolver.command_args == ['--out', 'samples/sub'] assert resolver.detect_upload_files() == [pathlib.Path('samples/sub'), pathlib.Path('samples/sub/source-c.txt')]
def test_fix_arguments(): the_path = (pathlib.Path.cwd().resolve() / 'README.md').as_posix() resolver = FileResolver(['-f', the_path], pathlib.Path()) assert resolver.command_args == ['-f', the_path[1:]] assert resolver.detect_upload_files() == [pathlib.Path(the_path)] resolver = FileResolver(['-f', 'tests/../README.md'], pathlib.Path()) assert resolver.command_args == ['-f', the_path[1:]] assert resolver.detect_upload_files() == [pathlib.Path(the_path)] resolver = FileResolver(['-f', '../no-such-file.txt'], pathlib.Path()) assert resolver.command_args == ['-f', '../no-such-file.txt'] assert resolver.detect_upload_files() == [] resolver = FileResolver(['-f', 'tests/../no-such-file.txt'], pathlib.Path()) assert resolver.command_args == ['-f', 'tests/../no-such-file.txt'] assert resolver.detect_upload_files() == []
def test_upload_file_detection(): resolver = FileResolver(['README.md'], pathlib.Path()) assert resolver.command_args == ['README.md'] assert resolver.detect_upload_files() == [pathlib.Path('README.md')] resolver = FileResolver(['README.md5'], pathlib.Path()) assert resolver.command_args == ['README.md5'] assert resolver.detect_upload_files() == [] resolver = FileResolver(['-f', 'README.md'], pathlib.Path()) assert resolver.command_args == ['-f', 'README.md'] assert resolver.detect_upload_files() == [pathlib.Path('README.md')] resolver = FileResolver(['-fREADME.md'], pathlib.Path()) assert resolver.command_args == ['-fREADME.md'] assert resolver.detect_upload_files() == [] # FIXME: Should be done by providing files in tar # resolver = FileResolver(['-fREADME.md'], pathlib.Path(), # input_filters=FileMatcher.parse(['README.md'])) # assert resolver.command_args == ['-fREADME.md'] # assert resolver.detect_upload_files() == [pathlib.Path('README.md')] resolver = FileResolver(['file=README.md'], pathlib.Path()) assert resolver.command_args == ['file=README.md'] assert resolver.detect_upload_files() == [pathlib.Path('README.md')] resolver = FileResolver(['file=README.md,die'], pathlib.Path()) assert resolver.command_args == ['file=README.md,die'] assert resolver.detect_upload_files() == [pathlib.Path('README.md')]
def test_upload_many_files(): resolver = FileResolver(['ls', 'samples/sub'], pathlib.Path()) assert resolver.command_args == ['ls', 'samples/sub'] assert resolver.detect_upload_files() == [pathlib.Path('samples/sub'), pathlib.Path('samples/sub/source-c.txt')]