Blob Blame History Raw
From 13d00928acf86f49fcced6cc034f663e8f2bbf64 Mon Sep 17 00:00:00 2001
From: Martin Curlej <mcurlej@redhat.com>
Date: Jun 04 2021 13:16:38 +0000
Subject: Fixed scratch build suffix bug


When building a scratch build of a module with static context
the scratch suffix was added twice.

Signed-off-by: Martin Curlej <mcurlej@redhat.com>

---

diff --git a/module_build_service/web/submit.py b/module_build_service/web/submit.py
--- a/module_build_service/web/submit.py
+++ b/module_build_service/web/submit.py
@@ -701,7 +701,9 @@ def submit_module_build(db_session, username, stream_or_packager, params, module_stream_version):
 
-            xmd = mmd.get_xmd()
-            if xmd["mbs"].get("static_context"):
+            if static_context:
+                # if the static_context is True we use the context from defined in the mmd
+                # and discard the computed one.
                 module.context = mmd.get_context()
-
-            module.context += context_suffix
+            else:
+                # if the context is defined by MSE, we need to add a context_suffix if it exists.
+                module.context += context_suffix
             db_session.commit()
@@ -769,10 +769,15 @@ def process_module_context_configuration(stream_or_packager):
         return streams, static_context
     else:
         xmd = stream_or_packager.get_xmd()
-        # check if we are handling rebuild of a static context module
+
+        # check if the static format is defined through `static_context` field
+        if stream_or_packager.is_static_context():
+            static_context = True
+            return [stream_or_packager], static_context
+
+        # check if we are handling rebuild of a static context module defined in xmd
         if "mbs" in xmd:
-            # check if it is a static context
-            if "static_context" in xmd["mbs"] or stream_or_packager.is_static_context():
+            if "static_context" in xmd["mbs"]:
                 static_context = True
                 return [stream_or_packager], static_context
 
diff --git a/tests/test_web/test_submit.py b/tests/test_web/test_submit.py
index a2f548f..b5e3869 100644
--- a/tests/test_web/test_submit.py
+++ b/tests/test_web/test_submit.py
@@ -25,10 +25,15 @@ from tests import (
     make_module,
     read_staged_data,
     init_data,
+    clean_database,
 )
 
 
 class TestSubmit:
+
+    def teardown_method(self, tested_method):
+        clean_database()
+
     def test_get_prefixed_version_f28(self):
         scheduler_init_data(1)
         build_one = models.ModuleBuild.get_by_id(db_session, 2)
@@ -144,6 +149,77 @@ class TestSubmit:
             assert "mbs_options" not in xmd
             assert xmd["mbs"]["static_context"]
 
+    def test_submit_build_module_scratch_v3_static_context(self):
+        """
+        Test if the static context in the v3 metadata format will contain the correct suffix
+        during a scratch build
+        """
+        init_data(multiple_stream_versions=True)
+        yaml_str = read_staged_data("v3/mmd_packager")
+        mmd = load_mmd(yaml_str)
+        ux_timestamp = "1613048427"
+        version = provide_module_stream_version_from_timestamp(ux_timestamp)
+        params = {"scratch": True}
+
+        builds = submit_module_build(db_session, "foo", mmd, params, version)
+
+        assert len(builds) == 2
+
+        expected_contexts = {"CTX1_1": {}, "CTX2_1": {}}
+
+        for build in builds:
+            mmd = build.mmd()
+            context = mmd.get_context()
+            assert context in expected_contexts
+
+    def test_submit_build_module_scratch_v2_static_context(self):
+        """
+        Test if the static context in the v2 metadata format will contain
+        the correct suffix during a scratch build
+        """
+        scheduler_init_data(1)
+        yaml_str = read_staged_data("static_context_v2")
+        mmd = load_mmd(yaml_str)
+        ux_timestamp = "1613048427"
+        version = provide_module_stream_version_from_timestamp(ux_timestamp)
+        params = {"scratch": True}
+
+        builds = submit_module_build(db_session, "app", mmd, params, version)
+
+        assert len(builds) == 2
+
+        expected_contexts = {"context1_1": {}, "context2_1": {}}
+
+        for build in builds:
+            mmd = build.mmd()
+            context = mmd.get_context()
+            assert context in expected_contexts
+
+    def test_submit_build_module_scratch_increment(self):
+        """
+        Test if the context suffix is incremented correctly during a repeated scratch build.
+        """
+        init_data(multiple_stream_versions=True)
+        yaml_str = read_staged_data("v3/mmd_packager")
+        mmd = load_mmd(yaml_str)
+        ux_timestamp = "1613048427"
+        version = provide_module_stream_version_from_timestamp(ux_timestamp)
+        params = {"scratch": True}
+
+        builds = submit_module_build(db_session, "foo", mmd, params, version)
+
+        assert len(builds) == 2
+
+        builds = submit_module_build(db_session, "foo", mmd, params, version)
+
+        assert len(builds) == 2
+
+        expected_contexts = {"CTX1_2": {}, "CTX2_2": {}}
+        for build in builds:
+            mmd = build.mmd()
+            context = mmd.get_context()
+            assert context in expected_contexts
+
 
 class TestProcessModuleContextConfiguration:
     """