From a946b5cf59c4e8d139f014f9166e44a0bfcb5d83 Mon Sep 17 00:00:00 2001
From: Steven Armstrong <steven@icarus.ethz.ch>
Date: Tue, 18 Oct 2011 14:23:06 +0200
Subject: [PATCH] verify manifest environment

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
---
 lib/cdist/test/manifest/__init__.py           | 41 +++++++++++++++++++
 .../fixtures/conf/manifest/dump_environment   | 12 +++---
 .../conf/type/__dump_environment/manifest     | 19 +++++----
 3 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/lib/cdist/test/manifest/__init__.py b/lib/cdist/test/manifest/__init__.py
index efda5b0a..f091e494 100644
--- a/lib/cdist/test/manifest/__init__.py
+++ b/lib/cdist/test/manifest/__init__.py
@@ -27,6 +27,8 @@ import shutil
 import string
 import random
 import logging
+import io
+import sys
 
 import cdist
 from cdist.exec import local
@@ -48,6 +50,8 @@ class ManifestTestCase(unittest.TestCase):
         return tempfile.mkstemp(prefix='tmp.cdist.test.', **kwargs)
 
     def setUp(self):
+        self.orig_environ = os.environ
+        os.environ = os.environ.copy()
         self.temp_dir = self.mkdtemp()
         self.target_host = 'localhost'
         out_path = self.temp_dir
@@ -58,17 +62,54 @@ class ManifestTestCase(unittest.TestCase):
         self.log = logging.getLogger(self.target_host)
 
     def tearDown(self):
+        os.environ = self.orig_environ
         shutil.rmtree(self.temp_dir)
 
     def test_initial_manifest_environment(self):
         initial_manifest = os.path.join(self.local.manifest_path, "dump_environment")
+        handle, output_file = self.mkstemp(dir=self.temp_dir)
+        os.environ['__cdist_test_out'] = output_file
         self.manifest.run_initial_manifest(initial_manifest)
 
+        with open(output_file, 'r') as fd:
+            output_string = fd.read()
+        print("output_string: %s" % output_string)
+        output_dict = {}
+        for line in output_string.split('\n'):
+            if line:
+                key,value = line.split(': ')
+                output_dict[key] = value
+        self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path))
+        self.assertEqual(output_dict['__target_host'], self.local.target_host)
+        self.assertEqual(output_dict['__global'], self.local.out_path)
+        self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_path)
+        self.assertEqual(output_dict['__manifest'], self.local.manifest_path)
+
     def test_type_manifest_environment(self):
         cdist_type = core.Type(self.local.type_path, '__dump_environment')
         cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever')
+        handle, output_file = self.mkstemp(dir=self.temp_dir)
+        os.environ['__cdist_test_out'] = output_file
         self.manifest.run_type_manifest(cdist_object)
 
+        with open(output_file, 'r') as fd:
+            output_string = fd.read()
+        print("output_string: %s" % output_string)
+        output_dict = {}
+        for line in output_string.split('\n'):
+            if line:
+                key,value = line.split(': ')
+                output_dict[key] = value
+        self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path))
+        self.assertEqual(output_dict['__target_host'], self.local.target_host)
+        self.assertEqual(output_dict['__global'], self.local.out_path)
+        self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_path)
+        self.assertEqual(output_dict['__type'], cdist_type.absolute_path)
+        self.assertEqual(output_dict['__object'], cdist_object.absolute_path)
+        self.assertEqual(output_dict['__self'], cdist_object.path)
+        self.assertEqual(output_dict['__object_id'], cdist_object.object_id)
+        self.assertEqual(output_dict['__object_fq'], cdist_object.path)
+
     def test_debug_env_setup(self):
         self.log.setLevel(logging.DEBUG)
         manifest = cdist.core.manifest.Manifest(self.target_host, self.local)
diff --git a/lib/cdist/test/manifest/fixtures/conf/manifest/dump_environment b/lib/cdist/test/manifest/fixtures/conf/manifest/dump_environment
index 1abe7755..7ce983ab 100755
--- a/lib/cdist/test/manifest/fixtures/conf/manifest/dump_environment
+++ b/lib/cdist/test/manifest/fixtures/conf/manifest/dump_environment
@@ -1,7 +1,9 @@
 #!/bin/sh
 
-echo "PATH: $PATH"
-echo "__target_host: $__target_host"
-echo "__global: $__global"
-echo "__cdist_type_base_path: $__cdist_type_base_path"
-echo "__manifest: $__manifest"
+cat > $__cdist_test_out << DONE
+PATH: $PATH
+__target_host: $__target_host
+__global: $__global
+__cdist_type_base_path: $__cdist_type_base_path
+__manifest: $__manifest
+DONE
diff --git a/lib/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest b/lib/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest
index 92f533a8..212de64d 100755
--- a/lib/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest
+++ b/lib/cdist/test/manifest/fixtures/conf/type/__dump_environment/manifest
@@ -1,10 +1,13 @@
 #!/bin/sh
 
-echo "PATH: $PATH"
-echo "__target_host: $__target_host"
-echo "__global: $__global"
-echo "__cdist_type_base_path: $__cdist_type_base_path"
-echo "__type: $__type"
-echo "__object: $__object"
-echo "__object_id: $__object_id"
-echo "__object_fq: $__object_fq"
+cat > $__cdist_test_out << DONE
+PATH: $PATH
+__target_host: $__target_host
+__global: $__global
+__cdist_type_base_path: $__cdist_type_base_path
+__type: $__type
+__self: $__self
+__object: $__object
+__object_id: $__object_id
+__object_fq: $__object_fq
+DONE