def test_lazy_git(): lazy_git = LazyGit(git_url=DOCKERFILE_GIT) with lazy_git: assert lazy_git.git_path is not None assert lazy_git.commit_id is not None assert len(lazy_git.commit_id) == 40 # current git hashes are this long previous_commit_id = lazy_git.commit_id lazy_git.reset('HEAD~2') # Go back two commits assert lazy_git.commit_id is not None assert lazy_git.commit_id != previous_commit_id assert len(lazy_git.commit_id) == 40 # current git hashes are this long
def run(self): """ this method will: 1) clone git repository with test files into temp location 2) execute tests 3) clear repository """ if not self.image_id: raise RuntimeError("no image_id specified (build probably failed)") tmpdir = tempfile.mkdtemp() g = LazyGit(self.git_uri, self.git_commit, tmpdir) with g: tests_file = os.path.abspath( os.path.join(g.git_path, self.tests_git_path)) self.log.debug("loading file with tests: '%s'", tests_file) module_name, dummy_module_ext = os.path.splitext( self.tests_git_path) tests_module = imp.load_source(module_name, tests_file) results, passed = tests_module.run(image_id=self.image_id, tests=self.tests, git_repo_path=tmpdir, logger=self.log, results_dir=self.results_dir, **self.kwargs) shutil.rmtree(tmpdir) if not passed: self.log.error("tests failed: %s", results) raise RuntimeError("Tests didn't pass!") return results
def test_lazy_git_with_tmpdir(tmpdir): t = str(tmpdir.realpath()) lazy_git = LazyGit(git_url=DOCKERFILE_GIT, tmpdir=t) assert lazy_git._tmpdir == t assert lazy_git.git_path is not None assert lazy_git.commit_id is not None assert len(lazy_git.commit_id) == 40 # current git hashes are this long
def test_lazy_git(): lazy_git = LazyGit(git_url=DOCKERFILE_GIT) with lazy_git: assert lazy_git.git_path is not None assert lazy_git.commit_id is not None assert len( lazy_git.commit_id) == 40 # current git hashes are this long
def prepare(): def set_annotations_on_build(build_id, labels, namespace='default'): assert namespace == 'namespace' new_environ = deepcopy(os.environ) new_environ["BUILD"] = ''' { "metadata": { "name": "asd", "namespace": "namespace" } } ''' flexmock(OSBS, set_annotations_on_build=set_annotations_on_build) flexmock(os) os.should_receive("environ").and_return(new_environ) workflow = DockerBuildWorkflow({ "provider": "git", "uri": "asd" }, "test-image") workflow.push_conf.add_pulp_registry("test", LOCALHOST_REGISTRY) workflow.tag_conf.add_primary_image(TEST_IMAGE) workflow.tag_conf.add_unique_image("namespace/image:asd123") setattr(workflow, 'builder', X) workflow.build_logs = ["a", "b"] workflow.source.lg = LazyGit(None, commit="commit") flexmock(workflow.source.lg) workflow.source.lg.should_receive("_commit_id").and_return("commit") return workflow
def prepare(pulp_registries=None, docker_registries=None): if pulp_registries is None: pulp_registries = ( ("test", LOCALHOST_REGISTRY), ) if docker_registries is None: docker_registries = (DOCKER0_REGISTRY,) def set_annotations_on_build(build_id, annotations): pass def update_labels_on_build(build_id, labels): pass new_environ = deepcopy(os.environ) new_environ["BUILD"] = dedent('''\ { "metadata": { "name": "asd", "namespace": "namespace" } } ''') flexmock(OSBS, set_annotations_on_build=set_annotations_on_build) flexmock(OSBS, update_labels_on_build=update_labels_on_build) (flexmock(osbs.conf) .should_call("Configuration") .with_args(namespace="namespace", conf_file=None, verify_ssl=True, openshift_url="http://example.com/", openshift_uri="http://example.com/", use_auth=True)) flexmock(os) os.should_receive("environ").and_return(new_environ) workflow = DockerBuildWorkflow({"provider": "git", "uri": "asd"}, "test-image") for name, crane_uri in pulp_registries: workflow.push_conf.add_pulp_registry(name, crane_uri) workflow.tag_conf.add_primary_image(TEST_IMAGE) workflow.tag_conf.add_unique_image("namespace/image:asd123") for docker_registry in docker_registries: r = workflow.push_conf.add_docker_registry(docker_registry) r.digests[TEST_IMAGE] = ManifestDigest(v1='not-used', v2=DIGEST1) r.digests["namespace/image:asd123"] = ManifestDigest(v1='not-used', v2=DIGEST2) setattr(workflow, 'builder', X) setattr(workflow, '_base_image_inspect', {'Id': '01234567'}) workflow.build_logs = [ "a", "b", ] workflow.source.lg = LazyGit(None, commit="commit") flexmock(workflow.source.lg) workflow.source.lg.should_receive("_commit_id").and_return("commit") return workflow
def get_reactor_tarball_path(self, tmpdir): """ generate atomic-reactor tarball :return: """ if self.reactor_tarball_path: if not os.path.isfile(self.reactor_tarball_path): logger.error( "atomic-reactor sdist tarball does not exist: '%s'", self.reactor_tarball_path) raise RuntimeError("File does not exist: '%s'" % self.reactor_tarball_path) return self.reactor_tarball_path elif self.reactor_local_path: if not os.path.isdir(self.reactor_local_path): logger.error( "local atomic-reactor git clone does not exist: '%s'", self.reactor_local_path) raise RuntimeError( "Local atomic-reactor git repo does not exist: '%s'" % self.reactor_local_path) local_reactor_git_path = self.reactor_local_path else: if self.use_official_reactor_git: self.reactor_remote_path = REACTOR_GIT_URL g = LazyGit(self.reactor_remote_path, tmpdir=tmpdir) local_reactor_git_path = g.git_path cwd = os.getcwd() os.chdir(local_reactor_git_path) try: logger.debug("executing sdist command in directory '%s'", os.getcwd()) python_bin = "python3" if six.PY3 else "python" subprocess.check_call( [python_bin, "setup.py", "sdist", "--dist-dir", tmpdir]) finally: os.chdir(cwd) candidates_list = glob(os.path.join(tmpdir, 'atomic-reactor-*.tar.gz')) if len(candidates_list) == 1: return candidates_list[0] else: logger.warning("len(atomic-reactor-*.tar.gz) != 1: '%s'", candidates_list) try: return candidates_list[0] except IndexError: raise RuntimeError("No atomic-reactor tarball built.")
def prepare(): def set_annotations_on_build(build_id, labels, namespace='default'): pass new_environ = deepcopy(os.environ) new_environ["BUILD"] = ''' { "metadata": { "name": "asd", "namespace": "namespace" } } ''' flexmock(OSBS, set_annotations_on_build=set_annotations_on_build) (flexmock(osbs.conf).should_call("Configuration").with_args( namespace="namespace", conf_file=None, verify_ssl=True, openshift_url="http://example.com/", openshift_uri="http://example.com/", use_auth=True)) flexmock(os) os.should_receive("environ").and_return(new_environ) workflow = DockerBuildWorkflow({ "provider": "git", "uri": "asd" }, "test-image") workflow.push_conf.add_pulp_registry("test", LOCALHOST_REGISTRY) workflow.tag_conf.add_primary_image(TEST_IMAGE) workflow.tag_conf.add_unique_image("namespace/image:asd123") r = workflow.push_conf.add_docker_registry(DOCKER0_REGISTRY) r.digests[TEST_IMAGE] = DIGEST1 r.digests["namespace/image:asd123"] = DIGEST2 setattr(workflow, 'builder', X) setattr(workflow, '_base_image_inspect', {'Id': '01234567'}) workflow.build_logs = [ "a", "b", ] workflow.source.lg = LazyGit(None, commit="commit") flexmock(workflow.source.lg) workflow.source.lg.should_receive("_commit_id").and_return("commit") return workflow
def prepare(docker_registries=None, before_dockerfile=False): if docker_registries is None: docker_registries = ( LOCALHOST_REGISTRY, DOCKER0_REGISTRY, ) def update_annotations_on_build(build_id, annotations): pass def update_labels_on_build(build_id, labels): pass new_environ = deepcopy(os.environ) new_environ["BUILD"] = dedent('''\ { "metadata": { "name": "asd", "namespace": "namespace" } } ''') flexmock(OSBS, update_annotations_on_build=update_annotations_on_build) flexmock(OSBS, update_labels_on_build=update_labels_on_build) config_kwargs = { 'namespace': 'namespace', 'verify_ssl': True, 'openshift_url': 'http://example.com/', 'use_auth': True, 'conf_file': None, 'build_json_dir': None } (flexmock(osbs.conf.Configuration).should_call("__init__").with_args( **config_kwargs)) flexmock(os) os.should_receive("environ").and_return(new_environ) # pylint: disable=no-member workflow = DockerBuildWorkflow(source=MOCK_SOURCE) openshift_map = { 'url': 'http://example.com/', 'insecure': False, 'auth': { 'enable': True }, } workflow.plugin_workspace[ReactorConfigPlugin.key] = {} workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\ ReactorConfig({'version': 1, 'openshift': openshift_map}) add_koji_map_in_workflow(workflow, hub_url='/', root_url='') workflow.tag_conf.add_floating_image(TEST_IMAGE) workflow.tag_conf.add_primary_image("namespace/image:version-release") workflow.tag_conf.add_unique_image("namespace/image:asd123") for docker_registry in docker_registries: r = workflow.push_conf.add_docker_registry(docker_registry) r.digests[TEST_IMAGE_NAME] = ManifestDigest(v1=DIGEST_NOT_USED, v2=DIGEST1) r.digests["namespace/image:asd123"] = ManifestDigest( v1=DIGEST_NOT_USED, v2=DIGEST2) if before_dockerfile: setattr(workflow, 'builder', XBeforeDockerfile()) setattr(workflow.builder, 'base_image_inspect', {}) else: setattr(workflow, 'builder', X()) setattr(workflow.builder, 'base_image_inspect', {'Id': '01234567'}) workflow.build_logs = [ "a", "b", ] workflow.source.lg = LazyGit(None, commit="commit") flexmock(workflow.source.lg) # pylint: disable=no-member workflow.source.lg.should_receive("_commit_id").and_return("commit") # pylint: enable=no-member return workflow
def prepare( pulp_registries=None, docker_registries=None, before_dockerfile=False, # noqa reactor_config_map=False): if pulp_registries is None: pulp_registries = (("test", LOCALHOST_REGISTRY), ) if docker_registries is None: docker_registries = (DOCKER0_REGISTRY, ) def update_annotations_on_build(build_id, annotations): pass def update_labels_on_build(build_id, labels): pass new_environ = deepcopy(os.environ) new_environ["BUILD"] = dedent('''\ { "metadata": { "name": "asd", "namespace": "namespace" } } ''') flexmock(OSBS, update_annotations_on_build=update_annotations_on_build) flexmock(OSBS, update_labels_on_build=update_labels_on_build) config_kwargs = { 'namespace': 'namespace', 'verify_ssl': True, 'openshift_url': 'http://example.com/', 'use_auth': True, 'conf_file': None, 'build_json_dir': None } (flexmock(osbs.conf.Configuration).should_call("__init__").with_args( **config_kwargs)) flexmock(os) os.should_receive("environ").and_return(new_environ) workflow = DockerBuildWorkflow({ "provider": "git", "uri": "asd" }, "test-image") if reactor_config_map: openshift_map = { 'url': 'http://example.com/', 'insecure': False, 'auth': { 'enable': True }, } workflow.plugin_workspace[ReactorConfigPlugin.key] = {} workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\ ReactorConfig({'version': 1, 'openshift': openshift_map}) for name, crane_uri in pulp_registries: workflow.push_conf.add_pulp_registry(name, crane_uri) workflow.tag_conf.add_primary_image(TEST_IMAGE) workflow.tag_conf.add_unique_image("namespace/image:asd123") for docker_registry in docker_registries: r = workflow.push_conf.add_docker_registry(docker_registry) r.digests[TEST_IMAGE] = ManifestDigest(v1=DIGEST_NOT_USED, v2=DIGEST1) r.digests["namespace/image:asd123"] = ManifestDigest( v1=DIGEST_NOT_USED, v2=DIGEST2) if before_dockerfile: setattr(workflow, 'builder', XBeforeDockerfile()) else: setattr(workflow, 'builder', X) setattr(workflow, '_base_image_inspect', {'Id': '01234567'}) workflow.build_logs = [ "a", "b", ] workflow.source.lg = LazyGit(None, commit="commit") flexmock(workflow.source.lg) workflow.source.lg.should_receive("_commit_id").and_return("commit") return workflow
def prepare(workflow, registry=None): if not registry: registry = LOCALHOST_REGISTRY def update_annotations_on_build(build_id, annotations): pass def update_labels_on_build(build_id, labels): pass flexmock(OSBS, update_annotations_on_build=update_annotations_on_build) flexmock(OSBS, update_labels_on_build=update_labels_on_build) config_kwargs = { 'namespace': workflow.namespace, 'verify_ssl': True, 'openshift_url': 'http://example.com/', 'use_auth': True, 'conf_file': None, } (flexmock(osbs.conf.Configuration).should_call("__init__").with_args( **config_kwargs)) openshift_map = { 'url': 'http://example.com/', 'insecure': False, 'auth': { 'enable': True }, } registries_conf = [{'url': registry, 'insecure': True}] rcm = { 'version': 1, 'openshift': openshift_map, 'registries': registries_conf } workflow.conf.conf = rcm add_koji_map_in_workflow(workflow, hub_url='/', root_url='') tag_conf = workflow.data.tag_conf tag_conf.add_floating_image(f'{registry}/{TEST_IMAGE}') tag_conf.add_primary_image(f'{registry}/namespace/image:version-release') tag_conf.add_unique_image(f'{registry}/namespace/image:asd123') (flexmock(RegistryClient).should_receive('get_manifest_digests').with_args( image=ImageName.parse(f'{registry}/{TEST_IMAGE_NAME}'), versions=('v1', 'v2', 'v2_list', 'oci', 'oci_index'), require_digest=True).and_return( ManifestDigest(v1=DIGEST_NOT_USED, v2=DIGEST1))) (flexmock(RegistryClient).should_receive('get_manifest_digests').with_args( image=ImageName.parse(f'{registry}/namespace/image:version-release'), versions=('v1', 'v2', 'v2_list', 'oci', 'oci_index'), require_digest=True).and_return(None)) (flexmock(RegistryClient).should_receive('get_manifest_digests').with_args( image=ImageName.parse(f'{registry}/namespace/image:asd123'), versions=('v1', 'v2', 'v2_list', 'oci', 'oci_index'), require_digest=True).and_return( ManifestDigest(v1=DIGEST_NOT_USED, v2=DIGEST2))) flexmock( workflow.imageutil).should_receive('base_image_inspect').and_return( {'Id': '01234567'}) workflow.build_logs = [ "a", "b", ] workflow.source.lg = LazyGit(None, commit="commit") flexmock(workflow.source.lg) # pylint: disable=no-member workflow.source.lg.should_receive("_commit_id").and_return("commit")