2011-10-10 07:30:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2010-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)
|
2014-02-09 16:43:31 +00:00
|
|
|
# 2014 Daniel Heule (hda at sfs.biz)
|
2011-10-10 07:30:28 +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
|
2015-03-05 17:32:47 +00:00
|
|
|
import tempfile
|
2011-10-10 07:30:28 +00:00
|
|
|
|
2011-10-19 15:59:21 +00:00
|
|
|
from cdist import test
|
2011-10-14 07:05:22 +00:00
|
|
|
from cdist import core
|
2011-10-10 07:30:28 +00:00
|
|
|
|
2012-02-12 00:27:25 +00:00
|
|
|
import cdist
|
|
|
|
|
2011-10-10 07:30:28 +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')
|
|
|
|
|
2015-03-05 17:32:47 +00:00
|
|
|
OBJECT_MARKER_NAME = '.cdist-pseudo-random'
|
|
|
|
|
|
|
|
expected_object_names = sorted([
|
|
|
|
'__first/child',
|
|
|
|
'__first/dog',
|
|
|
|
'__first/man',
|
|
|
|
'__first/woman',
|
|
|
|
'__second/on-the',
|
|
|
|
'__second/under-the',
|
|
|
|
'__third/moon'])
|
|
|
|
|
|
|
|
|
2011-10-19 15:59:21 +00:00
|
|
|
class ObjectClassTestCase(test.CdistTestCase):
|
2011-10-10 07:30:28 +00:00
|
|
|
|
2012-12-19 20:10:18 +00:00
|
|
|
def setUp(self):
|
2015-03-05 17:32:47 +00:00
|
|
|
|
|
|
|
self.tempdir = tempfile.mkdtemp(prefix="cdist.test")
|
|
|
|
self.object_base_path = self.tempdir
|
2012-12-19 20:10:18 +00:00
|
|
|
|
|
|
|
self.expected_objects = []
|
2015-03-05 17:32:47 +00:00
|
|
|
for cdist_object_name in expected_object_names:
|
2014-02-13 20:05:37 +00:00
|
|
|
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,
|
|
|
|
OBJECT_MARKER_NAME,
|
|
|
|
cdist_object_id)
|
2015-03-05 17:32:47 +00:00
|
|
|
cdist_object.create()
|
2012-12-19 20:10:18 +00:00
|
|
|
self.expected_objects.append(cdist_object)
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2015-03-05 17:32:47 +00:00
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.tempdir)
|
|
|
|
|
2012-12-19 20:10:18 +00:00
|
|
|
def test_list_object_names(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
found_object_names = sorted(list(core.CdistObject.list_object_names(
|
|
|
|
self.object_base_path, OBJECT_MARKER_NAME)))
|
|
|
|
self.assertEqual(found_object_names, expected_object_names)
|
2011-10-10 07:30:28 +00:00
|
|
|
|
|
|
|
def test_list_type_names(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
type_names = list(cdist.core.CdistObject.list_type_names(
|
|
|
|
self.object_base_path))
|
|
|
|
self.assertEqual(sorted(type_names),
|
|
|
|
['__first', '__second', '__third'])
|
2011-10-10 07:30:28 +00:00
|
|
|
|
|
|
|
def test_list_objects(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
found_objects = sorted(list(core.CdistObject.list_objects(
|
|
|
|
self.object_base_path, type_base_path, OBJECT_MARKER_NAME)))
|
2012-12-19 20:10:18 +00:00
|
|
|
self.assertEqual(found_objects, self.expected_objects)
|
2011-10-10 07:30:28 +00:00
|
|
|
|
2013-07-10 15:21:38 +00:00
|
|
|
def test_create_singleton(self):
|
|
|
|
"""Check whether creating an object without id (singleton) works"""
|
2016-07-05 18:44:24 +00:00
|
|
|
singleton = self.expected_objects[0].object_from_name(
|
|
|
|
"__test_singleton")
|
2013-07-10 15:21:38 +00:00
|
|
|
# came here - everything fine
|
|
|
|
|
|
|
|
def test_create_singleton_not_singleton_type(self):
|
|
|
|
"""try to create an object of a type that is not a singleton
|
|
|
|
without an object id"""
|
|
|
|
with self.assertRaises(cdist.core.cdist_object.MissingObjectIdError):
|
|
|
|
self.expected_objects[0].object_from_name("__first")
|
2011-10-10 07:30:28 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2011-10-19 15:59:21 +00:00
|
|
|
class ObjectIdTestCase(test.CdistTestCase):
|
2015-03-05 17:32:47 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.tempdir = tempfile.mkdtemp(prefix="cdist.test")
|
|
|
|
self.object_base_path = self.tempdir
|
|
|
|
|
|
|
|
self.expected_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,
|
|
|
|
OBJECT_MARKER_NAME,
|
|
|
|
cdist_object_id)
|
2015-03-05 17:32:47 +00:00
|
|
|
cdist_object.create()
|
|
|
|
self.expected_objects.append(cdist_object)
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2015-03-05 17:32:47 +00:00
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.tempdir)
|
|
|
|
|
2012-02-13 09:39:04 +00:00
|
|
|
def test_object_id_contains_double_slash(self):
|
2012-02-12 00:18:14 +00:00
|
|
|
cdist_type = core.CdistType(type_base_path, '__third')
|
2012-02-13 09:39:04 +00:00
|
|
|
illegal_object_id = '/object_id//may/not/contain/double/slash'
|
2011-10-14 09:21:12 +00:00
|
|
|
with self.assertRaises(core.IllegalObjectIdError):
|
2016-07-05 18:44:24 +00:00
|
|
|
core.CdistObject(cdist_type, self.object_base_path,
|
|
|
|
OBJECT_MARKER_NAME, illegal_object_id)
|
2011-10-14 09:21:12 +00:00
|
|
|
|
2011-11-09 19:38:22 +00:00
|
|
|
def test_object_id_contains_object_marker(self):
|
2012-02-12 00:18:14 +00:00
|
|
|
cdist_type = core.CdistType(type_base_path, '__third')
|
2016-07-05 18:44:24 +00:00
|
|
|
illegal_object_id = (
|
|
|
|
'object_id/may/not/contain/%s/anywhere' % OBJECT_MARKER_NAME)
|
2011-10-18 12:41:38 +00:00
|
|
|
with self.assertRaises(core.IllegalObjectIdError):
|
2016-07-05 18:44:24 +00:00
|
|
|
core.CdistObject(cdist_type, self.object_base_path,
|
|
|
|
OBJECT_MARKER_NAME, illegal_object_id)
|
2011-10-18 12:41:38 +00:00
|
|
|
|
2011-11-09 19:38:22 +00:00
|
|
|
def test_object_id_contains_object_marker_string(self):
|
2012-02-12 00:18:14 +00:00
|
|
|
cdist_type = core.CdistType(type_base_path, '__third')
|
2016-07-05 18:44:24 +00:00
|
|
|
illegal_object_id = (
|
|
|
|
'object_id/may/contain_%s_in_filename' % OBJECT_MARKER_NAME)
|
|
|
|
core.CdistObject(cdist_type, self.object_base_path,
|
|
|
|
OBJECT_MARKER_NAME, illegal_object_id)
|
2011-11-09 19:38:22 +00:00
|
|
|
# if we get here, the test passed
|
|
|
|
|
2014-02-09 16:10:43 +00:00
|
|
|
def test_object_id_contains_only_dot(self):
|
|
|
|
cdist_type = core.CdistType(type_base_path, '__third')
|
|
|
|
illegal_object_id = '.'
|
|
|
|
with self.assertRaises(core.IllegalObjectIdError):
|
2016-07-05 18:44:24 +00:00
|
|
|
core.CdistObject(cdist_type, self.object_base_path,
|
|
|
|
OBJECT_MARKER_NAME, illegal_object_id)
|
2014-02-09 16:10:43 +00:00
|
|
|
|
2017-08-10 17:08:44 +00:00
|
|
|
def test_object_id_equals_slash(self):
|
|
|
|
cdist_type = core.CdistType(type_base_path, '__third')
|
|
|
|
illegal_object_id = '/'
|
|
|
|
with self.assertRaises(core.IllegalObjectIdError):
|
|
|
|
core.CdistObject(cdist_type, self.object_base_path,
|
|
|
|
OBJECT_MARKER_NAME, illegal_object_id)
|
|
|
|
|
2014-02-09 16:10:43 +00:00
|
|
|
def test_object_id_on_singleton_type(self):
|
|
|
|
cdist_type = core.CdistType(type_base_path, '__test_singleton')
|
|
|
|
illegal_object_id = 'object_id'
|
|
|
|
with self.assertRaises(core.IllegalObjectIdError):
|
2016-07-05 18:44:24 +00:00
|
|
|
core.CdistObject(cdist_type, self.object_base_path,
|
|
|
|
OBJECT_MARKER_NAME, illegal_object_id)
|
|
|
|
|
2011-10-14 09:21:12 +00:00
|
|
|
|
2011-10-19 15:59:21 +00:00
|
|
|
class ObjectTestCase(test.CdistTestCase):
|
2011-10-10 07:30:28 +00:00
|
|
|
|
2011-10-10 07:34:16 +00:00
|
|
|
def setUp(self):
|
2015-03-05 17:32:47 +00:00
|
|
|
self.tempdir = tempfile.mkdtemp(prefix="cdist.test")
|
|
|
|
self.object_base_path = self.tempdir
|
|
|
|
|
2012-02-12 00:18:14 +00:00
|
|
|
self.cdist_type = core.CdistType(type_base_path, '__third')
|
2016-07-05 18:44:24 +00:00
|
|
|
self.cdist_object = core.CdistObject(self.cdist_type,
|
|
|
|
self.object_base_path,
|
|
|
|
OBJECT_MARKER_NAME, 'moon')
|
2015-03-05 17:32:47 +00:00
|
|
|
self.cdist_object.create()
|
|
|
|
|
|
|
|
self.cdist_object.parameters['planet'] = 'Saturn'
|
|
|
|
self.cdist_object.parameters['name'] = 'Prometheus'
|
|
|
|
|
2011-10-10 08:04:36 +00:00
|
|
|
def tearDown(self):
|
2011-10-10 08:05:46 +00:00
|
|
|
self.cdist_object.prepared = False
|
2011-10-10 08:06:41 +00:00
|
|
|
self.cdist_object.ran = False
|
2011-10-11 12:22:29 +00:00
|
|
|
self.cdist_object.source = []
|
2011-10-13 11:42:45 +00:00
|
|
|
self.cdist_object.code_local = ''
|
|
|
|
self.cdist_object.code_remote = ''
|
2011-10-19 12:50:16 +00:00
|
|
|
self.cdist_object.state = ''
|
2011-10-10 08:04:36 +00:00
|
|
|
|
2015-03-05 17:32:47 +00:00
|
|
|
shutil.rmtree(self.tempdir)
|
|
|
|
|
2011-10-10 07:34:16 +00:00
|
|
|
def test_name(self):
|
|
|
|
self.assertEqual(self.cdist_object.name, '__third/moon')
|
|
|
|
|
2011-10-10 07:44:29 +00:00
|
|
|
def test_object_id(self):
|
|
|
|
self.assertEqual(self.cdist_object.object_id, 'moon')
|
|
|
|
|
2011-10-10 07:34:16 +00:00
|
|
|
def test_path(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.cdist_object.path,
|
|
|
|
"__third/moon/%s" % OBJECT_MARKER_NAME)
|
2011-10-10 07:34:16 +00:00
|
|
|
|
|
|
|
def test_absolute_path(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.cdist_object.absolute_path,
|
|
|
|
os.path.join(self.object_base_path,
|
|
|
|
"__third/moon/%s" % OBJECT_MARKER_NAME))
|
2011-10-10 07:34:16 +00:00
|
|
|
|
|
|
|
def test_code_local_path(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.cdist_object.code_local_path,
|
|
|
|
"__third/moon/%s/code-local" % OBJECT_MARKER_NAME)
|
2011-10-10 07:34:16 +00:00
|
|
|
|
|
|
|
def test_code_remote_path(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.cdist_object.code_remote_path,
|
|
|
|
"__third/moon/%s/code-remote" % OBJECT_MARKER_NAME)
|
2011-10-10 07:34:16 +00:00
|
|
|
|
|
|
|
def test_parameter_path(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.cdist_object.parameter_path,
|
|
|
|
"__third/moon/%s/parameter" % OBJECT_MARKER_NAME)
|
2011-10-10 07:34:16 +00:00
|
|
|
|
2011-10-10 07:36:48 +00:00
|
|
|
def test_explorer_path(self):
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.cdist_object.explorer_path,
|
|
|
|
"__third/moon/%s/explorer" % OBJECT_MARKER_NAME)
|
2011-10-10 07:36:48 +00:00
|
|
|
|
2011-10-10 07:47:06 +00:00
|
|
|
def test_parameters(self):
|
|
|
|
expected_parameters = {'planet': 'Saturn', 'name': 'Prometheus'}
|
|
|
|
self.assertEqual(self.cdist_object.parameters, expected_parameters)
|
|
|
|
|
2011-10-11 13:45:10 +00:00
|
|
|
def test_explorers(self):
|
|
|
|
self.assertEqual(self.cdist_object.explorers, {})
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
# FIXME: actually testing fsproperty.DirectoryDictProperty here,
|
|
|
|
# move to their own test case
|
2011-10-13 19:23:04 +00:00
|
|
|
def test_explorers_assign_dict(self):
|
2011-10-11 13:45:10 +00:00
|
|
|
expected = {'first': 'foo', 'second': 'bar'}
|
|
|
|
# when set, written to file
|
|
|
|
self.cdist_object.explorers = expected
|
2016-07-05 18:44:24 +00:00
|
|
|
object_explorer_path = os.path.join(self.cdist_object.base_path,
|
|
|
|
self.cdist_object.explorer_path)
|
2011-10-13 19:23:04 +00:00
|
|
|
self.assertTrue(os.path.isdir(object_explorer_path))
|
|
|
|
# when accessed, read from file
|
|
|
|
self.assertEqual(self.cdist_object.explorers, expected)
|
|
|
|
# remove dynamically created folder
|
|
|
|
self.cdist_object.explorers = {}
|
2016-07-05 18:44:24 +00:00
|
|
|
os.rmdir(os.path.join(self.cdist_object.base_path,
|
|
|
|
self.cdist_object.explorer_path))
|
2011-10-13 19:23:04 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
# FIXME: actually testing fsproperty.DirectoryDictProperty here,
|
|
|
|
# move to their own test case
|
2011-10-13 19:23:04 +00:00
|
|
|
def test_explorers_assign_key_value(self):
|
|
|
|
expected = {'first': 'foo', 'second': 'bar'}
|
2016-07-05 18:44:24 +00:00
|
|
|
object_explorer_path = os.path.join(self.cdist_object.base_path,
|
|
|
|
self.cdist_object.explorer_path)
|
|
|
|
for key, value in expected.items():
|
2011-10-13 19:23:04 +00:00
|
|
|
# when set, written to file
|
|
|
|
self.cdist_object.explorers[key] = value
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertTrue(os.path.isfile(os.path.join(object_explorer_path,
|
|
|
|
key)))
|
2011-10-11 13:45:10 +00:00
|
|
|
# when accessed, read from file
|
|
|
|
self.assertEqual(self.cdist_object.explorers, expected)
|
|
|
|
# remove dynamically created folder
|
|
|
|
self.cdist_object.explorers = {}
|
2016-07-05 18:44:24 +00:00
|
|
|
os.rmdir(os.path.join(self.cdist_object.base_path,
|
|
|
|
self.cdist_object.explorer_path))
|
2011-10-11 13:45:10 +00:00
|
|
|
|
2011-10-10 08:00:42 +00:00
|
|
|
def test_requirements(self):
|
|
|
|
expected = []
|
|
|
|
self.assertEqual(list(self.cdist_object.requirements), expected)
|
|
|
|
|
2011-10-19 12:50:16 +00:00
|
|
|
def test_state(self):
|
|
|
|
self.assertEqual(self.cdist_object.state, '')
|
|
|
|
|
2011-10-19 12:53:06 +00:00
|
|
|
def test_state_prepared(self):
|
2012-02-11 23:08:18 +00:00
|
|
|
self.cdist_object.state = core.CdistObject.STATE_PREPARED
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.cdist_object.state,
|
|
|
|
core.CdistObject.STATE_PREPARED)
|
2011-10-19 12:50:16 +00:00
|
|
|
|
|
|
|
def test_state_running(self):
|
2012-02-11 23:08:18 +00:00
|
|
|
self.cdist_object.state = core.CdistObject.STATE_RUNNING
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.cdist_object.state,
|
|
|
|
core.CdistObject.STATE_RUNNING)
|
2011-10-19 12:50:16 +00:00
|
|
|
|
2011-10-19 12:53:06 +00:00
|
|
|
def test_state_done(self):
|
2012-02-11 23:08:18 +00:00
|
|
|
self.cdist_object.state = core.CdistObject.STATE_DONE
|
2016-07-05 18:44:24 +00:00
|
|
|
self.assertEqual(self.cdist_object.state,
|
|
|
|
core.CdistObject.STATE_DONE)
|
2011-10-19 12:50:16 +00:00
|
|
|
|
2011-10-11 07:38:31 +00:00
|
|
|
def test_source(self):
|
2011-10-11 12:22:29 +00:00
|
|
|
self.assertEqual(list(self.cdist_object.source), [])
|
2011-10-11 07:38:31 +00:00
|
|
|
|
|
|
|
def test_source_after_changing(self):
|
2011-10-11 12:22:29 +00:00
|
|
|
self.cdist_object.source = ['/path/to/manifest']
|
|
|
|
self.assertEqual(list(self.cdist_object.source), ['/path/to/manifest'])
|
2011-10-13 11:40:20 +00:00
|
|
|
|
|
|
|
def test_code_local(self):
|
|
|
|
self.assertEqual(self.cdist_object.code_local, '')
|
|
|
|
|
|
|
|
def test_code_local_after_changing(self):
|
|
|
|
self.cdist_object.code_local = 'Hello World'
|
|
|
|
self.assertEqual(self.cdist_object.code_local, 'Hello World')
|
2011-10-13 11:42:45 +00:00
|
|
|
|
|
|
|
def test_code_remote(self):
|
|
|
|
self.assertEqual(self.cdist_object.code_remote, '')
|
|
|
|
|
|
|
|
def test_code_remote_after_changing(self):
|
|
|
|
self.cdist_object.code_remote = 'Hello World'
|
|
|
|
self.assertEqual(self.cdist_object.code_remote, 'Hello World')
|
2011-10-14 07:03:18 +00:00
|
|
|
|
|
|
|
def test_object_from_name(self):
|
|
|
|
self.cdist_object.code_remote = 'Hello World'
|
|
|
|
other_name = '__first/man'
|
|
|
|
other_object = self.cdist_object.object_from_name(other_name)
|
2012-02-11 23:08:18 +00:00
|
|
|
self.assertTrue(isinstance(other_object, core.CdistObject))
|
2012-02-13 09:41:12 +00:00
|
|
|
self.assertEqual(other_object.cdist_type.name, '__first')
|
2011-10-14 07:03:18 +00:00
|
|
|
self.assertEqual(other_object.object_id, 'man')
|