Exemplo n.º 1
0
    def mock_env(self, base_image='fedora23/python', additional_tags=None):
        class MockParser(object):
            labels = {
                'name': 'fedora23/something',
                'com.redhat.component': TEST_COMPONENT,
                'version': TEST_VERSION,
            }
            baseimage = base_image

        class MockConfiguration(object):
            container = {
                'tags': additional_tags or [],
                'compose': {
                    'modules': ['mod_name:mod_stream:mod_version']
                }
            }

            module = container['compose']['modules'][0]
            container_module_specs = [ModuleSpec.from_str(module)]
            depth = 0

            def is_autorebuild_enabled(self):
                return False

        (flexmock(utils).should_receive('get_repo_info').with_args(
            TEST_GIT_URI, TEST_GIT_REF, git_branch=TEST_GIT_BRANCH,
            depth=None).and_return(RepoInfo(MockParser(),
                                            MockConfiguration())))

        # Trick create_orchestrator_build into return the *request* JSON
        flexmock(OSBS, _create_build_config_and_build=request_as_response)
        flexmock(OSBS, _create_scratch_build=request_as_response)
Exemplo n.º 2
0
    def test_image_labels_flatpak(self, tmpdir, modules, name, component,
                                  expected_name, expected_component):
        config_yaml = {
            'compose': {
                'modules': modules
            },
            'flatpak': {
                'id': 'org.gnome.Eog'
            }
        }
        if name:
            config_yaml['flatpak']['name'] = name
        if component:
            config_yaml['flatpak']['component'] = component

        yaml_file = tmpdir.join(REPO_CONTAINER_CONFIG)
        yaml_file.write(yaml.dump(config_yaml))

        repo_info = RepoInfo(configuration=RepoConfiguration(str(tmpdir)))

        if modules:
            _, name_label = repo_info.labels.get_name_and_value(
                Labels.LABEL_TYPE_NAME)
            _, component_label = repo_info.labels.get_name_and_value(
                Labels.LABEL_TYPE_COMPONENT)

            assert name_label == expected_name
            assert component_label == expected_component
        else:
            with pytest.raises(OsbsValidationException) as exc_info:
                assert repo_info.labels is None  # .labels access raises

            assert '"compose" config is missing "modules", required for Flatpak' in \
                   exc_info.value.message
Exemplo n.º 3
0
    def test_render_prod_request_with_repo_info(self, tmpdir,
                                                autorebuild_enabled, release_label,
                                                add_timestamp, expected):
        self.create_image_change_trigger_json(str(tmpdir))

        class MockDfParser(object):
            labels = {release_label: '13'} if release_label else {}

        (flexmock(RepoConfiguration)
            .should_receive('is_autorebuild_enabled')
            .and_return(autorebuild_enabled))

        repo_info = RepoInfo(MockDfParser())
        repo_info.configuration.autorebuild['add_timestamp_to_release'] = add_timestamp

        build_request_kwargs = get_sample_prod_params()
        base_image = build_request_kwargs['base_image']
        build_request = BuildRequestV2(str(tmpdir))
        build_request.set_params(**build_request_kwargs)
        build_request.set_repo_info(repo_info)
        if isinstance(expected, type):
            with pytest.raises(expected):
                build_json = build_request.render()
            return

        build_json = build_request.render()

        if expected:
            assert build_json["spec"]["triggers"][0]["imageChange"]["from"]["name"] == base_image

        else:
            assert 'triggers' not in build_json['spec']
Exemplo n.º 4
0
    def test_image_labels_not_flatpak(self, dockerfile_missing):
        labels = {
            'name': 'image1',
        }

        class MockParser(object):
            @property
            def labels(self):
                if dockerfile_missing:
                    raise IOError("Can't read")
                else:
                    return labels

            @property
            def baseimage(self):
                if dockerfile_missing:
                    raise IOError("Can't read")
                else:
                    return 'fedora:latest'

            @property
            def dockerfile_path(self):
                return '/foo/bar'

        repo_info = RepoInfo(MockParser())

        if dockerfile_missing:
            with pytest.raises(RuntimeError) as exc_info:
                assert repo_info.labels is None  # .labels access raises

            assert 'Could not parse Dockerfile' in str(exc_info.value)
        else:
            _, value = repo_info.labels.get_name_and_value(Labels.LABEL_TYPE_NAME)
            assert value == 'image1'
            assert repo_info.base_image == 'fedora:latest'
