2012-12-19 14:33:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2018-01-09 08:31:40 +00:00
|
|
|
# 2010-2017 Steven Armstrong (steven-cdist at armstrong.cc)
|
2015-03-05 17:32:47 +00:00
|
|
|
# 2012-2015 Nico Schottelius (nico-cdist at schottelius.org)
|
2014-02-11 20:17:19 +00:00
|
|
|
# 2014 Daniel Heule (hda at sfs.biz)
|
2012-12-19 14:33:50 +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
|
|
|
|
|
|
|
|
from cdist import test
|
|
|
|
from cdist import core
|
|
|
|
|
|
|
|
import cdist
|
2012-12-19 20:10:18 +00:00
|
|
|
import cdist.config
|
2013-06-21 20:39:20 +00:00
|
|
|
import cdist.core.cdist_type
|
|
|
|
import cdist.core.cdist_object
|
2012-12-19 14:33:50 +00:00
|
|
|
|
|
|
|
import os.path as op
|
|
|
|
my_dir = op.abspath(op.dirname(__file__))
|
|
|
|
fixtures = op.join(my_dir, 'fixtures')
|
|
|
|
type_base_path = op.join(fixtures, 'type')
|
|
|
|
add_conf_dir = op.join(fixtures, 'conf')
|
|
|
|
|
2015-03-05 17:51:25 +00:00
|
|
|
expected_object_names = sorted([
|
|
|
|
'__first/man',
|
|
|
|
'__second/on-the',
|
|
|
|
'__third/moon'])
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2020-06-20 19:16:23 +00:00
|
|
|
class CdistObjectErrorContext:
|
2018-01-09 08:31:40 +00:00
|
|
|
def __init__(self, original_error):
|
|
|
|
self.original_error = original_error
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, tb):
|
|
|
|
if exc_type is not None:
|
|
|
|
if exc_value.original_error:
|
|
|
|
raise exc_value.original_error
|
|
|
|
|
|
|
|
|
2013-08-29 19:56:53 +00:00
|
|
|
class ConfigRunTestCase(test.CdistTestCase):
|
2012-12-19 14:33:50 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
2012-12-19 20:10:18 +00:00
|
|
|
# Change env for context
|
|
|
|
self.orig_environ = os.environ
|
|
|
|
os.environ = os.environ.copy()
|
|
|
|
self.temp_dir = self.mkdtemp()
|
|
|
|
|
2013-08-19 11:35:03 +00:00
|
|
|
self.local_dir = os.path.join(self.temp_dir, "local")
|
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)
|
|
|
|
os.makedirs(self.host_base_path)
|
2013-08-19 09:33:44 +00:00
|
|
|
self.local = cdist.exec.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=self.host_base_path,
|
|
|
|
host_dir_name=self.hostdir)
|
2012-12-19 20:10:18 +00:00
|
|
|
|
2015-03-05 17:51:25 +00:00
|
|
|
# Setup test objects
|
|
|
|
self.object_base_path = op.join(self.temp_dir, 'object')
|
|
|
|
|
|
|
|
self.objects = []
|
|
|
|
for cdist_object_name in expected_object_names:
|
|
|
|
cdist_type, cdist_object_id = cdist_object_name.split("/", 1)
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(core.CdistType(type_base_path,
|
|
|
|
cdist_type),
|
|
|
|
self.object_base_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
cdist_object_id)
|
2015-03-05 17:51:25 +00:00
|
|
|
cdist_object.create()
|
|
|
|
self.objects.append(cdist_object)
|
|
|
|
|
|
|
|
self.object_index = dict((o.name, o) for o in self.objects)
|
|
|
|
self.object_names = [o.name for o in self.objects]
|
|
|
|
|
2013-08-19 09:33:44 +00:00
|
|
|
self.remote_dir = os.path.join(self.temp_dir, "remote")
|
|
|
|
os.mkdir(self.remote_dir)
|
|
|
|
self.remote = cdist.exec.remote.Remote(
|
2012-12-19 14:33:50 +00:00
|
|
|
target_host=self.target_host,
|
|
|
|
remote_copy=self.remote_copy,
|
|
|
|
remote_exec=self.remote_exec,
|
2018-01-09 08:31:40 +00:00
|
|
|
base_path=self.remote_dir,
|
|
|
|
stdout_base_path=self.local.stdout_base_path,
|
|
|
|
stderr_base_path=self.local.stderr_base_path)
|
2012-12-19 14:33:50 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
self.local.object_path = self.object_base_path
|
|
|
|
self.local.type_path = type_base_path
|
2012-12-19 20:10:18 +00:00
|
|
|
|
2013-08-19 09:33:44 +00:00
|
|
|
self.config = cdist.config.Config(self.local, self.remote)
|
2012-12-19 14:33:50 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
for o in self.objects:
|
|
|
|
o.requirements = []
|
2012-12-19 20:10:18 +00:00
|
|
|
o.state = ""
|
|
|
|
|
|
|
|
os.environ = self.orig_environ
|
|
|
|
shutil.rmtree(self.temp_dir)
|
2012-12-19 14:33:50 +00:00
|
|
|
|
2018-01-09 08:31:40 +00:00
|
|
|
def assertRaisesCdistObjectError(self, original_error, callable_obj):
|
|
|
|
"""
|
|
|
|
Test if a raised CdistObjectError was caused by the given
|
|
|
|
original_error.
|
|
|
|
"""
|
|
|
|
with self.assertRaises(original_error):
|
|
|
|
try:
|
|
|
|
callable_obj()
|
|
|
|
except cdist.CdistObjectError as e:
|
|
|
|
if e.original_error:
|
|
|
|
raise e.original_error
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2012-12-19 14:33:50 +00:00
|
|
|
def test_dependency_resolution(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
first = self.object_index['__first/man']
|
|
|
|
second = self.object_index['__second/on-the']
|
|
|
|
third = self.object_index['__third/moon']
|
2012-12-19 14:33:50 +00:00
|
|
|
|
|
|
|
first.requirements = [second.name]
|
|
|
|
second.requirements = [third.name]
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
# First run:
|
2012-12-19 14:33:50 +00:00
|
|
|
# solves first and maybe second (depending on the order in the set)
|
2013-06-21 19:48:15 +00:00
|
|
|
self.config.iterate_once()
|
2012-12-19 20:10:18 +00:00
|
|
|
self.assertTrue(third.state == third.STATE_DONE)
|
|
|
|
|
2013-06-21 19:48:15 +00:00
|
|
|
self.config.iterate_once()
|
2012-12-19 20:10:18 +00:00
|
|
|
self.assertTrue(second.state == second.STATE_DONE)
|
|
|
|
|
|
|
|
try:
|
2013-06-21 19:48:15 +00:00
|
|
|
self.config.iterate_once()
|
2012-12-19 20:10:18 +00:00
|
|
|
except cdist.Error:
|
2016-07-05 18:44:24 +00:00
|
|
|
# Allow failing, because the third run may or may not be
|
|
|
|
# unecessary already,
|
2012-12-19 20:10:18 +00:00
|
|
|
# depending on the order of the objects
|
|
|
|
pass
|
|
|
|
self.assertTrue(first.state == first.STATE_DONE)
|
|
|
|
|
|
|
|
def test_unresolvable_requirements(self):
|
|
|
|
"""Ensure an exception is thrown for unresolvable depedencies"""
|
|
|
|
|
|
|
|
# Create to objects depending on each other - no solution possible
|
2016-07-05 18:44:24 +00:00
|
|
|
first = self.object_index['__first/man']
|
|
|
|
second = self.object_index['__second/on-the']
|
2012-12-19 20:10:18 +00:00
|
|
|
|
|
|
|
first.requirements = [second.name]
|
|
|
|
second.requirements = [first.name]
|
|
|
|
|
2018-01-09 08:31:40 +00:00
|
|
|
self.assertRaisesCdistObjectError(
|
|
|
|
cdist.UnresolvableRequirementsError,
|
|
|
|
self.config.iterate_until_finished)
|
2013-06-21 19:50:21 +00:00
|
|
|
|
|
|
|
def test_missing_requirements(self):
|
2013-06-21 20:39:20 +00:00
|
|
|
"""Throw an error if requiring something non-existing"""
|
2013-06-21 19:50:21 +00:00
|
|
|
first = self.object_index['__first/man']
|
2013-06-21 20:39:20 +00:00
|
|
|
first.requirements = ['__first/not/exist']
|
2018-01-09 08:31:40 +00:00
|
|
|
self.assertRaisesCdistObjectError(
|
|
|
|
cdist.UnresolvableRequirementsError,
|
|
|
|
self.config.iterate_until_finished)
|
2013-06-21 20:39:20 +00:00
|
|
|
|
|
|
|
def test_requirement_broken_type(self):
|
|
|
|
"""Unknown type should be detected in the resolving process"""
|
|
|
|
first = self.object_index['__first/man']
|
|
|
|
first.requirements = ['__nosuchtype/not/exist']
|
2018-01-09 08:31:40 +00:00
|
|
|
self.assertRaisesCdistObjectError(
|
|
|
|
cdist.core.cdist_type.InvalidTypeError,
|
|
|
|
self.config.iterate_until_finished)
|
2013-06-21 20:39:20 +00:00
|
|
|
|
|
|
|
def test_requirement_singleton_where_no_singleton(self):
|
|
|
|
"""Missing object id should be detected in the resolving process"""
|
|
|
|
first = self.object_index['__first/man']
|
|
|
|
first.requirements = ['__first']
|
2018-01-09 08:31:40 +00:00
|
|
|
self.assertRaisesCdistObjectError(
|
|
|
|
cdist.core.cdist_object.MissingObjectIdError,
|
|
|
|
self.config.iterate_until_finished)
|
2013-06-21 20:39:20 +00:00
|
|
|
|
2014-02-11 20:17:19 +00:00
|
|
|
def test_dryrun(self):
|
|
|
|
"""Test if the dryrun option is working like expected"""
|
|
|
|
drylocal = cdist.exec.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=self.host_base_path,
|
|
|
|
host_dir_name=self.hostdir,
|
2016-07-05 18:44:24 +00:00
|
|
|
# exec_path can not derivated from sys.argv in case of unittest
|
|
|
|
exec_path=os.path.abspath(os.path.join(
|
2020-11-11 14:04:51 +00:00
|
|
|
my_dir, '../../../bin/cdist')),
|
2016-07-05 18:44:24 +00:00
|
|
|
initial_manifest=os.path.join(fixtures,
|
|
|
|
'manifest/dryrun_manifest'),
|
|
|
|
add_conf_dirs=[fixtures])
|
2014-02-11 20:17:19 +00:00
|
|
|
|
|
|
|
dryrun = cdist.config.Config(drylocal, self.remote, dry_run=True)
|
|
|
|
dryrun.run()
|
|
|
|
# if we are here, dryrun works like expected
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2019-05-01 12:18:39 +00:00
|
|
|
def test_deps_resolver(self):
|
2016-10-08 09:40:32 +00:00
|
|
|
"""Test to show dependency resolver warning message."""
|
|
|
|
local = cdist.exec.local.Local(
|
|
|
|
target_host=self.target_host,
|
2017-07-20 20:04:44 +00:00
|
|
|
target_host_tags=self.target_host_tags,
|
2016-10-08 09:40:32 +00:00
|
|
|
base_root_path=self.host_base_path,
|
|
|
|
host_dir_name=self.hostdir,
|
|
|
|
exec_path=os.path.abspath(os.path.join(
|
2020-11-11 14:04:51 +00:00
|
|
|
my_dir, '../../../bin/cdist')),
|
2016-10-08 09:40:32 +00:00
|
|
|
initial_manifest=os.path.join(
|
|
|
|
fixtures, 'manifest/init-deps-resolver'),
|
|
|
|
add_conf_dirs=[fixtures])
|
|
|
|
|
|
|
|
# dry_run is ok for dependency testing
|
|
|
|
config = cdist.config.Config(local, self.remote, dry_run=True)
|
|
|
|
config.run()
|
|
|
|
|
2019-05-01 12:18:39 +00:00
|
|
|
def test_graph_check_cycle_empty(self):
|
|
|
|
graph = {}
|
|
|
|
has_cycle, path = cdist.config.graph_check_cycle(graph)
|
|
|
|
self.assertFalse(has_cycle)
|
|
|
|
|
|
|
|
def test_graph_check_cycle_1(self):
|
|
|
|
#
|
|
|
|
# a -> b -> c
|
|
|
|
# |
|
|
|
|
# +--> d -> e
|
|
|
|
graph = {
|
|
|
|
'a': ['b', ],
|
|
|
|
'b': ['c', 'd', ],
|
|
|
|
'd': ['e', ],
|
|
|
|
}
|
|
|
|
has_cycle, path = cdist.config.graph_check_cycle(graph)
|
|
|
|
self.assertFalse(has_cycle)
|
|
|
|
|
|
|
|
def test_graph_check_cycle_2(self):
|
|
|
|
#
|
|
|
|
# a -> b -> c
|
|
|
|
# /\ |
|
|
|
|
# \ |
|
|
|
|
# +-------+
|
|
|
|
graph = {
|
|
|
|
'a': ['b', ],
|
|
|
|
'b': ['c', ],
|
|
|
|
'c': ['a', ],
|
|
|
|
}
|
|
|
|
has_cycle, path = cdist.config.graph_check_cycle(graph)
|
|
|
|
self.assertTrue(has_cycle)
|
|
|
|
self.assertGreater(path.count(path[-1]), 1)
|
|
|
|
|
|
|
|
def test_graph_check_cycle_3(self):
|
|
|
|
#
|
|
|
|
# a -> b -> c
|
|
|
|
# \ \
|
|
|
|
# \ +--> g
|
|
|
|
# \ /\
|
|
|
|
# \ /|
|
|
|
|
# +-> d -> e |
|
|
|
|
# \ |
|
|
|
|
# + --> f
|
|
|
|
#
|
|
|
|
# h -> i --> j
|
|
|
|
# | /\ |
|
|
|
|
# \/ | \/
|
|
|
|
# n m <- k
|
|
|
|
graph = {
|
|
|
|
'a': ['b', 'd', ],
|
|
|
|
'b': ['c', ],
|
|
|
|
'c': ['g', ],
|
|
|
|
'd': ['e', 'f', ],
|
|
|
|
'e': ['g', ],
|
|
|
|
'f': ['g', ],
|
|
|
|
'h': ['i', 'n', ],
|
|
|
|
'i': ['j', ],
|
|
|
|
'j': ['k', ],
|
|
|
|
'k': ['m', ],
|
|
|
|
'm': ['i', ],
|
|
|
|
}
|
|
|
|
has_cycle, path = cdist.config.graph_check_cycle(graph)
|
|
|
|
self.assertTrue(has_cycle)
|
|
|
|
self.assertGreater(path.count(path[-1]), 1)
|
|
|
|
|
2014-02-11 20:17:19 +00:00
|
|
|
|
2013-06-21 20:39:20 +00:00
|
|
|
# Currently the resolving code will simply detect that this object does
|
|
|
|
# not exist. It should probably check if the type is a singleton as well
|
|
|
|
# - but maybe only in the emulator - to be discussed.
|
|
|
|
#
|
|
|
|
# def test_requirement_no_singleton_where_singleton(self):
|
|
|
|
# """Missing object id should be detected in the resolving process"""
|
|
|
|
# first = self.object_index['__first/man']
|
|
|
|
# first.requirements = ['__singleton_test/foo']
|
|
|
|
# with self.assertRaises(cdist.core.?????):
|
|
|
|
# self.config.iterate_until_finished()
|
2016-07-23 14:13:59 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
unittest.main()
|