cleanup tests and ... - 100% green

Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
This commit is contained in:
Nico Schottelius 2013-06-21 22:39:20 +02:00
parent ef24ec4db8
commit 25bdcb1602
66 changed files with 36 additions and 198 deletions

View file

@ -47,6 +47,10 @@ class Error(Exception):
"""Base exception class for this project"""
pass
class UnresolvableRequirementsError(cdist.Error):
"""Resolving requirements failed"""
pass
class CdistObjectError(Error):
"""Something went wrong with an object"""

View file

@ -131,7 +131,7 @@ class ConfigInstall(object):
autorequire = ", ".join(autorequire_names)
info_string.append("%s requires: %s autorequires: %s" % (cdist_object.name, requirements, autorequire))
raise cdist.Error("The requirements of the following objects could not be resolved: %s" %
raise cdist.UnresolvableRequirementsError("The requirements of the following objects could not be resolved: %s" %
("; ".join(info_string)))
def object_prepare(self, cdist_object):

View file

@ -235,10 +235,3 @@ class CdistObject(object):
object_list.append(cdist_object)
return object_list
class RequirementNotFoundError(cdist.Error):
def __init__(self, requirement):
self.requirement = requirement
def __str__(self):
return 'Requirement could not be found: %s' % self.requirement

View file

@ -212,54 +212,3 @@ class ObjectTestCase(test.CdistTestCase):
self.assertTrue(isinstance(other_object, core.CdistObject))
self.assertEqual(other_object.cdist_type.name, '__first')
self.assertEqual(other_object.object_id, 'man')
class ObjectResolveRequirementsTestCase(test.CdistTestCase):
def setUp(self):
self.objects = list(core.CdistObject.list_objects(object_base_path, type_base_path))
self.object_index = dict((o.name, o) for o in self.objects)
self.object_names = [o.name for o in self.objects]
print(self.objects)
self.cdist_type = core.CdistType(type_base_path, '__third')
self.cdist_object = core.CdistObject(self.cdist_type, object_base_path, 'moon')
def tearDown(self):
for o in self.objects:
o.requirements = []
def test_find_requirements_by_name_string(self):
"""Check that resolving requirements by name works (require all objects)"""
requirements = self.object_names
self.cdist_object.requirements = requirements
found_requirements = sorted(self.cdist_object.find_requirements_by_name(self.cdist_object.requirements))
expected_requirements = sorted(self.objects)
self.assertEqual(found_requirements, expected_requirements)
def test_find_requirements_by_name_pattern(self):
"""Test whether pattern matching on requirements works"""
# Matches all objects in the end
requirements = ['__first/*', '__second/*-the', '__third/moon']
self.cdist_object.requirements = requirements
expected_requirements = sorted(self.objects)
found_requirements = sorted(self.cdist_object.find_requirements_by_name(self.cdist_object.requirements))
self.assertEqual(expected_requirements, found_requirements)
def test_requirement_not_found(self):
"""Ensure an exception is thrown for missing depedencies"""
cdist_object = self.object_index['__first/man']
cdist_object.requirements = ['__does/not/exist']
with self.assertRaises(core.cdist_object.RequirementNotFoundError):
# Use list, as generator does not (yet) raise the error
list(cdist_object.find_requirements_by_name(cdist_object.requirements))

View file

@ -29,6 +29,8 @@ from cdist import core
import cdist
import cdist.context
import cdist.config
import cdist.core.cdist_type
import cdist.core.cdist_object
import os.path as op
my_dir = op.abspath(op.dirname(__file__))
@ -111,11 +113,37 @@ class ConfigInstallRunTestCase(test.CdistTestCase):
first.requirements = [second.name]
second.requirements = [first.name]
with self.assertRaises(cdist.Error):
with self.assertRaises(cdist.UnresolvableRequirementsError):
self.config.iterate_until_finished()
def test_missing_requirements(self):
"""Throw an error if requiring something non-existing"""
first = self.object_index['__first/man']
first.requirements = ['__does/not/exist']
with self.assertRaises(cdist.Error):
first.requirements = ['__first/not/exist']
with self.assertRaises(cdist.UnresolvableRequirementsError):
self.config.iterate_until_finished()
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']
with self.assertRaises(cdist.core.cdist_type.NoSuchTypeError):
self.config.iterate_until_finished()
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']
with self.assertRaises(cdist.core.cdist_object.MissingObjectIdError):
self.config.iterate_until_finished()
# 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()

View file

@ -1,116 +0,0 @@
# -*- coding: utf-8 -*-
#
# 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc)
# 2012-2013 Nico Schottelius (nico-cdist at schottelius.org)
#
# 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 cdist
from cdist import test
from cdist import core
from cdist import config
from cdist.exec import local
from cdist.core import manifest
import cdist.context
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')
add_conf_dir = op.join(fixtures, 'conf')
type_base_path = op.join(add_conf_dir, 'type')
class MockContext(object):
"""A context object that has the required attributes"""
def __init__(self, target_host):
self.target_host = target_host
self.local = False
class MockLocal(object):
def __init__(self, temp_dir, type_path):
self.temp_dir = temp_dir
self.object_path = op.join(self.temp_dir, "object")
self.type_path = type_path
class ExecutionOrderTestCase(test.CdistTestCase):
def setUp(self):
# self.orig_environ = os.environ
# os.environ = os.environ.copy()
# os.environ['__cdist_out_dir'] = self.out_dir
# os.environ['__cdist_remote_out_dir'] = self.remote_out_dir
# self.out_dir = os.path.join(self.temp_dir, "out")
# self.remote_out_dir = os.path.join(self.temp_dir, "remote")
self.temp_dir = self.mkdtemp()
self.context = MockContext(self.target_host)
self.context.local = MockLocal(self.temp_dir, type_base_path)
self.config = config.Config(self.context)
self._init_objects()
def _init_objects(self):
"""copy base objects to context directory"""
shutil.copytree(object_base_path, self.context.local.object_path)
self.objects = list(core.CdistObject.list_objects(self.context.local.object_path, self.context.local.type_path))
self.object_index = dict((o.name, o) for o in self.objects)
for cdist_object in self.objects:
cdist_object.state = core.CdistObject.STATE_UNDEF
def tearDown(self):
# os.environ = self.orig_environ
shutil.rmtree(self.temp_dir)
def test_objects_changed(self):
pass
# self.assert_True(self.config.iterate_once())
class NotTheExecutionOrderTestCase(test.CdistTestCase):
def test_implicit_dependencies(self):
self.context.initial_manifest = os.path.join(self.context.local.manifest_path, 'implicit_dependencies')
self.config.stage_prepare()
objects = core.CdistObject.list_objects(self.context.local.object_path, self.context.local.type_path)
dependency_resolver = resolver.DependencyResolver(objects)
expected_dependencies = [
dependency_resolver.objects['__package_special/b'],
dependency_resolver.objects['__package/b'],
dependency_resolver.objects['__package_special/a']
]
resolved_dependencies = dependency_resolver.dependencies['__package_special/a']
self.assertEqual(resolved_dependencies, expected_dependencies)
self.assertTrue(False)
def test_circular_dependency(self):
self.context.initial_manifest = os.path.join(self.context.local.manifest_path, 'circular_dependency')
self.config.stage_prepare()
# raises CircularDependecyError
self.config.stage_run()
self.assertTrue(False)
def test_recursive_type(self):
self.context.initial_manifest = os.path.join(self.config.local.manifest_path, 'recursive_type')
self.config.stage_prepare()
# raises CircularDependecyError
self.config.stage_run()
self.assertTrue(False)

View file

@ -1,2 +0,0 @@
# this has triggered CircularReferenceError
__nfsroot_client test

View file

@ -1,3 +0,0 @@
# this creates implicit dependencies through autorequire.
# this failed because autorequired dependencies where not aware of their anchestors dependencies
__top test

View file

@ -1,2 +0,0 @@
__git foo
require="__git/foo" __git bar

View file

@ -1 +0,0 @@
__package git

View file

@ -1,3 +0,0 @@
__user root
__root_ssh_authorized_key john
__root_ssh_authorized_key frank

View file

@ -1 +0,0 @@
__package_special "$__object_id"

View file

@ -1,4 +0,0 @@
user="$__object_id"
__directory /root/.ssh
require="__directory/root/.ssh" \
__addifnosuchline "ssh-root-$user"

View file

@ -1,2 +0,0 @@
__package b
require="__package/b" __package a