2011-10-13 12:26:36 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
2015-03-05 17:32:47 +00:00
|
|
|
# 2012-2015 Nico Schottelius (nico-cdist at schottelius.org)
|
2011-10-13 12:26:36 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2012-11-07 10:47:26 +00:00
|
|
|
import getpass
|
2011-10-13 12:26:36 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
import cdist
|
|
|
|
from cdist import core
|
|
|
|
from cdist import test
|
|
|
|
from cdist.exec import local
|
|
|
|
from cdist.exec import remote
|
|
|
|
from cdist.core import code
|
|
|
|
|
|
|
|
import os.path as op
|
|
|
|
my_dir = op.abspath(op.dirname(__file__))
|
|
|
|
fixtures = op.join(my_dir, 'fixtures')
|
2012-11-06 15:58:30 +00:00
|
|
|
conf_dir = op.join(fixtures, 'conf')
|
2011-10-13 12:26:36 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2011-10-19 15:59:21 +00:00
|
|
|
class CodeTestCase(test.CdistTestCase):
|
2011-10-13 12:26:36 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2013-08-19 11:37:40 +00:00
|
|
|
self.local_dir = self.mkdtemp()
|
2016-08-10 21:56:56 +00:00
|
|
|
self.hostdir = cdist.str_hash(self.target_host[0])
|
2016-07-23 14:13:59 +00:00
|
|
|
self.host_base_path = os.path.join(self.local_dir, self.hostdir)
|
2012-11-06 15:58:30 +00:00
|
|
|
|
|
|
|
self.local = local.Local(
|
2016-07-05 18:44:24 +00:00
|
|
|
target_host=self.target_host,
|
2017-07-20 20:04:44 +00:00
|
|
|
target_host_tags=self.target_host_tags,
|
2016-07-23 14:13:59 +00:00
|
|
|
base_root_path=self.host_base_path,
|
|
|
|
host_dir_name=self.hostdir,
|
2016-07-05 18:44:24 +00:00
|
|
|
exec_path=cdist.test.cdist_exec_path,
|
2012-11-06 15:58:30 +00:00
|
|
|
add_conf_dirs=[conf_dir])
|
2012-11-01 14:20:10 +00:00
|
|
|
self.local.create_files_dirs()
|
2011-10-13 12:26:36 +00:00
|
|
|
|
2013-08-19 11:37:40 +00:00
|
|
|
self.remote_dir = self.mkdtemp()
|
2012-11-06 15:58:30 +00:00
|
|
|
remote_exec = self.remote_exec
|
|
|
|
remote_copy = self.remote_copy
|
2013-08-28 13:39:17 +00:00
|
|
|
self.remote = remote.Remote(
|
2016-07-05 18:44:24 +00:00
|
|
|
target_host=self.target_host,
|
|
|
|
remote_exec=remote_exec,
|
2013-08-28 13:44:52 +00:00
|
|
|
remote_copy=remote_copy,
|
|
|
|
base_path=self.remote_dir)
|
|
|
|
self.remote.create_files_dirs()
|
2011-10-13 12:26:36 +00:00
|
|
|
|
2011-10-14 21:02:47 +00:00
|
|
|
self.code = code.Code(self.target_host, self.local, self.remote)
|
2011-10-13 12:26:36 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
self.cdist_type = core.CdistType(self.local.type_path,
|
|
|
|
'__dump_environment')
|
|
|
|
self.cdist_object = core.CdistObject(
|
|
|
|
self.cdist_type, self.local.object_path, 'whatever',
|
|
|
|
self.local.object_marker_name)
|
2011-10-13 12:57:09 +00:00
|
|
|
self.cdist_object.create()
|
2011-10-13 12:50:50 +00:00
|
|
|
|
2011-10-13 12:26:36 +00:00
|
|
|
def tearDown(self):
|
2013-08-19 11:37:40 +00:00
|
|
|
shutil.rmtree(self.local_dir)
|
|
|
|
shutil.rmtree(self.remote_dir)
|
2011-10-13 12:26:36 +00:00
|
|
|
|
|
|
|
def test_run_gencode_local_environment(self):
|
2011-10-13 12:50:50 +00:00
|
|
|
output_string = self.code.run_gencode_local(self.cdist_object)
|
2011-10-13 12:26:36 +00:00
|
|
|
output_dict = {}
|
|
|
|
for line in output_string.split('\n'):
|
|
|
|
if line:
|
2016-07-05 18:44:24 +00:00
|
|
|
junk, value = line.split(': ')
|
2011-10-13 12:26:36 +00:00
|
|
|
key = junk.split(' ')[1]
|
|
|
|
output_dict[key] = value
|
2016-08-10 21:56:56 +00:00
|
|
|
self.assertEqual(output_dict['__target_host'],
|
|
|
|
self.local.target_host[0])
|
|
|
|
self.assertEqual(output_dict['__target_hostname'],
|
|
|
|
self.local.target_host[1])
|
|
|
|
self.assertEqual(output_dict['__target_fqdn'],
|
|
|
|
self.local.target_host[2])
|
2013-08-19 10:03:25 +00:00
|
|
|
self.assertEqual(output_dict['__global'], self.local.base_path)
|
2011-10-13 12:50:50 +00:00
|
|
|
self.assertEqual(output_dict['__type'], self.cdist_type.absolute_path)
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(output_dict['__object'],
|
|
|
|
self.cdist_object.absolute_path)
|
|
|
|
self.assertEqual(output_dict['__object_id'],
|
|
|
|
self.cdist_object.object_id)
|
2011-10-19 13:15:05 +00:00
|
|
|
self.assertEqual(output_dict['__object_name'], self.cdist_object.name)
|
2016-06-11 10:02:13 +00:00
|
|
|
self.assertEqual(output_dict['__files'], self.local.files_path)
|
2017-07-20 20:04:44 +00:00
|
|
|
self.assertEqual(output_dict['__target_host_tags'],
|
|
|
|
self.local.target_host_tags)
|
2011-10-13 12:26:36 +00:00
|
|
|
|
|
|
|
def test_run_gencode_remote_environment(self):
|
2011-10-13 12:50:50 +00:00
|
|
|
output_string = self.code.run_gencode_remote(self.cdist_object)
|
2011-10-13 12:26:36 +00:00
|
|
|
output_dict = {}
|
|
|
|
for line in output_string.split('\n'):
|
|
|
|
if line:
|
2016-07-05 18:44:24 +00:00
|
|
|
junk, value = line.split(': ')
|
2011-10-13 12:26:36 +00:00
|
|
|
key = junk.split(' ')[1]
|
|
|
|
output_dict[key] = value
|
2016-08-10 21:56:56 +00:00
|
|
|
self.assertEqual(output_dict['__target_host'],
|
|
|
|
self.local.target_host[0])
|
|
|
|
self.assertEqual(output_dict['__target_hostname'],
|
|
|
|
self.local.target_host[1])
|
|
|
|
self.assertEqual(output_dict['__target_fqdn'],
|
|
|
|
self.local.target_host[2])
|
2013-08-19 10:03:25 +00:00
|
|
|
self.assertEqual(output_dict['__global'], self.local.base_path)
|
2011-10-13 12:50:50 +00:00
|
|
|
self.assertEqual(output_dict['__type'], self.cdist_type.absolute_path)
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(output_dict['__object'],
|
|
|
|
self.cdist_object.absolute_path)
|
|
|
|
self.assertEqual(output_dict['__object_id'],
|
|
|
|
self.cdist_object.object_id)
|
2011-10-19 13:15:05 +00:00
|
|
|
self.assertEqual(output_dict['__object_name'], self.cdist_object.name)
|
2016-06-11 10:02:13 +00:00
|
|
|
self.assertEqual(output_dict['__files'], self.local.files_path)
|
2017-07-20 20:04:44 +00:00
|
|
|
self.assertEqual(output_dict['__target_host_tags'],
|
|
|
|
self.local.target_host_tags)
|
2011-10-13 12:26:36 +00:00
|
|
|
|
2011-10-13 12:57:09 +00:00
|
|
|
def test_transfer_code_remote(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.cdist_object.code_remote = self.code.run_gencode_remote(
|
|
|
|
self.cdist_object)
|
2011-10-13 12:57:09 +00:00
|
|
|
self.code.transfer_code_remote(self.cdist_object)
|
2016-07-05 18:44:24 +00:00
|
|
|
destination = os.path.join(self.remote.object_path,
|
|
|
|
self.cdist_object.code_remote_path)
|
2011-10-13 12:57:09 +00:00
|
|
|
self.assertTrue(os.path.isfile(destination))
|
|
|
|
|
2011-10-14 13:58:42 +00:00
|
|
|
def test_run_code_local(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.cdist_object.code_local = self.code.run_gencode_local(
|
|
|
|
self.cdist_object)
|
2011-10-14 13:58:42 +00:00
|
|
|
self.code.run_code_local(self.cdist_object)
|
2011-10-14 21:02:47 +00:00
|
|
|
|
2011-10-13 13:09:05 +00:00
|
|
|
def test_run_code_remote_environment(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.cdist_object.code_remote = self.code.run_gencode_remote(
|
|
|
|
self.cdist_object)
|
2011-10-13 15:11:03 +00:00
|
|
|
self.code.transfer_code_remote(self.cdist_object)
|
2011-10-14 13:58:42 +00:00
|
|
|
self.code.run_code_remote(self.cdist_object)
|
2016-06-11 10:02:13 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import unittest
|
|
|
|
unittest.main()
|