Exemplo n.º 5
0
def get_repo_info(git_uri, git_ref, git_branch=None):
    with checkout_git_repo(git_uri, git_ref, git_branch) as code_dir:
        dfp = DockerfileParser(os.path.join(code_dir), cache_content=True)
        config = RepoConfiguration(dir_path=code_dir)
        tags_config = AdditionalTagsConfig(dir_path=code_dir,
                                           tags=config.container.get(
                                               'tags', set()))
    return RepoInfo(dfp, config, tags_config)
    def test_render_tag_from_config(self, tmpdir, from_container_yaml,
                                    extra_args, has_platform_tag, extra_tags,
                                    primary_tags):
        kwargs = get_sample_prod_params(BUILD_TYPE_WORKER)
        kwargs.pop('platforms', None)
        kwargs.pop('platform', None)
        expected_primary = set(primary_tags)
        exclude_for_override = set(['latest', '{version}'])

        if extra_tags and not from_container_yaml:
            with open(os.path.join(str(tmpdir), ADDITIONAL_TAGS_FILE),
                      'w') as f:
                f.write('\n'.join(extra_tags))
        kwargs.update(extra_args)

        if from_container_yaml:
            if extra_tags:
                expected_primary -= exclude_for_override
            (flexmock(utils).should_receive('get_repo_info').with_args(
                TEST_GIT_URI, TEST_GIT_REF,
                git_branch=TEST_GIT_BRANCH).and_return(
                    RepoInfo(additional_tags=AdditionalTagsConfig(
                        tags=extra_tags))))
        else:
            (flexmock(utils).should_receive('get_repo_info').with_args(
                TEST_GIT_URI, TEST_GIT_REF,
                git_branch=TEST_GIT_BRANCH).and_return(
                    RepoInfo(additional_tags=AdditionalTagsConfig(
                        dir_path=str(tmpdir)))))

        user_params = BuildUserParams(INPUTS_PATH)
        user_params.set_params(**kwargs)
        build_json = PluginsConfiguration(user_params).render()

        plugins = get_plugins_from_build_json(build_json)

        assert get_plugin(plugins, 'postbuild_plugins', 'tag_from_config')
        tag_suffixes = plugin_value_get(plugins, 'postbuild_plugins',
                                        'tag_from_config', 'args',
                                        'tag_suffixes')
        assert len(tag_suffixes['unique']) == 1
        if has_platform_tag:
            unique_tag_suffix = tag_suffixes['unique'][0]
            assert unique_tag_suffix.endswith('-x86_64') == has_platform_tag
        assert len(tag_suffixes['primary']) == len(expected_primary)
        assert set(tag_suffixes['primary']) == expected_primary
Exemplo n.º 7
0
    def test_explicit_params(self):
        df_parser = flexmock()
        configuration = RepoConfiguration()
        tags_config = AdditionalTagsConfig()

        repo_info = RepoInfo(df_parser, configuration, tags_config)
        assert repo_info.dockerfile_parser is df_parser
        assert repo_info.configuration is configuration
        assert repo_info.additional_tags is tags_config
Exemplo n.º 8
0
def get_repo_info(git_uri, git_ref, git_branch=None, depth=None):
    with checkout_git_repo(git_uri, commit=git_ref, branch=git_branch,
                           depth=depth) as code_dir_info:
        code_dir = code_dir_info.repo_path
        depth = code_dir_info.commit_depth
        dfp = DockerfileParser(os.path.join(code_dir), cache_content=True)
        config = RepoConfiguration(git_uri=git_uri, git_ref=git_ref, git_branch=git_branch,
                                   dir_path=code_dir, depth=depth)
        tags_config = AdditionalTagsConfig(dir_path=code_dir,
                                           tags=config.container.get('tags', set()))
    repo_info = RepoInfo(dfp, config, tags_config)
    return repo_info
Exemplo n.º 9
0
 def get_minimal_kwargs(self, git_args=None, conf_args=None):
     if not git_args:
         git_args = {}
     git_args.setdefault('git_uri', TEST_GIT_URI)
     if not conf_args:
         conf_args = {'build_from': 'image:buildroot:latest'}
     repo_conf = RepoConfiguration(**git_args)
     return {
         # Params needed to avoid exceptions.
         'base_image': 'base_image',
         'build_conf': Configuration(**conf_args),
         'name_label': 'name_label',
         'repo_info': RepoInfo(configuration=repo_conf),
         'user': TEST_USER,
     }
