2011-10-12 23:25:31 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
2013-08-19 10:03:25 +00:00
|
|
|
# 2011-2013 Nico Schottelius (nico-cdist at schottelius.org)
|
2011-10-12 23:25:31 +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/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import getpass
|
2016-08-11 21:54:31 +00:00
|
|
|
import multiprocessing
|
2011-10-12 23:25:31 +00:00
|
|
|
|
|
|
|
import cdist
|
|
|
|
from cdist import core
|
|
|
|
from cdist import test
|
|
|
|
from cdist.exec import local
|
|
|
|
from cdist.exec import remote
|
|
|
|
from cdist.core import explorer
|
|
|
|
|
|
|
|
import os.path as op
|
|
|
|
my_dir = op.abspath(op.dirname(__file__))
|
|
|
|
fixtures = op.join(my_dir, 'fixtures')
|
2012-11-01 17:21:56 +00:00
|
|
|
conf_dir = op.join(fixtures, "conf")
|
2011-10-12 23:25:31 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2011-10-19 15:59:21 +00:00
|
|
|
class ExplorerClassTestCase(test.CdistTestCase):
|
2011-10-12 23:25:31 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.temp_dir = self.mkdtemp()
|
|
|
|
self.local_path = os.path.join(self.temp_dir, "local")
|
2016-08-10 21:56:56 +00:00
|
|
|
hostdir = cdist.str_hash(self.target_host[0])
|
2016-07-23 14:13:59 +00:00
|
|
|
base_root_path = os.path.join(self.local_path, hostdir)
|
2016-07-05 18:44:24 +00:00
|
|
|
self.remote_base_path = os.path.join(self.temp_dir, "remote")
|
2012-11-02 13:15:14 +00:00
|
|
|
os.makedirs(self.remote_base_path)
|
2012-11-01 17:21:56 +00:00
|
|
|
|
|
|
|
self.local = local.Local(
|
|
|
|
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=base_root_path,
|
|
|
|
host_dir_name=hostdir,
|
2012-11-01 17:21:56 +00:00
|
|
|
exec_path=test.cdist_exec_path,
|
2013-08-19 10:03:25 +00:00
|
|
|
add_conf_dirs=[conf_dir],
|
|
|
|
)
|
2012-11-01 17:21:56 +00:00
|
|
|
|
2012-11-01 14:20:10 +00:00
|
|
|
self.local.create_files_dirs()
|
2011-10-12 23:25:31 +00:00
|
|
|
|
2012-11-01 17:21:56 +00:00
|
|
|
self.remote = remote.Remote(
|
2016-07-05 18:44:24 +00:00
|
|
|
target_host=self.target_host,
|
2013-08-19 10:03:25 +00:00
|
|
|
remote_exec=self.remote_exec,
|
|
|
|
remote_copy=self.remote_copy,
|
|
|
|
base_path=self.remote_base_path)
|
2012-11-06 15:47:51 +00:00
|
|
|
self.remote.create_files_dirs()
|
2011-10-12 23:25:31 +00:00
|
|
|
|
2012-11-01 17:21:56 +00:00
|
|
|
self.explorer = explorer.Explorer(
|
2016-07-05 18:44:24 +00:00
|
|
|
self.target_host,
|
|
|
|
self.local,
|
2012-11-01 17:21:56 +00:00
|
|
|
self.remote)
|
2011-10-14 21:11:57 +00:00
|
|
|
|
2011-10-12 23:25:31 +00:00
|
|
|
def tearDown(self):
|
2012-11-01 17:21:56 +00:00
|
|
|
shutil.rmtree(self.temp_dir)
|
2011-10-12 23:25:31 +00:00
|
|
|
|
2011-10-13 11:30:41 +00:00
|
|
|
def test_list_global_explorer_names(self):
|
2013-09-04 21:33:52 +00:00
|
|
|
"""Ensure that all explorers are listed"""
|
2012-11-01 17:21:56 +00:00
|
|
|
names = self.explorer.list_global_explorer_names()
|
|
|
|
self.assertIn("foobar", names)
|
|
|
|
self.assertIn("global", names)
|
2011-10-13 11:30:41 +00:00
|
|
|
|
2011-10-12 23:25:31 +00:00
|
|
|
def test_transfer_global_explorers(self):
|
2013-09-04 21:33:52 +00:00
|
|
|
"""Ensure transferring explorers to remote works"""
|
2011-10-12 23:25:31 +00:00
|
|
|
self.explorer.transfer_global_explorers()
|
2011-10-13 11:20:07 +00:00
|
|
|
source = self.local.global_explorer_path
|
|
|
|
destination = self.remote.global_explorer_path
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(sorted(os.listdir(source)),
|
|
|
|
sorted(os.listdir(destination)))
|
2011-10-12 23:25:31 +00:00
|
|
|
|
|
|
|
def test_run_global_explorer(self):
|
2012-11-06 15:47:51 +00:00
|
|
|
"""Checkt that running ONE global explorer works"""
|
2011-10-12 23:25:31 +00:00
|
|
|
self.explorer.transfer_global_explorers()
|
2011-10-13 11:21:15 +00:00
|
|
|
output = self.explorer.run_global_explorer('global')
|
|
|
|
self.assertEqual(output, 'global\n')
|
2011-10-12 23:25:31 +00:00
|
|
|
|
2013-09-04 21:33:52 +00:00
|
|
|
def test_global_explorer_output(self):
|
2012-11-06 15:47:51 +00:00
|
|
|
"""Ensure output is created for every global explorer"""
|
2011-10-20 08:55:11 +00:00
|
|
|
out_path = self.mkdtemp()
|
2012-11-06 15:47:51 +00:00
|
|
|
|
2011-10-20 08:55:11 +00:00
|
|
|
self.explorer.run_global_explorers(out_path)
|
2012-11-06 15:47:51 +00:00
|
|
|
names = sorted(self.explorer.list_global_explorer_names())
|
|
|
|
output = sorted(os.listdir(out_path))
|
|
|
|
|
|
|
|
self.assertEqual(names, output)
|
2011-10-20 08:55:11 +00:00
|
|
|
shutil.rmtree(out_path)
|
|
|
|
|
2011-10-13 11:33:24 +00:00
|
|
|
def test_list_type_explorer_names(self):
|
2012-02-12 00:18:14 +00:00
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__test_type')
|
2011-10-13 11:33:24 +00:00
|
|
|
expected = cdist_type.explorers
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.explorer.list_type_explorer_names(cdist_type),
|
|
|
|
expected)
|
2011-10-13 11:33:24 +00:00
|
|
|
|
2011-10-12 23:25:31 +00:00
|
|
|
def test_transfer_type_explorers(self):
|
2013-09-04 21:33:52 +00:00
|
|
|
"""Test if transferring type explorers works"""
|
2012-02-12 00:18:14 +00:00
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__test_type')
|
2011-10-12 23:25:31 +00:00
|
|
|
self.explorer.transfer_type_explorers(cdist_type)
|
2011-10-13 11:18:34 +00:00
|
|
|
source = os.path.join(self.local.type_path, cdist_type.explorer_path)
|
2016-07-05 18:44:24 +00:00
|
|
|
destination = os.path.join(self.remote.type_path,
|
|
|
|
cdist_type.explorer_path)
|
2011-10-13 11:18:34 +00:00
|
|
|
self.assertEqual(os.listdir(source), os.listdir(destination))
|
2011-10-12 23:25:31 +00:00
|
|
|
|
2011-10-14 21:42:16 +00:00
|
|
|
def test_transfer_type_explorers_only_once(self):
|
2012-02-12 00:18:14 +00:00
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__test_type')
|
2011-10-14 21:42:16 +00:00
|
|
|
# first transfer
|
|
|
|
self.explorer.transfer_type_explorers(cdist_type)
|
|
|
|
source = os.path.join(self.local.type_path, cdist_type.explorer_path)
|
2016-07-05 18:44:24 +00:00
|
|
|
destination = os.path.join(self.remote.type_path,
|
|
|
|
cdist_type.explorer_path)
|
2011-10-14 21:42:16 +00:00
|
|
|
self.assertEqual(os.listdir(source), os.listdir(destination))
|
|
|
|
# nuke destination folder content, but recreate directory
|
|
|
|
shutil.rmtree(destination)
|
|
|
|
os.makedirs(destination)
|
|
|
|
# second transfer, should not happen
|
|
|
|
self.explorer.transfer_type_explorers(cdist_type)
|
|
|
|
self.assertFalse(os.listdir(destination))
|
|
|
|
|
2011-10-13 11:14:46 +00:00
|
|
|
def test_transfer_object_parameters(self):
|
2012-02-12 00:18:14 +00:00
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__test_type')
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
'whatever')
|
2011-10-13 22:35:23 +00:00
|
|
|
cdist_object.create()
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object.parameters = {
|
|
|
|
'first': 'first value',
|
|
|
|
'second': 'second value'
|
|
|
|
}
|
2011-10-13 11:14:46 +00:00
|
|
|
self.explorer.transfer_object_parameters(cdist_object)
|
2016-07-05 18:44:24 +00:00
|
|
|
source = os.path.join(self.local.object_path,
|
|
|
|
cdist_object.parameter_path)
|
|
|
|
destination = os.path.join(self.remote.object_path,
|
|
|
|
cdist_object.parameter_path)
|
|
|
|
self.assertEqual(sorted(os.listdir(source)),
|
|
|
|
sorted(os.listdir(destination)))
|
2011-10-13 11:14:46 +00:00
|
|
|
|
2011-10-12 23:25:31 +00:00
|
|
|
def test_run_type_explorer(self):
|
2012-02-12 00:18:14 +00:00
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__test_type')
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
'whatever')
|
2011-10-12 23:25:31 +00:00
|
|
|
self.explorer.transfer_type_explorers(cdist_type)
|
2011-10-13 11:22:52 +00:00
|
|
|
output = self.explorer.run_type_explorer('world', cdist_object)
|
|
|
|
self.assertEqual(output, 'hello\n')
|
2011-10-20 09:04:41 +00:00
|
|
|
|
|
|
|
def test_run_type_explorers(self):
|
2012-02-12 00:18:14 +00:00
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__test_type')
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
'whatever')
|
2011-10-20 09:04:41 +00:00
|
|
|
cdist_object.create()
|
|
|
|
self.explorer.run_type_explorers(cdist_object)
|
|
|
|
self.assertEqual(cdist_object.explorers, {'world': 'hello'})
|
2016-08-11 21:54:31 +00:00
|
|
|
|
|
|
|
def test_jobs_parameter(self):
|
|
|
|
self.assertIsNone(self.explorer.jobs)
|
|
|
|
expl = explorer.Explorer(
|
|
|
|
self.target_host,
|
|
|
|
self.local,
|
|
|
|
self.remote,
|
|
|
|
jobs=8)
|
|
|
|
self.assertEqual(expl.jobs, 8)
|
|
|
|
|
2016-08-12 19:14:56 +00:00
|
|
|
def test_transfer_global_explorers_parallel(self):
|
|
|
|
expl = explorer.Explorer(
|
|
|
|
self.target_host,
|
|
|
|
self.local,
|
|
|
|
self.remote,
|
|
|
|
jobs=multiprocessing.cpu_count())
|
|
|
|
self.assertIsNotNone(expl.jobs)
|
|
|
|
|
|
|
|
expl.transfer_global_explorers()
|
|
|
|
source = self.local.global_explorer_path
|
|
|
|
destination = self.remote.global_explorer_path
|
|
|
|
self.assertEqual(sorted(os.listdir(source)),
|
|
|
|
sorted(os.listdir(destination)))
|
|
|
|
|
2016-08-11 21:54:31 +00:00
|
|
|
def test_run_parallel_jobs(self):
|
|
|
|
expl = explorer.Explorer(
|
|
|
|
self.target_host,
|
|
|
|
self.local,
|
|
|
|
self.remote,
|
|
|
|
jobs=multiprocessing.cpu_count())
|
|
|
|
self.assertIsNotNone(expl.jobs)
|
|
|
|
out_path = self.mkdtemp()
|
|
|
|
|
|
|
|
expl.run_global_explorers(out_path)
|
|
|
|
names = sorted(expl.list_global_explorer_names())
|
|
|
|
output = sorted(os.listdir(out_path))
|
|
|
|
|
|
|
|
self.assertEqual(names, output)
|
|
|
|
shutil.rmtree(out_path)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
unittest.main()
|