def test_return_code_multiple_extensions(self): plugins = list_plugins() desired_plugins = ['home', 'user'] active_extensions = [e() for e in plugins.values() if e.get_name() in desired_plugins] dig = DockerImageGenerator(active_extensions, {}, 'ubuntu:bionic') self.assertEqual(dig.build(), 0) self.assertEqual(dig.run('true'), 0) self.assertEqual(dig.run('false'), 1)
def test_x11_xpdyinfo(self): plugins = list_plugins() desired_plugins = ['x11'] active_extensions = [e() for e in plugins.values() if e.get_name() in desired_plugins] for tag in self.dockerfile_tags: dig = DockerImageGenerator(active_extensions, {}, tag) self.assertEqual(dig.build(), 0) self.assertEqual(dig.run(), 0)
def test_nvidia_glmark2(self): plugins = list_plugins() desired_plugins = ['x11', 'nvidia', 'user'] #TODO(Tfoote) encode the x11 dependency into the plugin and remove from test here active_extensions = [e() for e in plugins.values() if e.get_name() in desired_plugins] for tag in self.dockerfile_tags: dig = DockerImageGenerator(active_extensions, {}, tag) self.assertEqual(dig.build(), 0) self.assertEqual(dig.run(), 0)
def run_drone_demo(command): plugins = list_plugins() base_image = 'tfoote/drone_demo' desired_plugins = ['nvidia', 'pulse', 'user', 'home', 'x11'] active_extensions = [e() for e in plugins.values() if e.get_name() in desired_plugins] pull_image(base_image) dig = DockerImageGenerator(active_extensions, {}, base_image) if dig.build() != 0: print ("Failed to build") sys.exit(1) if dig.run(command) != 0: print ("Failed to run") sys.exit(1)
def main(): parser = argparse.ArgumentParser( description='Generate documentation for a specific package', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('documentation-package-dir') #parser.add_argument('command', nargs='*', default='') parser.add_argument('--nocache', action='store_true', help='Force a rebuild of the image') # parser.add_argument('--develop', action='store_true', # help='Build the image locally not using the prebuilt image.') parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + get_rocker_version()) parser.add_argument('--debug-inside', action='store_true') # TODO(tfoote) add verbose parser.add_argument('--verbose', action='store_true') extension_manager = RockerExtensionManager() default_args = {'doc_rocker': True, 'user': True} extension_manager.extend_cli_parser(parser, default_args) args = parser.parse_args() args_dict = vars(args) args_dict['documentation-package-dir'] = os.path.abspath( args_dict['documentation-package-dir']) args_dict['command'] = 'python3 /doc_root/build_docs.py' active_extensions = extension_manager.get_active_extensions(args_dict) print("Active extensions %s" % [e.get_name() for e in active_extensions]) dig = DockerImageGenerator(active_extensions, args_dict, 'ubuntu:focal') exit_code = dig.build(**vars(args)) if exit_code != 0: print("Build failed exiting") return exit_code if args.debug_inside: args_dict['command'] = 'bash' print(args_dict) return dig.run(**args_dict)
def test_docker_cmd_nocleanup(self): dig = DockerImageGenerator([], {}, 'ubuntu:bionic') self.assertIn('--rm', dig.generate_docker_cmd()) self.assertIn('--rm', dig.generate_docker_cmd(mode='dry-run')) self.assertIn('--rm', dig.generate_docker_cmd(nocleanup='')) self.assertNotIn('--rm', dig.generate_docker_cmd(nocleanup='true'))
def test_user_collisions(self): plugins = list_plugins() user_plugin = plugins['user'] self.assertEqual(user_plugin.get_name(), 'user') uid = os.getuid() + 1 COLLIDING_UID_DOCKERFILE = f"""FROM ubuntu:jammy RUN useradd test -u{uid} """ iof = StringIO(COLLIDING_UID_DOCKERFILE.encode()) image_id = docker_build( fileobj=iof, #output_callback=output_callback, nocache=True, forcerm=True, tag="rocker:" + f"user_extension_test_uid_collision") print(f'Image id is {image_id}') self.assertTrue( image_id, f"Image failed to build >>>{COLLIDING_UID_DOCKERFILE}<<<") # Test Colliding UID but not name build_args = { 'user': True, 'user_override_name': 'test2', 'user_preserve_home': True, # 'command': 'ls -l && touch /home/test2/home_directory_access_verification', 'command': 'touch /home/test2/testwrite', } dig = DockerImageGenerator([user_plugin()], build_args, image_id) exit_code = dig.build(**build_args) self.assertTrue(exit_code == 0, f"Build failed with exit code {exit_code}") run_exit_code = dig.run(**build_args) self.assertTrue(run_exit_code == 0, f"Run failed with exit code {run_exit_code}") # Test colliding UID and name build_args['user_override_name'] = 'test' build_args['command'] = 'touch /home/test/testwrite' dig = DockerImageGenerator([user_plugin()], build_args, image_id) exit_code = dig.build(**build_args) self.assertTrue(exit_code == 0, f"Build failed with exit code {exit_code}") run_exit_code = dig.run(**build_args) self.assertTrue(run_exit_code == 0, f"Run failed with exit code {run_exit_code}")
def test_docker_cmd_interactive(self): dig = DockerImageGenerator([], {}, 'ubuntu:bionic') self.assertNotIn('-it', dig.generate_docker_cmd(mode='')) self.assertIn('-it', dig.generate_docker_cmd(mode='dry-run')) # TODO(tfoote) mock this appropriately # google actions tests don't have a tty, local tests do import os, sys if os.isatty(sys.__stdin__.fileno()): self.assertIn('-it', dig.generate_docker_cmd(mode='interactive')) else: self.assertNotIn('-it', dig.generate_docker_cmd(mode='interactive')) self.assertNotIn('-it', dig.generate_docker_cmd(mode='non-interactive'))
def test_run_before_build(self): dig = DockerImageGenerator([], {}, 'ubuntu:bionic') self.assertEqual(dig.run('true'), 1) self.assertEqual(dig.build(), 0) self.assertEqual(dig.run('true'), 0)
def test_no_x11_xpdyinfo(self): for tag in self.dockerfile_tags: dig = DockerImageGenerator([], {}, tag) self.assertEqual(dig.build(), 0) self.assertNotEqual(dig.run(), 0)
def test_no_nvidia_glmark2(self): for tag in self.dockerfile_tags: dig = DockerImageGenerator([], {}, tag) self.assertEqual(dig.build(), 0) self.assertNotEqual(dig.run(), 0)
def test_device(self): dig = DockerImageGenerator([], {}, 'ubuntu:bionic') self.assertEqual(dig.build(), 0) self.assertEqual(dig.run('true', devices=['/dev/random']), 0) self.assertEqual(dig.run('true', devices=['/dev/does_not_exist']), 0)
def test_non_interactive(self): dig = DockerImageGenerator([], {}, 'ubuntu:bionic') self.assertEqual(dig.build(), 0) self.assertEqual(dig.run('true', mode='non-interactive'), 0) self.assertEqual(dig.run('false', mode='non-interactive'), 1)
def test_dry_run(self): dig = DockerImageGenerator([], {}, 'ubuntu:bionic') self.assertEqual(dig.build(), 0) self.assertEqual(dig.run('true', mode='dry-run'), 0) self.assertEqual(dig.run('false', mode='dry-run'), 0)
def test_noexecute(self): dig = DockerImageGenerator([], {}, 'ubuntu:bionic') self.assertEqual(dig.build(), 0) self.assertEqual(dig.run('true', noexecute=True), 0)
def test_return_code_no_extensions(self): dig = DockerImageGenerator([], {}, 'ubuntu:bionic') self.assertEqual(dig.build(), 0) self.assertEqual(dig.run('true'), 0) self.assertEqual(dig.run('false'), 1)
def main(): parser = argparse.ArgumentParser( description='A tool for building and testing gh-pages locally', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('directory') #parser.add_argument('command', nargs='*', default='') parser.add_argument('--nocache', action='store_true', help='Force a rebuild of the image') # TODO(tfoote) add prebuilt images for faster operations # parser.add_argument('--develop', action='store_true', # help='Build the image locally not using the prebuilt image.') parser.add_argument('--config', type=str, nargs="+", action='append', default=None) parser.add_argument('--port', type=int, action='store', default='4000') parser.add_argument('--baseurl', type=str, action='store', default=None) parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + get_rocker_version()) parser.add_argument('--build-only', action='store_true') parser.add_argument('--debug-inside', action='store_true') # TODO(tfoote) add verbose parser.add_argument('--verbose', action='store_true') extension_manager = RockerExtensionManager() default_args = {'ghpages': True, 'user': True, 'network': 'host'} extension_manager.extend_cli_parser(parser, default_args) args = parser.parse_args() args_dict = vars(args) args_dict['directory'] = os.path.abspath(args_dict['directory']) if args.build_only and args.baseurl: parser.error("build and baseurl options are incompatible") if args.build_only: args_dict['command'] = 'jekyll build -V --trace' del args_dict['network'] else: args_dict['command'] = 'jekyll serve -w' if args.baseurl is not None: # Don't output to the default location if generating using a modified baseurl args_dict[ 'command'] += ' --baseurl=\'{baseurl}\' -d /tmp/aliased_site'.format( **args_dict) if args.config: config_args = ','.join(args.config[0]) args_dict['command'] += ' --config={config_args}'.format(**locals()) active_extensions = extension_manager.get_active_extensions(args_dict) print("Active extensions %s" % [e.get_name() for e in active_extensions]) dig = DockerImageGenerator(active_extensions, args_dict, 'ubuntu:focal') exit_code = dig.build(**vars(args)) if exit_code != 0: print("Build failed exiting") return exit_code if args.debug_inside: args_dict['command'] = 'bash' return dig.run(**args_dict)
def test_network(self): dig = DockerImageGenerator([], {}, 'ubuntu:bionic') self.assertEqual(dig.build(), 0) networks = ['bridge', 'host', 'none'] for n in networks: self.assertEqual(dig.run('true', network=n), 0)