Exemplo n.º 1
0
 def test_push_operator_manifest_publish_repo_no_public(self):
     """Make repos won't be published for non-public organizations"""
     qo = QuayOrganization(self.org,
                           self.cnr_token,
                           oauth_token='random',
                           public=False)
     (flexmock(qo).should_receive('publish_repo').and_return(None).never())
     qo.push_operator_manifest(self.repo, self.version, self.source_dir)
Exemplo n.º 2
0
 def test_push_operator_manifest_publish_repo_no_oauth(self, caplog):
     """Make sure that proper warning msg is logged"""
     qo = QuayOrganization(self.org, self.cnr_token, public=True)
     (flexmock(qo).should_receive('publish_repo').and_return(None).never())
     caplog.clear()
     with caplog.at_level(logging.ERROR):
         qo.push_operator_manifest(self.repo, self.version, self.source_dir)
     messages = (rec.message for rec in caplog.records)
     assert any('Oauth access is not configured' in m for m in messages)
Exemplo n.º 3
0
    def test_replace_registries_unconfigured(self):
        """Test if replace operation returns unchanged text"""
        org = 'testorg'
        qo = QuayOrganization(org, TOKEN)
        text = 'text'

        res = qo.replace_registries(text)
        assert res == text
        assert id(res) == id(text)
Exemplo n.º 4
0
 def test_push_operator_manifest_publish_repo(self):
     """Organizations marked as public will try to publish new
     repositories"""
     qo = QuayOrganization(self.org,
                           self.cnr_token,
                           oauth_token='random',
                           public=True)
     (flexmock(qo).should_receive('publish_repo').and_return(None).once())
     qo.push_operator_manifest(self.repo, self.version, self.source_dir)
Exemplo n.º 5
0
    def test_delete_release(self):
        """Test of deleting releases"""
        org = "test_org"
        repo = "test_repo"
        version = '1.2.3'

        qo = QuayOrganization(org, TOKEN)

        with requests_mock.Mocker() as m:
            m.delete(f'/cnr/api/v1/packages/{org}/{repo}/{version}/helm', )
            qo.delete_release(repo, version)
Exemplo n.º 6
0
    def test_get_releases(self):
        """Test if only proper releases are used and returned"""
        org = "test_org"
        repo = "test_repo"

        qo = QuayOrganization(org, TOKEN)
        (flexmock(qo).should_receive('get_releases_raw').and_return(
            ["1.0.0", "1.0.1-random", "1.2.0"]))

        expected = [ReleaseVersion.from_str(v) for v in ["1.0.0", "1.2.0"]]

        assert qo.get_releases(repo) == expected
Exemplo n.º 7
0
    def test_delete_release_quay_error(self, code, exc_class):
        """Test of error handling from quay errors"""
        org = "test_org"
        repo = "test_repo"
        version = '1.2.3'

        qo = QuayOrganization(org, TOKEN)

        with requests_mock.Mocker() as m:
            m.delete(f'/cnr/api/v1/packages/{org}/{repo}/{version}/helm',
                     status_code=code)
            with pytest.raises(exc_class):
                qo.delete_release(repo, version)
Exemplo n.º 8
0
    def test_publish_repo(self):
        """Test publishing repository"""
        org = 'testorg'
        repo = 'testrepo'

        qo = QuayOrganization(org, TOKEN, oauth_token='randomtoken')

        with requests_mock.Mocker() as m:
            m.post(
                f'/api/v1/repository/{org}/{repo}/changevisibility',
                status_code=requests.codes.ok,
            )
            qo.publish_repo(repo)
Exemplo n.º 9
0
    def test_get_releases_raw_errors(self, error_code, expected_exc_type):
        """Test that the proper exceptions are raised for various kinds
        of HTTP errors"""
        org = "test_org"
        repo = "test_repo"

        qo = QuayOrganization(org, TOKEN)

        with requests_mock.Mocker() as m:
            m.get(f'/cnr/api/v1/packages/{org}/{repo}', status_code=error_code)

            with pytest.raises(expected_exc_type):
                qo.get_releases_raw(repo)
