From df57b5188b3604a06bd5564d261110636505c84c Mon Sep 17 00:00:00 2001
From: Steven Armstrong <steven@icarus.ethz.ch>
Date: Thu, 13 Oct 2011 14:26:36 +0200
Subject: [PATCH] tests for code

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
---
 lib/cdist/test/code/__init__.py               | 132 ++++++++++++++++++
 .../type/__dump_environment/gencode-local     |   8 ++
 .../type/__dump_environment/gencode-remote    |   1 +
 3 files changed, 141 insertions(+)
 create mode 100644 lib/cdist/test/code/__init__.py
 create mode 100755 lib/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-local
 create mode 120000 lib/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-remote

diff --git a/lib/cdist/test/code/__init__.py b/lib/cdist/test/code/__init__.py
new file mode 100644
index 00000000..397922e8
--- /dev/null
+++ b/lib/cdist/test/code/__init__.py
@@ -0,0 +1,132 @@
+# -*- coding: utf-8 -*-
+#
+# 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
+import getpass
+
+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
+
+import os.path as op
+my_dir = op.abspath(op.dirname(__file__))
+fixtures = op.join(my_dir, 'fixtures')
+local_base_path = fixtures
+
+class ExplorerClassTestCase(unittest.TestCase):
+
+    def mkdtemp(self, **kwargs):
+        return tempfile.mkdtemp(prefix='tmp.cdist.test.', **kwargs)
+
+    def mkstemp(self, **kwargs):
+        return tempfile.mkstemp(prefix='tmp.cdist.test.', **kwargs)
+
+    def setUp(self):
+        target_host = 'localhost'
+
+        self.local_base_path = local_base_path
+        self.out_path = self.mkdtemp()
+        self.local = local.Local(target_host, self.local_base_path, self.out_path)
+        self.local.create_directories()
+
+        self.remote_base_path = self.mkdtemp()
+        self.user = getpass.getuser()
+        remote_exec = "ssh -o User=%s -q" % self.user
+        remote_copy = "scp -o User=%s -q" % self.user
+        self.remote = remote.Remote(target_host, self.remote_base_path, remote_exec, remote_copy)
+
+        self.code = code.Code(target_host, self.local, self.remote)
+
+    def tearDown(self):
+        shutil.rmtree(self.out_path)
+        shutil.rmtree(self.remote_base_path)
+
+    def test_run_gencode_local_environment(self):
+        cdist_type = core.Type(self.local.type_path, '__dump_environment')
+        cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever')
+        output_string = self.code.run_gencode_local(cdist_object)
+        output_dict = {}
+        for line in output_string.split('\n'):
+            if line:
+                junk,value = line.split(': ')
+                key = junk.split(' ')[1]
+                output_dict[key] = value
+        self.assertEqual(output_dict['__target_host'], self.local.target_host)
+        self.assertEqual(output_dict['__global'], self.local.out_path)
+        self.assertEqual(output_dict['__type'], cdist_type.absolute_path)
+        self.assertEqual(output_dict['__object'], cdist_object.absolute_path)
+        self.assertEqual(output_dict['__object_id'], cdist_object.object_id)
+        self.assertEqual(output_dict['__object_fq'], cdist_object.path)
+
+    def test_run_gencode_remote_environment(self):
+        cdist_type = core.Type(self.local.type_path, '__dump_environment')
+        cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever')
+        output_string = self.code.run_gencode_remote(cdist_object)
+        output_dict = {}
+        for line in output_string.split('\n'):
+            if line:
+                junk,value = line.split(': ')
+                key = junk.split(' ')[1]
+                output_dict[key] = value
+        self.assertEqual(output_dict['__target_host'], self.local.target_host)
+        self.assertEqual(output_dict['__global'], self.local.out_path)
+        self.assertEqual(output_dict['__type'], cdist_type.absolute_path)
+        self.assertEqual(output_dict['__object'], cdist_object.absolute_path)
+        self.assertEqual(output_dict['__object_id'], cdist_object.object_id)
+        self.assertEqual(output_dict['__object_fq'], cdist_object.path)
+
+
+
+'''
+    def test_list_type_explorer_names(self):
+        cdist_type = core.Type(self.local.type_path, '__test_type')
+        expected = cdist_type.explorers
+        self.assertEqual(self.explorer.list_type_explorer_names(cdist_type), expected)
+
+    def test_transfer_type_explorers(self):
+        cdist_type = core.Type(self.local.type_path, '__test_type')
+        self.explorer.transfer_type_explorers(cdist_type)
+        source = os.path.join(self.local.type_path, cdist_type.explorer_path)
+        destination = os.path.join(self.remote.type_path, cdist_type.explorer_path)
+        self.assertEqual(os.listdir(source), os.listdir(destination))
+
+    def test_transfer_object_parameters(self):
+        cdist_type = core.Type(self.local.type_path, '__test_type')
+        cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever')
+        cdist_object.parameters = {'first': 'first value', 'second': 'second value'}
+        self.explorer.transfer_object_parameters(cdist_object)
+        source = os.path.join(self.local.object_path, cdist_object.parameter_path)
+        destination = os.path.join(self.remote.object_path, cdist_object.parameter_path)
+        self.assertEqual(os.listdir(source), os.listdir(destination))
+
+    def test_run_type_explorer(self):
+        cdist_type = core.Type(self.local.type_path, '__test_type')
+        cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever')
+        self.explorer.transfer_type_explorers(cdist_type)
+        output = self.explorer.run_type_explorer('world', cdist_object)
+        self.assertEqual(output, 'hello\n')
+'''
diff --git a/lib/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-local b/lib/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-local
new file mode 100755
index 00000000..ac292546
--- /dev/null
+++ b/lib/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-local
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+echo "echo __target_host: $__target_host"
+echo "echo __global: $__global"
+echo "echo __type: $__type"
+echo "echo __object: $__object"
+echo "echo __object_id: $__object_id"
+echo "echo __object_fq: $__object_fq"
diff --git a/lib/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-remote b/lib/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-remote
new file mode 120000
index 00000000..7b427cac
--- /dev/null
+++ b/lib/cdist/test/code/fixtures/conf/type/__dump_environment/gencode-remote
@@ -0,0 +1 @@
+gencode-local
\ No newline at end of file