Repository: jboss-dockerfiles/dogen

Issue:
build-time cct user depends on whether or not scripts run
since we don't explicitly define a USER for the cct block, it uses whatever was last set. That depends on what scripts are run and under what uid.

We should probably default to USER root in the cct block and perhaps let it be overridden in the YAML.

Discussion:

Patch:
diff --git a/dogen/plugins/cct.py b/dogen/plugins/cct.py
index b4ada30..3b3ab63 100644
--- a/dogen/plugins/cct.py
+++ b/dogen/plugins/cct.py
@@ -19,7 +19,7 @@ class CCT(Plugin):
         Read in a schema definition for our part of the config and hook it
         into the parent schema at the cct: top-level key.
         """
-        schema_path = os.path.join(self.dogen.pwd, "plugins", "cct", "cct_schema.yaml")
+        schema_path = os.path.join(self.dogen.pwd, "schema", "cct_schema.yaml")
         schema = {}
         with open(schema_path, 'r') as fh:
             schema = yaml.safe_load(fh)
@@ -50,6 +50,9 @@ class CCT(Plugin):
         if 'runtime' in cfg['cct']:
             self.runtime_changes(cfg)
 
+        if not 'user' in cfg['cct']:
+            cfg['cct']['user'] = 'root'
+
     def _prepare_modules(self, cfg):
         for module in cfg['cct']['modules']:
             name = None
diff --git a/dogen/plugins/cct/__init__.py b/dogen/plugins/cct/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/dogen/plugins/cct/cct_schema.yaml b/dogen/schema/cct_schema.yaml
similarity index 96%
rename from dogen/plugins/cct/cct_schema.yaml
rename to dogen/schema/cct_schema.yaml
index 1cf2751..0b32caf 100644
--- a/dogen/plugins/cct/cct_schema.yaml
+++ b/dogen/schema/cct_schema.yaml
@@ -15,3 +15,4 @@ map:
   runtime_changes: { type: str }
   # same rationale as for configure
   runtime: { type: any }
+  user: { type: any }
diff --git a/dogen/templates/template.jinja b/dogen/templates/template.jinja
index eac14d4..b0a6b41 100644
--- a/dogen/templates/template.jinja
+++ b/dogen/templates/template.jinja
@@ -102,6 +102,7 @@ RUN [ "bash", "-x", "/tmp/scripts/{{ script.package }}/{{ script.exec }}" ]
 {% if cct %}
 ADD cct /tmp/cct/
 ENV CCT_MODULES_PATH=/tmp/cct/
+USER {{ cct.user }}
 RUN cct {%if cct.verbose %}-v{% endif %} {% for run in cct.run %} /tmp/cct/{{ run }} {% endfor %}
 
 # Add cct in command mode as entrypoint


Test patch:
diff --git a/tests/test_cct_plugin.py b/tests/test_cct_plugin.py
new file mode 100644
index 0000000..1cc4c24
--- /dev/null
+++ b/tests/test_cct_plugin.py
@@ -0,0 +1,26 @@
+import tempfile
+import unittest
+import shutil
+
+from dogen.plugins import cct
+
+# minimal fake dogen object
+class MockDogen():
+    def __init__(self):
+        self.log = 0
+        self.descriptor = 0
+        self.output = ""
+
+class TestCCTPlugin(unittest.TestCase):
+    def setUp(self):
+        self.workdir = tempfile.mkdtemp(prefix='test_cct_plugin')
+        self.cct = cct.CCT(dogen=MockDogen())
+
+    def teardown(self):
+        shutil.rmtree(self.workdir)
+
+    def test_default_user_root(self):
+        cfg = { 'cct': { 'configure': '' }}
+        self.cct.prepare(cfg)
+        self.assertTrue('user' in cfg['cct'])
+        self.assertEqual(cfg['cct']['user'], "root")