Exemplo n.º 10
0
    def test_publish_repo_error(self):
        """Test if publishing repository raises proper exception"""
        org = 'testorg'
        repo = 'testrepo'

        qo = QuayOrganization(org, TOKEN, oauth_token='randomtoken')

        with requests_mock.Mocker() as m:
            m.post(
                f'/api/v1/repository/{org}/{repo}/changevisibility',
                status_code=requests.codes.server_error,
            )
            with pytest.raises(QuayPackageError):
                qo.publish_repo(repo)
Exemplo n.º 11
0
    def test_get_latest_release_version_not_found(self):
        """Test if proper exception is raised when no package is not found"""
        org = "test_org"
        repo = "test_repo"

        with requests_mock.Mocker() as m:
            m.get(
                '/cnr/api/v1/packages/{}/{}'.format(org, repo),
                status_code=404,
            )

            qo = QuayOrganization(org, "token")
            with pytest.raises(QuayPackageNotFound):
                qo.get_latest_release_version(repo)
Exemplo n.º 12
0
    def _test_courier_exception(self, courier_exception,
                                expected_omps_exception, expected_dict, caplog,
                                op_courier_push_raising):
        qo = QuayOrganization(self.org, self.cnr_token)

        with op_courier_push_raising(courier_exception):
            with pytest.raises(expected_omps_exception) as exc_info, \
                    caplog.at_level(logging.ERROR):
                qo.push_operator_manifest(self.repo, self.version,
                                          self.source_dir)

        e = exc_info.value
        assert e.to_dict() == expected_dict
        assert any('Operator courier call failed' in message
                   for message in caplog.messages)
Exemplo n.º 13
0
    def test_get_latest_release_version_not_found(self):
        """Test if proper exception is raised when no package is not found"""
        org = "test_org"
        repo = "test_repo"

        with requests_mock.Mocker() as m:
            m.get(f'/cnr/api/v1/packages?namespace={org}',
                  json=[{
                      'name': 'org/something_else',
                      'releases': ["2.0.0"],
                  }])

            qo = QuayOrganization(org, "token")
            with pytest.raises(QuayPackageNotFound):
                qo.get_latest_release_version(repo)
Exemplo n.º 14
0
def test_replace_registries(datadir):
    """Test if registry is replaced in all files"""
    def _yield_yaml_files(dir_path):
        for root, _, files in os.walk(dir_path):
            for fname in files:
                fname_lower = fname.lower()
                if fname_lower.endswith('.yml') or fname_lower.endswith(
                        '.yaml'):
                    yield os.path.join(root, fname)

    dir_path = os.path.join(datadir, 'etcd_op_nested')
    old = 'quay.io'
    new = 'example.com'
    qo = QuayOrganization('testorg',
                          'random token',
                          replace_registry_conf=[{
                              'old': old,
                              'new': new,
                              'regexp': False
                          }])

    should_be_replaced = set()
    for fpath in _yield_yaml_files(dir_path):
        with open(fpath, 'r') as f:
            text = f.read()
            if old in text:
                should_be_replaced.add(fpath)

    replace_registries(qo, dir_path)

    for fpath in should_be_replaced:
        with open(fpath, 'r') as f:
            text = f.read()
            assert new in text
            assert old not in text
Exemplo n.º 15
0
    def test_get_latest_release_version(self):
        """Test getting the latest release version"""
        org = "test_org"
        repo = "test_repo"

        with requests_mock.Mocker() as m:
            m.get(f'/cnr/api/v1/packages?namespace={org}',
                  json=[
                      {
                          'name': 'org/something_else',
                          'releases': ["2.0.0"],
                      },
                      {
                          'name': f'{org}/{repo}',
                          'releases': ["1.2.0", "1.1.0", "1.0.0"]
                      },
                  ])

            qo = QuayOrganization(org, "token")
            latest = qo.get_latest_release_version(repo)
            assert str(latest) == "1.2.0"
