2011-10-12 12:30:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import getpass
|
|
|
|
import shutil
|
|
|
|
import string
|
|
|
|
import random
|
2016-08-12 19:14:56 +00:00
|
|
|
import multiprocessing
|
2011-10-12 12:30:10 +00:00
|
|
|
|
|
|
|
import cdist
|
2011-10-21 12:48:47 +00:00
|
|
|
from cdist import test
|
2011-10-12 12:30:10 +00:00
|
|
|
from cdist.exec import remote
|
|
|
|
|
|
|
|
|
2011-10-19 15:59:21 +00:00
|
|
|
class RemoteTestCase(test.CdistTestCase):
|
2011-10-12 12:30:10 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.temp_dir = self.mkdtemp()
|
2016-08-10 21:56:56 +00:00
|
|
|
self.target_host = (
|
|
|
|
'localhost',
|
|
|
|
'localhost',
|
|
|
|
'localhost',
|
|
|
|
)
|
2011-10-12 22:24:52 +00:00
|
|
|
self.base_path = self.temp_dir
|
2011-10-12 12:30:10 +00:00
|
|
|
user = getpass.getuser()
|
|
|
|
remote_exec = "ssh -o User=%s -q" % user
|
|
|
|
remote_copy = "scp -o User=%s -q" % user
|
2016-07-05 18:44:24 +00:00
|
|
|
self.remote = remote.Remote(self.target_host, base_path=self.base_path,
|
|
|
|
remote_exec=remote_exec,
|
|
|
|
remote_copy=remote_copy)
|
2011-10-12 12:30:10 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.temp_dir)
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
# test api
|
2011-10-12 22:24:52 +00:00
|
|
|
|
|
|
|
def test_conf_path(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.remote.conf_path,
|
|
|
|
os.path.join(self.base_path, "conf"))
|
2011-10-12 22:24:52 +00:00
|
|
|
|
|
|
|
def test_object_path(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.remote.object_path,
|
|
|
|
os.path.join(self.base_path, "object"))
|
2011-10-12 22:24:52 +00:00
|
|
|
|
|
|
|
def test_type_path(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.remote.type_path,
|
|
|
|
os.path.join(self.base_path, "conf", "type"))
|
2011-10-12 22:24:52 +00:00
|
|
|
|
|
|
|
def test_global_explorer_path(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.remote.global_explorer_path,
|
|
|
|
os.path.join(self.base_path, "conf", "explorer"))
|
2011-10-12 22:24:52 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
# /test api
|
2011-10-12 22:24:52 +00:00
|
|
|
|
2011-10-12 12:30:10 +00:00
|
|
|
def test_run_success(self):
|
2016-08-11 19:55:20 +00:00
|
|
|
self.remote.run(['true'])
|
2011-10-12 12:30:10 +00:00
|
|
|
|
|
|
|
def test_run_fail(self):
|
2016-08-11 19:55:20 +00:00
|
|
|
self.assertRaises(cdist.Error, self.remote.run, ['false'])
|
2011-10-12 12:30:10 +00:00
|
|
|
|
|
|
|
def test_run_script_success(self):
|
|
|
|
handle, script = self.mkstemp(dir=self.temp_dir)
|
2011-10-21 13:26:27 +00:00
|
|
|
with os.fdopen(handle, "w") as fd:
|
2016-08-11 19:55:20 +00:00
|
|
|
fd.writelines(["#!/bin/sh\n", "true"])
|
2011-10-12 12:30:10 +00:00
|
|
|
self.remote.run_script(script)
|
|
|
|
|
|
|
|
def test_run_script_fail(self):
|
|
|
|
handle, script = self.mkstemp(dir=self.temp_dir)
|
2011-10-21 13:26:27 +00:00
|
|
|
with os.fdopen(handle, "w") as fd:
|
2016-08-11 19:55:20 +00:00
|
|
|
fd.writelines(["#!/bin/sh\n", "false"])
|
|
|
|
self.assertRaises(cdist.Error, self.remote.run_script,
|
2016-07-05 18:44:24 +00:00
|
|
|
script)
|
2011-10-12 12:30:10 +00:00
|
|
|
|
|
|
|
def test_run_script_get_output(self):
|
|
|
|
handle, script = self.mkstemp(dir=self.temp_dir)
|
2011-10-21 13:26:27 +00:00
|
|
|
with os.fdopen(handle, "w") as fd:
|
|
|
|
fd.writelines(["#!/bin/sh\n", "echo foobar"])
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.remote.run_script(script, return_output=True),
|
|
|
|
"foobar\n")
|
2011-10-12 12:30:10 +00:00
|
|
|
|
|
|
|
def test_mkdir(self):
|
|
|
|
temp_dir = self.mkdtemp(dir=self.temp_dir)
|
|
|
|
os.rmdir(temp_dir)
|
|
|
|
self.remote.mkdir(temp_dir)
|
|
|
|
self.assertTrue(os.path.isdir(temp_dir))
|
|
|
|
|
|
|
|
def test_rmdir(self):
|
|
|
|
temp_dir = self.mkdtemp(dir=self.temp_dir)
|
|
|
|
self.remote.rmdir(temp_dir)
|
|
|
|
self.assertFalse(os.path.isdir(temp_dir))
|
|
|
|
|
|
|
|
def test_transfer_file(self):
|
|
|
|
handle, source = self.mkstemp(dir=self.temp_dir)
|
2011-10-21 13:26:27 +00:00
|
|
|
os.close(handle)
|
2011-10-12 12:30:10 +00:00
|
|
|
target = self.mkdtemp(dir=self.temp_dir)
|
|
|
|
self.remote.transfer(source, target)
|
|
|
|
self.assertTrue(os.path.isfile(target))
|
|
|
|
|
|
|
|
def test_transfer_dir(self):
|
|
|
|
source = self.mkdtemp(dir=self.temp_dir)
|
|
|
|
# put a file in the directory as payload
|
|
|
|
handle, source_file = self.mkstemp(dir=source)
|
2011-10-21 13:26:27 +00:00
|
|
|
os.close(handle)
|
2011-10-12 12:30:10 +00:00
|
|
|
source_file_name = os.path.split(source_file)[-1]
|
|
|
|
target = self.mkdtemp(dir=self.temp_dir)
|
|
|
|
self.remote.transfer(source, target)
|
|
|
|
# test if the payload file is in the target directory
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(target, source_file_name)))
|
|
|
|
|
2016-08-12 19:14:56 +00:00
|
|
|
def test_transfer_dir_parallel(self):
|
|
|
|
source = self.mkdtemp(dir=self.temp_dir)
|
|
|
|
# put 8 files in the directory as payload
|
|
|
|
filenames = []
|
|
|
|
for x in range(8):
|
|
|
|
handle, source_file = self.mkstemp(dir=source)
|
|
|
|
os.close(handle)
|
|
|
|
source_file_name = os.path.split(source_file)[-1]
|
|
|
|
filenames.append(source_file_name)
|
|
|
|
target = self.mkdtemp(dir=self.temp_dir)
|
|
|
|
self.remote.transfer_dir_parallel(source, target,
|
|
|
|
multiprocessing.cpu_count())
|
|
|
|
# test if the payload files are in the target directory
|
|
|
|
for filename in filenames:
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(target, filename)))
|
|
|
|
|
2016-08-11 19:55:20 +00:00
|
|
|
def test_create_files_dirs(self):
|
|
|
|
self.remote.create_files_dirs()
|
2011-10-12 12:30:10 +00:00
|
|
|
self.assertTrue(os.path.isdir(self.remote.base_path))
|
|
|
|
self.assertTrue(os.path.isdir(self.remote.conf_path))
|
2011-10-21 13:10:41 +00:00
|
|
|
|
2011-10-21 13:16:38 +00:00
|
|
|
def test_run_target_host_in_env(self):
|
|
|
|
handle, remote_exec_path = self.mkstemp(dir=self.temp_dir)
|
|
|
|
with os.fdopen(handle, 'w') as fd:
|
|
|
|
fd.writelines(["#!/bin/sh\n", "echo $__target_host"])
|
|
|
|
os.chmod(remote_exec_path, 0o755)
|
|
|
|
remote_exec = remote_exec_path
|
|
|
|
remote_copy = "echo"
|
2016-07-05 18:44:24 +00:00
|
|
|
r = remote.Remote(self.target_host, base_path=self.base_path,
|
|
|
|
remote_exec=remote_exec, remote_copy=remote_copy)
|
2016-08-11 19:55:20 +00:00
|
|
|
self.assertEqual(r.run('true', return_output=True),
|
2016-08-10 21:56:56 +00:00
|
|
|
"%s\n" % self.target_host[0])
|
2011-10-21 13:16:38 +00:00
|
|
|
|
2011-10-21 13:10:41 +00:00
|
|
|
def test_run_script_target_host_in_env(self):
|
|
|
|
handle, remote_exec_path = self.mkstemp(dir=self.temp_dir)
|
|
|
|
with os.fdopen(handle, 'w') as fd:
|
|
|
|
fd.writelines(["#!/bin/sh\n", "echo $__target_host"])
|
|
|
|
os.chmod(remote_exec_path, 0o755)
|
|
|
|
remote_exec = remote_exec_path
|
|
|
|
remote_copy = "echo"
|
2016-07-05 18:44:24 +00:00
|
|
|
r = remote.Remote(self.target_host, base_path=self.base_path,
|
|
|
|
remote_exec=remote_exec, remote_copy=remote_copy)
|
2011-10-21 13:10:41 +00:00
|
|
|
handle, script = self.mkstemp(dir=self.temp_dir)
|
|
|
|
with os.fdopen(handle, "w") as fd:
|
2016-08-11 19:55:20 +00:00
|
|
|
fd.writelines(["#!/bin/sh\n", "true"])
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(r.run_script(script, return_output=True),
|
2016-08-10 21:56:56 +00:00
|
|
|
"%s\n" % self.target_host[0])
|
2016-03-18 20:57:50 +00:00
|
|
|
|
|
|
|
def test_run_script_with_env_target_host_in_env(self):
|
|
|
|
handle, script = self.mkstemp(dir=self.temp_dir)
|
|
|
|
with os.fdopen(handle, "w") as fd:
|
2016-07-05 18:44:24 +00:00
|
|
|
fd.writelines([
|
|
|
|
"#!/bin/sh\n",
|
|
|
|
('if [ "$__object" ]; then echo $__object; '
|
|
|
|
'else echo no_env; fi\n')])
|
2016-03-18 20:57:50 +00:00
|
|
|
os.chmod(script, 0o755)
|
|
|
|
handle, remote_exec_path = self.mkstemp(dir=self.temp_dir)
|
|
|
|
with os.fdopen(handle, 'w') as fd:
|
|
|
|
fd.writelines(["#!/bin/sh\n", 'shift; cmd=$1; shift; $cmd "$@"\n'])
|
|
|
|
os.chmod(remote_exec_path, 0o755)
|
|
|
|
remote_exec = remote_exec_path
|
|
|
|
remote_copy = "echo"
|
2016-07-05 18:44:24 +00:00
|
|
|
r = remote.Remote(self.target_host, base_path=self.base_path,
|
|
|
|
remote_exec=remote_exec, remote_copy=remote_copy)
|
2016-03-18 20:57:50 +00:00
|
|
|
output = r.run_script(script, return_output=True)
|
|
|
|
self.assertEqual(output, "no_env\n")
|
2016-04-01 08:42:32 +00:00
|
|
|
|
|
|
|
handle, remote_exec_path = self.mkstemp(dir=self.temp_dir)
|
|
|
|
with os.fdopen(handle, 'w') as fd:
|
|
|
|
fd.writelines(["#!/bin/sh\n", 'shift; cmd=$1; eval $cmd\n'])
|
|
|
|
os.chmod(remote_exec_path, 0o755)
|
|
|
|
remote_exec = remote_exec_path
|
2016-03-18 20:57:50 +00:00
|
|
|
env = {
|
|
|
|
'__object': 'test_object',
|
|
|
|
}
|
2016-07-05 18:44:24 +00:00
|
|
|
r = remote.Remote(self.target_host, base_path=self.base_path,
|
|
|
|
remote_exec=remote_exec, remote_copy=remote_copy)
|
2016-03-18 20:57:50 +00:00
|
|
|
output = r.run_script(script, env=env, return_output=True)
|
|
|
|
self.assertEqual(output, "test_object\n")
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
unittest.main()
|