From 217d2bcb23516b3cfe5fa733928db91a6b8fdbca Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Wed, 7 Nov 2012 10:49:51 +0100 Subject: [PATCH] Fix test: StdinTestCase Signed-off-by: Nico Schottelius --- cdist/test/autorequire/__init__.py | 1 - cdist/test/emulator/__init__.py | 69 ++++++++++++------- cdist/test/emulator/fixtures/conf/type/__file | 1 - 3 files changed, 45 insertions(+), 26 deletions(-) delete mode 120000 cdist/test/emulator/fixtures/conf/type/__file diff --git a/cdist/test/autorequire/__init__.py b/cdist/test/autorequire/__init__.py index 4b47b06b..2a647954 100644 --- a/cdist/test/autorequire/__init__.py +++ b/cdist/test/autorequire/__init__.py @@ -42,7 +42,6 @@ class AutorequireTestCase(test.CdistTestCase): def setUp(self): self.orig_environ = os.environ os.environ = os.environ.copy() - self.target_host = 'localhost' self.temp_dir = self.mkdtemp() self.out_dir = os.path.join(self.temp_dir, "out") diff --git a/cdist/test/emulator/__init__.py b/cdist/test/emulator/__init__.py index 26530f44..ec254a21 100644 --- a/cdist/test/emulator/__init__.py +++ b/cdist/test/emulator/__init__.py @@ -20,6 +20,7 @@ # # +import io import os import shutil import string @@ -141,7 +142,6 @@ class ArgumentsTestCase(test.CdistTestCase): def setUp(self): self.temp_dir = self.mkdtemp() - self.target_host = 'localhost' out_path = self.temp_dir handle, self.script = self.mkstemp(dir=self.temp_dir) os.close(handle) @@ -236,38 +236,59 @@ class StdinTestCase(test.CdistTestCase): def setUp(self): self.orig_environ = os.environ os.environ = os.environ.copy() - self.target_host = 'localhost' + self.temp_dir = self.mkdtemp() - os.environ['__cdist_out_dir'] = self.temp_dir + out_path = os.path.join(self.temp_dir, "out") - self.context = cdist.context.Context( + self.local = local.Local( target_host=self.target_host, - remote_copy='scp -o User=root -q', - remote_exec='ssh -o User=root -q', + out_path=out_path, exec_path=test.cdist_exec_path, - debug=False) - self.config = config.Config(self.context) + add_conf_dirs=[conf_dir]) + self.local.create_files_dirs() + + self.manifest = core.Manifest( + target_host=self.target_host, + local = self.local) + def tearDown(self): os.environ = self.orig_environ shutil.rmtree(self.temp_dir) def test_file_from_stdin(self): - handle, destination = self.mkstemp(dir=self.temp_dir) - os.close(handle) - source_handle, source = self.mkstemp(dir=self.temp_dir) - candidates = string.ascii_letters+string.digits - with os.fdopen(source_handle, 'w') as fd: - for x in range(100): - fd.write(''.join(random.sample(candidates, len(candidates)))) + """ + Test whether reading from stdin works + """ - handle, initial_manifest = self.mkstemp(dir=self.temp_dir) - with os.fdopen(handle, 'w') as fd: - fd.write('__file_from_stdin %s --source %s\n' % (destination, source)) - self.context.initial_manifest = initial_manifest - self.config.stage_prepare() + ###################################################################### + # Create string with random content + random_string = str(random.sample(range(1000), 800)) + random_buffer = io.BytesIO(bytes(random_string, 'utf-8')) - cdist_type = core.CdistType(self.config.local.type_path, '__file') - cdist_object = core.CdistObject(cdist_type, self.config.local.object_path, destination) - # Test weither stdin has been stored correctly - self.assertTrue(filecmp.cmp(source, os.path.join(cdist_object.absolute_path, 'stdin'))) + ###################################################################### + # Prepare required args and environment for emulator + type_name = '__file' + object_id = "cdist-test-id" + argv = [type_name, object_id] + + initial_manifest_path = "/cdist-test/path/that/does/not/exist" + env = self.manifest.env_initial_manifest(initial_manifest_path) + + ###################################################################### + # Create path where stdin should reside at + cdist_type = core.CdistType(self.local.type_path, type_name) + cdist_object = core.CdistObject(cdist_type, self.local.object_path, object_id) + 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) diff --git a/cdist/test/emulator/fixtures/conf/type/__file b/cdist/test/emulator/fixtures/conf/type/__file deleted file mode 120000 index 1ed684b9..00000000 --- a/cdist/test/emulator/fixtures/conf/type/__file +++ /dev/null @@ -1 +0,0 @@ -../../../../../conf/type/__file \ No newline at end of file