Exemplo n.º 16
0
    def test_registry_replacing_enabled(self, enabled):
        """Test if property returns correct value"""
        if enabled:
            replace_conf = [{'old': 'reg_old', 'new': 'reg_new'}]
        else:
            replace_conf = None

        org = 'testorg'

        qo = QuayOrganization(org, TOKEN, replace_registry_conf=replace_conf)

        assert qo.registry_replacing_enabled == enabled
Exemplo n.º 17
0
    def test_get_latest_release_version(self):
        """Test getting the latest release version"""
        org = "test_org"
        repo = "test_repo"

        with requests_mock.Mocker() as m:
            m.get('/cnr/api/v1/packages/{}/{}'.format(org, repo),
                  json=[
                      {
                          'release': "1.0.0"
                      },
                      {
                          'release': "1.2.0"
                      },
                      {
                          'release': "1.0.1"
                      },
                  ])

            qo = QuayOrganization(org, "token")
            latest = qo.get_latest_release_version(repo)
            assert str(latest) == "1.2.0"
Exemplo n.º 18
0
    def test_get_releases_raw(self):
        """Test if all release are returned from quay.io, including format that
        is OMPS invalid"""
        org = "test_org"
        repo = "test_repo"

        with requests_mock.Mocker() as m:
            m.get(f'/cnr/api/v1/packages?namespace={org}',
                  json=[
                      {
                          'name': 'org/something_else',
                          'releases': ["2.0.0"],
                      },
                      {
                          'name': f'{org}/{repo}',
                          'releases': ["1.2.0", "1.0.1-random", "1.0.0"]
                      },
                  ])

            qo = QuayOrganization(org, "token")
            releases = qo.get_releases_raw(repo)
            assert sorted(releases) == ["1.0.0", "1.0.1-random", "1.2.0"]
Exemplo n.º 19
0
    def test_get_releases_raw(self):
        """Test if all release are returned from quay.io, including format that
        is OMPS invalid"""
        org = "test_org"
        repo = "test_repo"

        with requests_mock.Mocker() as m:
            m.get('/cnr/api/v1/packages/{}/{}'.format(org, repo),
                  json=[
                      {
                          'release': "1.0.0"
                      },
                      {
                          'release': "1.2.0"
                      },
                      {
                          'release': "1.0.1-random"
                      },
                  ])

            qo = QuayOrganization(org, "token")
            releases = qo.get_releases_raw(repo)
            assert sorted(releases) == ["1.0.0", "1.0.1-random", "1.2.0"]
Exemplo n.º 20
0
    def test_get_latest_release_version_invalid_version_only(self):
        """Test if proper exception is raised when packages only with invalid
        version are available

        Invalid versions should be ignored, thus QuayPackageNotFound
        should be raised as may assume that OMPS haven't managed that packages
        previously
        """
        org = "test_org"
        repo = "test_repo"

        with requests_mock.Mocker() as m:
            m.get(f'/cnr/api/v1/packages?namespace={org}',
                  json=[
                      {
                          'name': f'{org}/{repo}',
                          'releases': ["1.0.0-invalid"]
                      },
                  ])

            qo = QuayOrganization(org, "token")
            with pytest.raises(QuayPackageNotFound):
                qo.get_latest_release_version(repo)
Exemplo n.º 21
0
def test_adjust_csv_annotations(datadir):
    """Test if annotations are applied in ClusterServiceVersion"""
    dir_path = os.path.join(datadir, 'etcd_op_nested')
    csv_annotations = [
        {
            'name': 'simple',
            'value': 'simple-value'
        },
        {
            'name': 'complex',
            'value': 'value-{package_name}'
        },
    ]
    expected_annotations = {
        'simple': 'simple-value',
        'complex': 'value-etcd',
    }
    quay_org = QuayOrganization('testorg',
                                'random token',
                                csv_annotations=csv_annotations)

    should_have_annotations = set()
    for fpath in _yield_yaml_files(dir_path):
        with open(fpath, 'r') as f:
            text = f.read()
            if 'ClusterServiceVersion' in text:
                should_have_annotations.add(fpath)

    assert should_have_annotations, 'Insufficient test data'

    adjust_csv_annotations(quay_org, dir_path, {'package_name': 'etcd'})

    yaml = get_yaml_parser()
    for fpath in should_have_annotations:
        with open(fpath, 'r') as f:
            contents = yaml.load(f.read())
            for name, value in expected_annotations.items():
                assert contents['metadata']['annotations'][name] == value
 def test_repository_suffix(self, repo, suffix, expected, organization):
     """Test if repository_suffix property returns correct value"""
     qo = QuayOrganization(organization, TOKEN, repository_suffix=suffix)
     assert qo.adjust_repository_name(repo) == expected