Exemplo n.º 10
0
    def mock_env(self, base_image='fedora23/python'):
        class MockParser(object):
            labels = {
                'name': 'fedora23/something',
                'com.redhat.component': TEST_COMPONENT,
                'version': TEST_VERSION,
            }
            baseimage = base_image

        (flexmock(utils).should_receive('get_repo_info').with_args(
            TEST_GIT_URI, TEST_GIT_REF,
            git_branch=TEST_GIT_BRANCH).and_return(RepoInfo(MockParser())))

        # Trick create_orchestrator_build into return the *request* JSON
        flexmock(OSBS, _create_build_config_and_build=request_as_response)
        flexmock(OSBS, _create_scratch_build=request_as_response)
Exemplo n.º 11
0
    def mock_env(self,
                 base_image='fedora23/python',
                 additional_tags=None,
                 flatpak=False):
        class MockParser(object):
            labels = {
                'name': 'fedora23/something',
                'com.redhat.component': TEST_COMPONENT,
                'version': TEST_VERSION,
            }
            baseimage = base_image

        class MockConfiguration(object):
            def __init__(self, git_uri, git_ref, git_branch, depth):
                self.container = {
                    'tags': additional_tags or [],
                    'compose': {
                        'modules': ['mod_name:mod_stream:mod_version']
                    }
                }

                self.module = self.container['compose']['modules'][0]
                self.container_module_specs = [
                    ModuleSpec.from_str(self.module)
                ]
                self.depth = int(depth) if depth else 0
                self.is_flatpak = flatpak
                self.flatpak_base_image = None
                self.flatpak_component = None
                self.flatpak_name = None
                self.git_uri = git_uri
                self.git_ref = git_ref
                self.git_branch = git_branch

            def is_autorebuild_enabled(self):
                return False

        mock_conf = MockConfiguration(TEST_GIT_URI, TEST_GIT_REF,
                                      TEST_GIT_BRANCH, None)
        (flexmock(utils).should_receive('get_repo_info').with_args(
            TEST_GIT_URI, TEST_GIT_REF, git_branch=TEST_GIT_BRANCH,
            depth=None).and_return(RepoInfo(MockParser(), mock_conf)))

        # Trick create_orchestrator_build into return the *request* JSON
        flexmock(OSBS, _create_build_config_and_build=request_as_response)
        flexmock(OSBS, _create_scratch_build=request_as_response)
Exemplo n.º 12
0
    def test_base_image_flatpak(self, tmpdir, base_image):
        config_yaml = {
            'compose': {
                'modules': ['mod_name:mod_stream'],
            },
            'flatpak': {
                'id': 'org.gnome.Eog'
            }
        }
        if base_image:
            config_yaml['flatpak']['base_image'] = base_image

        yaml_file = tmpdir.join(REPO_CONTAINER_CONFIG)
        yaml_file.write(yaml.dump(config_yaml))

        repo_info = RepoInfo(configuration=RepoConfiguration(str(tmpdir)))

        assert repo_info.base_image == base_image
Exemplo n.º 13
0
 def mock_repo_info(self, additional_tags=None):
     (flexmock(utils)
         .should_receive('get_repo_info')
         .with_args(TEST_GIT_URI, TEST_GIT_REF, git_branch=TEST_GIT_BRANCH)
         .and_return(RepoInfo(additional_tags=additional_tags)))
