diff --git a/lib/cdist/core/manifest.py b/lib/cdist/core/manifest.py index 8f9fc7ae..e97ba095 100644 --- a/lib/cdist/core/manifest.py +++ b/lib/cdist/core/manifest.py @@ -38,7 +38,7 @@ common: __global: full qualified path to the global output dir == local.out_path __cdist_manifest: full qualified path of the manifest == script __cdist_type_base_path: full qualified path to the directory where types are defined for use in type emulator - == local.type_base_path + == local.type_path initial manifest is: script: full qualified path to the initial manifest @@ -72,7 +72,7 @@ class Manifest(object): 'PATH': "%s:%s" % (self.local.bin_path, os.environ['PATH']), '__target_host': self.target_host, '__global': self.local.out_path, - '__cdist_type_base_path': self.local.type_base_path, # for use in type emulator + '__cdist_type_base_path': self.local.type_path, # for use in type emulator } def run_initial_manifest(self, script): @@ -90,5 +90,5 @@ class Manifest(object): '__object_fq': cdist_object.path, '__type': cdist_object.type.absolute_path, }) - script = os.path.join(self.local.type_base_path, cdist_object.type.manifest_path) + script = os.path.join(self.local.type_path, cdist_object.type.manifest_path) return self.local.run_script(script, env=env) diff --git a/lib/cdist/exec/local.py b/lib/cdist/exec/local.py index 521d56c3..1f253f09 100644 --- a/lib/cdist/exec/local.py +++ b/lib/cdist/exec/local.py @@ -59,7 +59,7 @@ class Local(object): self.conf_path = os.path.join(self.base_path, "conf") self.global_explorer_path = os.path.join(self.conf_path, "explorer") self.manifest_path = os.path.join(self.conf_path, "manifest") - self.type_base_path = os.path.join(self.conf_path, "type") + self.type_path = os.path.join(self.conf_path, "type") # FIXME: should not be needed anywhere self.lib_path = os.path.join(self.base_path, "lib") @@ -67,7 +67,7 @@ class Local(object): self.out_path = out_path self.bin_path = os.path.join(self.out_path, "bin") self.global_explorer_out_path = os.path.join(self.out_path, "explorer") - self.object_base_path = os.path.join(self.out_path, "object") + self.object_path = os.path.join(self.out_path, "object") self.log = logging.getLogger(self.target_host) @@ -123,7 +123,7 @@ class Local(object): def link_emulator(self, exec_path): """Link emulator to types""" src = os.path.abspath(exec_path) - for cdist_type in core.Type.list_types(self.type_base_path): + for cdist_type in core.Type.list_types(self.type_path): dst = os.path.join(self.bin_path, cdist_type.name) self.log.debug("Linking emulator: %s to %s", src, dst) diff --git a/lib/cdist/exec/remote.py b/lib/cdist/exec/remote.py index 7821e993..2ffc73fd 100644 --- a/lib/cdist/exec/remote.py +++ b/lib/cdist/exec/remote.py @@ -94,7 +94,7 @@ class Remote(object): cmd = self._exec.split() cmd.append(self.target_host) cmd.extend(command) - return self.run_command(cmd, env=None) + return self.run_command(cmd, env=env) def run_command(self, command, env=None): """Run the given command with the given environment. @@ -102,9 +102,18 @@ class Remote(object): """ assert isinstance(command, (list, tuple)), "list or tuple argument expected, got: %s" % command + + # can't pass environment to remote side, so prepend command with + # variable declarations + if env: + cmd = ["%s=%s" % item for item in env.items()] + cmd.extend(command) + else: + cmd = command + self.log.debug("Remote run: %s", command) try: - return subprocess.check_output(command, env=env).decode() + return subprocess.check_output(cmd).decode() except subprocess.CalledProcessError: raise cdist.Error("Command failed: " + " ".join(command)) except OSError as error: @@ -117,6 +126,12 @@ class Remote(object): """ command = self._exec.split() command.append(self.target_host) + + # can't pass environment to remote side, so prepend command with + # variable declarations + if env: + command.extend(["%s=%s" % item for item in env.items()]) + command.extend(["/bin/sh", "-e"]) command.append(script) @@ -125,7 +140,7 @@ class Remote(object): self.log.debug("Remote run script env: %s", env) try: - return subprocess.check_output(command, env=env).decode() + return subprocess.check_output(command).decode() except subprocess.CalledProcessError as error: script_content = self.run(["cat", script]) self.log.error("Code that raised the error:\n%s", script_content) diff --git a/lib/cdist/test/exec/test_local.py b/lib/cdist/test/exec/local.py similarity index 69% rename from lib/cdist/test/exec/test_local.py rename to lib/cdist/test/exec/local.py index b74f412e..8585f87a 100644 --- a/lib/cdist/test/exec/test_local.py +++ b/lib/cdist/test/exec/local.py @@ -50,12 +50,45 @@ class LocalTestCase(unittest.TestCase): def setUp(self): self.temp_dir = self.mkdtemp() target_host = 'localhost' - out_path = self.temp_dir - self.local = local.Local(target_host, local_base_path, out_path) + self.out_path = self.temp_dir + self.base_path = local_base_path + self.local = local.Local(target_host, self.base_path, self.out_path) def tearDown(self): shutil.rmtree(self.temp_dir) + ### test api + + def test_cache_path(self): + self.assertEqual(self.local.cache_path, os.path.join(self.base_path, "cache")) + + def test_conf_path(self): + self.assertEqual(self.local.conf_path, os.path.join(self.base_path, "conf")) + + def test_global_explorer_path(self): + self.assertEqual(self.local.global_explorer_path, os.path.join(self.base_path, "conf", "explorer")) + + def test_manifest_path(self): + self.assertEqual(self.local.manifest_path, os.path.join(self.base_path, "conf", "manifest")) + + def test_type_path(self): + self.assertEqual(self.local.type_path, os.path.join(self.base_path, "conf", "type")) + + def test_out_path(self): + self.assertEqual(self.local.out_path, self.out_path) + + def test_bin_path(self): + self.assertEqual(self.local.bin_path, os.path.join(self.out_path, "bin")) + + def test_global_explorer_out_path(self): + self.assertEqual(self.local.global_explorer_out_path, os.path.join(self.out_path, "explorer")) + + def test_object_path(self): + self.assertEqual(self.local.object_path, os.path.join(self.out_path, "object")) + + ### /test api + + def test_run_success(self): self.local.run(['/bin/true']) @@ -96,6 +129,5 @@ class LocalTestCase(unittest.TestCase): def test_create_directories(self): self.local.create_directories() - print('self.local.bin_path: %s' % self.local.bin_path) self.assertTrue(os.path.isdir(self.local.out_path)) self.assertTrue(os.path.isdir(self.local.bin_path)) diff --git a/lib/cdist/test/exec/test_remote.py b/lib/cdist/test/exec/remote.py similarity index 84% rename from lib/cdist/test/exec/test_remote.py rename to lib/cdist/test/exec/remote.py index 1fdb5833..ea9a17aa 100644 --- a/lib/cdist/test/exec/test_remote.py +++ b/lib/cdist/test/exec/remote.py @@ -42,15 +42,31 @@ class RemoteTestCase(unittest.TestCase): def setUp(self): self.temp_dir = self.mkdtemp() target_host = 'localhost' - remote_base_path = self.temp_dir + self.base_path = self.temp_dir user = getpass.getuser() remote_exec = "ssh -o User=%s -q" % user remote_copy = "scp -o User=%s -q" % user - self.remote = remote.Remote(target_host, remote_base_path, remote_exec, remote_copy) + self.remote = remote.Remote(target_host, self.base_path, remote_exec, remote_copy) def tearDown(self): shutil.rmtree(self.temp_dir) + ### test api + + def test_conf_path(self): + self.assertEqual(self.remote.conf_path, os.path.join(self.base_path, "conf")) + + def test_object_path(self): + self.assertEqual(self.remote.object_path, os.path.join(self.base_path, "object")) + + def test_type_path(self): + self.assertEqual(self.remote.type_path, os.path.join(self.base_path, "conf", "type")) + + def test_global_explorer_path(self): + self.assertEqual(self.remote.global_explorer_path, os.path.join(self.base_path, "conf", "explorer")) + + ### /test api + def test_run_success(self): self.remote.run(['/bin/true']) diff --git a/lib/cdist/test/manifest/__init__.py b/lib/cdist/test/manifest/__init__.py index ffbbff29..341f4893 100644 --- a/lib/cdist/test/manifest/__init__.py +++ b/lib/cdist/test/manifest/__init__.py @@ -72,12 +72,12 @@ class ManifestTestCase(unittest.TestCase): self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path)) self.assertEqual(output_dict['__target_host'], self.local.target_host) self.assertEqual(output_dict['__global'], self.local.out_path) - self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_base_path) + self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_path) self.assertEqual(output_dict['__manifest'], self.local.manifest_path) def test_type_manifest_environment(self): - cdist_type = core.Type(self.local.type_base_path, '__dump_environment') - cdist_object = core.Object(cdist_type, self.local.object_base_path, 'whatever') + cdist_type = core.Type(self.local.type_path, '__dump_environment') + cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever') output_string = self.manifest.run_type_manifest(cdist_object) output_dict = {} @@ -88,7 +88,7 @@ class ManifestTestCase(unittest.TestCase): self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path)) self.assertEqual(output_dict['__target_host'], self.local.target_host) self.assertEqual(output_dict['__global'], self.local.out_path) - self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_base_path) + self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_path) self.assertEqual(output_dict['__type'], cdist_type.absolute_path) self.assertEqual(output_dict['__object'], cdist_object.absolute_path) self.assertEqual(output_dict['__object_id'], cdist_object.object_id)