def test_push_zipfile_with_package_name_suffix(
        mocked_org_manager, client, valid_manifests_archive,
        endpoint_push_zipfile, mocked_quay_io, mocked_op_courier_push,
        auth_header, suffix_func, expected_pkg_name_func):
    """Test REST API for pushing operators form zipfile with package_name_suffix"""
    original_pkg_name = valid_manifests_archive.pkg_name
    expected_pkg_name = expected_pkg_name_func(original_pkg_name)

    mocked_org_manager.get_org.return_value = QuayOrganization(
        endpoint_push_zipfile.org,
        'cnr_token',
        package_name_suffix=suffix_func(original_pkg_name))

    def verify_modified_package_name(repo, version, source_dir):
        # The modified YAML file should retain comments and defined order. The
        # only modification should be the package name.
        if valid_manifests_archive.pkg_name == 'marketplace':
            pkg_manifest = ('deploy/chart/catalog_resources/rh-operators/'
                            'marketplace.v0.0.1.clusterserviceversion.yaml')
            expected_packages_yaml = dedent("""\
                #! package-manifest: {pkg_manifest}
                packageName: {pkg_name}
                channels:
                - name: alpha
                  currentCSV: marketplace-operator.v0.0.1
                """)
            packages_yaml_path = os.path.join(source_dir, 'packages.yaml')
        elif valid_manifests_archive.pkg_name == 'etcd':
            pkg_manifest = ('./deploy/chart/catalog_resources/rh-operators/'
                            'etcdoperator.v0.9.2.clusterserviceversion.yaml')
            expected_packages_yaml = dedent("""\
                #! package-manifest: {pkg_manifest}
                packageName: {pkg_name}
                channels:
                - name: alpha
                  currentCSV: etcdoperator.v0.9.2
                """)
            packages_yaml_path = os.path.join(source_dir, 'etcd.package.yaml')
        else:
            raise ValueError(
                f'Unsupported manifests archive, {valid_manifests_archive}')
        expected_packages_yaml = expected_packages_yaml.format(
            pkg_manifest=pkg_manifest, pkg_name=expected_pkg_name)
        with open(packages_yaml_path) as f:
            assert f.read() == expected_packages_yaml

    flexmock(QuayOrganization)\
        .should_receive('push_operator_manifest')\
        .replace_with(verify_modified_package_name)\
        .once()

    with open(valid_manifests_archive.path, 'rb') as f:
        rv = client.post(
            endpoint_push_zipfile.url_path,
            headers=auth_header,
            data={'file': (f, f.name)},
            content_type='multipart/form-data',
        )

    assert rv.status_code == 200, rv.get_json()
    # In V1, package_name_suffix does not change the repository name.
    assert rv.get_json()['repo'] == endpoint_push_zipfile.repo
Exemplo n.º 24
0
 def test_regexp_replace_registries(self, text, expected):
     """Test if registries are replaced properly with regexp"""
     replace_conf = [{'old': 'reg_old$', 'new': 'reg_new', 'regexp': True}]
     org = 'testorg'
     qo = QuayOrganization(org, TOKEN, replace_registry_conf=replace_conf)
     assert qo.replace_registries(text) == expected
Exemplo n.º 25
0
    def test_push_operator_manifest(self):
        """Test for pushing operator manifest"""

        qo = QuayOrganization(self.org, self.cnr_token)
        qo.push_operator_manifest(self.repo, self.version, self.source_dir)