Save output streams.

This commit is contained in:
Steven Armstrong 2013-09-05 22:01:21 +02:00 • committed by Darko Poljak
commit 00cd5fa91e
19 changed files with 450 additions and 110 deletions

View file

@ -0,0 +1,137 @@
# -*- coding: utf-8 -*-
#
# 2011-2013 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 core
from cdist import test
from cdist.exec import local
from cdist.exec import remote
from cdist.core import code
from cdist.core import manifest
import os.path as op
my_dir = op.abspath(op.dirname(__file__))
fixtures = op.join(my_dir, 'fixtures')
conf_dir = op.join(fixtures, 'conf')
class CaptureOutputTestCase(test.CdistTestCase):
def setUp(self):
# logging.root.setLevel(logging.TRACE)
self.temp_dir = self.mkdtemp()
self.local_dir = os.path.join(self.temp_dir, "local")
self.hostdir = cdist.str_hash(self.target_host[0])
self.host_base_path = os.path.join(self.local_dir, self.hostdir)
os.makedirs(self.host_base_path)
self.local = local.Local(
target_host=self.target_host,
target_host_tags=None,
base_root_path=self.host_base_path,
host_dir_name=self.hostdir,
exec_path=cdist.test.cdist_exec_path,
add_conf_dirs=[conf_dir])
self.local.create_files_dirs()
self.remote_dir = self.mkdtemp()
remote_exec = self.remote_exec
remote_copy = self.remote_copy
self.remote = remote.Remote(
target_host=self.target_host,
remote_exec=remote_exec,
remote_copy=remote_copy,
base_path=self.remote_dir,
stdout_base_path=self.local.stdout_base_path,
stderr_base_path=self.local.stderr_base_path)
self.remote.create_files_dirs()
self.code = code.Code(self.target_host, self.local, self.remote)
self.manifest = manifest.Manifest(self.target_host, self.local)
self.cdist_type = core.CdistType(self.local.type_path,
'__write_to_stdout_and_stderr')
self.cdist_object = core.CdistObject(self.cdist_type,
self.local.object_path,
self.local.object_marker_name,
'')
self.cdist_object.create()
self.output_dirs = {
'object': {
'stdout': os.path.join(self.cdist_object.absolute_path,
'stdout'),
'stderr': os.path.join(self.cdist_object.absolute_path,
'stderr'),
},
'init': {
'stdout': os.path.join(self.local.base_path, 'stdout'),
'stderr': os.path.join(self.local.base_path, 'stderr'),
},
}
def tearDown(self):
shutil.rmtree(self.local_dir)
shutil.rmtree(self.remote_dir)
shutil.rmtree(self.temp_dir)
def _test_output(self, which, target, streams=('stdout', 'stderr')):
for stream in streams:
_should = '{0}: {1}\n'.format(which, stream)
stream_path = os.path.join(self.output_dirs[target][stream], which)
with open(stream_path, 'r') as fd:
_is = fd.read()
self.assertEqual(_should, _is)
def test_capture_code_output(self):
self.cdist_object.code_local = self.code.run_gencode_local(
self.cdist_object)
self._test_output('gencode-local', 'object', ('stderr',))
self.code.run_code_local(self.cdist_object)
self._test_output('code-local', 'object')
self.cdist_object.code_remote = self.code.run_gencode_remote(
self.cdist_object)
self._test_output('gencode-remote', 'object', ('stderr',))
self.code.transfer_code_remote(self.cdist_object)
self.code.run_code_remote(self.cdist_object)
self._test_output('code-remote', 'object')
def test_capture_manifest_output(self):
self.manifest.run_type_manifest(self.cdist_object)
self._test_output('manifest', 'object')
def test_capture_init_manifest_output(self):
initial_manifest = os.path.join(conf_dir, 'manifest', 'init')
self.manifest.run_initial_manifest(initial_manifest)
self._test_output('init', 'init')
if __name__ == "__main__":
import unittest
unittest.main()

View file

