2011-10-18 11:19:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
2015-03-05 18:20:06 +00:00
|
|
|
# 2012-2015 Nico Schottelius (nico-cdist at schottelius.org)
|
2014-02-07 13:24:12 +00:00
|
|
|
# 2014 Daniel Heule (hda at sfs.biz)
|
2011-10-18 11:19:37 +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 09:49:51 +00:00
|
|
|
import io
|
2011-10-18 11:19:37 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
2012-06-04 12:11:34 +00:00
|
|
|
import random
|
2017-09-07 14:36:23 +00:00
|
|
|
import logging
|
2011-10-18 11:19:37 +00:00
|
|
|
|
2011-11-02 22:57:13 +00:00
|
|
|
import cdist
|
2011-10-18 11:19:37 +00:00
|
|
|
from cdist import test
|
|
|
|
from cdist.exec import local
|
|
|
|
from cdist import emulator
|
|
|
|
from cdist import core
|
|
|
|
|
2012-11-06 20:32:54 +00:00
|
|
|
import os.path as op
|
|
|
|
my_dir = op.abspath(op.dirname(__file__))
|
|
|
|
fixtures = op.join(my_dir, 'fixtures')
|
|
|
|
conf_dir = op.join(fixtures, 'conf')
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2011-10-19 15:59:21 +00:00
|
|
|
class EmulatorTestCase(test.CdistTestCase):
|
2011-10-18 11:19:37 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.temp_dir = self.mkdtemp()
|
|
|
|
handle, self.script = self.mkstemp(dir=self.temp_dir)
|
2011-10-21 13:26:27 +00:00
|
|
|
os.close(handle)
|
2013-08-19 10:03:25 +00:00
|
|
|
base_path = self.temp_dir
|
2016-08-10 21:56:56 +00:00
|
|
|
hostdir = cdist.str_hash(self.target_host[0])
|
2016-07-23 14:13:59 +00:00
|
|
|
host_base_path = os.path.join(base_path, hostdir)
|
2012-11-06 21:39:25 +00:00
|
|
|
|
2012-11-01 14:20:10 +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=host_base_path,
|
|
|
|
host_dir_name=hostdir,
|
2012-11-06 20:32:54 +00:00
|
|
|
exec_path=test.cdist_exec_path,
|
|
|
|
add_conf_dirs=[conf_dir])
|
2012-11-01 12:35:52 +00:00
|
|
|
self.local.create_files_dirs()
|
2012-11-06 21:39:25 +00:00
|
|
|
|
|
|
|
self.manifest = core.Manifest(self.target_host, self.local)
|
|
|
|
self.env = self.manifest.env_initial_manifest(self.script)
|
2015-03-05 17:32:47 +00:00
|
|
|
self.env['__cdist_object_marker'] = self.local.object_marker_name
|
2017-09-09 19:17:29 +00:00
|
|
|
if '__cdist_log_level' in self.env:
|
|
|
|
del self.env['__cdist_log_level']
|
2011-10-18 11:19:37 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.temp_dir)
|
|
|
|
|
2015-03-05 17:32:47 +00:00
|
|
|
# def test_missing_object_marker_variable(self):
|
|
|
|
# del self.env['__cdist_object_marker']
|
|
|
|
# self.assertRaises(KeyError, emulator.Emulator, argv, env=self.env)
|
|
|
|
|
2011-10-18 11:19:37 +00:00
|
|
|
def test_nonexistent_type_exec(self):
|
|
|
|
argv = ['__does-not-exist']
|
2017-11-02 12:57:36 +00:00
|
|
|
self.assertRaises(core.cdist_type.InvalidTypeError, emulator.Emulator,
|
2016-07-05 18:44:24 +00:00
|
|
|
argv, env=self.env)
|
2011-10-18 11:19:37 +00:00
|
|
|
|
|
|
|
def test_nonexistent_type_requirement(self):
|
|
|
|
argv = ['__file', '/tmp/foobar']
|
2012-11-07 10:05:40 +00:00
|
|
|
self.env['require'] = '__does-not-exist/some-id'
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
2017-11-02 12:57:36 +00:00
|
|
|
self.assertRaises(core.cdist_type.InvalidTypeError, emu.run)
|
2011-10-18 12:48:06 +00:00
|
|
|
|
|
|
|
def test_illegal_object_id_requirement(self):
|
|
|
|
argv = ['__file', '/tmp/foobar']
|
2021-03-30 05:56:38 +00:00
|
|
|
self.env['require'] = "__file/bad/id/with/{}/inside".format(
|
|
|
|
self.local.object_marker_name)
|
2012-11-07 10:05:40 +00:00
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
2011-10-18 12:48:06 +00:00
|
|
|
self.assertRaises(core.IllegalObjectIdError, emu.run)
|
2011-11-02 22:57:13 +00:00
|
|
|
|
2011-11-03 14:44:12 +00:00
|
|
|
def test_missing_object_id_requirement(self):
|
|
|
|
argv = ['__file', '/tmp/foobar']
|
2012-11-07 10:05:40 +00:00
|
|
|
self.env['require'] = '__file'
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
2013-05-27 14:36:20 +00:00
|
|
|
self.assertRaises(core.cdist_object.MissingObjectIdError, emu.run)
|
2011-11-03 14:44:12 +00:00
|
|
|
|
2013-07-10 15:21:38 +00:00
|
|
|
def test_no_singleton_no_requirement(self):
|
|
|
|
argv = ['__file', '/tmp/foobar']
|
|
|
|
self.env['require'] = '__test_singleton'
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
# If reached here, everything is fine
|
|
|
|
|
2011-11-03 14:44:12 +00:00
|
|
|
def test_singleton_object_requirement(self):
|
|
|
|
argv = ['__file', '/tmp/foobar']
|
2012-11-07 10:05:40 +00:00
|
|
|
self.env['require'] = '__issue'
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
2011-11-03 14:44:12 +00:00
|
|
|
emu.run()
|
|
|
|
# if we get here all is fine
|
2011-11-15 09:47:16 +00:00
|
|
|
|
2012-01-19 06:51:02 +00:00
|
|
|
def test_requirement_pattern(self):
|
|
|
|
argv = ['__file', '/tmp/foobar']
|
2012-11-07 10:05:40 +00:00
|
|
|
self.env['require'] = '__file/etc/*'
|
Redefine/reimplement CDIST_ORDER_DEPENDENCY
CDIST_ORDER_DEPENDENCY now defines type order dependency context.
cdist (emulator) maintains global state variables, as files,
order_dep_state and typeorder_dep, and per object state variable,
as file, typeorder_dep.
If order_dep_state exists then this defines that order dependency is
turned on.
If order_dep_state does not exist then order dependency is turned off.
If order dependency is on then objects created after it is turned on are
recorded into:
* global typeorder_dep, in case of init manifest
* object's typeorder_dep, in case of type's manifest.
If order dependency is on then requirement is injected, where object
created before current, is read from:
* global typeorder_dep, in case of init manifest
* object's typeorder_dep, in case of type's manifest.
Every time order dependency is turned off, typeorder_dep files are
removed, which means that type order list is cleared, context is
cleaned.
In the end cdist cleans after itself, i.e. mentioned files are removed.
When running type manifest is finished typeorder_dep file is removed.
When running config finishes global typeorder_dep and order_dep_state
files are removed.
Global type order recording is untouched.
Furthermore, for completeness, type order is now recorded for each object
too.
2019-11-22 23:25:51 +00:00
|
|
|
emulator.Emulator(argv, env=self.env)
|
2012-01-19 06:51:02 +00:00
|
|
|
# if we get here all is fine
|
|
|
|
|
2017-09-07 14:36:23 +00:00
|
|
|
def test_loglevel(self):
|
|
|
|
argv = ['__file', '/tmp/foobar']
|
|
|
|
self.env['require'] = '__file/etc/*'
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu_loglevel = emu.log.getEffectiveLevel()
|
|
|
|
self.assertEqual(emu_loglevel, logging.WARNING)
|
2017-09-11 07:06:47 +00:00
|
|
|
self.env['__cdist_log_level'] = str(logging.DEBUG)
|
2017-09-07 14:36:23 +00:00
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu_loglevel = emu.log.getEffectiveLevel()
|
|
|
|
self.assertEqual(emu_loglevel, logging.DEBUG)
|
2017-09-09 19:17:29 +00:00
|
|
|
del self.env['__cdist_log_level']
|
2017-09-07 14:36:23 +00:00
|
|
|
|
|
|
|
def test_invalid_loglevel_value(self):
|
|
|
|
argv = ['__file', '/tmp/foobar']
|
|
|
|
self.env['require'] = '__file/etc/*'
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu_loglevel = emu.log.getEffectiveLevel()
|
|
|
|
self.assertEqual(emu_loglevel, logging.WARNING)
|
|
|
|
# lowercase is invalid
|
2017-09-09 19:17:29 +00:00
|
|
|
self.env['__cdist_log_level'] = 'debug'
|
2017-09-07 14:36:23 +00:00
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu_loglevel = emu.log.getEffectiveLevel()
|
|
|
|
self.assertEqual(emu_loglevel, logging.WARNING)
|
2017-09-09 19:17:29 +00:00
|
|
|
del self.env['__cdist_log_level']
|
2017-09-07 14:36:23 +00:00
|
|
|
|
2014-02-11 14:20:26 +00:00
|
|
|
def test_requirement_via_order_dependency(self):
|
|
|
|
self.env['CDIST_ORDER_DEPENDENCY'] = 'on'
|
|
|
|
argv = ['__planet', 'erde']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
argv = ['__planet', 'mars']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
# In real world, this is not shared over instances
|
|
|
|
del self.env['require']
|
|
|
|
argv = ['__file', '/tmp/cdisttest']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
# now load the objects and verify the require parameter of the objects
|
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__planet')
|
2016-07-05 18:44:24 +00:00
|
|
|
erde_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name, 'erde')
|
|
|
|
mars_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name, 'mars')
|
2014-02-11 14:20:26 +00:00
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__file')
|
2016-07-05 18:44:24 +00:00
|
|
|
file_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
'/tmp/cdisttest')
|
2014-02-11 14:20:26 +00:00
|
|
|
# now test the recorded requirements
|
|
|
|
self.assertTrue(len(erde_object.requirements) == 0)
|
|
|
|
self.assertEqual(list(mars_object.requirements), ['__planet/erde'])
|
|
|
|
self.assertEqual(list(file_object.requirements), ['__planet/mars'])
|
|
|
|
# if we get here all is fine
|
|
|
|
|
Redefine/reimplement CDIST_ORDER_DEPENDENCY
CDIST_ORDER_DEPENDENCY now defines type order dependency context.
cdist (emulator) maintains global state variables, as files,
order_dep_state and typeorder_dep, and per object state variable,
as file, typeorder_dep.
If order_dep_state exists then this defines that order dependency is
turned on.
If order_dep_state does not exist then order dependency is turned off.
If order dependency is on then objects created after it is turned on are
recorded into:
* global typeorder_dep, in case of init manifest
* object's typeorder_dep, in case of type's manifest.
If order dependency is on then requirement is injected, where object
created before current, is read from:
* global typeorder_dep, in case of init manifest
* object's typeorder_dep, in case of type's manifest.
Every time order dependency is turned off, typeorder_dep files are
removed, which means that type order list is cleared, context is
cleaned.
In the end cdist cleans after itself, i.e. mentioned files are removed.
When running type manifest is finished typeorder_dep file is removed.
When running config finishes global typeorder_dep and order_dep_state
files are removed.
Global type order recording is untouched.
Furthermore, for completeness, type order is now recorded for each object
too.
2019-11-22 23:25:51 +00:00
|
|
|
def test_order_dependency_context(self):
|
|
|
|
test_seq = ('A', True, 'B', 'C', 'D', False, 'E', 'F', True, 'G',
|
|
|
|
'H', False, 'I', )
|
|
|
|
expected_requirements = {
|
|
|
|
'C': set(('__planet/B', )),
|
|
|
|
'D': set(('__planet/C', )),
|
|
|
|
'H': set(('__planet/G', )),
|
|
|
|
}
|
|
|
|
# Ensure env var is not in env
|
|
|
|
if 'CDIST_ORDER_DEPENDENCY' in self.env:
|
|
|
|
del self.env['CDIST_ORDER_DEPENDENCY']
|
|
|
|
|
|
|
|
for x in test_seq:
|
|
|
|
if isinstance(x, str):
|
|
|
|
# Clear because of order dep injection
|
|
|
|
# In real world, this is not shared over instances
|
|
|
|
if 'require' in self.env:
|
|
|
|
del self.env['require']
|
|
|
|
argv = ['__planet', x]
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
elif isinstance(x, bool):
|
|
|
|
if x:
|
|
|
|
self.env['CDIST_ORDER_DEPENDENCY'] = 'on'
|
|
|
|
elif 'CDIST_ORDER_DEPENDENCY' in self.env:
|
|
|
|
del self.env['CDIST_ORDER_DEPENDENCY']
|
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__planet')
|
|
|
|
for x in test_seq:
|
|
|
|
if isinstance(x, str):
|
|
|
|
obj = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name, x)
|
|
|
|
reqs = set(obj.requirements)
|
|
|
|
if x in expected_requirements:
|
|
|
|
self.assertEqual(reqs, expected_requirements[x])
|
|
|
|
else:
|
|
|
|
self.assertTrue(len(reqs) == 0)
|
|
|
|
# if we get here all is fine
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2016-06-10 05:50:07 +00:00
|
|
|
class EmulatorConflictingRequirementsTestCase(test.CdistTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.temp_dir = self.mkdtemp()
|
|
|
|
handle, self.script = self.mkstemp(dir=self.temp_dir)
|
|
|
|
os.close(handle)
|
|
|
|
base_path = self.temp_dir
|
2016-08-10 21:56:56 +00:00
|
|
|
hostdir = cdist.str_hash(self.target_host[0])
|
2016-07-23 14:13:59 +00:00
|
|
|
host_base_path = os.path.join(base_path, hostdir)
|
2016-06-10 05:50:07 +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=host_base_path,
|
|
|
|
host_dir_name=hostdir,
|
2016-06-10 05:50:07 +00:00
|
|
|
exec_path=test.cdist_exec_path,
|
|
|
|
add_conf_dirs=[conf_dir])
|
|
|
|
self.local.create_files_dirs()
|
|
|
|
|
|
|
|
self.manifest = core.Manifest(self.target_host, self.local)
|
|
|
|
self.env = self.manifest.env_initial_manifest(self.script)
|
|
|
|
self.env['__cdist_object_marker'] = self.local.object_marker_name
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.temp_dir)
|
|
|
|
|
2016-06-30 18:31:01 +00:00
|
|
|
def test_object_different_requirements_req_none(self):
|
2016-06-10 05:50:07 +00:00
|
|
|
argv = ['__directory', 'spam']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
argv = ['__file', 'eggs']
|
|
|
|
self.env['require'] = '__directory/spam'
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
argv = ['__file', 'eggs']
|
|
|
|
if 'require' in self.env:
|
|
|
|
del self.env['require']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
2016-06-30 18:31:01 +00:00
|
|
|
emu.run()
|
|
|
|
|
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__file')
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name, 'eggs')
|
2016-06-30 18:31:01 +00:00
|
|
|
reqs = set(('__directory/spam',))
|
|
|
|
self.assertEqual(reqs, set(cdist_object.requirements))
|
2016-06-10 05:50:07 +00:00
|
|
|
|
2016-06-30 18:31:01 +00:00
|
|
|
def test_object_different_requirements_none_req(self):
|
2016-06-10 05:50:07 +00:00
|
|
|
argv = ['__directory', 'spam']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
argv = ['__file', 'eggs']
|
|
|
|
if 'require' in self.env:
|
|
|
|
del self.env['require']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
argv = ['__file', 'eggs']
|
|
|
|
self.env['require'] = '__directory/spam'
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
2016-06-30 18:31:01 +00:00
|
|
|
emu.run()
|
|
|
|
|
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__file')
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name, 'eggs')
|
2016-06-30 18:31:01 +00:00
|
|
|
reqs = set(('__directory/spam',))
|
|
|
|
self.assertEqual(reqs, set(cdist_object.requirements))
|
|
|
|
|
|
|
|
def test_object_different_requirements(self):
|
|
|
|
argv = ['__directory', 'spam']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
argv = ['__directory', 'spameggs']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
|
|
|
|
argv = ['__file', 'eggs']
|
|
|
|
if 'require' in self.env:
|
|
|
|
del self.env['require']
|
|
|
|
self.env['require'] = '__directory/spam'
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
|
|
|
|
argv = ['__file', 'eggs']
|
|
|
|
self.env['require'] = '__directory/spameggs'
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
|
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__file')
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name, 'eggs')
|
2016-06-30 18:31:01 +00:00
|
|
|
reqs = set(('__directory/spam', '__directory/spameggs',))
|
|
|
|
self.assertEqual(reqs, set(cdist_object.requirements))
|
2016-06-10 05:50:07 +00:00
|
|
|
|
2011-11-02 22:57:13 +00:00
|
|
|
|
|
|
|
class AutoRequireEmulatorTestCase(test.CdistTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.temp_dir = self.mkdtemp()
|
2013-08-19 10:03:25 +00:00
|
|
|
base_path = os.path.join(self.temp_dir, "out")
|
2016-08-10 21:56:56 +00:00
|
|
|
hostdir = cdist.str_hash(self.target_host[0])
|
2016-07-23 14:13:59 +00:00
|
|
|
host_base_path = os.path.join(base_path, hostdir)
|
2012-11-07 10:05:40 +00:00
|
|
|
|
2012-11-01 14:20:10 +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=host_base_path,
|
|
|
|
host_dir_name=hostdir,
|
2012-11-07 10:05:40 +00:00
|
|
|
exec_path=test.cdist_exec_path,
|
|
|
|
add_conf_dirs=[conf_dir])
|
2012-11-01 12:35:52 +00:00
|
|
|
self.local.create_files_dirs()
|
2011-11-02 22:57:13 +00:00
|
|
|
self.manifest = core.Manifest(self.target_host, self.local)
|
|
|
|
|
|
|
|
def tearDown(self):
|
2012-06-04 12:11:34 +00:00
|
|
|
shutil.rmtree(self.temp_dir)
|
2011-11-02 22:57:13 +00:00
|
|
|
|
|
|
|
def test_autorequire(self):
|
|
|
|
initial_manifest = os.path.join(self.local.manifest_path, "init")
|
|
|
|
self.manifest.run_initial_manifest(initial_manifest)
|
2012-02-12 00:18:14 +00:00
|
|
|
cdist_type = core.CdistType(self.local.type_path, '__saturn')
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name, '')
|
2011-11-02 22:57:13 +00:00
|
|
|
self.manifest.run_type_manifest(cdist_object)
|
|
|
|
expected = ['__planet/Saturn', '__moon/Prometheus']
|
2012-05-03 08:16:08 +00:00
|
|
|
self.assertEqual(sorted(cdist_object.autorequire), sorted(expected))
|
2011-11-17 12:08:30 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2014-02-06 23:28:02 +00:00
|
|
|
class OverrideTestCase(test.CdistTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.temp_dir = self.mkdtemp()
|
|
|
|
handle, self.script = self.mkstemp(dir=self.temp_dir)
|
|
|
|
os.close(handle)
|
|
|
|
base_path = self.temp_dir
|
2016-08-10 21:56:56 +00:00
|
|
|
hostdir = cdist.str_hash(self.target_host[0])
|
2016-07-23 14:13:59 +00:00
|
|
|
host_base_path = os.path.join(base_path, hostdir)
|
2014-02-06 23:28:02 +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=host_base_path,
|
|
|
|
host_dir_name=hostdir,
|
2014-02-06 23:28:02 +00:00
|
|
|
exec_path=test.cdist_exec_path,
|
|
|
|
add_conf_dirs=[conf_dir])
|
|
|
|
self.local.create_files_dirs()
|
|
|
|
|
|
|
|
self.manifest = core.Manifest(self.target_host, self.local)
|
|
|
|
self.env = self.manifest.env_initial_manifest(self.script)
|
2015-03-05 17:32:47 +00:00
|
|
|
self.env['__cdist_object_marker'] = self.local.object_marker_name
|
2014-02-06 23:28:02 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.temp_dir)
|
|
|
|
|
2014-02-07 13:24:12 +00:00
|
|
|
def test_override_negative(self):
|
2014-02-06 23:28:02 +00:00
|
|
|
argv = ['__file', '/tmp/foobar']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
2014-02-07 12:28:22 +00:00
|
|
|
emu.run()
|
2016-07-05 18:44:24 +00:00
|
|
|
argv = ['__file', '/tmp/foobar', '--mode', '404']
|
2014-02-07 13:24:12 +00:00
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
self.assertRaises(cdist.Error, emu.run)
|
|
|
|
|
|
|
|
def test_override_feature(self):
|
2014-02-07 12:28:22 +00:00
|
|
|
argv = ['__file', '/tmp/foobar']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
2016-07-05 18:44:24 +00:00
|
|
|
argv = ['__file', '/tmp/foobar', '--mode', '404']
|
2014-02-07 13:24:12 +00:00
|
|
|
self.env['CDIST_OVERRIDE'] = 'on'
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
2014-02-06 23:28:02 +00:00
|
|
|
|
2011-11-17 12:08:30 +00:00
|
|
|
|
2012-02-15 13:44:16 +00:00
|
|
|
class ArgumentsTestCase(test.CdistTestCase):
|
2011-11-17 12:03:56 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.temp_dir = self.mkdtemp()
|
2013-08-19 10:03:25 +00:00
|
|
|
base_path = self.temp_dir
|
2016-08-10 21:56:56 +00:00
|
|
|
hostdir = cdist.str_hash(self.target_host[0])
|
2016-07-23 14:13:59 +00:00
|
|
|
host_base_path = os.path.join(base_path, hostdir)
|
2011-11-17 12:03:56 +00:00
|
|
|
handle, self.script = self.mkstemp(dir=self.temp_dir)
|
|
|
|
os.close(handle)
|
2012-11-01 14:20:10 +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=host_base_path,
|
|
|
|
host_dir_name=hostdir,
|
2012-11-07 10:05:40 +00:00
|
|
|
exec_path=test.cdist_exec_path,
|
|
|
|
add_conf_dirs=[conf_dir])
|
2012-11-01 12:35:52 +00:00
|
|
|
self.local.create_files_dirs()
|
2012-11-01 14:20:10 +00:00
|
|
|
|
2012-11-07 10:05:40 +00:00
|
|
|
self.manifest = core.Manifest(self.target_host, self.local)
|
|
|
|
self.env = self.manifest.env_initial_manifest(self.script)
|
2015-03-05 17:32:47 +00:00
|
|
|
self.env['__cdist_object_marker'] = self.local.object_marker_name
|
2011-11-17 12:03:56 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.temp_dir)
|
|
|
|
|
|
|
|
def test_arguments_with_dashes(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
argv = ['__arguments_with_dashes', 'some-id', '--with-dash',
|
|
|
|
'some value']
|
2011-11-17 12:03:56 +00:00
|
|
|
os.environ.update(self.env)
|
|
|
|
emu = emulator.Emulator(argv)
|
|
|
|
emu.run()
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_type = core.CdistType(self.local.type_path,
|
|
|
|
'__arguments_with_dashes')
|
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
'some-id')
|
2011-11-17 12:03:56 +00:00
|
|
|
self.assertTrue('with-dash' in cdist_object.parameters)
|
2012-02-15 13:44:16 +00:00
|
|
|
|
|
|
|
def test_boolean(self):
|
|
|
|
type_name = '__arguments_boolean'
|
|
|
|
object_id = 'some-id'
|
|
|
|
argv = [type_name, object_id, '--boolean1']
|
|
|
|
os.environ.update(self.env)
|
|
|
|
emu = emulator.Emulator(argv)
|
|
|
|
emu.run()
|
|
|
|
|
|
|
|
cdist_type = core.CdistType(self.local.type_path, type_name)
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
object_id)
|
2012-02-15 13:44:16 +00:00
|
|
|
self.assertTrue('boolean1' in cdist_object.parameters)
|
|
|
|
self.assertFalse('boolean2' in cdist_object.parameters)
|
|
|
|
# empty file -> True
|
|
|
|
self.assertTrue(cdist_object.parameters['boolean1'] == '')
|
|
|
|
|
2013-07-10 15:02:19 +00:00
|
|
|
def test_required_arguments(self):
|
|
|
|
"""check whether assigning required parameter works"""
|
2015-03-05 17:32:47 +00:00
|
|
|
|
2012-02-15 13:44:16 +00:00
|
|
|
type_name = '__arguments_required'
|
|
|
|
object_id = 'some-id'
|
|
|
|
value = 'some value'
|
2016-07-05 18:44:24 +00:00
|
|
|
argv = [type_name, object_id, '--required1', value,
|
|
|
|
'--required2', value]
|
2012-02-15 13:44:16 +00:00
|
|
|
os.environ.update(self.env)
|
|
|
|
emu = emulator.Emulator(argv)
|
|
|
|
emu.run()
|
|
|
|
|
|
|
|
cdist_type = core.CdistType(self.local.type_path, type_name)
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
object_id)
|
2012-02-15 13:44:16 +00:00
|
|
|
self.assertTrue('required1' in cdist_object.parameters)
|
|
|
|
self.assertTrue('required2' in cdist_object.parameters)
|
|
|
|
self.assertEqual(cdist_object.parameters['required1'], value)
|
|
|
|
self.assertEqual(cdist_object.parameters['required2'], value)
|
|
|
|
|
2018-10-13 14:11:51 +00:00
|
|
|
def test_required_multiple_arguments(self):
|
|
|
|
"""check whether assigning required multiple parameter works"""
|
|
|
|
|
|
|
|
type_name = '__arguments_required_multiple'
|
|
|
|
object_id = 'some-id'
|
|
|
|
value1 = 'value1'
|
|
|
|
value2 = 'value2'
|
|
|
|
argv = [type_name, object_id, '--required1', value1,
|
|
|
|
'--required1', value2]
|
|
|
|
os.environ.update(self.env)
|
|
|
|
emu = emulator.Emulator(argv)
|
|
|
|
emu.run()
|
|
|
|
|
|
|
|
cdist_type = core.CdistType(self.local.type_path, type_name)
|
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
object_id)
|
|
|
|
self.assertTrue('required1' in cdist_object.parameters)
|
|
|
|
self.assertTrue(value1 in cdist_object.parameters['required1'])
|
|
|
|
self.assertTrue(value2 in cdist_object.parameters['required1'])
|
|
|
|
|
2012-02-15 13:44:16 +00:00
|
|
|
# def test_required_missing(self):
|
|
|
|
# type_name = '__arguments_required'
|
|
|
|
# object_id = 'some-id'
|
|
|
|
# value = 'some value'
|
|
|
|
# argv = [type_name, object_id, '--required1', value]
|
|
|
|
# os.environ.update(self.env)
|
|
|
|
# emu = emulator.Emulator(argv)
|
2012-11-07 09:51:13 +00:00
|
|
|
#
|
2012-02-15 13:44:16 +00:00
|
|
|
# self.assertRaises(SystemExit, emu.run)
|
|
|
|
|
|
|
|
def test_optional(self):
|
|
|
|
type_name = '__arguments_optional'
|
|
|
|
object_id = 'some-id'
|
|
|
|
value = 'some value'
|
|
|
|
argv = [type_name, object_id, '--optional1', value]
|
|
|
|
os.environ.update(self.env)
|
|
|
|
emu = emulator.Emulator(argv)
|
|
|
|
emu.run()
|
|
|
|
|
|
|
|
cdist_type = core.CdistType(self.local.type_path, type_name)
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
object_id)
|
2012-02-15 13:44:16 +00:00
|
|
|
self.assertTrue('optional1' in cdist_object.parameters)
|
|
|
|
self.assertFalse('optional2' in cdist_object.parameters)
|
|
|
|
self.assertEqual(cdist_object.parameters['optional1'], value)
|
2012-06-04 12:11:34 +00:00
|
|
|
|
2018-10-13 14:11:51 +00:00
|
|
|
def test_optional_multiple(self):
|
|
|
|
type_name = '__arguments_optional_multiple'
|
|
|
|
object_id = 'some-id'
|
|
|
|
value1 = 'value1'
|
|
|
|
value2 = 'value2'
|
|
|
|
argv = [type_name, object_id, '--optional1', value1, '--optional1',
|
|
|
|
value2]
|
|
|
|
os.environ.update(self.env)
|
|
|
|
emu = emulator.Emulator(argv)
|
|
|
|
emu.run()
|
|
|
|
|
|
|
|
cdist_type = core.CdistType(self.local.type_path, type_name)
|
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
object_id)
|
|
|
|
self.assertTrue('optional1' in cdist_object.parameters)
|
|
|
|
self.assertTrue(value1 in cdist_object.parameters['optional1'])
|
|
|
|
self.assertTrue(value2 in cdist_object.parameters['optional1'])
|
|
|
|
|
2013-09-04 20:11:08 +00:00
|
|
|
def test_argument_defaults(self):
|
|
|
|
type_name = '__argument_defaults'
|
|
|
|
object_id = 'some-id'
|
|
|
|
value = 'value1'
|
|
|
|
argv = [type_name, object_id]
|
|
|
|
os.environ.update(self.env)
|
|
|
|
emu = emulator.Emulator(argv)
|
|
|
|
emu.run()
|
|
|
|
|
|
|
|
cdist_type = core.CdistType(self.local.type_path, type_name)
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
object_id)
|
2013-09-04 20:11:08 +00:00
|
|
|
self.assertTrue('optional1' in cdist_object.parameters)
|
|
|
|
self.assertFalse('optional2' in cdist_object.parameters)
|
|
|
|
self.assertEqual(cdist_object.parameters['optional1'], value)
|
|
|
|
|
2018-10-13 14:11:51 +00:00
|
|
|
def test_object_params_in_context(self):
|
|
|
|
type_name = '__arguments_all'
|
|
|
|
object_id = 'some-id'
|
|
|
|
argv = [type_name, object_id, '--opt', 'opt', '--req', 'req',
|
|
|
|
'--bool', '--optmul', 'val1', '--optmul', 'val2',
|
|
|
|
'--reqmul', 'val3', '--reqmul', 'val4',
|
|
|
|
'--optmul1', 'val5', '--reqmul1', 'val6']
|
|
|
|
os.environ.update(self.env)
|
|
|
|
emu = emulator.Emulator(argv)
|
|
|
|
emu.run()
|
|
|
|
|
|
|
|
obj_params = emu._object_params_in_context()
|
|
|
|
obj_params_expected = {
|
|
|
|
'bool': '',
|
|
|
|
'opt': 'opt',
|
|
|
|
'optmul1': ['val5', ],
|
|
|
|
'optmul': ['val1', 'val2', ],
|
|
|
|
'req': 'req',
|
|
|
|
'reqmul1': ['val6', ],
|
|
|
|
'reqmul': ['val3', 'val4', ],
|
|
|
|
}
|
|
|
|
self.assertEqual(obj_params, obj_params_expected)
|
|
|
|
|
2012-06-04 12:11:34 +00:00
|
|
|
|
|
|
|
class StdinTestCase(test.CdistTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.orig_environ = os.environ
|
|
|
|
os.environ = os.environ.copy()
|
2012-11-07 09:49:51 +00:00
|
|
|
|
2012-06-04 12:11:34 +00:00
|
|
|
self.temp_dir = self.mkdtemp()
|
2013-08-19 10:03:25 +00:00
|
|
|
base_path = os.path.join(self.temp_dir, "out")
|
2016-08-10 21:56:56 +00:00
|
|
|
hostdir = cdist.str_hash(self.target_host[0])
|
2016-07-23 14:13:59 +00:00
|
|
|
host_base_path = os.path.join(base_path, hostdir)
|
2012-06-04 12:11:34 +00:00
|
|
|
|
2012-11-07 09:49:51 +00:00
|
|
|
self.local = local.Local(
|
2012-06-04 12:11:34 +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=host_base_path,
|
|
|
|
host_dir_name=hostdir,
|
2012-06-04 12:11:34 +00:00
|
|
|
exec_path=test.cdist_exec_path,
|
2012-11-07 09:49:51 +00:00
|
|
|
add_conf_dirs=[conf_dir])
|
|
|
|
|
|
|
|
self.local.create_files_dirs()
|
2012-06-04 12:11:34 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.environ = self.orig_environ
|
|
|
|
shutil.rmtree(self.temp_dir)
|
|
|
|
|
|
|
|
def test_file_from_stdin(self):
|
2012-11-07 09:49:51 +00:00
|
|
|
"""
|
|
|
|
Test whether reading from stdin works
|
|
|
|
"""
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Create string with random content
|
|
|
|
random_string = str(random.sample(range(1000), 800))
|
|
|
|
random_buffer = io.BytesIO(bytes(random_string, 'utf-8'))
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Prepare required args and environment for emulator
|
|
|
|
type_name = '__file'
|
|
|
|
object_id = "cdist-test-id"
|
|
|
|
argv = [type_name, object_id]
|
|
|
|
|
2015-03-05 18:20:06 +00:00
|
|
|
env = os.environ.copy()
|
|
|
|
env['__cdist_manifest'] = "/cdist-test/path/that/does/not/exist"
|
|
|
|
env['__cdist_object_marker'] = self.local.object_marker_name
|
|
|
|
env['__cdist_type_base_path'] = self.local.type_path
|
|
|
|
env['__global'] = self.local.base_path
|
2012-11-07 09:49:51 +00:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Create path where stdin should reside at
|
|
|
|
cdist_type = core.CdistType(self.local.type_path, type_name)
|
2016-07-05 18:44:24 +00:00
|
|
|
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
|
|
|
|
self.local.object_marker_name,
|
|
|
|
object_id)
|
2012-11-07 09:49:51 +00:00
|
|
|
stdin_out_path = os.path.join(cdist_object.absolute_path, 'stdin')
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Run emulator
|
|
|
|
emu = emulator.Emulator(argv, stdin=random_buffer, env=env)
|
|
|
|
emu.run()
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Read where emulator should have placed stdin
|
|
|
|
with open(stdin_out_path, 'r') as fd:
|
|
|
|
stdin_saved_by_emulator = fd.read()
|
|
|
|
|
|
|
|
self.assertEqual(random_string, stdin_saved_by_emulator)
|
2016-06-10 05:50:07 +00:00
|
|
|
|
|
|
|
|
2016-10-08 09:40:32 +00:00
|
|
|
class EmulatorAlreadyExistingRequirementsWarnTestCase(test.CdistTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.temp_dir = self.mkdtemp()
|
|
|
|
handle, self.script = self.mkstemp(dir=self.temp_dir)
|
|
|
|
os.close(handle)
|
|
|
|
base_path = self.temp_dir
|
|
|
|
hostdir = cdist.str_hash(self.target_host[0])
|
|
|
|
host_base_path = os.path.join(base_path, hostdir)
|
|
|
|
|
|
|
|
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-10-08 09:40:32 +00:00
|
|
|
base_root_path=host_base_path,
|
|
|
|
host_dir_name=hostdir,
|
|
|
|
exec_path=test.cdist_exec_path,
|
|
|
|
add_conf_dirs=[conf_dir])
|
|
|
|
self.local.create_files_dirs()
|
|
|
|
|
|
|
|
self.manifest = core.Manifest(self.target_host, self.local)
|
|
|
|
self.env = self.manifest.env_initial_manifest(self.script)
|
|
|
|
self.env['__cdist_object_marker'] = self.local.object_marker_name
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.temp_dir)
|
|
|
|
|
|
|
|
def test_object_existing_requirements_req_none(self):
|
|
|
|
"""Test to show dependency resolver warning message."""
|
|
|
|
argv = ['__directory', 'spam']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
argv = ['__file', 'eggs']
|
|
|
|
self.env['require'] = '__directory/spam'
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
argv = ['__file', 'eggs']
|
|
|
|
if 'require' in self.env:
|
|
|
|
del self.env['require']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
|
|
|
|
def test_object_existing_requirements_none_req(self):
|
|
|
|
"""Test to show dependency resolver warning message."""
|
|
|
|
argv = ['__directory', 'spam']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
argv = ['__file', 'eggs']
|
|
|
|
if 'require' in self.env:
|
|
|
|
del self.env['require']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
emu.run()
|
|
|
|
argv = ['__file', 'eggs']
|
|
|
|
self.env['require'] = '__directory/spam'
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
|
2020-08-22 17:53:42 +00:00
|
|
|
def test_parse_require(self):
|
|
|
|
require = " \t \n \t\t\n\t\na\tb\nc d \te\t\nf\ng\t "
|
|
|
|
expected = ['', 'a', 'b', 'c', 'd', 'e', 'f', 'g', '', ]
|
|
|
|
|
|
|
|
argv = ['__directory', 'spam']
|
|
|
|
emu = emulator.Emulator(argv, env=self.env)
|
|
|
|
requirements = emu._parse_require(require)
|
|
|
|
|
|
|
|
self.assertEqual(expected, requirements)
|
|
|
|
|
2016-10-08 09:40:32 +00:00
|
|
|
|
2016-06-10 05:50:07 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import unittest
|
|
|
|
unittest.main()
|