Repository: cekit/cekit

Issue:
When OSBS builder is exeuted with --dry-run we should not clone dist-git repository
**Describe the solution you'd like**
Currently the dist-git repository is cloned even if we specify `--dry-run`. We should refrain from doing it. It should be cloned only when a regular run is executed.

**Additional context**
None.


Discussion:

Patch:
diff --git a/cekit/builder.py b/cekit/builder.py
index 7b2b163..c03bf46 100644
--- a/cekit/builder.py
+++ b/cekit/builder.py
@@ -29,10 +29,15 @@ class Command(object):
     def prepare(self):
         pass
 
+    def before_run(self):
+        pass
+
     def run(self):
         raise CekitError(
             "Command.run() method is not implemented for '{}' command and '{}' type. Please report it!".format(self._command, self._type))
 
+    def after_run(self):
+        pass
 
 class Builder(Command):
     """
@@ -57,8 +62,16 @@ class Builder(Command):
             LOGGER.info("The --dry-run parameter was specified, build will not be executed")
             return
 
+        self.before_run()
+
         self.run()
 
+        self.after_run()
+
+    def before_run(self):
+        LOGGER.debug("Checking CEKit build dependencies...")
+        self.dependency_handler.handle(self)
+
     def prepare(self):
         if self.build_engine == 'docker' or self.build_engine == 'buildah' or self.build_engine == "podman":
             from cekit.generator.docker import DockerGenerator as generator_impl
@@ -87,6 +100,3 @@ class Builder(Command):
 
         self.generator.init()
         self.generator.generate(self.build_engine)
-
-        LOGGER.debug("Checking CEKit build dependencies...")
-        self.dependency_handler.handle(self)
diff --git a/cekit/builders/osbs.py b/cekit/builders/osbs.py
index 8d01362..bd70bd9 100644
--- a/cekit/builders/osbs.py
+++ b/cekit/builders/osbs.py
@@ -80,10 +80,10 @@ class OSBSBuilder(Builder):
 
         return deps
 
-    def prepare(self):
+    def before_run(self):
         """Prepares dist-git repository for OSBS build."""
 
-        super(OSBSBuilder, self).prepare()
+        super(OSBSBuilder, self).before_run()
 
         self.prepare_dist_git()
         self.copy_to_dist_git()


Test patch:
diff --git a/tests/test_builder.py b/tests/test_builder.py
index 74334f5..20707e3 100644
--- a/tests/test_builder.py
+++ b/tests/test_builder.py
@@ -444,6 +444,7 @@ def test_osbs_copy_artifacts_to_dist_git(mocker, tmpdir, artifact, src, target):
 
     with mocker.patch('cekit.tools.get_brew_url', side_effect=subprocess.CalledProcessError(1, 'command')):
         builder.prepare()
+        builder.before_run()
 
     dist_git_class.assert_called_once_with(os.path.join(
         str(tmpdir), 'osbs', 'repo'), str(tmpdir), 'repo', 'branch')
@@ -460,6 +461,68 @@ def test_docker_builder_defaults():
     assert builder.params.tags == ['foo', 'bar']
 
 
+def test_osbs_dist_git_sync_called(mocker, tmpdir):
+    mocker.patch('cekit.tools.DependencyHandler.handle')
+
+    image_descriptor = {
+        'schema_version': 1,
+        'from': 'centos:latest',
+        'name': 'test/image',
+        'version': '1.0',
+        'labels': [{'name': 'foo', 'value': 'bar'}, {'name': 'labela', 'value': 'a'}],
+        'osbs': {
+            'repository': {
+                'name': 'repo',
+                'branch': 'branch'
+            }
+        }
+    }
+
+    builder = create_builder_object(
+        mocker, 'osbs', {}, {'descriptor': yaml.dump(image_descriptor), 'target': str(tmpdir)})
+
+    prepare_dist_git = mocker.patch.object(builder, 'prepare_dist_git')
+    copy_to_dist_git = mocker.patch.object(builder, 'copy_to_dist_git')
+    run = mocker.patch.object(builder, 'run')
+
+    builder.execute()
+
+    prepare_dist_git.assert_called_once_with()
+    copy_to_dist_git.assert_called_once_with()
+    run.assert_called_once_with()
+
+
+def test_osbs_dist_git_sync_NOT_called_when_dry_run_set(mocker, tmpdir):
+    mocker.patch('cekit.tools.DependencyHandler.handle')
+
+    image_descriptor = {
+        'schema_version': 1,
+        'from': 'centos:latest',
+        'name': 'test/image',
+        'version': '1.0',
+        'labels': [{'name': 'foo', 'value': 'bar'}, {'name': 'labela', 'value': 'a'}],
+        'osbs': {
+            'repository': {
+                'name': 'repo',
+                'branch': 'branch'
+            }
+        }
+    }
+
+    builder = create_builder_object(
+        mocker, 'osbs', {'dry_run': True}, {'descriptor': yaml.dump(image_descriptor), 'target': str(tmpdir)})
+
+    prepare_dist_git = mocker.patch.object(builder, 'prepare_dist_git')
+    copy_to_dist_git = mocker.patch.object(builder, 'copy_to_dist_git')
+    run = mocker.patch.object(builder, 'run')
+
+    builder.execute()
+
+    prepare_dist_git.assert_not_called()
+    copy_to_dist_git.assert_not_called()
+    run.assert_not_called()
+
+
 def test_docker_build_default_tags(mocker):
     builder = DockerBuilder(Map({'target': 'something'}), Map())