Merge remote-tracking branch 'steven/master'
This commit is contained in:
		
				commit
				
					
						722f857b68
					
				
			
		
					 6 changed files with 81 additions and 18 deletions
				
			
		| 
						 | 
					@ -38,7 +38,7 @@ common:
 | 
				
			||||||
        __global: full qualified path to the global output dir == local.out_path
 | 
					        __global: full qualified path to the global output dir == local.out_path
 | 
				
			||||||
        __cdist_manifest: full qualified path of the manifest == script
 | 
					        __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
 | 
					        __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:
 | 
					initial manifest is:
 | 
				
			||||||
    script: full qualified path to the initial manifest
 | 
					    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']),
 | 
					            'PATH': "%s:%s" % (self.local.bin_path, os.environ['PATH']),
 | 
				
			||||||
            '__target_host': self.target_host,
 | 
					            '__target_host': self.target_host,
 | 
				
			||||||
            '__global': self.local.out_path,
 | 
					            '__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):
 | 
					    def run_initial_manifest(self, script):
 | 
				
			||||||
| 
						 | 
					@ -90,5 +90,5 @@ class Manifest(object):
 | 
				
			||||||
            '__object_fq': cdist_object.path,
 | 
					            '__object_fq': cdist_object.path,
 | 
				
			||||||
            '__type': cdist_object.type.absolute_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)
 | 
					        return self.local.run_script(script, env=env)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -59,7 +59,7 @@ class Local(object):
 | 
				
			||||||
        self.conf_path = os.path.join(self.base_path, "conf")
 | 
					        self.conf_path = os.path.join(self.base_path, "conf")
 | 
				
			||||||
        self.global_explorer_path = os.path.join(self.conf_path, "explorer")
 | 
					        self.global_explorer_path = os.path.join(self.conf_path, "explorer")
 | 
				
			||||||
        self.manifest_path = os.path.join(self.conf_path, "manifest")
 | 
					        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
 | 
					        # FIXME: should not be needed anywhere
 | 
				
			||||||
        self.lib_path = os.path.join(self.base_path, "lib")
 | 
					        self.lib_path = os.path.join(self.base_path, "lib")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -67,7 +67,7 @@ class Local(object):
 | 
				
			||||||
        self.out_path = out_path
 | 
					        self.out_path = out_path
 | 
				
			||||||
        self.bin_path = os.path.join(self.out_path, "bin")
 | 
					        self.bin_path = os.path.join(self.out_path, "bin")
 | 
				
			||||||
        self.global_explorer_out_path = os.path.join(self.out_path, "explorer")
 | 
					        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)
 | 
					        self.log = logging.getLogger(self.target_host)
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
| 
						 | 
					@ -123,7 +123,7 @@ class Local(object):
 | 
				
			||||||
    def link_emulator(self, exec_path):
 | 
					    def link_emulator(self, exec_path):
 | 
				
			||||||
        """Link emulator to types"""
 | 
					        """Link emulator to types"""
 | 
				
			||||||
        src = os.path.abspath(exec_path)
 | 
					        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)
 | 
					            dst = os.path.join(self.bin_path, cdist_type.name)
 | 
				
			||||||
            self.log.debug("Linking emulator: %s to %s", src, dst)
 | 
					            self.log.debug("Linking emulator: %s to %s", src, dst)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -94,7 +94,7 @@ class Remote(object):
 | 
				
			||||||
        cmd = self._exec.split()
 | 
					        cmd = self._exec.split()
 | 
				
			||||||
        cmd.append(self.target_host)
 | 
					        cmd.append(self.target_host)
 | 
				
			||||||
        cmd.extend(command)
 | 
					        cmd.extend(command)
 | 
				
			||||||
        return self.run_command(cmd, env=None)
 | 
					        return self.run_command(cmd, env=env)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def run_command(self, command, env=None):
 | 
					    def run_command(self, command, env=None):
 | 
				
			||||||
        """Run the given command with the given environment.
 | 
					        """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
 | 
					        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)
 | 
					        self.log.debug("Remote run: %s", command)
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            return subprocess.check_output(command, env=env).decode()
 | 
					            return subprocess.check_output(cmd).decode()
 | 
				
			||||||
        except subprocess.CalledProcessError:
 | 
					        except subprocess.CalledProcessError:
 | 
				
			||||||
            raise cdist.Error("Command failed: " + " ".join(command))
 | 
					            raise cdist.Error("Command failed: " + " ".join(command))
 | 
				
			||||||
        except OSError as error:
 | 
					        except OSError as error:
 | 
				
			||||||
