From c15a7cadf390677fcdc62e85e15f414294b8a7fd Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Sat, 29 Jun 2019 17:08:52 +0800 Subject: [PATCH] Reuse function read_staged_data In addition, reuse staged_data_filename to construct file and directory name. Signed-off-by: Chenxiong Qi --- tests/__init__.py | 36 +++++++--------- tests/conftest.py | 10 ++--- tests/test_build/test_build.py | 39 +++++++---------- tests/test_builder/test_mock.py | 8 ++-- tests/test_models/test_models.py | 14 ++----- tests/test_resolver/test_db.py | 10 ++--- tests/test_resolver/test_local.py | 4 +- tests/test_resolver/test_mbs.py | 2 +- tests/test_scheduler/test_module_init.py | 13 +++--- tests/test_scheduler/test_module_wait.py | 10 ++--- tests/test_utils/test_utils.py | 52 ++++++++++------------- tests/test_utils/test_utils_mse.py | 13 ++---- tests/test_views/test_views.py | 53 ++++++++++-------------- 13 files changed, 104 insertions(+), 160 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 620e5811..caa2ade3 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -26,7 +26,7 @@ from mock import patch import time import hashlib from traceback import extract_stack -from module_build_service.utils import to_text_type, load_mmd_file +from module_build_service.utils import to_text_type, load_mmd import koji import module_build_service @@ -44,16 +44,22 @@ app = module_build_service.app conf = init_config(app) +def staged_data_filename(filename): + return os.path.join(base_dir, "staged_data", filename) + + def read_staged_data(yaml_name): """Read module YAML content from staged_data directory - :param str yaml_name: name of YAML file without extension ``.yaml``. + :param str yaml_name: name of YAML file which could be with or without + extension ``.yaml``. ``.yaml`` will be added if extension is omitted. :return: module YAML file's content. :rtype: str :raises ValueError: if specified module YAML file does not exist in staged_data directory. """ - filename = os.path.join(base_dir, "staged_data", "{}.yaml".format(yaml_name)) + filename = staged_data_filename( + yaml_name if '.' in yaml_name else "{}.yaml".format(yaml_name)) if not os.path.exists(filename): raise ValueError("Staged data {}.yaml does not exist.".format(yaml_name)) with open(filename, "r") as mmd: @@ -114,7 +120,7 @@ def clean_database(add_platform_module=True, add_default_arches=True): db.session.commit() if add_platform_module: - mmd = load_mmd_file(os.path.join(base_dir, "staged_data", "platform.yaml")) + mmd = load_mmd(read_staged_data("platform")) import_mmd(db.session, mmd) @@ -133,7 +139,7 @@ def init_data(data_size=10, contexts=False, multiple_stream_versions=None, scrat if multiple_stream_versions: if multiple_stream_versions is True: multiple_stream_versions = ["f28.0.0", "f29.0.0", "f29.1.0", "f29.2.0"] - mmd = load_mmd_file(os.path.join(base_dir, "staged_data", "platform.yaml")) + mmd = load_mmd(read_staged_data("platform")) for stream in multiple_stream_versions: mmd = mmd.copy("platform", stream) @@ -334,10 +340,7 @@ def scheduler_init_data(db_session, tangerine_state=None, scratch=False): """ clean_database() - current_dir = os.path.dirname(__file__) - formatted_testmodule_yml_path = os.path.join( - current_dir, "staged_data", "formatted_testmodule.yaml") - mmd = load_mmd_file(formatted_testmodule_yml_path) + mmd = load_mmd(read_staged_data("formatted_testmodule")) mmd.get_rpm_component("tangerine").set_buildorder(0) module_build = module_build_service.models.ModuleBuild( @@ -440,10 +443,7 @@ def scheduler_init_data(db_session, tangerine_state=None, scratch=False): def reuse_component_init_data(): clean_database() - current_dir = os.path.dirname(__file__) - formatted_testmodule_yml_path = os.path.join( - current_dir, "staged_data", "formatted_testmodule.yaml") - mmd = load_mmd_file(formatted_testmodule_yml_path) + mmd = load_mmd(read_staged_data("formatted_testmodule")) build_one = module_build_service.models.ModuleBuild( name="testmodule", @@ -635,10 +635,7 @@ def reuse_shared_userspace_init_data(): with make_session(conf) as session: # Create shared-userspace-570, state is COMPLETE, all components # are properly built. - current_dir = os.path.dirname(__file__) - formatted_testmodule_yml_path = os.path.join( - current_dir, "staged_data", "shared-userspace-570.yaml") - mmd = load_mmd_file(formatted_testmodule_yml_path) + mmd = load_mmd(read_staged_data("shared-userspace-570")) module_build = module_build_service.models.ModuleBuild( name=mmd.get_module_name(), @@ -691,10 +688,7 @@ def reuse_shared_userspace_init_data(): session.commit() # Create shared-userspace-577, state is WAIT, no component built - formatted_testmodule_yml_path = os.path.join( - current_dir, "staged_data", "shared-userspace-577.yaml" - ) - mmd2 = load_mmd_file(formatted_testmodule_yml_path) + mmd2 = load_mmd(read_staged_data("shared-userspace-577")) module_build = module_build_service.models.ModuleBuild( name=mmd2.get_module_name(), diff --git a/tests/conftest.py b/tests/conftest.py index c2ec80a3..51c96e9a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -24,19 +24,19 @@ import pytest from module_build_service import conf from module_build_service.models import make_session -from module_build_service.utils.general import load_mmd_file, mmd_to_str - +from module_build_service.utils.general import mmd_to_str, load_mmd +from tests import read_staged_data BASE_DIR = os.path.dirname(__file__) STAGED_DATA_DIR = os.path.join(BASE_DIR, "staged_data") -_mmd = load_mmd_file(os.path.join(STAGED_DATA_DIR, "platform.yaml")) +_mmd = load_mmd(read_staged_data("platform")) PLATFORM_MODULEMD = mmd_to_str(_mmd) -_mmd2 = load_mmd_file(os.path.join(STAGED_DATA_DIR, "formatted_testmodule.yaml")) +_mmd2 = load_mmd(read_staged_data("formatted_testmodule")) TESTMODULE_MODULEMD = mmd_to_str(_mmd2) -_mmd3 = load_mmd_file(os.path.join(STAGED_DATA_DIR, "formatted_testmodule.yaml")) +_mmd3 = load_mmd(read_staged_data("formatted_testmodule")) _mmd3.set_context("c2c572ed") TESTMODULE_MODULEMD_SECOND_CONTEXT = mmd_to_str(_mmd3) diff --git a/tests/test_build/test_build.py b/tests/test_build/test_build.py index 1e4c40e7..0fcb028e 100644 --- a/tests/test_build/test_build.py +++ b/tests/test_build/test_build.py @@ -29,9 +29,9 @@ from shutil import copyfile from datetime import datetime, timedelta from random import randint import hashlib -from module_build_service.utils import to_text_type import module_build_service.messaging +import module_build_service.scheduler.consumer import module_build_service.scheduler.handlers.repos import module_build_service.utils from module_build_service.errors import Forbidden @@ -43,14 +43,15 @@ from werkzeug.datastructures import FileStorage import kobo import pytest -from tests import app, reuse_component_init_data, clean_database import json import itertools from module_build_service.builder.base import GenericBuilder from module_build_service.builder.KojiModuleBuilder import KojiModuleBuilder -import module_build_service.scheduler.consumer from module_build_service.messaging import MBSModule +from tests import ( + app, reuse_component_init_data, clean_database, read_staged_data, staged_data_filename +) base_dir = dirname(dirname(__file__)) @@ -81,9 +82,7 @@ class FakeSCM(object): def checkout(self, temp_dir): self.sourcedir = path.join(temp_dir, self.name) mkdir(self.sourcedir) - base_dir = path.abspath(path.dirname(__file__)) - copyfile( - path.join(base_dir, "..", "staged_data", self.mmd_filename), self.get_module_yaml()) + copyfile(staged_data_filename(self.mmd_filename), self.get_module_yaml()) return self.sourcedir @@ -578,9 +577,8 @@ class TestBuild(BaseTestBuild): ): FakeSCM(mocked_scm, "testmodule", "testmodule.yaml") - testmodule = os.path.join(base_dir, "staged_data", "testmodule.yaml") - with open(testmodule) as f: - yaml = to_text_type(f.read()) + testmodule_filename = staged_data_filename("testmodule.yaml") + yaml = read_staged_data("testmodule") with patch.object( module_build_service.config.Config, @@ -591,7 +589,7 @@ class TestBuild(BaseTestBuild): rv = self.client.post( "/module-build-service/1/module-builds/", content_type="multipart/form-data", - data={"yaml": (testmodule, yaml)}, + data={"yaml": (testmodule_filename, yaml)}, ) data = json.loads(rv.data) assert data["status"] == 403 @@ -604,7 +602,6 @@ class TestBuild(BaseTestBuild): ): FakeSCM( mocked_scm, "testmodule", "testmodule.yaml", "620ec77321b2ea7b0d67d82992dda3e1d67055b4") - testmodule = os.path.join(base_dir, "staged_data", "testmodule.yaml") with patch.object( module_build_service.config.Config, @@ -612,7 +609,7 @@ class TestBuild(BaseTestBuild): new_callable=PropertyMock, return_value=True, ): - with open(testmodule, "rb") as f: + with open(staged_data_filename("testmodule.yaml"), "rb") as f: yaml_file = FileStorage(f) rv = self.client.post( "/module-build-service/1/module-builds/", @@ -1123,11 +1120,7 @@ class TestBuild(BaseTestBuild): build_one.runtime_context = "9c690d0e" build_one.context = "9c690d0e" build_one.state = models.BUILD_STATES["failed"] - current_dir = os.path.dirname(__file__) - formatted_testmodule_yml_path = os.path.join( - current_dir, "..", "staged_data", "formatted_testmodule.yaml") - with open(formatted_testmodule_yml_path, "r") as f: - build_one.modulemd = to_text_type(f.read()) + build_one.modulemd = read_staged_data("formatted_testmodule") build_one.koji_tag = "module-testmodule-master-20180205135154-9c690d0e" build_one.scmurl = "https://src.stg.fedoraproject.org/modules/testmodule.git?#7fea453" build_one.batch = 2 @@ -1266,11 +1259,7 @@ class TestBuild(BaseTestBuild): # this is not calculated by real but just a value to # match the calculated context from expanded test mmd build_one.context = "9c690d0e" - current_dir = os.path.dirname(__file__) - formatted_testmodule_yml_path = os.path.join( - current_dir, "..", "staged_data", "formatted_testmodule.yaml") - with open(formatted_testmodule_yml_path, "r") as f: - build_one.modulemd = to_text_type(f.read()) + build_one.modulemd = read_staged_data("formatted_testmodule") build_one.koji_tag = "module-testmodule-master-20180205135154-6ef9a711" build_one.scmurl = "https://src.stg.fedoraproject.org/modules/testmodule.git?#7fea453" build_one.batch = 2 @@ -1684,8 +1673,8 @@ class TestBuild(BaseTestBuild): Test that when a build is submitted with a buildrequire without a Koji tag, MBS doesn't supply it as a dependency to the builder. """ - metadata_mmd = module_build_service.utils.load_mmd_file( - path.join(base_dir, "staged_data", "build_metadata_module.yaml") + metadata_mmd = module_build_service.utils.load_mmd( + read_staged_data("build_metadata_module") ) module_build_service.utils.import_mmd(db.session, metadata_mmd) @@ -1744,7 +1733,7 @@ class TestLocalBuild(BaseTestBuild): @patch( "module_build_service.config.Config.mock_resultsdir", new_callable=PropertyMock, - return_value=path.join(base_dir, "staged_data", "local_builds"), + return_value=staged_data_filename('local_builds'), ) def test_submit_build_local_dependency( self, resultsdir, mocked_scm, mocked_get_user, conf_system, hmsc, db_session diff --git a/tests/test_builder/test_mock.py b/tests/test_builder/test_mock.py index 2384eaef..14efb04b 100644 --- a/tests/test_builder/test_mock.py +++ b/tests/test_builder/test_mock.py @@ -10,8 +10,8 @@ import kobo.rpmlib from module_build_service import conf from module_build_service.models import ModuleBuild, ComponentBuild, make_session from module_build_service.builder.MockModuleBuilder import MockModuleBuilder -from module_build_service.utils import import_fake_base_module, load_mmd_file, mmd_to_str -from tests import clean_database, make_module +from module_build_service.utils import import_fake_base_module, mmd_to_str, load_mmd +from tests import clean_database, make_module, read_staged_data class TestMockModuleBuilder: @@ -24,9 +24,7 @@ class TestMockModuleBuilder: shutil.rmtree(self.resultdir) def _create_module_with_filters(self, session, batch, state): - base_dir = os.path.abspath(os.path.dirname(__file__)) - mmd = load_mmd_file( - os.path.join(base_dir, "..", "staged_data", "testmodule-with-filters.yaml")) + mmd = load_mmd(read_staged_data("testmodule-with-filters")) # Set the name and stream mmd = mmd.copy("mbs-testmodule", "test") mmd.set_xmd({ diff --git a/tests/test_models/test_models.py b/tests/test_models/test_models.py index 2fcf66fb..0ab3d54a 100644 --- a/tests/test_models/test_models.py +++ b/tests/test_models/test_models.py @@ -20,14 +20,13 @@ # # Written by Ralph Bean -import os import pytest from mock import patch from module_build_service import conf from module_build_service.models import ComponentBuild, ModuleBuild, make_session -from module_build_service.utils.general import load_mmd_file, mmd_to_str -from tests import init_data as init_data_contexts, clean_database, make_module +from module_build_service.utils.general import mmd_to_str, load_mmd +from tests import init_data as init_data_contexts, clean_database, make_module, read_staged_data from tests.test_models import init_data, module_build_from_modulemd @@ -66,10 +65,7 @@ class TestModels: """ Test that the build_context, runtime_context, and context hashes are correctly determined""" build = ModuleBuild.query.filter_by(id=1).one() - yaml_path = os.path.join( - os.path.dirname(__file__), "..", "staged_data", "testmodule_dependencies.yaml") - mmd = load_mmd_file(yaml_path) - build.modulemd = mmd_to_str(mmd) + build.modulemd = read_staged_data("testmodule_dependencies") ( build.ref_build_context, build.build_context, @@ -86,9 +82,7 @@ class TestModels: the same name:stream:version """ clean_database() - yaml_path = os.path.join( - os.path.dirname(__file__), "..", "staged_data", "formatted_testmodule.yaml") - mmd = load_mmd_file(yaml_path) + mmd = load_mmd(read_staged_data("formatted_testmodule")) with make_session(conf) as session: for i in range(3): build = module_build_from_modulemd(mmd_to_str(mmd)) diff --git a/tests/test_resolver/test_db.py b/tests/test_resolver/test_db.py index 22ea5fa2..6ad0bbea 100644 --- a/tests/test_resolver/test_db.py +++ b/tests/test_resolver/test_db.py @@ -28,7 +28,7 @@ import pytest import module_build_service.resolver as mbs_resolver from module_build_service import app, conf, db, models, utils, Modulemd -from module_build_service.utils import import_mmd, load_mmd_file, mmd_to_str +from module_build_service.utils import import_mmd, mmd_to_str, load_mmd from module_build_service.models import ModuleBuild import tests @@ -41,7 +41,7 @@ class TestDBModule: tests.reuse_component_init_data() def test_get_buildrequired_modulemds(self): - mmd = load_mmd_file(os.path.join(base_dir, "staged_data", "platform.yaml")) + mmd = load_mmd(tests.read_staged_data("platform")) mmd = mmd.copy(mmd.get_module_name(), "f30.1.3") with models.make_session(conf) as db_session: import_mmd(db_session, mmd) @@ -153,7 +153,7 @@ class TestDBModule: @patch( "module_build_service.config.Config.mock_resultsdir", new_callable=PropertyMock, - return_value=os.path.join(base_dir, "staged_data", "local_builds"), + return_value=tests.staged_data_filename("local_builds"), ) def test_get_module_build_dependencies_recursive_requires(self, resultdir, conf_system): """ @@ -166,7 +166,7 @@ class TestDBModule: resolver = mbs_resolver.GenericResolver.create(tests.conf, backend="db") result = resolver.get_module_build_dependencies(mmd=build[0].mmd()).keys() - local_path = os.path.join(base_dir, "staged_data", "local_builds") + local_path = tests.staged_data_filename("local_builds") expected = [os.path.join(local_path, "module-parent-master-20170816080815/results")] assert set(result) == set(expected) @@ -240,7 +240,7 @@ class TestDBModule: @patch( "module_build_service.config.Config.mock_resultsdir", new_callable=PropertyMock, - return_value=os.path.join(base_dir, "staged_data", "local_builds"), + return_value=tests.staged_data_filename("local_builds") ) def test_resolve_profiles_local_module(self, local_builds, conf_system): """ diff --git a/tests/test_resolver/test_local.py b/tests/test_resolver/test_local.py index 3218a64b..07384513 100644 --- a/tests/test_resolver/test_local.py +++ b/tests/test_resolver/test_local.py @@ -25,7 +25,7 @@ from datetime import datetime import module_build_service.resolver as mbs_resolver from module_build_service import db -from module_build_service.utils.general import import_mmd, load_mmd_file, mmd_to_str +from module_build_service.utils.general import import_mmd, mmd_to_str, load_mmd from module_build_service.models import ModuleBuild import tests @@ -38,7 +38,7 @@ class TestLocalResolverModule: tests.reuse_component_init_data() def test_get_buildrequired_modulemds(self): - mmd = load_mmd_file(os.path.join(base_dir, "staged_data", "platform.yaml")) + mmd = load_mmd(tests.read_staged_data("platform")) mmd = mmd.copy(mmd.get_module_name(), "f8") import_mmd(db.session, mmd) platform_f8 = ModuleBuild.query.filter_by(stream="f8").one() diff --git a/tests/test_resolver/test_mbs.py b/tests/test_resolver/test_mbs.py index 12a82cae..271feab4 100644 --- a/tests/test_resolver/test_mbs.py +++ b/tests/test_resolver/test_mbs.py @@ -348,7 +348,7 @@ class TestMBSModule: @patch( "module_build_service.config.Config.mock_resultsdir", new_callable=PropertyMock, - return_value=os.path.join(base_dir, "staged_data", "local_builds"), + return_value=tests.staged_data_filename("local_builds") ) def test_resolve_profiles_local_module( self, local_builds, conf_system, formatted_testmodule_mmd diff --git a/tests/test_scheduler/test_module_init.py b/tests/test_scheduler/test_module_init.py index b05a79ed..8f0f2d80 100644 --- a/tests/test_scheduler/test_module_init.py +++ b/tests/test_scheduler/test_module_init.py @@ -23,21 +23,20 @@ import os from mock import patch, PropertyMock -from tests import conf, clean_database +from tests import conf, clean_database, read_staged_data from tests.test_views.test_views import FakeSCM import module_build_service.messaging import module_build_service.scheduler.handlers.modules from module_build_service import build_logs, db from module_build_service.models import make_session, ModuleBuild, ComponentBuild -from module_build_service.utils.general import mmd_to_str, load_mmd, load_mmd_file +from module_build_service.utils.general import mmd_to_str, load_mmd class TestModuleInit: def setup_method(self, test_method): self.fn = module_build_service.scheduler.handlers.modules.init - self.staged_data_dir = os.path.join(os.path.dirname(__file__), "../", "staged_data") - testmodule_yml_path = os.path.join(self.staged_data_dir, "testmodule_init.yaml") - mmd = load_mmd_file(testmodule_yml_path) + testmodule_yml_path = read_staged_data("testmodule_init") + mmd = load_mmd(testmodule_yml_path) # Set the name and stream mmd = mmd.copy("testmodule", "1") scmurl = "git://pkgs.domain.local/modules/testmodule?#620ec77" @@ -146,8 +145,8 @@ class TestModuleInit: @patch("module_build_service.utils.submit.get_build_arches", return_value=["x86_64"]) def test_init_includedmodule(self, get_build_arches, mocked_scm, mocked_mod_allow_repo): FakeSCM(mocked_scm, "includedmodules", ["testmodule_init.yaml"]) - includedmodules_yml_path = os.path.join(self.staged_data_dir, "includedmodules.yaml") - mmd = load_mmd_file(includedmodules_yml_path) + includedmodules_yml_path = read_staged_data("includedmodules") + mmd = load_mmd(includedmodules_yml_path) # Set the name and stream mmd = mmd.copy("includedmodules", "1") scmurl = "git://pkgs.domain.local/modules/includedmodule?#da95886" diff --git a/tests/test_scheduler/test_module_wait.py b/tests/test_scheduler/test_module_wait.py index 03061532..df3361be 100644 --- a/tests/test_scheduler/test_module_wait.py +++ b/tests/test_scheduler/test_module_wait.py @@ -27,10 +27,10 @@ import module_build_service.scheduler.handlers.modules import os import koji import pytest -from tests import conf, db, scheduler_init_data +from tests import conf, db, scheduler_init_data, read_staged_data import module_build_service.resolver from module_build_service import build_logs, Modulemd -from module_build_service.utils.general import load_mmd_file +from module_build_service.utils.general import load_mmd from module_build_service.models import ComponentBuild, ModuleBuild base_dir = os.path.dirname(os.path.dirname(__file__)) @@ -71,12 +71,8 @@ class TestModuleWait: "state": "some state", "id": 1, } - - formatted_testmodule_yml_path = os.path.join( - base_dir, "staged_data", "formatted_testmodule.yaml") - mmd = load_mmd_file(formatted_testmodule_yml_path) mocked_module_build.id = 1 - mocked_module_build.mmd.return_value = mmd + mocked_module_build.mmd.return_value = load_mmd(read_staged_data("formatted_testmodule")) mocked_module_build.component_builds = [] from_module_event.return_value = mocked_module_build diff --git a/tests/test_utils/test_utils.py b/tests/test_utils/test_utils.py index f28a4f69..82c20276 100644 --- a/tests/test_utils/test_utils.py +++ b/tests/test_utils/test_utils.py @@ -31,6 +31,7 @@ import module_build_service.utils import module_build_service.scm from module_build_service import models, conf from module_build_service.errors import ProgrammingError, ValidationError, UnprocessableEntity +from module_build_service.utils.general import load_mmd from tests import ( reuse_component_init_data, db, @@ -39,7 +40,7 @@ from tests import ( init_data, scheduler_init_data, make_module, -) + read_staged_data, staged_data_filename) import mock import koji import pytest @@ -74,9 +75,7 @@ class FakeSCM(object): def checkout(self, temp_dir): self.sourcedir = path.join(temp_dir, self.name) mkdir(self.sourcedir) - base_dir = path.abspath(path.dirname(__file__)) - copyfile( - path.join(base_dir, "..", "staged_data", self.mmd_filename), self.get_module_yaml()) + copyfile(staged_data_filename(self.mmd_filename), self.get_module_yaml()) return self.sourcedir @@ -315,7 +314,7 @@ class TestUtils: def test_get_build_arches(self, ClientSession): session = ClientSession.return_value session.getTag.return_value = {"arches": "ppc64le"} - mmd = load_mmd_file(path.join(BASE_DIR, "..", "staged_data", "formatted_testmodule.yaml")) + mmd = load_mmd(read_staged_data("formatted_testmodule")) r = module_build_service.utils.get_build_arches(mmd, conf) assert r == ["ppc64le"] @@ -326,7 +325,7 @@ class TestUtils: """ session = ClientSession.return_value session.getTag.return_value = {"arches": ""} - mmd = load_mmd_file(path.join(BASE_DIR, "..", "staged_data", "formatted_testmodule.yaml")) + mmd = load_mmd(read_staged_data("formatted_testmodule")) r = module_build_service.utils.get_build_arches(mmd, conf) assert set(r) == set(conf.arches) @@ -336,7 +335,7 @@ class TestUtils: return_value=["testmodule"], ) def test_get_build_arches_koji_tag_arches(self, cfg): - mmd = load_mmd_file(path.join(BASE_DIR, "..", "staged_data", "formatted_testmodule.yaml")) + mmd = load_mmd(read_staged_data("formatted_testmodule")) xmd = mmd.get_xmd() xmd["mbs"]["koji_tag_arches"] = ["ppc64", "ppc64le"] mmd.set_xmd(xmd) @@ -346,7 +345,7 @@ class TestUtils: @patch.object(conf, "base_module_arches", new={"platform:xx": ["x86_64", "i686"]}) def test_get_build_arches_base_module_override(self): - mmd = load_mmd_file(path.join(BASE_DIR, "..", "staged_data", "formatted_testmodule.yaml")) + mmd = load_mmd(read_staged_data("formatted_testmodule")) xmd = mmd.get_xmd() mbs_options = xmd["mbs"] if "mbs" in xmd.keys() else {} mbs_options["buildrequires"] = {"platform": {"stream": "xx"}} @@ -358,7 +357,7 @@ class TestUtils: @pytest.mark.parametrize("context", ["c1", None]) def test_import_mmd_contexts(self, context): - mmd = load_mmd_file(path.join(BASE_DIR, "..", "staged_data", "formatted_testmodule.yaml")) + mmd = load_mmd(read_staged_data("formatted_testmodule")) mmd.set_context(context) xmd = mmd.get_xmd() @@ -376,7 +375,7 @@ class TestUtils: assert build.context == models.DEFAULT_MODULE_CONTEXT def test_import_mmd_multiple_dependencies(self): - mmd = load_mmd_file(path.join(BASE_DIR, "..", "staged_data", "formatted_testmodule.yaml")) + mmd = load_mmd(read_staged_data("formatted_testmodule")) mmd.add_dependencies(mmd.get_dependencies()[0].copy()) expected_error = "The imported module's dependencies list should contain just one element" @@ -385,7 +384,7 @@ class TestUtils: assert str(e.value) == expected_error def test_import_mmd_no_xmd_buildrequires(self): - mmd = load_mmd_file(path.join(BASE_DIR, "..", "staged_data", "formatted_testmodule.yaml")) + mmd = load_mmd(read_staged_data("formatted_testmodule")) xmd = mmd.get_xmd() del xmd["mbs"]["buildrequires"] mmd.set_xmd(xmd) @@ -399,7 +398,7 @@ class TestUtils: assert str(e.value) == expected_error def test_import_mmd_minimal_xmd_from_local_repository(self): - mmd = load_mmd_file(path.join(BASE_DIR, "..", "staged_data", "formatted_testmodule.yaml")) + mmd = load_mmd(read_staged_data("formatted_testmodule")) xmd = mmd.get_xmd() xmd["mbs"] = {} xmd["mbs"]["koji_tag"] = "repofile:///etc/yum.repos.d/fedora-modular.repo" @@ -423,7 +422,7 @@ class TestUtils: ) def test_import_mmd_base_module(self, stream, disttag_marking, error_msg): clean_database(add_platform_module=False) - mmd = load_mmd_file(path.join(BASE_DIR, "..", "staged_data", "platform.yaml")) + mmd = load_mmd(read_staged_data("platform")) mmd = mmd.copy(mmd.get_module_name(), stream) if disttag_marking: @@ -484,11 +483,7 @@ class TestUtils: and that module has the xmd.mbs.disttag_marking field set, it should influence the disttag. """ scheduler_init_data(db_session, 1) - mmd_path = path.abspath( - path.join( - __file__, path.pardir, path.pardir, "staged_data", "build_metadata_module.yaml") - ) - metadata_mmd = module_build_service.utils.load_mmd_file(mmd_path) + metadata_mmd = load_mmd(read_staged_data("build_metadata_module")) module_build_service.utils.import_mmd(db_session, metadata_mmd) build_one = db_session.query(models.ModuleBuild).get(2) @@ -565,7 +560,7 @@ class TestUtils: return hashes_returned[ref] mocked_scm.return_value.get_latest = mocked_get_latest - mmd = load_mmd_file(path.join(BASE_DIR, "..", "staged_data", "testmodule.yaml")) + mmd = load_mmd(read_staged_data("testmodule")) # Modify the component branches so we can identify them later on mmd.get_rpm_component("perl-Tangerine").set_ref("f28") mmd.get_rpm_component("tangerine").set_ref("f27") @@ -724,8 +719,7 @@ class TestUtils: "fbed359411a1baa08d4a88e0d12d426fbf8f602c", ] - testmodule_mmd_path = path.join(BASE_DIR, "..", "staged_data", "testmodule.yaml") - mmd = load_mmd_file(testmodule_mmd_path) + mmd = load_mmd(read_staged_data("testmodule")) mmd = mmd.copy("testmodule-variant", "master") module_build = module_build_service.models.ModuleBuild() module_build.name = "testmodule-variant" @@ -770,8 +764,7 @@ class TestUtils: "dbed259411a1baa08d4a88e0d12d426fbf8f6037", ] - testmodule_mmd_path = path.join(BASE_DIR, "..", "staged_data", "testmodule.yaml") - mmd = load_mmd_file(testmodule_mmd_path) + mmd = load_mmd(read_staged_data("testmodule")) # Set the module name and stream mmd = mmd.copy("testmodule", "master") module_build = module_build_service.models.ModuleBuild() @@ -811,7 +804,7 @@ class TestUtils: "dbed259411a1baa08d4a88e0d12d426fbf8f6037", ] - testmodule_mmd_path = path.join(BASE_DIR, "..", "staged_data", "testmodule.yaml") + testmodule_mmd_path = staged_data_filename("testmodule.yaml") test_archs = ["powerpc", "i486"] mmd1 = load_mmd_file(testmodule_mmd_path) @@ -848,13 +841,11 @@ class TestUtils: test_datetime = datetime(2019, 2, 14, 11, 11, 45, 42968) - testmodule_mmd_path = path.join(BASE_DIR, "..", "staged_data", "testmodule.yaml") - - mmd1 = load_mmd_file(testmodule_mmd_path) + mmd = load_mmd(read_staged_data("testmodule")) with patch("module_build_service.utils.submit.datetime") as dt: dt.utcnow.return_value = test_datetime - module_build_service.utils.format_mmd(mmd1, None, build, db.session) + module_build_service.utils.format_mmd(mmd, None, build, db.session) assert build.time_modified == test_datetime @@ -1304,7 +1295,7 @@ class TestBatches: @patch( "module_build_service.config.Config.mock_resultsdir", new_callable=mock.PropertyMock, - return_value=path.join(BASE_DIR, "..", "staged_data", "local_builds"), + return_value=staged_data_filename("local_builds") ) @patch( "module_build_service.config.Config.system", new_callable=mock.PropertyMock, return_value="mock" @@ -1404,8 +1395,7 @@ class TestOfflineLocalBuilds: with patch("dnf.Base") as dnf_base: repo = mock.MagicMock() repo.repofile = "/etc/yum.repos.d/foo.repo" - tm_path = path.join(BASE_DIR, "..", "staged_data", "formatted_testmodule.yaml") - mmd = load_mmd_file(tm_path) + mmd = load_mmd(read_staged_data("formatted_testmodule")) repo.get_metadata_content.return_value = mmd_to_str(mmd) base = dnf_base.return_value base.repos = {"reponame": repo} diff --git a/tests/test_utils/test_utils_mse.py b/tests/test_utils/test_utils_mse.py index 85e23589..d693a628 100644 --- a/tests/test_utils/test_utils_mse.py +++ b/tests/test_utils/test_utils_mse.py @@ -18,15 +18,13 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -import os - from mock import patch, PropertyMock import pytest import module_build_service.utils from module_build_service import Modulemd from module_build_service.errors import StreamAmbigous -from tests import db, clean_database, make_module, init_data, base_dir +from tests import db, clean_database, make_module, init_data, read_staged_data class TestUtilsModuleStreamExpansion: @@ -407,8 +405,7 @@ class TestUtilsModuleStreamExpansion: def test__get_base_module_mmds(self): """Ensure the correct results are returned without duplicates.""" init_data(data_size=1, multiple_stream_versions=True) - mmd = module_build_service.utils.load_mmd_file( - os.path.join(base_dir, "staged_data", "testmodule_v2.yaml")) + mmd = module_build_service.utils.load_mmd(read_staged_data("testmodule_v2.yaml")) deps = mmd.get_dependencies()[0] new_deps = Modulemd.Dependencies() for stream in deps.get_runtime_streams("platform"): @@ -432,8 +429,7 @@ class TestUtilsModuleStreamExpansion: def test__get_base_module_mmds_virtual_streams(self, virtual_streams, db_session): """Ensure the correct results are returned without duplicates.""" init_data(data_size=1, multiple_stream_versions=True) - mmd = module_build_service.utils.load_mmd_file( - os.path.join(base_dir, "staged_data", "testmodule_v2.yaml")) + mmd = module_build_service.utils.load_mmd(read_staged_data("testmodule_v2")) deps = mmd.get_dependencies()[0] new_deps = Modulemd.Dependencies() for stream in deps.get_runtime_streams("platform"): @@ -465,8 +461,7 @@ class TestUtilsModuleStreamExpansion: def test__get_base_module_mmds_virtual_streams_only_major_versions(self, cfg): """Ensure the correct results are returned without duplicates.""" init_data(data_size=1, multiple_stream_versions=["foo28", "foo29", "foo30"]) - mmd = module_build_service.utils.load_mmd_file( - os.path.join(base_dir, "staged_data", "testmodule_v2.yaml")) + mmd = module_build_service.utils.load_mmd(read_staged_data("testmodule_v2")) deps = mmd.get_dependencies()[0] new_deps = Modulemd.Dependencies() for stream in deps.get_runtime_streams("platform"): diff --git a/tests/test_views/test_views.py b/tests/test_views/test_views.py index a870b171..cffe8fe5 100644 --- a/tests/test_views/test_views.py +++ b/tests/test_views/test_views.py @@ -35,7 +35,7 @@ import pytest import re import sqlalchemy -from tests import app, init_data, clean_database, reuse_component_init_data +from tests import app, init_data, clean_database, reuse_component_init_data, staged_data_filename from tests import read_staged_data from tests.test_scm import base_dir as scm_base_dir from module_build_service.errors import UnprocessableEntity @@ -44,7 +44,7 @@ from module_build_service import db, version import module_build_service.config as mbs_config import module_build_service.scheduler.handlers.modules from module_build_service.utils.general import ( - import_mmd, mmd_to_str, to_text_type, load_mmd_file, load_mmd + import_mmd, mmd_to_str, load_mmd ) @@ -114,8 +114,7 @@ class FakeSCM(object): self.sourcedir = path.join(temp_dir, self.name) mkdir(self.sourcedir) - base_dir = path.abspath(path.dirname(__file__)) - copyfile(path.join(base_dir, "..", "staged_data", mmd_filename), self.get_module_yaml()) + copyfile(staged_data_filename(mmd_filename), self.get_module_yaml()) self.checkout_id += 1 @@ -204,8 +203,7 @@ class TestViews: assert data["build_context"] is None assert data["runtime_context"] is None assert data["id"] == 2 - with open(path.join(base_dir, "staged_data", "nginx_mmd.yaml")) as mmd: - assert data["modulemd"] == to_text_type(mmd.read()) + assert data["modulemd"] == read_staged_data("nginx_mmd") assert data["name"] == "nginx" assert data["owner"] == "Moe Szyslak" assert data["rebuild_strategy"] == "changed-and-after" @@ -829,7 +827,7 @@ class TestViews: def test_query_base_module_br_filters(self): reuse_component_init_data() - mmd = load_mmd_file(path.join(base_dir, "staged_data", "platform.yaml")) + mmd = load_mmd(read_staged_data("platform")) mmd = mmd.copy(mmd.get_module_name(), "f30.1.3") import_mmd(db.session, mmd) platform_f300103 = ModuleBuild.query.filter_by(stream="f30.1.3").one() @@ -2138,18 +2136,15 @@ class TestViews: def test_submit_scratch_build_with_mmd( self, mocked_allow_yaml, mocked_allow_scratch, mocked_get_user, api_version ): - base_dir = path.abspath(path.dirname(__file__)) - mmd_path = path.join(base_dir, "..", "staged_data", "testmodule.yaml") - post_url = "/module-build-service/{0}/module-builds/".format(api_version) - with open(mmd_path, "rb") as f: - modulemd = f.read().decode("utf-8") + modulemd = read_staged_data("testmodule") post_data = { "branch": "master", "scratch": True, "modulemd": modulemd, - "module_name": str(splitext(basename(mmd_path))[0]), + "module_name": str(splitext(basename(staged_data_filename("testmodule")))[0]), } + post_url = "/module-build-service/{0}/module-builds/".format(api_version) rv = self.client.post(post_url, data=json.dumps(post_data)) data = json.loads(rv.data) @@ -2205,13 +2200,12 @@ class TestViews: def test_submit_scratch_build_with_mmd_no_module_name( self, mocked_allow_yaml, mocked_allow_scratch, mocked_get_user ): - base_dir = path.abspath(path.dirname(__file__)) - mmd_path = path.join(base_dir, "..", "staged_data", "testmodule.yaml") + post_data = { + "branch": "master", + "scratch": True, + "modulemd": read_staged_data("testmodule") + } post_url = "/module-build-service/1/module-builds/" - with open(mmd_path, "rb") as f: - modulemd = f.read().decode("utf-8") - - post_data = {"branch": "master", "scratch": True, "modulemd": modulemd} rv = self.client.post(post_url, data=json.dumps(post_data)) assert rv.status_code == 400 data = json.loads(rv.data) @@ -2240,18 +2234,13 @@ class TestViews: def test_submit_scratch_build_with_mmd_yaml_not_allowed( self, mocked_allow_yaml, mocked_allow_scratch, mocked_get_user, api_version ): - base_dir = path.abspath(path.dirname(__file__)) - mmd_path = path.join(base_dir, "..", "staged_data", "testmodule.yaml") - post_url = "/module-build-service/{0}/module-builds/".format(api_version) - with open(mmd_path, "rb") as f: - modulemd = f.read().decode("utf-8") - post_data = { "branch": "master", "scratch": True, - "modulemd": modulemd, - "module_name": str(splitext(basename(mmd_path))[0]), + "modulemd": read_staged_data("testmodule"), + "module_name": str(splitext(basename(staged_data_filename("testmodule")))[0]), } + post_url = "/module-build-service/{0}/module-builds/".format(api_version) rv = self.client.post(post_url, data=json.dumps(post_data)) data = json.loads(rv.data) @@ -2283,7 +2272,7 @@ class TestViews: init_data(data_size=1, multiple_stream_versions=True) # Create a platform for whatever the override is so the build submission succeeds if platform_override: - platform_mmd = load_mmd_file(path.join(base_dir, "staged_data", "platform.yaml")) + platform_mmd = load_mmd(read_staged_data("platform")) platform_mmd = platform_mmd.copy(platform_mmd.get_module_name(), platform_override) if platform_override == "el8.0.0": xmd = platform_mmd.get_xmd() @@ -2330,7 +2319,7 @@ class TestViews: mocked_regexes.return_value = [r"(?:\-LP\-)(.+)$"] init_data(data_size=1, multiple_stream_versions=True) # Create a platform for the override so the build submission succeeds - platform_mmd = load_mmd_file(path.join(base_dir, "staged_data", "platform.yaml")) + platform_mmd = load_mmd(read_staged_data('platform')) platform_mmd = platform_mmd.copy(platform_mmd.get_module_name(), "product1.3") import_mmd(db.session, platform_mmd) @@ -2369,7 +2358,7 @@ class TestViews: versioning and no virtual streams, that the dependency resolution succeeds. """ init_data(data_size=1, multiple_stream_versions=True) - platform_mmd = load_mmd_file(path.join(base_dir, "staged_data", "platform.yaml")) + platform_mmd = load_mmd(read_staged_data("platform")) platform_mmd = platform_mmd.copy(platform_mmd.get_module_name(), "el8.0.0") import_mmd(db.session, platform_mmd) @@ -2427,7 +2416,7 @@ class TestViews: @patch("module_build_service.scm.SCM") def test_submit_build_request_platform_virtual_stream(self, mocked_scm, mocked_get_user): # Create a platform with el8.25.0 but with the virtual stream el8 - mmd = load_mmd_file(path.join(base_dir, "staged_data", "platform.yaml")) + mmd = load_mmd(read_staged_data("platform")) mmd = mmd.copy(mmd.get_module_name(), "el8.25.0") xmd = mmd.get_xmd() xmd["mbs"]["virtual_streams"] = ["el8"] @@ -2562,7 +2551,7 @@ class TestViews: mock_pp_streams.return_value = pp_streams # Mock the Product Pages query mock_get.return_value.json.return_value = get_rv - mmd = load_mmd_file(path.join(base_dir, "staged_data", "platform.yaml")) + mmd = load_mmd(read_staged_data("platform")) # Create the required platforms for stream in ("el8.0.0", "el8.0.0.z", "el8.2.1", "el8.2.1.z"): mmd = mmd.copy(mmd.get_module_name(), stream)