Exemplo n.º 14
0
    def test_v2_all_values_and_json(self):
        repo_conf = RepoConfiguration(git_branch=TEST_GIT_BRANCH,
                                      git_ref=TEST_GIT_REF,
                                      git_uri=TEST_GIT_URI)
        repo_info = RepoInfo(configuration=repo_conf)
        build_conf = Configuration(conf_file=None,
                                   build_from='image:buildroot:latest',
                                   orchestrator_deadline=4,
                                   scratch=False,
                                   worker_deadline=3)

        # all values that BuildUserParams stores
        param_kwargs = {
            # 'arrangement_version': self.arrangement_version,  # calculated value
            'base_image': 'buildroot:old',
            # 'build_from': 'buildroot:old',  # only one of build_*
            'build_json_dir': INPUTS_PATH,
            # 'build_image': 'buildroot:latest',
            # 'build_imagestream': 'buildroot:name_label',
            'build_type': BUILD_TYPE_WORKER,
            'component': TEST_COMPONENT,
            'compose_ids': [1, 2],
            'filesystem_koji_task_id': TEST_FILESYSTEM_KOJI_TASK_ID,
            'flatpak': False,
            # 'flatpak_base_image': self.flatpak_base_image,  # not used with false flatpack
            # 'git_branch': TEST_GIT_BRANCH,
            # 'git_ref': TEST_GIT_REF,
            # 'git_uri': TEST_GIT_URI,
            'image_tag': 'user/None:none-0-0',
            # 'imagestream_name': TEST_IMAGESTREAM,
            'include_koji_repo': True,
            'isolated': False,
            'koji_parent_build': 'fedora-26-9',
            'koji_target': 'tothepoint',
            'operator_bundle_replacement_pullspecs': {
                'foo/fedora:30': 'bar/fedora@sha256:deadbeef'
            },
            # "orchestrator_deadline": 4,  # set in config
            'parent_images_digests': {
                'registry.fedorahosted.org/fedora:29': {
                    'x86_64':
                    'registry.fedorahosted.org/fedora@sha256:8b96f2f9f88179a065738b2b37'
                    '35e386efb2534438c2a2f45b74358c0f344c81'
                }
            },
            # 'name': self.name,  # calculated value
            'platform': 'x86_64',
            'platforms': [
                'x86_64',
            ],
            # 'reactor_config_map': 'reactor-config-map',  # set in config
            'reactor_config_override': 'reactor-config-override',
            'release': '29',
            # 'scratch': True,  # set in config
            'signing_intent': False,
            'task_id': TEST_KOJI_TASK_ID,
            # 'trigger_imagestreamtag': 'base_image:latest',  # generated from base_image
            'user': TEST_USER,
            # 'yum_repourls': ,  # not used with compose_ids
            # "worker_deadline": 3,  # set in config
        }
        # additional values that BuildUserParams requires but stores under different names
        param_kwargs.update({
            'build_conf': build_conf,
            'name_label': 'name_label',
            'repo_info': repo_info,
        })
        rand = '12345'
        timestr = '20170731111111'
        (flexmock(sys.modules['osbs.build.user_params']).should_receive(
            'utcnow').once().and_return(
                datetime.datetime.strptime(timestr, '%Y%m%d%H%M%S')))

        (flexmock(random).should_receive('randrange').once().with_args(
            10**(len(rand) - 1), 10**len(rand)).and_return(int(rand)))

        spec = BuildUserParams.make_params(**param_kwargs)
        expected_json = {
            "arrangement_version":
            REACTOR_CONFIG_ARRANGEMENT_VERSION,
            "base_image":
            "buildroot:old",
            "build_from":
            "image:buildroot:latest",
            "build_image":
            "buildroot:latest",
            "build_json_dir":
            INPUTS_PATH,
            "build_type":
            "worker",
            "component":
            TEST_COMPONENT,
            "compose_ids": [1, 2],
            "customize_conf":
            "worker_customize.json",
            "filesystem_koji_task_id":
            TEST_FILESYSTEM_KOJI_TASK_ID,
            "include_koji_repo":
            True,
            "git_branch":
            TEST_GIT_BRANCH,
            "git_ref":
            TEST_GIT_REF,
            "git_uri":
            TEST_GIT_URI,
            "image_tag":
            "{}/{}:tothepoint-{}-{}-x86_64".format(TEST_USER, TEST_COMPONENT,
                                                   rand, timestr),
            "imagestream_name":
            "name_label",
            "kind":
            "build_user_params",
            "koji_parent_build":
            "fedora-26-9",
            "koji_target":
            "tothepoint",
            "name":
            "path-master-cd1e4",
            'operator_bundle_replacement_pullspecs': {
                'foo/fedora:30': 'bar/fedora@sha256:deadbeef'
            },
            "orchestrator_deadline":
            4,
            'parent_images_digests': {
                'registry.fedorahosted.org/fedora:29': {
                    'x86_64':
                    'registry.fedorahosted.org/fedora@sha256:8b96f2f9f88179a065738b2b37'
                    '35e386efb2534438c2a2f45b74358c0f344c81'
                }
            },
            "platform":
            "x86_64",
            "platforms": ["x86_64"],
            "reactor_config_override":
            "reactor-config-override",
            "release":
            "29",
            "trigger_imagestreamtag":
            "buildroot:old",
            "user":
            TEST_USER,
            "worker_deadline":
            3,
        }
        assert spec.to_json() == json.dumps(expected_json, sort_keys=True)

        spec2 = BuildUserParams.from_json(spec.to_json())
        assert spec2.to_json() == json.dumps(expected_json, sort_keys=True)
Exemplo n.º 15
0
 def test_default_params(self):
     repo_info = RepoInfo()
     assert repo_info.dockerfile_parser is None
     assert isinstance(repo_info.configuration, RepoConfiguration)
     assert isinstance(repo_info.additional_tags, AdditionalTagsConfig)
Exemplo n.º 16
0
 def mock_repo_info(self, mock_df_parser=None, mock_config=None):
     mock_df_parser = mock_df_parser or MockDfParser()
     config = mock_config or MockConfiguration()
     return RepoInfo(mock_df_parser, config)