From 9df1c9933e3dc0e0f271f56e5a0281309deac150 Mon Sep 17 00:00:00 2001 From: Jeffrey 'jf' Lim Date: Sun, 31 Jul 2016 04:41:49 +0800 Subject: [PATCH 1/5] Unit tests in place of integration tests for sys.list_{functions,modules} NOTE that while I modify test_doc(), this is in order to make it pass. test_doc() should not be seen as a proper test. Please do a better mock! --- tests/unit/modules/sysmod_test.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tests/unit/modules/sysmod_test.py b/tests/unit/modules/sysmod_test.py index d26f275d72..c89ea1a3ed 100644 --- a/tests/unit/modules/sysmod_test.py +++ b/tests/unit/modules/sysmod_test.py @@ -21,9 +21,24 @@ ensure_in_syspath('../../') # Import Salt Libs from salt.modules import sysmod +modules = set() +functions = [ + 'sys.doc', 'sys.list_functions', 'sys.list_modules', + 'sysctl.get', 'sysctl.show', + 'system.halt', 'system.reboot', + + 'udev.name', 'udev.path', + 'user.add', 'user.info', 'user.rename', +] + sysmod.__salt__ = {} sysmod.__opts__ = {} +for func in functions: + sysmod.__salt__[func] = None + modules.add(func.split('.')[0]) +modules = sorted(list(modules)) + class Mockstate(object): """ @@ -97,7 +112,7 @@ class SysmodTestCase(TestCase): ''' Test if it return the docstrings for all modules. ''' - self.assertDictEqual(sysmod.doc(), {}) + self.assertDictEqual(sysmod.doc(), sysmod.__salt__) # 'state_doc' function tests: 1 @@ -145,9 +160,9 @@ class SysmodTestCase(TestCase): ''' Test if it list the functions for all modules. ''' - self.assertListEqual(sysmod.list_functions(), []) + self.assertListEqual(sysmod.list_functions(), functions) - self.assertListEqual(sysmod.list_functions('sys'), []) + self.assertListEqual(sysmod.list_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules']) # 'list_modules' function tests: 1 @@ -155,9 +170,11 @@ class SysmodTestCase(TestCase): ''' Test if it list the modules loaded on the minion ''' - self.assertListEqual(sysmod.list_modules(), []) + self.assertListEqual(sysmod.list_modules(), modules) - self.assertListEqual(sysmod.list_modules('s*'), []) + self.assertListEqual(sysmod.list_modules('s*'), ['sys', 'sysctl', 'system']) + + self.assertListEqual(sysmod.list_modules('user'), ['user']) # 'reload_modules' function tests: 1 From 84a2512605005f0ebc599994d2f556a0fad6b47d Mon Sep 17 00:00:00 2001 From: Jeffrey 'jf' Lim Date: Sun, 31 Jul 2016 05:09:59 +0800 Subject: [PATCH 2/5] Throw in "sys." to the unit test for sys.list_functions too (source: integration test for sys.list_functions) --- tests/unit/modules/sysmod_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/modules/sysmod_test.py b/tests/unit/modules/sysmod_test.py index c89ea1a3ed..5ce798d48f 100644 --- a/tests/unit/modules/sysmod_test.py +++ b/tests/unit/modules/sysmod_test.py @@ -162,7 +162,8 @@ class SysmodTestCase(TestCase): ''' self.assertListEqual(sysmod.list_functions(), functions) - self.assertListEqual(sysmod.list_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules']) + self.assertListEqual(sysmod.list_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules']) + self.assertListEqual(sysmod.list_functions('sys.'), ['sys.doc', 'sys.list_functions', 'sys.list_modules']) # 'list_modules' function tests: 1 From 8ab96d9b5084703c7ac1391e59233a46f026033c Mon Sep 17 00:00:00 2001 From: Jeffrey 'jf' Lim Date: Sun, 31 Jul 2016 05:50:41 +0800 Subject: [PATCH 3/5] Remove integration tests for sys.list_{functions,modules} --- tests/integration/modules/sysmod.py | 54 ----------------------------- 1 file changed, 54 deletions(-) diff --git a/tests/integration/modules/sysmod.py b/tests/integration/modules/sysmod.py index a98d4ebe94..d33c71a82c 100644 --- a/tests/integration/modules/sysmod.py +++ b/tests/integration/modules/sysmod.py @@ -19,60 +19,6 @@ class SysModuleTest(integration.ModuleCase): ''' Validate the sys module ''' - def test_list_functions(self): - ''' - sys.list_functions - ''' - # Get all functions - funcs = self.run_function('sys.list_functions') - self.assertIn('hosts.list_hosts', funcs) - self.assertIn('pkg.install', funcs) - - # Just sysctl - funcs = self.run_function('sys.list_functions', ('sysctl',)) - self.assertNotIn('sys.doc', funcs) - self.assertIn('sysctl.get', funcs) - - # Just sys - funcs = self.run_function('sys.list_functions', ('sys.',)) - self.assertNotIn('sysctl.get', funcs) - self.assertIn('sys.doc', funcs) - - # Staring with sys - funcs = self.run_function('sys.list_functions', ('sys',)) - self.assertNotIn('sysctl.get', funcs) - self.assertIn('sys.doc', funcs) - - def test_list_modules(self): - ''' - sys.list_modules - ''' - mods = self.run_function('sys.list_modules') - self.assertTrue('hosts' in mods) - self.assertTrue('pkg' in mods) - - def test_list_modules_with_arg_glob(self): - ''' - sys.list_modules u* - - Tests getting the list of modules with 'u*', and looking for the - "user" module - ''' - mods = self.run_function('sys.list_modules', ['u*']) - self.assertNotIn('bigip', mods) - self.assertIn('user', mods) - - def test_list_modules_with_arg_exact_match(self): - ''' - sys.list_modules user - - Tests getting the list of modules looking for the "user" module with - an exact match of 'user' being passed at the CLI instead of something - with '*'. - ''' - mods = self.run_function('sys.list_modules', ['user']) - self.assertEqual(mods, ['user']) - def test_valid_docs(self): ''' Make sure no functions are exposed that don't have valid docstrings From 75816fed9386e31250818ab3898fc8f15c7dbc37 Mon Sep 17 00:00:00 2001 From: Jeffrey 'jf' Lim Date: Tue, 2 Aug 2016 12:06:46 +0800 Subject: [PATCH 4/5] Address linter issues for tests/unit/states/sysmod.py ("Exactly one space required after comma") --- tests/unit/modules/sysmod_test.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/unit/modules/sysmod_test.py b/tests/unit/modules/sysmod_test.py index 5ce798d48f..0f725fd64f 100644 --- a/tests/unit/modules/sysmod_test.py +++ b/tests/unit/modules/sysmod_test.py @@ -23,12 +23,12 @@ from salt.modules import sysmod modules = set() functions = [ - 'sys.doc', 'sys.list_functions', 'sys.list_modules', - 'sysctl.get', 'sysctl.show', + 'sys.doc', 'sys.list_functions', 'sys.list_modules', + 'sysctl.get', 'sysctl.show', 'system.halt', 'system.reboot', - 'udev.name', 'udev.path', - 'user.add', 'user.info', 'user.rename', + 'udev.name', 'udev.path', + 'user.add', 'user.info', 'user.rename', ] sysmod.__salt__ = {} @@ -162,7 +162,7 @@ class SysmodTestCase(TestCase): ''' self.assertListEqual(sysmod.list_functions(), functions) - self.assertListEqual(sysmod.list_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules']) + self.assertListEqual(sysmod.list_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules']) self.assertListEqual(sysmod.list_functions('sys.'), ['sys.doc', 'sys.list_functions', 'sys.list_modules']) # 'list_modules' function tests: 1 From fe87097abdaf39e179530ac56cbcbef823cac15d Mon Sep 17 00:00:00 2001 From: Jeffrey 'jf' Lim Date: Tue, 2 Aug 2016 12:24:22 +0800 Subject: [PATCH 5/5] Round out (unit) test cases for sys.list_{functions,modules} --- tests/unit/modules/sysmod_test.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/unit/modules/sysmod_test.py b/tests/unit/modules/sysmod_test.py index 0f725fd64f..29e2f1f19b 100644 --- a/tests/unit/modules/sysmod_test.py +++ b/tests/unit/modules/sysmod_test.py @@ -162,9 +162,18 @@ class SysmodTestCase(TestCase): ''' self.assertListEqual(sysmod.list_functions(), functions) + self.assertListEqual(sysmod.list_modules('nonexist'), []) + self.assertListEqual(sysmod.list_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules']) + self.assertListEqual(sysmod.list_functions('sys.'), ['sys.doc', 'sys.list_functions', 'sys.list_modules']) + self.assertListEqual(sysmod.list_functions('sys.l'), []) + + self.assertListEqual(sysmod.list_functions('sys.list*'), ['sys.list_functions', 'sys.list_modules']) + + self.assertListEqual(sysmod.list_functions('sys*'), ['sys.doc', 'sys.list_functions', 'sys.list_modules', 'sysctl.get', 'sysctl.show', 'system.halt', 'system.reboot']) + # 'list_modules' function tests: 1 def test_list_modules(self): @@ -173,10 +182,12 @@ class SysmodTestCase(TestCase): ''' self.assertListEqual(sysmod.list_modules(), modules) - self.assertListEqual(sysmod.list_modules('s*'), ['sys', 'sysctl', 'system']) + self.assertListEqual(sysmod.list_modules('nonexist'), []) self.assertListEqual(sysmod.list_modules('user'), ['user']) + self.assertListEqual(sysmod.list_modules('s*'), ['sys', 'sysctl', 'system']) + # 'reload_modules' function tests: 1 def test_reload_modules(self):