def setUpClass(cls): m = MagicMock() cls.service_mock = MagicMock() cls.service_mock.__class__ = MagicMock() cls.service_lbmonitor_binding_mock = MagicMock() cls.lbmonitor_service_binding_mock = MagicMock() nssrc_modules_mock = { 'nssrc.com.citrix.netscaler.nitro.resource.config.basic': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.basic.service': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.basic.service.service': cls.service_mock, 'nssrc.com.citrix.netscaler.nitro.resource.config.basic.service_lbmonitor_binding': cls.service_lbmonitor_binding_mock, 'nssrc.com.citrix.netscaler.nitro.resource.config.basic.service_lbmonitor_binding.service_lbmonitor_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.lb': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_service_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_service_binding.lbmonitor_service_binding': cls.lbmonitor_service_binding_mock, } cls.nitro_specific_patcher = patch.dict(sys.modules, nssrc_modules_mock) cls.nitro_base_patcher = nitro_base_patcher
def test_validate_runnable_exists(self, *args): with patch.dict( 'sys.modules', **{ 'vdirect_client': self.module_mock, 'vdirect_client.rest_client': self.module_mock, }): from ansible.modules.network.radware import vdirect_runnable Runnable.set_runnable_objects_result(RUNNABLE_OBJECTS_RESULT) BASE_PARAMS.update(RUNNABLE_PARAMS) vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) vdirectRunnable._validate_runnable_exists() assert True BASE_PARAMS.update(RUNNABLE_PARAMS) BASE_PARAMS['runnable_name'] = "missing" vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) try: vdirectRunnable._validate_runnable_exists() self.fail( "MissingRunnableException was not thrown for missing runnable name" ) except vdirect_runnable.MissingRunnableException: assert True
def test_validate_action_name(self, *args): with patch.dict('sys.modules', **{ 'vdirect_client': self.module_mock, 'vdirect_client.rest_client': self.module_mock, }): from ansible.modules.network.radware import vdirect_runnable Runnable.set_runnable_objects_result(RUNNABLE_OBJECTS_RESULT) BASE_PARAMS.update(RUNNABLE_PARAMS) vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable._validate_action_name() assert vdirectRunnable.action_name == vdirect_runnable.VdirectRunnable.RUN_ACTION BASE_PARAMS['runnable_type'] = vdirect_runnable.WORKFLOW_TEMPLATE_RUNNABLE_TYPE vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable._validate_action_name() assert vdirectRunnable.action_name == vdirect_runnable.VdirectRunnable.CREATE_WORKFLOW_ACTION BASE_PARAMS['runnable_type'] = vdirect_runnable.WORKFLOW_RUNNABLE_TYPE BASE_PARAMS['action_name'] = 'a' vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) Runnable.set_available_actions_result(AVAILABLE_ACTIONS_RESULT) vdirectRunnable._validate_action_name() assert vdirectRunnable.action_name == 'a' BASE_PARAMS['action_name'] = 'c' vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) Runnable.set_available_actions_result(AVAILABLE_ACTIONS_RESULT) try: vdirectRunnable._validate_action_name() self.fail("WrongActionNameException was not thrown for wrong action name") except vdirect_runnable.WrongActionNameException: assert True
def setUpClass(cls): class MockException(Exception): pass cls.MockException = MockException m = MagicMock() cls.cs_vserver_mock = MagicMock() cls.cs_vserver_mock.__class__ = MagicMock(add=Mock()) nssrc_modules_mock = { 'nssrc.com.citrix.netscaler.nitro.resource.config.cs': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.cs.csvserver': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.cs.csvserver.csvserver': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.cs.csvserver_cspolicy_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.cs.csvserver_cspolicy_binding.csvserver_cspolicy_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.cs.csvserver_lbvserver_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.cs.csvserver_lbvserver_binding.csvserver_lbvserver_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.ssl': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.ssl.sslvserver_sslcertkey_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.ssl.sslvserver_sslcertkey_binding.sslvserver_sslcertkey_binding': m, } cls.nitro_specific_patcher = patch.dict(sys.modules, nssrc_modules_mock) cls.nitro_base_patcher = nitro_base_patcher
def setUpClass(cls): class MockException(Exception): pass cls.MockException = MockException m = MagicMock() nssrc_modules_mock = { 'nssrc.com.citrix.netscaler.nitro.resource.config.gslb': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.gslb.gslbvserver': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.gslb.gslbvserver.gslbvserver': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.gslb.gslbvserver_gslbservice_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.gslb.gslbvserver_gslbservice_binding.gslbvserver_gslbservice_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.gslb.gslbvserver_domain_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.gslb.gslbvserver_domain_binding.gslbvserver_domain_binding': m, } cls.nitro_specific_patcher = patch.dict(sys.modules, nssrc_modules_mock) cls.nitro_base_patcher = nitro_base_patcher
def test_module_utils_basic_ansible_module_selinux_default_context(self): from ansible.module_utils import basic basic._ANSIBLE_ARGS = None am = basic.AnsibleModule( argument_spec=dict(), ) am.selinux_initial_context = MagicMock(return_value=[None, None, None, None]) am.selinux_enabled = MagicMock(return_value=True) # we first test the cases where the python selinux lib is not installed basic.HAVE_SELINUX = False self.assertEqual(am.selinux_default_context(path='/foo/bar'), [None, None, None, None]) # all following tests assume the python selinux bindings are installed basic.HAVE_SELINUX = True basic.selinux = Mock() with patch.dict('sys.modules', {'selinux': basic.selinux}): # next, we test with a mocked implementation of selinux.matchpathcon to simulate # an actual context being found with patch('selinux.matchpathcon', return_value=[0, 'unconfined_u:object_r:default_t:s0']): self.assertEqual(am.selinux_default_context(path='/foo/bar'), ['unconfined_u', 'object_r', 'default_t', 's0']) # we also test the case where matchpathcon returned a failure with patch('selinux.matchpathcon', return_value=[-1, '']): self.assertEqual(am.selinux_default_context(path='/foo/bar'), [None, None, None, None]) # finally, we test where an OSError occurred during matchpathcon's call with patch('selinux.matchpathcon', side_effect=OSError): self.assertEqual(am.selinux_default_context(path='/foo/bar'), [None, None, None, None]) delattr(basic, 'selinux')
def test_module_utils_basic_ansible_module_selinux_enabled(self): from ansible.module_utils import basic basic._ANSIBLE_ARGS = None am = basic.AnsibleModule( argument_spec=dict(), ) # we first test the cases where the python selinux lib is # not installed, which has two paths: one in which the system # does have selinux installed (and the selinuxenabled command # is present and returns 0 when run), or selinux is not installed basic.HAVE_SELINUX = False am.get_bin_path = MagicMock() am.get_bin_path.return_value = '/path/to/selinuxenabled' am.run_command = MagicMock() am.run_command.return_value = (0, '', '') self.assertRaises(SystemExit, am.selinux_enabled) am.get_bin_path.return_value = None self.assertEqual(am.selinux_enabled(), False) # finally we test the case where the python selinux lib is installed, # and both possibilities there (enabled vs. disabled) basic.HAVE_SELINUX = True basic.selinux = Mock() with patch.dict('sys.modules', {'selinux': basic.selinux}): with patch('selinux.is_selinux_enabled', return_value=0): self.assertEqual(am.selinux_enabled(), False) with patch('selinux.is_selinux_enabled', return_value=1): self.assertEqual(am.selinux_enabled(), True) delattr(basic, 'selinux')
def test_validate_workflow_template_action_name(self, *args): with patch.dict('sys.modules', **{ 'vdirect_client': self.module_mock, 'vdirect_client.rest_client': self.module_mock, }): from ansible.modules.network.radware import vdirect_runnable Runnable.set_available_actions_result(ACTIONS_RESULT) BASE_PARAMS.update(WORKFLOW_TEMPLATE_RUNNABLE_PARAMS) vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable._validate_action_name() assert vdirectRunnable.action_name == vdirect_runnable.VdirectRunnable.CREATE_WORKFLOW_ACTION
def setUpClass(cls): class MockException(Exception): pass cls.MockException = MockException m = MagicMock() nssrc_modules_mock = { 'nssrc.com.citrix.netscaler.nitro.resource.config.cs': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.cs.cspolicy': m, } cls.nitro_specific_patcher = patch.dict(sys.modules, nssrc_modules_mock) cls.nitro_base_patcher = nitro_base_patcher
def test_missing_parameter(self, *args): with patch.dict('sys.modules', **{ 'vdirect_client': self.module_mock, 'vdirect_client.rest_client': self.module_mock, }): from ansible.modules.network.radware import vdirect_runnable try: params = BASE_PARAMS.copy() vdirect_runnable.VdirectRunnable(params) self.fail("KeyError was not thrown for missing parameter") except KeyError: assert True
def test_commit(self, *args): with patch.dict( 'sys.modules', **{ 'vdirect_client': self.module_mock, 'vdirect_client.rest_client.RestClient': self.module_mock, }): from ansible.modules.network.radware import vdirect_commit BASE_PARAMS.update(COMMIT_PARAMS) vdirectcommit = vdirect_commit.VdirectCommit(BASE_PARAMS) vdirectcommit.client.adc = DeviceMock('adc', vdirectcommit.client) vdirectcommit.client.container = DeviceMock( 'vx', vdirectcommit.client) vdirectcommit.client.appWall = DeviceMock('appwall', vdirectcommit.client) vdirectcommit.client.defensePro = DeviceMock( 'defensepro', vdirectcommit.client) res = vdirectcommit.commit() assert res == MODULE_RESULT vdirectcommit.sync = False for detail in MODULE_RESULT['details']: if 'sync' in detail: detail['sync'] = vdirect_commit.NOT_PERFORMED res = vdirectcommit.commit() assert res == MODULE_RESULT vdirectcommit.client.adc.control_result = COMMIT_RESULT_204 vdirectcommit.client.adc.control_result[ self.module_mock.rest_client.RESP_STATUS] = 500 vdirectcommit.client.adc.control_result[ self.module_mock.rest_client.RESP_STR] = 'Some Failure' MODULE_RESULT[ 'msg'] = 'Failure occurred while performing requested actions on devices. See details' for detail in MODULE_RESULT['details']: if detail['device_name'] == 'adc': detail['apply'] = vdirect_commit.FAILED detail['failure_description'] = 'Some Failure' detail['save'] = vdirect_commit.NOT_PERFORMED detail['sync'] = vdirect_commit.NOT_PERFORMED res = vdirectcommit.commit() assert res == MODULE_RESULT vdirectcommit.client.adc.throw_exception(control_throw=True) for detail in MODULE_RESULT['details']: if detail['device_name'] == 'adc': detail['failure_description'] = 'Exception occurred while performing apply action. ' \ 'Exception: exception message' res = vdirectcommit.commit() assert res == MODULE_RESULT
def test_validate_workflow_template_exists(self, *args): with patch.dict('sys.modules', **{ 'vdirect_client': self.module_mock, 'vdirect_client.rest_client': self.module_mock, }): from ansible.modules.network.radware import vdirect_runnable Catalog.set_catalog_item_200() BASE_PARAMS.update(WORKFLOW_TEMPLATE_RUNNABLE_PARAMS) vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) vdirectRunnable.client.catalog = Catalog(vdirectRunnable.client) vdirectRunnable._validate_runnable_exists() assert True
def test_validate_required_action_params(self, *args): with patch.dict('sys.modules', **{ 'vdirect_client': self.module_mock, 'vdirect_client.rest_client': self.module_mock, }): from ansible.modules.network.radware import vdirect_runnable Runnable.set_action_info_result(ACTIONS_PARAMS_RESULT_BASIC) BASE_PARAMS.update(CONFIGURATION_TEMPLATE_RUNNABLE_PARAMS) vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) try: vdirectRunnable._validate_required_action_params() self.fail("MissingActionParametersException was not thrown for missing parameters") except vdirect_runnable.MissingActionParametersException: assert True BASE_PARAMS['parameters'] = {"alteon": "x"} vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) try: vdirectRunnable._validate_required_action_params() self.fail("MissingActionParametersException was not thrown for missing parameters") except vdirect_runnable.MissingActionParametersException: assert True BASE_PARAMS['parameters'] = {"pin": "x", "alteon": "a1"} vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable._validate_action_name() vdirectRunnable._validate_required_action_params() assert True Runnable.set_action_info_result(ACTIONS_PARAMS_RESULT_FULL) vdirectRunnable._validate_action_name() try: vdirectRunnable._validate_required_action_params() self.fail("MissingActionParametersException was not thrown for missing parameters") except vdirect_runnable.MissingActionParametersException: assert True BASE_PARAMS['parameters'].update( {"alteon_array": "[a1, a2]", "dp": "dp1", "dp_array": "[dp1, dp2]", "appWall": "appWall1", "appWall_array": "[appWall1, appWall2]" }) vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable._validate_action_name() vdirectRunnable._validate_required_action_params() assert True
def test_validate_devices(self, *args): with patch.dict( 'sys.modules', **{ 'vdirect_client': self.module_mock, 'vdirect_client.rest_client.RestClient': self.module_mock, }): from ansible.modules.network.radware import vdirect_commit BASE_PARAMS.update(COMMIT_PARAMS) vdirectcommit = vdirect_commit.VdirectCommit(BASE_PARAMS) vdirectcommit.client.adc = DeviceMock('adc', vdirectcommit.client) vdirectcommit.client.container = DeviceMock( 'vx', vdirectcommit.client) vdirectcommit.client.appWall = DeviceMock('appwall', vdirectcommit.client) vdirectcommit.client.defensePro = DeviceMock( 'defensepro', vdirectcommit.client) vdirectcommit._validate_devices() assert True vdirectcommit.client.adc.throw_exception(True) try: vdirectcommit._validate_devices() self.fail( "CommitException was not thrown for device communication failure" ) except vdirect_commit.CommitException: assert True vdirectcommit.client.adc.throw_exception(False) vdirectcommit.client.defensePro.throw_exception(True) try: vdirectcommit._validate_devices() self.fail( "CommitException was not thrown for device communication failure" ) except vdirect_commit.CommitException: assert True vdirectcommit.client.defensePro.throw_exception(False) vdirectcommit.client.adc.name = 'wrong' try: vdirectcommit._validate_devices() self.fail( "MissingDeviceException was not thrown for missing device") except vdirect_commit.MissingDeviceException: assert True
def test_run(self, *args): with patch.dict('sys.modules', **{ 'vdirect_client': self.module_mock, 'vdirect_client.rest_client': self.module_mock, }): from ansible.modules.network.radware import vdirect_runnable Catalog.set_catalog_item_200() BASE_PARAMS.update(CONFIGURATION_TEMPLATE_RUNNABLE_PARAMS) Runnable.set_available_actions_result(ACTIONS_RESULT) Runnable.set_action_info_result(ACTIONS_PARAMS_RESULT_BASIC) BASE_PARAMS['parameters'] = {"pin": "x", "alteon": "x"} vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) Runnable.set_run_result(RUN_RESULT) res = vdirectRunnable.run() assert res['msg'] == MODULE_RESULT['msg'] result_parameters = {"param1": "value1", "param2": "value2"} RUN_RESULT[self.module_mock.rest_client.RESP_DATA]['parameters'] = result_parameters MODULE_RESULT['parameters'] = result_parameters res = vdirectRunnable.run() assert res['msg'] == MODULE_RESULT['msg'] assert res['output']['parameters'] == result_parameters RUN_RESULT[self.module_mock.rest_client.RESP_DATA]['status'] = 404 vdirectRunnable.run() assert res['msg'] == MODULE_RESULT['msg'] RUN_RESULT[self.module_mock.rest_client.RESP_STATUS] = 400 RUN_RESULT[self.module_mock.rest_client.RESP_REASON] = "Reason" RUN_RESULT[self.module_mock.rest_client.RESP_STR] = "Details" try: vdirectRunnable.run() self.fail("RunnableException was not thrown for failed run.") except vdirect_runnable.RunnableException as e: assert str(e) == "Reason: Reason. Details:Details." RUN_RESULT[self.module_mock.rest_client.RESP_STATUS] = 200 RUN_RESULT[self.module_mock.rest_client.RESP_DATA]["status"] = 400 RUN_RESULT[self.module_mock.rest_client.RESP_DATA]["success"] = False RUN_RESULT[self.module_mock.rest_client.RESP_DATA]["exception"] = {"message": "exception message"} try: vdirectRunnable.run() self.fail("RunnableException was not thrown for failed run.") except vdirect_runnable.RunnableException as e: assert str(e) == "Reason: exception message. Details:Details."
def test_module_utils_basic_ansible_module_selinux_mls_enabled(self): from ansible.module_utils import basic basic._ANSIBLE_ARGS = None am = basic.AnsibleModule(argument_spec=dict(), ) basic.HAVE_SELINUX = False self.assertEqual(am.selinux_mls_enabled(), False) basic.HAVE_SELINUX = True basic.selinux = Mock() with patch.dict('sys.modules', {'selinux': basic.selinux}): with patch('selinux.is_selinux_mls_enabled', return_value=0): self.assertEqual(am.selinux_mls_enabled(), False) with patch('selinux.is_selinux_mls_enabled', return_value=1): self.assertEqual(am.selinux_mls_enabled(), True) delattr(basic, 'selinux')
def test_validate_required_action_params(self, *args): with patch.dict( 'sys.modules', **{ 'vdirect_client': self.module_mock, 'vdirect_client.rest_client': self.module_mock, }): from ansible.modules.network.radware import vdirect_runnable Runnable.set_runnable_objects_result(RUNNABLE_OBJECTS_RESULT) BASE_PARAMS.update(RUNNABLE_PARAMS) BASE_PARAMS[ 'runnable_type'] = vdirect_runnable.WORKFLOW_RUNNABLE_TYPE BASE_PARAMS['action_name'] = 'a' BASE_PARAMS['parameters'] = {"alteon": "x"} vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) Runnable.set_available_actions_result(AVAILABLE_ACTIONS_RESULT) Runnable.set_action_info_result(ACTIONS_PARAMS_RESULT) vdirectRunnable._validate_action_name() try: vdirectRunnable._validate_required_action_params() self.fail( "MissingActionParametersException was not thrown for missing parameters" ) except vdirect_runnable.MissingActionParametersException: assert True BASE_PARAMS['parameters'] = {"alteon": "x"} vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable._validate_action_name() try: vdirectRunnable._validate_required_action_params() self.fail( "MissingActionParametersException was not thrown for missing parameters" ) except vdirect_runnable.MissingActionParametersException: assert True BASE_PARAMS['parameters'] = {"pin": "x", "alteon": "x"} vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable._validate_action_name() vdirectRunnable._validate_required_action_params() assert True
def test_module_utils_basic_ansible_module_set_context_if_different(self): from ansible.module_utils import basic basic._ANSIBLE_ARGS = None am = basic.AnsibleModule( argument_spec=dict(), ) basic.HAVE_SELINUX = False am.selinux_enabled = MagicMock(return_value=False) self.assertEqual(am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], True), True) self.assertEqual(am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False), False) basic.HAVE_SELINUX = True am.selinux_enabled = MagicMock(return_value=True) am.selinux_context = MagicMock(return_value=['bar_u', 'bar_r', None, None]) am.is_special_selinux_path = MagicMock(return_value=(False, None)) basic.selinux = Mock() with patch.dict('sys.modules', {'selinux': basic.selinux}): with patch('selinux.lsetfilecon', return_value=0) as m: self.assertEqual(am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False), True) m.assert_called_with('/path/to/file', 'foo_u:foo_r:foo_t:s0') m.reset_mock() am.check_mode = True self.assertEqual(am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False), True) self.assertEqual(m.called, False) am.check_mode = False with patch('selinux.lsetfilecon', return_value=1) as m: self.assertRaises(SystemExit, am.set_context_if_different, '/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], True) with patch('selinux.lsetfilecon', side_effect=OSError) as m: self.assertRaises(SystemExit, am.set_context_if_different, '/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], True) am.is_special_selinux_path = MagicMock(return_value=(True, ['sp_u', 'sp_r', 'sp_t', 's0'])) with patch('selinux.lsetfilecon', return_value=0) as m: self.assertEqual(am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False), True) m.assert_called_with('/path/to/file', 'sp_u:sp_r:sp_t:s0') delattr(basic, 'selinux')
def setUpClass(cls): class MockException(Exception): pass cls.MockException = MockException m = MagicMock() cls.server_mock = MagicMock() cls.server_mock.__class__ = MagicMock(add=Mock()) nssrc_modules_mock = { 'nssrc.com.citrix.netscaler.nitro.resource.config.basic': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.basic.server': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.basic.server.server': cls.server_mock, } cls.nitro_specific_patcher = patch.dict(sys.modules, nssrc_modules_mock) cls.nitro_base_patcher = nitro_base_patcher
def test_validate_configuration_template_action_name(self, *args): with patch.dict('sys.modules', **{ 'vdirect_client': self.module_mock, 'vdirect_client.rest_client': self.module_mock, }): from ansible.modules.network.radware import vdirect_runnable Catalog.set_catalog_item_200() BASE_PARAMS.update(PLUGIN_RUNNABLE_PARAMS) vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) vdirectRunnable.client.catalog = Catalog(vdirectRunnable.client) vdirectRunnable._validate_runnable_exists() assert True Catalog.set_catalog_item_404() try: vdirectRunnable._validate_runnable_exists() self.fail("MissingRunnableException was not thrown for missing runnable name") except vdirect_runnable.MissingRunnableException: assert True
def test_validate_plugin_action_name(self, *args): with patch.dict('sys.modules', **{ 'vdirect_client': self.module_mock, 'vdirect_client.rest_client': self.module_mock, }): from ansible.modules.network.radware import vdirect_runnable Runnable.set_available_actions_result(ACTIONS_RESULT) BASE_PARAMS.update(PLUGIN_RUNNABLE_PARAMS) vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) vdirectRunnable._validate_action_name() assert vdirectRunnable.action_name == 'two' BASE_PARAMS['action_name'] = 'three' vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) try: vdirectRunnable._validate_action_name() self.fail("WrongActionNameException was not thrown for wrong action name") except vdirect_runnable.WrongActionNameException: assert True
def setUpClass(cls): class MockException(Exception): pass cls.MockException = MockException m = MagicMock() cls.servicegroup_mock = MagicMock() cls.servicegroup_mock.__class__ = MagicMock() nssrc_modules_mock = { 'nssrc.com.citrix.netscaler.nitro.resource.config.basic': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.basic.servicegroup': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.basic.servicegroup.servicegroup': cls.servicegroup_mock, 'nssrc.com.citrix.netscaler.nitro.resource.config.basic.servicegroup_servicegroupmember_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.basic.servicegroup_servicegroupmember_binding.servicegroup_servicegroupmember_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.basic.servicegroup_lbmonitor_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.basic.servicegroup_lbmonitor_binding.servicegroup_lbmonitor_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.lb': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_servicegroup_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_servicegroup_binding.lbmonitor_servicegroup_binding': m } cls.nitro_specific_patcher = patch.dict(sys.modules, nssrc_modules_mock) cls.nitro_base_patcher = nitro_base_patcher
def setUpClass(cls): class MockException(Exception): pass cls.MockException = MockException m = MagicMock() nssrc_modules_mock = { 'nssrc.com.citrix.netscaler.nitro.resource.config.gslb': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.gslb.gslbservice': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.gslb.gslbservice.gslbservice': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.gslb.gslbservice_lbmonitor_binding': m, 'nssrc.com.citrix.netscaler.nitro.resource.config.gslb.gslbservice_lbmonitor_binding.gslbservice_lbmonitor_binding': m, # The following are needed because of monkey_patch_nitro_api() 'nssrc.com.citrix.netscaler.nitro.resource.base': m, 'nssrc.com.citrix.netscaler.nitro.resource.base.Json': m, 'nssrc.com.citrix.netscaler.nitro.resource.base.Json.Json': m, 'nssrc.com.citrix.netscaler.nitro.util': m, 'nssrc.com.citrix.netscaler.nitro.util.nitro_util': m, 'nssrc.com.citrix.netscaler.nitro.util.nitro_util.nitro_util': m, } cls.nitro_specific_patcher = patch.dict(sys.modules, nssrc_modules_mock) cls.nitro_base_patcher = nitro_base_patcher
def test_run(self, *args): with patch.dict( 'sys.modules', **{ 'vdirect_client': self.module_mock, 'vdirect_client.rest_client': self.module_mock, }): from ansible.modules.network.radware import vdirect_runnable Runnable.set_runnable_objects_result(RUNNABLE_OBJECTS_RESULT) BASE_PARAMS.update(RUNNABLE_PARAMS) BASE_PARAMS[ 'runnable_type'] = vdirect_runnable.CONFIGURATION_TEMPLATE_RUNNABLE_TYPE BASE_PARAMS['parameters'] = {"pin": "x", "alteon": "x"} vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) Runnable.set_available_actions_result(AVAILABLE_ACTIONS_RESULT) Runnable.set_action_info_result(ACTIONS_PARAMS_RESULT) Runnable.set_run_result(RUN_RESULT) res = vdirectRunnable.run() assert res == MODULE_RESULT BASE_PARAMS[ 'runnable_type'] = vdirect_runnable.WORKFLOW_TEMPLATE_RUNNABLE_TYPE MODULE_RESULT['msg'] = "Workflow created." vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) res = vdirectRunnable.run() assert res == MODULE_RESULT BASE_PARAMS[ 'runnable_type'] = vdirect_runnable.WORKFLOW_RUNNABLE_TYPE BASE_PARAMS['action_name'] = 'a' MODULE_RESULT['msg'] = "Workflow action run completed." vdirectRunnable = vdirect_runnable.VdirectRunnable(BASE_PARAMS) vdirectRunnable.client.runnable = Runnable(vdirectRunnable.client) Runnable.set_available_actions_result(AVAILABLE_ACTIONS_RESULT) Runnable.set_action_info_result(ACTIONS_PARAMS_RESULT) res = vdirectRunnable.run() assert res == MODULE_RESULT result_parameters = {"param1": "value1", "param2": "value2"} RUN_RESULT[self.module_mock.rest_client. RESP_DATA]['parameters'] = result_parameters MODULE_RESULT['parameters'] = result_parameters res = vdirectRunnable.run() assert res == MODULE_RESULT RUN_RESULT[self.module_mock.rest_client.RESP_DATA]['status'] = 404 vdirectRunnable.run() assert res == MODULE_RESULT RUN_RESULT[self.module_mock.rest_client.RESP_STATUS] = 400 RUN_RESULT[self.module_mock.rest_client.RESP_REASON] = "Reason" RUN_RESULT[self.module_mock.rest_client.RESP_STR] = "Details" try: vdirectRunnable.run() self.fail("RunnableException was not thrown for failed run.") except vdirect_runnable.RunnableException as e: assert str(e) == "Reason: Reason. Details:Details." RUN_RESULT[self.module_mock.rest_client.RESP_STATUS] = 200 RUN_RESULT[self.module_mock.rest_client.RESP_DATA]["status"] = 400 RUN_RESULT[ self.module_mock.rest_client.RESP_DATA]["success"] = False RUN_RESULT[self.module_mock.rest_client.RESP_DATA]["exception"] = { "message": "exception message" } try: vdirectRunnable.run() self.fail("RunnableException was not thrown for failed run.") except vdirect_runnable.RunnableException as e: assert str(e) == "Reason: exception message. Details:Details."
# Make coding more python3-ish from __future__ import (absolute_import, division, print_function) __metaclass__ = type from units.compat.mock import patch, MagicMock from units.modules.utils import set_module_args from .junos_module import TestJunosModule jnpr_mock = MagicMock() modules = { 'jnpr': jnpr_mock, 'jnpr.junos': jnpr_mock.junos, 'jnpr.junos.utils': jnpr_mock.junos.utils, 'jnpr.junos.utils.sw': jnpr_mock.junos.utils.sw, } module_patcher = patch.dict('sys.modules', modules) module_patcher.start() from ansible.modules.network.junos import junos_package class TestJunosPackageModule(TestJunosModule): module = junos_package def setUp(self): super(TestJunosPackageModule, self).setUp() self.mock_get_device = patch('ansible.modules.network.junos.junos_package.get_device') self.get_device = self.mock_get_device.start() def tearDown(self):
base_modules_mock, 'nssrc.com.citrix.netscaler.nitro.exception': base_modules_mock, 'nssrc.com.citrix.netscaler.nitro.exception.nitro_exception': base_modules_mock, 'nssrc.com.citrix.netscaler.nitro.exception.nitro_exception.nitro_exception': nitro_exception_mock, 'nssrc.com.citrix.netscaler.nitro.service': base_modules_mock, 'nssrc.com.citrix.netscaler.nitro.service.nitro_service': base_modules_mock, 'nssrc.com.citrix.netscaler.nitro.service.nitro_service.nitro_service': nitro_service_mock, } nitro_base_patcher = patch.dict(sys.modules, base_modules_to_mock) class TestModule(ModuleTestCase): def failed(self): with self.assertRaises(AnsibleFailJson) as exc: self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def exited(self, changed=False): with self.assertRaises(AnsibleExitJson) as exc: self.module.main()