2011-10-10 07:30:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
|
|
|
#
|
|
|
|
# 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 tempfile
|
|
|
|
import unittest
|
|
|
|
import shutil
|
|
|
|
|
2011-10-14 07:05:22 +00:00
|
|
|
from cdist import core
|
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')
|
|
|
|
object_base_path = op.join(fixtures, 'object')
|
|
|
|
type_base_path = op.join(fixtures, 'type')
|
|
|
|
|
|
|
|
class ObjectClassTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_list_object_names(self):
|
2011-10-14 07:05:22 +00:00
|
|
|
object_names = list(core.Object.list_object_names(object_base_path))
|
2011-10-10 07:30:28 +00:00
|
|
|
self.assertEqual(object_names, ['__first/man', '__second/on-the', '__third/moon'])
|
|
|
|
|
|
|
|
def test_list_type_names(self):
|
2011-10-14 07:05:22 +00:00
|
|
|
type_names = list(core.Object.list_type_names(object_base_path))
|
2011-10-10 07:30:28 +00:00
|
|
|
self.assertEqual(type_names, ['__first', '__second', '__third'])
|
|
|
|
|
|
|
|
def test_list_objects(self):
|
2011-10-14 07:05:22 +00:00
|
|
|
objects = list(core.Object.list_objects(object_base_path, type_base_path))
|
2011-10-10 07:30:28 +00:00
|
|
|
objects_expected = [
|
2011-10-14 07:05:22 +00:00
|
|
|
core.Object(core.Type(type_base_path, '__first'), object_base_path, 'man'),
|
|
|
|
core.Object(core.Type(type_base_path, '__second'), object_base_path, 'on-the'),
|
|
|
|
core.Object(core.Type(type_base_path, '__third'), object_base_path, 'moon'),
|
2011-10-10 07:30:28 +00:00
|
|
|
]
|
|
|
|
self.assertEqual(objects, objects_expected)
|
|
|
|
|
|
|
|
|
2011-10-10 07:34:16 +00:00
|
|
|
class ObjectTestCase(unittest.TestCase):
|
2011-10-10 07:30:28 +00:00
|
|
|
|
2011-10-10 07:34:16 +00:00
|
|
|
def setUp(self):
|
2011-10-14 07:05:22 +00:00
|
|
|
self.cdist_type = core.Type(type_base_path, '__third')
|
|
|
|
self.cdist_object = core.Object(self.cdist_type, object_base_path, 'moon')
|
2011-10-10 07:34:16 +00:00
|
|
|
|
2011-10-10 08:04:36 +00:00
|
|
|
def tearDown(self):
|
|
|
|
self.cdist_object.changed = False
|
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-10 08:04:36 +00:00
|
|
|
|
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):
|
|
|
|
self.assertEqual(self.cdist_object.path, '__third/moon/.cdist')
|
|
|
|
|
|
|
|
def test_absolute_path(self):
|
|
|
|
self.assertEqual(self.cdist_object.absolute_path, os.path.join(object_base_path, '__third/moon/.cdist'))
|
|
|
|
|
|
|
|
def test_code_local_path(self):
|
|
|
|
self.assertEqual(self.cdist_object.code_local_path, '__third/moon/.cdist/code-local')
|
|
|
|
|
|
|
|
def test_code_remote_path(self):
|
|
|
|
self.assertEqual(self.cdist_object.code_remote_path, '__third/moon/.cdist/code-remote')
|
|
|
|
|
|
|
|
def test_parameter_path(self):
|
|
|
|
self.assertEqual(self.cdist_object.parameter_path, '__third/moon/.cdist/parameter')
|
|
|
|
|
2011-10-10 07:36:48 +00:00
|
|
|
def test_explorer_path(self):
|
|
|
|
self.assertEqual(self.cdist_object.explorer_path, '__third/moon/.cdist/explorer')
|
|
|
|
|
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, {})
|
|
|
|
|
2011-10-13 19:23:04 +00:00
|
|
|
# FIXME: actually testing fsproperty.DirectoryDictProperty here, move to their own test case
|
|
|
|
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
|
2011-10-13 19:23:04 +00:00
|
|
|
object_explorer_path = os.path.join(self.cdist_object.base_path, self.cdist_object.explorer_path)
|
|
|
|
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 = {}
|
|
|
|
os.rmdir(os.path.join(self.cdist_object.base_path, self.cdist_object.explorer_path))
|
|
|
|
|
|
|
|
# FIXME: actually testing fsproperty.DirectoryDictProperty here, move to their own test case
|
|
|
|
def test_explorers_assign_key_value(self):
|
|
|
|
expected = {'first': 'foo', 'second': 'bar'}
|
|
|
|
object_explorer_path = os.path.join(self.cdist_object.base_path, self.cdist_object.explorer_path)
|
|
|
|
for key,value in expected.items():
|
|
|
|
# when set, written to file
|
|
|
|
self.cdist_object.explorers[key] = value
|
|
|
|
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 = {}
|
|
|
|
os.rmdir(os.path.join(self.cdist_object.base_path, self.cdist_object.explorer_path))
|
|
|
|
|
2011-10-10 08:00:42 +00:00
|
|
|
def test_requirements(self):
|
|
|
|
expected = []
|
|
|
|
self.assertEqual(list(self.cdist_object.requirements), expected)
|
|
|
|
|
2011-10-10 08:04:36 +00:00
|
|
|
def test_changed(self):
|
|
|
|
self.assertFalse(self.cdist_object.changed)
|
|
|
|
|
|
|
|
def test_changed_after_changing(self):
|
|
|
|
self.cdist_object.changed = True
|
|
|
|
self.assertTrue(self.cdist_object.changed)
|
|
|
|
|
2011-10-10 08:05:46 +00:00
|
|
|
def test_prepared(self):
|
|
|
|
self.assertFalse(self.cdist_object.prepared)
|
|
|
|
|
|
|
|
def test_prepared_after_changing(self):
|
|
|
|
self.cdist_object.prepared = True
|
|
|
|
self.assertTrue(self.cdist_object.prepared)
|
2011-10-10 08:06:41 +00:00
|
|
|
|
|
|
|
def test_ran(self):
|
|
|
|
self.assertFalse(self.cdist_object.ran)
|
|
|
|
|
|
|
|
def test_ran_after_changing(self):
|
|
|
|
self.cdist_object.ran = True
|
|
|
|
self.assertTrue(self.cdist_object.ran)
|
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)
|
|
|
|
self.assertTrue(isinstance(other_object, core.Object))
|
|
|
|
self.assertEqual(other_object.type.name, '__first')
|
|
|
|
self.assertEqual(other_object.object_id, 'man')
|