From e97854238122a6dfd2db77a96569eb32154d2471 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Tue, 2 Aug 2011 15:20:32 +0200 Subject: [PATCH 1/7] safer quoting Signed-off-by: Steven Armstrong --- conf/type/__user/gencode-remote | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conf/type/__user/gencode-remote b/conf/type/__user/gencode-remote index 31c93655..c4f9123a 100755 --- a/conf/type/__user/gencode-remote +++ b/conf/type/__user/gencode-remote @@ -55,7 +55,7 @@ if grep -q "^${name}:" "$__object/explorer/passwd"; then current_value="$(awk -F: '{ print $ENVIRON["field"] }' < "$file")" if [ "$new_value" != "$current_value" ]; then - set -- "$@" "--$property" \"$new_value\" + set -- "$@" "--$property" \'$new_value\' fi done @@ -67,7 +67,7 @@ if grep -q "^${name}:" "$__object/explorer/passwd"; then else for property in $(ls .); do new_value="$(cat "$property")" - set -- "$@" "--$property" \"$new_value\" + set -- "$@" "--$property" \'$new_value\' done echo useradd "$@" "$name" From 7d290a7755532a66618caa94093db1a040d90492 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 1 Oct 2011 16:34:25 +0200 Subject: [PATCH 2/7] begin to splitup test cases Signed-off-by: Nico Schottelius --- test.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/test.py b/test.py index 5d57fae4..1e0973c5 100755 --- a/test.py +++ b/test.py @@ -142,9 +142,7 @@ class Config(unittest.TestCase): manifest_fd.close() try: - print("a") self.config.run_initial_manifest() - print("b") except cdist.Error: failed = True else: @@ -166,6 +164,30 @@ class UI(unittest.TestCase): for cmd in cdist_commands: self.assertEqual(subprocess.call([cdist_exec_path, "config", "localhost"]), 0) - + +def almost_all_tests(): + suite = unittest.TestSuite([ + unittest.TestLoader().loadTestsFromTestCase(Config), + unittest.TestLoader().loadTestsFromTestCase(Exec)]) + + return suite + +def all_tests(): + suite = unittest.defaultTestLoader + return suite + if __name__ == '__main__': - unittest.main() + result = unittest.TestResult() + # only run some tests, when giving -a -> stuff that usually breaks + if len(sys.argv) >= 2: + if sys.argv[1] == "-a": + suite = all_tests(); + else: + sys.exit(1) + else: + suite = almost_all_tests(); + + # suite.run(result) + # unittest.main() + # unittest.TextTestRunner().run(suite) + From f3bedd8d93b56fc3c2c3aa29b6638ed441f972d9 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 1 Oct 2011 17:25:12 +0200 Subject: [PATCH 3/7] move tests to test/ again, but seperate them now and add ./build.sh test Signed-off-by: Nico Schottelius --- build.sh | 4 ++ test/test_config.py | 101 ++++++++++++++++++++++++++++++++++++++++++++ test/test_exec.py | 80 +++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 test/test_config.py create mode 100755 test/test_exec.py diff --git a/build.sh b/build.sh index d20e0211..24c907e6 100755 --- a/build.sh +++ b/build.sh @@ -126,6 +126,10 @@ case "$1" in | xargs rm -f ;; + test) + python3 -m unittest discover test 'test_*.py' + ;; + *) echo '' echo 'Welcome to cdist!' diff --git a/test/test_config.py b/test/test_config.py new file mode 100644 index 00000000..0632ebcc --- /dev/null +++ b/test/test_config.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# 2011 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 . +# +# + +import os +import sys +import tempfile +import unittest + +sys.path.insert(0, os.path.abspath( + os.path.join(os.path.dirname(os.path.realpath(__file__)), '../lib'))) + +import cdist.config + +cdist_exec_path = os.path.abspath( + os.path.join(os.path.dirname(os.path.realpath(__file__)), "bin/cdist")) + + +class Config(unittest.TestCase): + def setUp(self): + self.temp_dir = tempfile.mkdtemp() + self.init_manifest = os.path.join(self.temp_dir, "manifest") + self.config = cdist.config.Config("localhost", + initial_manifest=self.init_manifest, + exec_path=cdist_exec_path) + self.config.link_emulator() + + def test_initial_manifest_different_parameter(self): + manifest_fd = open(self.init_manifest, "w") + manifest_fd.writelines(["#!/bin/sh\n", + "__file " + self.temp_dir + " --mode 0700\n", + "__file " + self.temp_dir + " --mode 0600\n", + ]) + manifest_fd.close() + + self.assertRaises(cdist.Error, self.config.run_initial_manifest) + + def test_initial_manifest_parameter_added(self): + manifest_fd = open(self.init_manifest, "w") + manifest_fd.writelines(["#!/bin/sh\n", + "__file " + self.temp_dir + '\n', + "__file " + self.temp_dir + " --mode 0600\n", + ]) + manifest_fd.close() + + self.assertRaises(cdist.Error, self.config.run_initial_manifest) + + def test_initial_manifest_parameter_removed(self): + manifest_fd = open(self.init_manifest, "w") + manifest_fd.writelines(["#!/bin/sh\n", + "__file " + self.temp_dir + " --mode 0600\n", + "__file " + self.temp_dir + "\n", + ]) + manifest_fd.close() + + self.assertRaises(cdist.Error, self.config.run_initial_manifest) + + def test_initial_manifest_non_existent_command(self): + manifest_fd = open(self.init_manifest, "w") + manifest_fd.writelines(["#!/bin/sh\n", + "thereisdefinitelynosuchcommend"]) + manifest_fd.close() + + self.assertRaises(cdist.Error, self.config.run_initial_manifest) + + def test_initial_manifest_parameter_twice(self): + manifest_fd = open(self.init_manifest, "w") + manifest_fd.writelines(["#!/bin/sh\n", + "__file " + self.temp_dir + " --mode 0600\n", + "__file " + self.temp_dir + " --mode 0600\n", + ]) + manifest_fd.close() + + try: + self.config.run_initial_manifest() + except cdist.Error: + failed = True + else: + failed = False + + self.assertFalse(failed) + + diff --git a/test/test_exec.py b/test/test_exec.py new file mode 100755 index 00000000..901b5efd --- /dev/null +++ b/test/test_exec.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# 2011 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 . +# +# + + +import os +import sys +import shutil +import subprocess +import tempfile +import unittest + +sys.path.insert(0, os.path.abspath( + os.path.join(os.path.dirname(os.path.realpath(__file__)), '../lib'))) + +import cdist.exec + +class Exec(unittest.TestCase): + def setUp(self): + """Create shell code and co.""" + + self.temp_dir = tempfile.mkdtemp() + self.shell_false = os.path.join(self.temp_dir, "shell_false") + self.shell_true = os.path.join(self.temp_dir, "shell_true") + + true_fd = open(self.shell_true, "w") + true_fd.writelines(["#!/bin/sh\n", "/bin/true"]) + true_fd.close() + + false_fd = open(self.shell_false, "w") + false_fd.writelines(["#!/bin/sh\n", "/bin/false"]) + false_fd.close() + + def tearDown(self): + shutil.rmtree(self.temp_dir) + + def test_local_success_shell(self): + try: + cdist.exec.shell_run_or_debug_fail(self.shell_true, [self.shell_true]) + except cdist.Error: + failed = True + else: + failed = False + + self.assertFalse(failed) + + def test_local_fail_shell(self): + self.assertRaises(cdist.Error, cdist.exec.shell_run_or_debug_fail, + self.shell_false, [self.shell_false]) + + def test_local_success(self): + try: + cdist.exec.run_or_fail(["/bin/true"]) + except cdist.Error: + failed = True + else: + failed = False + + self.assertFalse(failed) + + def test_local_fail(self): + self.assertRaises(cdist.Error, cdist.exec.run_or_fail, ["/bin/false"]) From 390342b324c0b7bca68bdcf381c37bac98c5c326 Mon Sep 17 00:00:00 2001 From: Daniel Roth Date: Sat, 1 Oct 2011 14:02:45 +0200 Subject: [PATCH 4/7] Mac OS X Tutorial --- README | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README b/README index aab4c3a5..2c6465f2 100644 --- a/README +++ b/README @@ -115,6 +115,12 @@ If you want to ensure nothing breaks you must set back the python version to wha eselect python list eselect python list set python3.2 +#### Max OS X + +Ensure you have port installed and configured (http://www.macports.org/install.php). + + port install python32 + ### Get cdist You can clone cdist from git, which gives you the advantage of having From 6425f1d55c86d383464d64e177eb2d28ac544f41 Mon Sep 17 00:00:00 2001 From: Daniel Roth Date: Sat, 1 Oct 2011 14:46:41 +0200 Subject: [PATCH 5/7] Finished Mac OS X installation --- README | 1 + 1 file changed, 1 insertion(+) diff --git a/README b/README index 2c6465f2..3dff690f 100644 --- a/README +++ b/README @@ -120,6 +120,7 @@ If you want to ensure nothing breaks you must set back the python version to wha Ensure you have port installed and configured (http://www.macports.org/install.php). port install python32 + ln -s /opt/local/bin/python3.2 /opt/local/bin/python3 ### Get cdist From df727a6b6bf52bd35c820480640ec651c3ede841 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 2 Oct 2011 15:52:19 +0200 Subject: [PATCH 6/7] remove everything but ui from ui test Signed-off-by: Nico Schottelius --- test.py | 193 ------------------------------------------------ test/nico_ui.py | 41 ++++++++++ 2 files changed, 41 insertions(+), 193 deletions(-) delete mode 100755 test.py create mode 100755 test/nico_ui.py diff --git a/test.py b/test.py deleted file mode 100755 index 1e0973c5..00000000 --- a/test.py +++ /dev/null @@ -1,193 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -# -# 2011 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 . -# -# - - -import os -import sys -import shutil -import subprocess -import tempfile -import unittest - -sys.path.insert(0, os.path.abspath( - os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))) - -cdist_exec_path = os.path.abspath( - os.path.join(os.path.dirname(os.path.realpath(__file__)), "bin/cdist")) - -cdist_commands=["banner", "config", "install"] - -import cdist -import cdist.config -import cdist.exec - -class Exec(unittest.TestCase): - def setUp(self): - """Create shell code and co.""" - - self.temp_dir = tempfile.mkdtemp() - self.shell_false = os.path.join(self.temp_dir, "shell_false") - self.shell_true = os.path.join(self.temp_dir, "shell_true") - - true_fd = open(self.shell_true, "w") - true_fd.writelines(["#!/bin/sh\n", "/bin/true"]) - true_fd.close() - - false_fd = open(self.shell_false, "w") - false_fd.writelines(["#!/bin/sh\n", "/bin/false"]) - false_fd.close() - - def tearDown(self): - shutil.rmtree(self.temp_dir) - - def test_local_success_shell(self): - try: - cdist.exec.shell_run_or_debug_fail(self.shell_true, [self.shell_true]) - except cdist.Error: - failed = True - else: - failed = False - - self.assertFalse(failed) - - def test_local_fail_shell(self): - self.assertRaises(cdist.Error, cdist.exec.shell_run_or_debug_fail, - self.shell_false, [self.shell_false]) - - def test_local_success(self): - try: - cdist.exec.run_or_fail(["/bin/true"]) - except cdist.Error: - failed = True - else: - failed = False - - self.assertFalse(failed) - - def test_local_fail(self): - self.assertRaises(cdist.Error, cdist.exec.run_or_fail, ["/bin/false"]) - -class Config(unittest.TestCase): - def setUp(self): - self.temp_dir = tempfile.mkdtemp() - self.init_manifest = os.path.join(self.temp_dir, "manifest") - self.config = cdist.config.Config("localhost", - initial_manifest=self.init_manifest, - exec_path=cdist_exec_path) - self.config.link_emulator() - - def test_initial_manifest_different_parameter(self): - manifest_fd = open(self.init_manifest, "w") - manifest_fd.writelines(["#!/bin/sh\n", - "__file " + self.temp_dir + " --mode 0700\n", - "__file " + self.temp_dir + " --mode 0600\n", - ]) - manifest_fd.close() - - self.assertRaises(cdist.Error, self.config.run_initial_manifest) - - def test_initial_manifest_parameter_added(self): - manifest_fd = open(self.init_manifest, "w") - manifest_fd.writelines(["#!/bin/sh\n", - "__file " + self.temp_dir + '\n', - "__file " + self.temp_dir + " --mode 0600\n", - ]) - manifest_fd.close() - - self.assertRaises(cdist.Error, self.config.run_initial_manifest) - - def test_initial_manifest_parameter_removed(self): - manifest_fd = open(self.init_manifest, "w") - manifest_fd.writelines(["#!/bin/sh\n", - "__file " + self.temp_dir + " --mode 0600\n", - "__file " + self.temp_dir + "\n", - ]) - manifest_fd.close() - - self.assertRaises(cdist.Error, self.config.run_initial_manifest) - - def test_initial_manifest_non_existent_command(self): - manifest_fd = open(self.init_manifest, "w") - manifest_fd.writelines(["#!/bin/sh\n", - "thereisdefinitelynosuchcommend"]) - manifest_fd.close() - - self.assertRaises(cdist.Error, self.config.run_initial_manifest) - - def test_initial_manifest_parameter_twice(self): - manifest_fd = open(self.init_manifest, "w") - manifest_fd.writelines(["#!/bin/sh\n", - "__file " + self.temp_dir + " --mode 0600\n", - "__file " + self.temp_dir + " --mode 0600\n", - ]) - manifest_fd.close() - - try: - self.config.run_initial_manifest() - except cdist.Error: - failed = True - else: - failed = False - - self.assertFalse(failed) - - -class UI(unittest.TestCase): - def test_banner(self): - self.assertEqual(subprocess.call([cdist_exec_path, "banner"]), 0) - - def test_help(self): - for cmd in cdist_commands: - self.assertEqual(subprocess.call([cdist_exec_path, cmd, "-h"]), 0) - - # FIXME: mockup needed - def test_config_localhost(self): - for cmd in cdist_commands: - self.assertEqual(subprocess.call([cdist_exec_path, "config", "localhost"]), 0) - - -def almost_all_tests(): - suite = unittest.TestSuite([ - unittest.TestLoader().loadTestsFromTestCase(Config), - unittest.TestLoader().loadTestsFromTestCase(Exec)]) - - return suite - -def all_tests(): - suite = unittest.defaultTestLoader - return suite - -if __name__ == '__main__': - result = unittest.TestResult() - # only run some tests, when giving -a -> stuff that usually breaks - if len(sys.argv) >= 2: - if sys.argv[1] == "-a": - suite = all_tests(); - else: - sys.exit(1) - else: - suite = almost_all_tests(); - - # suite.run(result) - # unittest.main() - # unittest.TextTestRunner().run(suite) - diff --git a/test/nico_ui.py b/test/nico_ui.py new file mode 100755 index 00000000..4e5b1a17 --- /dev/null +++ b/test/nico_ui.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# 2011 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 . +# +# + + +import subprocess + +cdist_exec_path = os.path.abspath( + os.path.join(os.path.dirname(os.path.realpath(__file__)), "bin/cdist")) + +class UI(unittest.TestCase): + def test_banner(self): + self.assertEqual(subprocess.call([cdist_exec_path, "banner"]), 0) + + def test_help(self): + for cmd in cdist_commands: + self.assertEqual(subprocess.call([cdist_exec_path, cmd, "-h"]), 0) + + # FIXME: mockup needed + def test_config_localhost(self): + for cmd in cdist_commands: + self.assertEqual(subprocess.call([cdist_exec_path, "config", "localhost"]), 0) + From cadb4fa852870f872fc65ea7e85680406eff20a5 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 2 Oct 2011 15:56:27 +0200 Subject: [PATCH 7/7] add test all and make ui tests work again Signed-off-by: Nico Schottelius --- build.sh | 4 ++++ test/nico_ui.py | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 24c907e6..021fb480 100755 --- a/build.sh +++ b/build.sh @@ -130,6 +130,10 @@ case "$1" in python3 -m unittest discover test 'test_*.py' ;; + test-all) + python3 -m unittest discover test '*.py' + ;; + *) echo '' echo 'Welcome to cdist!' diff --git a/test/nico_ui.py b/test/nico_ui.py index 4e5b1a17..8ce98043 100755 --- a/test/nico_ui.py +++ b/test/nico_ui.py @@ -21,10 +21,14 @@ # +import os import subprocess +import unittest + +cdist_commands=["banner", "config", "install"] cdist_exec_path = os.path.abspath( - os.path.join(os.path.dirname(os.path.realpath(__file__)), "bin/cdist")) + os.path.join(os.path.dirname(os.path.realpath(__file__)), "../bin/cdist")) class UI(unittest.TestCase): def test_banner(self):