Repository: jboss-dockerfiles/dogen

Issue:
force a failure if a package installation fails
Yum has some annoying behaviour, if you perform `yum install foo bar baz` and at least one package exists/succeeds, then it will return success. This masks some error conditions: missing package, possible missing repos files etc.

We should generate some additional CMDs to ensure that we bail out if a package does not exist or install.

Discussion:

Patch:
diff --git a/dogen/templates/template.jinja b/dogen/templates/template.jinja
index 84e3c4c..170fd59 100644
--- a/dogen/templates/template.jinja
+++ b/dogen/templates/template.jinja
@@ -63,9 +63,10 @@ COPY repos/*.repo /etc/yum.repos.d/
 COPY *.rpm /tmp/rpms/
 {% endif %}
 
-# Install required RPMs
+# Install required RPMs and ensure that the packages were installed
 RUN yum install -y {% if additional_repos -%} --disablerepo=\* {%- for repo in additional_repos %} --enablerepo={{ repo }}{% endfor %}{% endif %} {%- for package in packages %} {{ package }}{% endfor %} {%- for rpm in rpms %} /tmp/rpms/{{ rpm }} {% endfor %} \
-    && yum clean all
+    && yum clean all && \
+    rpm -q {% for package in packages %} {{ package }}{% endfor %}
 
 {% if additional_repos %}
 # Remove custom repo files


Test patch:
diff --git a/tests/test_package.py b/tests/test_package.py
new file mode 100644
index 0000000..b742e65
--- /dev/null
+++ b/tests/test_package.py
@@ -0,0 +1,50 @@
+import argparse
+import mock
+import os
+import tempfile
+import unittest
+import shutil
+import re
+import sys
+
+from dogen.plugins.repo import Repo
+from dogen.generator import Generator
+
+class TestPackage(unittest.TestCase):
+    def setUp(self):
+        self.workdir = tempfile.mkdtemp(prefix='test_repo_plugin')
+        self.descriptor = tempfile.NamedTemporaryFile(delete=False)
+        self.target_dir = os.path.join(self.workdir, "target")
+        self.log = mock.Mock()
+
+    def teardown(self):
+        shutil.rmtree(self.workdir)
+
+    def write_config(self, config):
+        with self.descriptor as f:
+            f.write(config.encode())
+
+    def prepare_dogen(self, repo_files_dir=None):
+        args = argparse.Namespace(path=self.descriptor.name, output=self.target_dir, without_sources=None,
+                                  template=None, scripts_path=None, additional_script=None,
+                                  skip_ssl_verification=None, repo_files_dir=repo_files_dir)
+        self.dogen = Generator(self.log, args, [Repo])
+
+    def test_custom_repo_files_should_add_two(self):
+        open(os.path.join(self.workdir, "fedora.repo"), 'a').close()
+        open(os.path.join(self.workdir, "test.repo"), 'a').close()
+
+        self.write_config("release: '1'\nversion: '1'\ncmd:\n - whoami\nfrom: scratch\nname: someimage\npackages:\n - wget")
+        self.prepare_dogen(self.workdir)
+        self.dogen.run()
+
+        self.assertIsNotNone(self.dogen.cfg)
+        self.assertIsNotNone(self.dogen.cfg.get('packages'))
+        self.assertIsInstance(self.dogen.cfg.get('packages'), list)
+        self.assertIn("wget", self.dogen.cfg.get('packages'))
+
+        dockerfile = open(os.path.join(self.target_dir, "Dockerfile")).read()
+
+        sys.stderr.write("\t\t\tDEBUGDEBUG\n{}\n".format(dockerfile))
+        self.assertTrue(re.match(r'.*yum install[^\n]+wget', dockerfile, re.DOTALL))
+        self.assertTrue(re.match(r'.*rpm -q +wget', dockerfile, re.DOTALL))