821720f
From 13d00928acf86f49fcced6cc034f663e8f2bbf64 Mon Sep 17 00:00:00 2001
821720f
From: Martin Curlej <mcurlej@redhat.com>
821720f
Date: Jun 04 2021 13:16:38 +0000
821720f
Subject: Fixed scratch build suffix bug
821720f
821720f
821720f
When building a scratch build of a module with static context
821720f
the scratch suffix was added twice.
821720f
821720f
Signed-off-by: Martin Curlej <mcurlej@redhat.com>
821720f
821720f
---
821720f
821720f
diff --git a/module_build_service/web/submit.py b/module_build_service/web/submit.py
821720f
index b4343e1..57fcb3d 100644
821720f
--- a/module_build_service/web/submit.py
821720f
+++ b/module_build_service/web/submit.py
1c1caff
@@ -699,10 +699,12 @@ def submit_module_build(db_session, username, stream_or_packager, params, module
821720f
             module.build_context, module.runtime_context, module.context, \
821720f
                 module.build_context_no_bms = module.contexts_from_mmd(module.modulemd)
821720f
 
821720f
-            xmd = mmd.get_xmd()
821720f
-            if xmd["mbs"].get("static_context"):
821720f
+            if static_context:
821720f
+                # if the static_context is True we use the context from defined in the mmd
821720f
+                # and discard the computed one.
821720f
                 module.context = mmd.get_context()
821720f
-
821720f
-            module.context += context_suffix
821720f
+            else:
821720f
+                # if the context is defined by MSE, we need to add a context_suffix if it exists.
821720f
+                module.context += context_suffix
f2f7629
             db_session.commit()
821720f
 
821720f
@@ -786,10 +788,15 @@ def process_module_context_configuration(stream_or_packager):
821720f
         return streams, static_context
821720f
     else:
821720f
         xmd = stream_or_packager.get_xmd()
821720f
-        # check if we are handling rebuild of a static context module
821720f
+
821720f
+        # check if the static format is defined through `static_context` field
821720f
+        if stream_or_packager.is_static_context():
821720f
+            static_context = True
821720f
+            return [stream_or_packager], static_context
821720f
+
821720f
+        # check if we are handling rebuild of a static context module defined in xmd
821720f
         if "mbs" in xmd:
821720f
-            # check if it is a static context
821720f
-            if "static_context" in xmd["mbs"] or stream_or_packager.is_static_context():
821720f
+            if "static_context" in xmd["mbs"]:
821720f
                 static_context = True
821720f
                 return [stream_or_packager], static_context
821720f
 
821720f
diff --git a/tests/test_web/test_submit.py b/tests/test_web/test_submit.py
821720f
index a2f548f..b5e3869 100644
821720f
--- a/tests/test_web/test_submit.py
821720f
+++ b/tests/test_web/test_submit.py
821720f
@@ -25,10 +25,15 @@ from tests import (
821720f
     make_module,
821720f
     read_staged_data,
821720f
     init_data,
821720f
+    clean_database,
821720f
 )
821720f
 
821720f
 
821720f
 class TestSubmit:
821720f
+
821720f
+    def teardown_method(self, tested_method):
821720f
+        clean_database()
821720f
+
821720f
     def test_get_prefixed_version_f28(self):
821720f
         scheduler_init_data(1)
821720f
         build_one = models.ModuleBuild.get_by_id(db_session, 2)
821720f
@@ -144,6 +149,77 @@ class TestSubmit:
821720f
             assert "mbs_options" not in xmd
821720f
             assert xmd["mbs"]["static_context"]
821720f
 
821720f
+    def test_submit_build_module_scratch_v3_static_context(self):
821720f
+        """
821720f
+        Test if the static context in the v3 metadata format will contain the correct suffix
821720f
+        during a scratch build
821720f
+        """
821720f
+        init_data(multiple_stream_versions=True)
821720f
+        yaml_str = read_staged_data("v3/mmd_packager")
821720f
+        mmd = load_mmd(yaml_str)
821720f
+        ux_timestamp = "1613048427"
821720f
+        version = provide_module_stream_version_from_timestamp(ux_timestamp)
821720f
+        params = {"scratch": True}
821720f
+
821720f
+        builds = submit_module_build(db_session, "foo", mmd, params, version)
821720f
+
821720f
+        assert len(builds) == 2
821720f
+
821720f
+        expected_contexts = {"CTX1_1": {}, "CTX2_1": {}}
821720f
+
821720f
+        for build in builds:
821720f
+            mmd = build.mmd()
821720f
+            context = mmd.get_context()
821720f
+            assert context in expected_contexts
821720f
+
821720f
+    def test_submit_build_module_scratch_v2_static_context(self):
821720f
+        """
821720f
+        Test if the static context in the v2 metadata format will contain
821720f
+        the correct suffix during a scratch build
821720f
+        """
821720f
+        scheduler_init_data(1)
821720f
+        yaml_str = read_staged_data("static_context_v2")
821720f
+        mmd = load_mmd(yaml_str)
821720f
+        ux_timestamp = "1613048427"
821720f
+        version = provide_module_stream_version_from_timestamp(ux_timestamp)
821720f
+        params = {"scratch": True}
821720f
+
821720f
+        builds = submit_module_build(db_session, "app", mmd, params, version)
821720f
+
821720f
+        assert len(builds) == 2
821720f
+
821720f
+        expected_contexts = {"context1_1": {}, "context2_1": {}}
821720f
+
821720f
+        for build in builds:
821720f
+            mmd = build.mmd()
821720f
+            context = mmd.get_context()
821720f
+            assert context in expected_contexts
821720f
+
821720f
+    def test_submit_build_module_scratch_increment(self):
821720f
+        """
821720f
+        Test if the context suffix is incremented correctly during a repeated scratch build.
821720f
+        """
821720f
+        init_data(multiple_stream_versions=True)
821720f
+        yaml_str = read_staged_data("v3/mmd_packager")
821720f
+        mmd = load_mmd(yaml_str)
821720f
+        ux_timestamp = "1613048427"
821720f
+        version = provide_module_stream_version_from_timestamp(ux_timestamp)
821720f
+        params = {"scratch": True}
821720f
+
821720f
+        builds = submit_module_build(db_session, "foo", mmd, params, version)
821720f
+
821720f
+        assert len(builds) == 2
821720f
+
821720f
+        builds = submit_module_build(db_session, "foo", mmd, params, version)
821720f
+
821720f
+        assert len(builds) == 2
821720f
+
821720f
+        expected_contexts = {"CTX1_2": {}, "CTX2_2": {}}
821720f
+        for build in builds:
821720f
+            mmd = build.mmd()
821720f
+            context = mmd.get_context()
821720f
+            assert context in expected_contexts
821720f
+
821720f
 
821720f
 class TestProcessModuleContextConfiguration:
821720f
     """
821720f