@ -0,0 +1,4 @@
#!/bin/sh
echo "init: stdout"
echo "init: stderr" >&2

View file

@ -0,0 +1,6 @@
#!/bin/sh
echo "gencode-local: stderr" >&2
echo "echo \"code-local: stdout\""
echo "echo \"code-local: stderr\" >&2"

View file

@ -0,0 +1,6 @@
#!/bin/sh
echo "gencode-remote: stderr" >&2
echo "echo \"code-remote: stdout\""
echo "echo \"code-remote: stderr\" >&2"

View file

@ -0,0 +1,4 @@
#!/bin/sh
echo "manifest: stdout"
echo "manifest: stderr" >&2

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
# 2011-2017 Steven Armstrong (steven-cdist at armstrong.cc)
# 2012-2015 Nico Schottelius (nico-cdist at schottelius.org)
#
# This file is part of cdist.
@ -61,7 +61,9 @@ class CodeTestCase(test.CdistTestCase):
target_host=self.target_host,
remote_exec=remote_exec,
remote_copy=remote_copy,
base_path=self.remote_dir)
base_path=self.remote_dir,
stdout_base_path=self.local.stdout_base_path,
stderr_base_path=self.local.stderr_base_path)
self.remote.create_files_dirs()
self.code = code.Code(self.target_host, self.local, self.remote)
@ -152,6 +154,7 @@ class CodeTestCase(test.CdistTestCase):
self.code.transfer_code_remote(self.cdist_object)
self.code.run_code_remote(self.cdist_object)
if __name__ == '__main__':
import unittest
unittest.main()

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# 2010-2011 Steven Armstrong (steven-cdist at armstrong.cc)
# 2010-2017 Steven Armstrong (steven-cdist at armstrong.cc)
# 2012-2015 Nico Schottelius (nico-cdist at schottelius.org)
# 2014 Daniel Heule (hda at sfs.biz)
#
@ -45,6 +45,19 @@ expected_object_names = sorted([
'__third/moon'])
class CdistObjectErrorContext(object):
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
class ConfigRunTestCase(test.CdistTestCase):
def setUp(self):
@ -87,7 +100,9 @@ class ConfigRunTestCase(test.CdistTestCase):
target_host=self.target_host,
remote_copy=self.remote_copy,
remote_exec=self.remote_exec,
base_path=self.remote_dir)
base_path=self.remote_dir,
stdout_base_path=self.local.stdout_base_path,
stderr_base_path=self.local.stderr_base_path)
self.local.object_path = self.object_base_path
self.local.type_path = type_base_path
@ -102,6 +117,20 @@ class ConfigRunTestCase(test.CdistTestCase):
os.environ = self.orig_environ
shutil.rmtree(self.temp_dir)
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
def test_dependency_resolution(self):
first = self.object_index['__first/man']
second = self.object_index['__second/on-the']
@ -137,29 +166,33 @@ class ConfigRunTestCase(test.CdistTestCase):
first.requirements = [second.name]
second.requirements = [first.name]
with self.assertRaises(cdist.UnresolvableRequirementsError):
self.config.iterate_until_finished()
self.assertRaisesCdistObjectError(
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 = ['__first/not/exist']
with self.assertRaises(cdist.UnresolvableRequirementsError):
self.config.iterate_until_finished()
self.assertRaisesCdistObjectError(
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.InvalidTypeError):
self.config.iterate_until_finished()
self.assertRaisesCdistObjectError(
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()
self.assertRaisesCdistObjectError(
cdist.core.cdist_object.MissingObjectIdError,
self.config.iterate_until_finished)
def test_dryrun(self):
"""Test if the dryrun option is working like expected"""

View file

@ -40,12 +40,24 @@ class RemoteTestCase(test.CdistTestCase):
)
# another temp dir for remote base path
self.base_path = self.mkdtemp()
self.remote = self.create_remote()
def create_remote(self, *args, **kwargs):
if not args:
args = (self.target_host,)
kwargs.setdefault('base_path', self.base_path)
user = getpass.getuser()
remote_exec = "ssh -o User=%s -q" % user
remote_copy = "scp -o User=%s -q" % user
self.remote = remote.Remote(self.target_host, base_path=self.base_path,
remote_exec=remote_exec,
remote_copy=remote_copy)
kwargs.setdefault('remote_exec', 'ssh -o User=%s -q' % user)
kwargs.setdefault('remote_copy', 'scp -o User=%s -q' % user)
if 'stdout_base_path' not in kwargs:
stdout_path = os.path.join(self.temp_dir, 'stdout')
os.makedirs(stdout_path, exist_ok=True)
kwargs['stdout_base_path'] = stdout_path
if 'stderr_base_path' not in kwargs:
stderr_path = os.path.join(self.temp_dir, 'stderr')
os.makedirs(stderr_path, exist_ok=True)
kwargs['stderr_base_path'] = stderr_path
return remote.Remote(*args, **kwargs)
def tearDown(self):
shutil.rmtree(self.temp_dir)
@ -155,8 +167,8 @@ class RemoteTestCase(test.CdistTestCase):
os.chmod(remote_exec_path, 0o755)
remote_exec = remote_exec_path
remote_copy = "echo"
r = remote.Remote(self.target_host, base_path=self.base_path,
remote_exec=remote_exec, remote_copy=remote_copy)
r = self.create_remote(remote_exec=remote_exec,
remote_copy=remote_copy)
self.assertEqual(r.run('true', return_output=True),
"%s\n" % self.target_host[0])
@ -167,8 +179,8 @@ class RemoteTestCase(test.CdistTestCase):
os.chmod(remote_exec_path, 0o755)
remote_exec = remote_exec_path
remote_copy = "echo"
r = remote.Remote(self.target_host, base_path=self.base_path,
remote_exec=remote_exec, remote_copy=remote_copy)
r = self.create_remote(remote_exec=remote_exec,
remote_copy=remote_copy)
handle, script = self.mkstemp(dir=self.temp_dir)
with os.fdopen(handle, "w") as fd:
fd.writelines(["#!/bin/sh\n", "true"])
@ -189,8 +201,8 @@ class RemoteTestCase(test.CdistTestCase):
os.chmod(remote_exec_path, 0o755)
remote_exec = remote_exec_path
remote_copy = "echo"
r = remote.Remote(self.target_host, base_path=self.base_path,
remote_exec=remote_exec, remote_copy=remote_copy)
r = self.create_remote(remote_exec=remote_exec,
remote_copy=remote_copy)
output = r.run_script(script, return_output=True)
self.assertEqual(output, "no_env\n")
@ -202,8 +214,8 @@ class RemoteTestCase(test.CdistTestCase):
env = {
'__object': 'test_object',
}
r = remote.Remote(self.target_host, base_path=self.base_path,
remote_exec=remote_exec, remote_copy=remote_copy)
r = self.create_remote(remote_exec=remote_exec,
remote_copy=remote_copy)
output = r.run_script(script, env=env, return_output=True)
self.assertEqual(output, "test_object\n")

View file

@ -64,7 +64,9 @@ class ExplorerClassTestCase(test.CdistTestCase):
target_host=self.target_host,
remote_exec=self.remote_exec,
remote_copy=self.remote_copy,
base_path=self.remote_base_path)
base_path=self.remote_base_path,
stdout_base_path=self.local.stdout_base_path,
stderr_base_path=self.local.stderr_base_path)
self.remote.create_files_dirs()
self.explorer = explorer.Explorer(

View file

@ -109,6 +109,7 @@ class ManifestTestCase(test.CdistTestCase):
cdist_object = core.CdistObject(cdist_type, self.local.object_path,
self.local.object_marker_name,
'whatever')
cdist_object.create()
handle, output_file = self.mkstemp(dir=self.temp_dir)
os.close(handle)
os.environ['__cdist_test_out'] = output_file