| 
						 | 
					@ -117,6 +126,12 @@ class Remote(object):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        command = self._exec.split()
 | 
					        command = self._exec.split()
 | 
				
			||||||
        command.append(self.target_host)
 | 
					        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.extend(["/bin/sh", "-e"])
 | 
				
			||||||
        command.append(script)
 | 
					        command.append(script)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -125,7 +140,7 @@ class Remote(object):
 | 
				
			||||||
            self.log.debug("Remote run script env: %s", env)
 | 
					            self.log.debug("Remote run script env: %s", env)
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            return subprocess.check_output(command, env=env).decode()
 | 
					            return subprocess.check_output(command).decode()
 | 
				
			||||||
        except subprocess.CalledProcessError as error:
 | 
					        except subprocess.CalledProcessError as error:
 | 
				
			||||||
            script_content = self.run(["cat", script])
 | 
					            script_content = self.run(["cat", script])
 | 
				
			||||||
            self.log.error("Code that raised the error:\n%s", script_content)
 | 
					            self.log.error("Code that raised the error:\n%s", script_content)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -50,12 +50,45 @@ class LocalTestCase(unittest.TestCase):
 | 
				
			||||||
    def setUp(self):
 | 
					    def setUp(self):
 | 
				
			||||||
        self.temp_dir = self.mkdtemp()
 | 
					        self.temp_dir = self.mkdtemp()
 | 
				
			||||||
        target_host = 'localhost'
 | 
					        target_host = 'localhost'
 | 
				
			||||||
        out_path = self.temp_dir
 | 
					        self.out_path = self.temp_dir
 | 
				
			||||||
        self.local = local.Local(target_host, local_base_path, out_path)
 | 
					        self.base_path = local_base_path
 | 
				
			||||||
 | 
					        self.local = local.Local(target_host, self.base_path, self.out_path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def tearDown(self):
 | 
					    def tearDown(self):
 | 
				
			||||||
        shutil.rmtree(self.temp_dir)
 | 
					        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):
 | 
					    def test_run_success(self):
 | 
				
			||||||
        self.local.run(['/bin/true'])
 | 
					        self.local.run(['/bin/true'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -96,6 +129,5 @@ class LocalTestCase(unittest.TestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_create_directories(self):
 | 
					    def test_create_directories(self):
 | 
				
			||||||
        self.local.create_directories()
 | 
					        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.out_path))
 | 
				
			||||||
        self.assertTrue(os.path.isdir(self.local.bin_path))
 | 
					        self.assertTrue(os.path.isdir(self.local.bin_path))
 | 
				
			||||||
| 
						 | 
					@ -42,15 +42,31 @@ class RemoteTestCase(unittest.TestCase):
 | 
				
			||||||
    def setUp(self):
 | 
					    def setUp(self):
 | 
				
			||||||
        self.temp_dir = self.mkdtemp()
 | 
					        self.temp_dir = self.mkdtemp()
 | 
				
			||||||
        target_host = 'localhost'
 | 
					        target_host = 'localhost'
 | 
				
			||||||
        remote_base_path = self.temp_dir
 | 
					        self.base_path = self.temp_dir
 | 
				
			||||||
        user = getpass.getuser()
 | 
					        user = getpass.getuser()
 | 
				
			||||||
        remote_exec = "ssh -o User=%s -q" % user
 | 
					        remote_exec = "ssh -o User=%s -q" % user
 | 
				
			||||||
        remote_copy = "scp -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):
 | 
					    def tearDown(self):
 | 
				
			||||||
        shutil.rmtree(self.temp_dir)
 | 
					        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):
 | 
					    def test_run_success(self):
 | 
				
			||||||
        self.remote.run(['/bin/true'])
 | 
					        self.remote.run(['/bin/true'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -72,12 +72,12 @@ class ManifestTestCase(unittest.TestCase):
 | 
				
			||||||
        self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path))
 | 
					        self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path))
 | 
				
			||||||
        self.assertEqual(output_dict['__target_host'], self.local.target_host)
 | 
					        self.assertEqual(output_dict['__target_host'], self.local.target_host)
 | 
				
			||||||
        self.assertEqual(output_dict['__global'], self.local.out_path)
 | 
					        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)
 | 
					        self.assertEqual(output_dict['__manifest'], self.local.manifest_path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_type_manifest_environment(self):
 | 
					    def test_type_manifest_environment(self):
 | 
				
			||||||
        cdist_type = core.Type(self.local.type_base_path, '__dump_environment')
 | 
					        cdist_type = core.Type(self.local.type_path, '__dump_environment')
 | 
				
			||||||
        cdist_object = core.Object(cdist_type, self.local.object_base_path, 'whatever')
 | 
					        cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever')
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        output_string = self.manifest.run_type_manifest(cdist_object)
 | 
					        output_string = self.manifest.run_type_manifest(cdist_object)
 | 
				
			||||||
        output_dict = {}
 | 
					        output_dict = {}
 | 
				
			||||||
| 
						 | 
					@ -88,7 +88,7 @@ class ManifestTestCase(unittest.TestCase):
 | 
				
			||||||
        self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path))
 | 
					        self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path))
 | 
				
			||||||
        self.assertEqual(output_dict['__target_host'], self.local.target_host)
 | 
					        self.assertEqual(output_dict['__target_host'], self.local.target_host)
 | 
				
			||||||
        self.assertEqual(output_dict['__global'], self.local.out_path)
 | 
					        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['__type'], cdist_type.absolute_path)
 | 
				
			||||||
        self.assertEqual(output_dict['__object'], cdist_object.absolute_path)
 | 
					        self.assertEqual(output_dict['__object'], cdist_object.absolute_path)
 | 
				
			||||||
        self.assertEqual(output_dict['__object_id'], cdist_object.object_id)
 | 
					        self.assertEqual(output_dict['__object_id'], cdist_object.object_id)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue