Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Teach the integration tests how to check nested bundles
Browse files Browse the repository at this point in the history
Signed-off-by: Hunor Csomortáni <csomh@redhat.com>
  • Loading branch information
Hunor Csomortáni authored and MartinBasti committed Jul 22, 2019
1 parent 2d73d9b commit 79afce8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 21 deletions.
19 changes: 9 additions & 10 deletions tests/integration/push_nvr/push_nvr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import requests
import pytest
from operatorcourier import api as courier
from tests.integration.utils import test_env, Bundle
from tests.integration.utils import test_env, make_bundle


def test_invalid_zip(omps):
Expand Down Expand Up @@ -86,11 +86,11 @@ def test_valid_zip_default_version(omps, quay, koji, tmp_path):
test_env['test_package'], '1.0.0',
authorization=None)

quay_bundle = Bundle(quay.get_bundle(test_env['test_namespace'],
test_env['test_package'], '1.0.0',
authorization=None))
quay_bundle = make_bundle(quay.get_bundle(test_env['test_namespace'],
test_env['test_package'], '1.0.0',
authorization=None))
koji.download_manifest(nvr, tmp_path)
koji_bundle = Bundle(
koji_bundle = make_bundle(
courier.build_and_verify(source_dir=tmp_path.as_posix()).bundle)

# Note: this only confirms that OMPS used the right data from Koji,
Expand Down Expand Up @@ -162,13 +162,12 @@ def test_nested_manifest(omps, quay, koji, tmp_path):
test_env['test_package'], '1.0.0',
authorization=None)

quay_bundle = Bundle(quay.get_bundle(test_env['test_namespace'],
test_env['test_package'], '1.0.0',
authorization=None))
quay_bundle = make_bundle(quay.get_bundle(test_env['test_namespace'],
test_env['test_package'], '1.0.0',
authorization=None))

koji.download_manifest(nvr, tmp_path)
koji_bundle = Bundle(
courier.build_and_verify(source_dir=tmp_path.as_posix()).bundle)
koji_bundle = make_bundle(tmp_path.as_posix())

# Note: this only confirms that OMPS used the right data from Koji,
# but tells nothing about the correctness of that data.
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/replace_registry/replace_registry_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest
from distutils import dir_util
from itertools import cycle
from tests.integration.utils import test_env, Bundle
from tests.integration.utils import test_env, make_bundle


def is_yaml_file(path):
Expand Down Expand Up @@ -72,7 +72,7 @@ def koji_registries(replace_conf, path):
)
response.raise_for_status()

quay_bundle = Bundle(
quay_bundle = make_bundle(
quay.get_bundle(
test_env["test_namespace"],
test_env["test_package"],
Expand Down Expand Up @@ -149,7 +149,7 @@ def test_replace_during_archive_push(omps, quay, tmp_path_factory, manifest_path
)
response.raise_for_status()

quay_bundle = Bundle(
quay_bundle = make_bundle(
quay.get_bundle(
test_env["test_namespace"],
test_env["test_package"],
Expand Down
69 changes: 61 additions & 8 deletions tests/integration/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,41 @@ def load_test_env():
test_env = load_test_env()


class Bundle:
"""Wrapper to ease working with bundles.
def make_bundle(bundle):
"""Make a bundle to be used by the tests.
Args:
bundle: either a bundle as a dictionary OR
a nested bundle as concatenated files OR
a path to a directory of a nested bundle.
Returns:
A FlatBundle or a string, which has the files of a nested bundle
concatenated.
Raises:
AssertionError if the argument is of a type that is not handled.
"""
if isinstance(bundle, dict):
return FlatBundle(bundle)
elif isinstance(bundle, str):
b = bundle
if os.path.isdir(bundle):
b = concatenated_files(bundle)
return b
else:
assert False


class FlatBundle():
"""Wrapper to ease working with flat-bundles.
"""

FIELDS = ('clusterServiceVersions', 'customResourceDefinitions', 'packages')

def __init__(self, bundle_dict):
self.bundle_dict = bundle_dict

def __eq__(self, other):
for field in Bundle.FIELDS:
for field in FlatBundle.FIELDS:
sd = yaml.safe_load(self.bundle_dict['data'][field])
od = yaml.safe_load(other.bundle_dict['data'][field])

Expand All @@ -47,7 +71,7 @@ def __eq__(self, other):
return True

def __contains__(self, text):
for field in Bundle.FIELDS:
for field in FlatBundle.FIELDS:
if text in self.bundle_dict['data'][field]:
return True

Expand Down Expand Up @@ -293,9 +317,15 @@ def get_bundle(self, namespace, package, release, authorization=True):
with tarfile.open(archive, 'r:gz') as tar:
tar.extractall(path=tempdir)

bundle_file = '{tempdir}/bundle.yaml'.format(tempdir=tempdir)
with open(bundle_file, 'r') as f:
bundle = yaml.safe_load(f)
os.remove(archive)
bundle_file = f"{tempdir}/bundle.yaml"
if os.path.isfile(bundle_file):
with open(bundle_file, 'r') as f:
bundle = yaml.safe_load(f)
else:
# Assume that this is a nested bundle. Concatenate the sorted
# files in a single string.
bundle = concatenated_files(tempdir)

return bundle

Expand Down Expand Up @@ -442,3 +472,26 @@ def download_manifest(self, nvr, tmpdir):
z.extractall(tmpdir)

os.remove(archive)


def concatenated_files(directory):
"""Takes a directory and recursively concatenates all the files in it.
Files are sorted before concatenation.
Args:
directory: path to the directory to be parsed
Returns:
String with the content of all the files found, concatenated.
"""
ret = ''
files = []
for dirpath, dirnames, filenames in os.walk(directory):
for filename in filenames:
files.append(os.path.join(dirpath, filename))
files.sort()
for file in files:
with open(file, 'r') as fp:
ret += fp.read()
return ret

0 comments on commit 79afce8

Please sign in to comment.