def test_ip_ns_route(self, mock): ns_list = commands.ip_ns_list() for ns in ns_list: expected = ['/usr/bin/sudo', '-n', 'ip', 'netns', 'exec', ns, 'ip', 'r'] commands.ip_ns_route(ns) mock.assert_called_once_with(expected, **self.subprocess_args)
def test_instance_destroy_error(self, mock): mock.return_value = (400, None, "Error Response") json_response = maccli.dao.api_instance.destroy(DEFAULT_SERVER_ID) mock.assert_called_once_with("DELETE", "/instance/%s" % DEFAULT_SERVER_ID) error = self.buf.getvalue() self.assertEqual(" ".join(MOCK_RESPONSE_INSTANCE_DESTROY_ERROR.split()), " ".join(error.split())) self.assertEqual(json_response, None)
def test_search_several_param(self, mock): keywords = ['one', 'two'] mock.return_value = (200, MOCK_CONFIGURATION_SEARCH_JSON, MOCK_CONFIGURATION_SEARCH_JSON_RAW) status_code, json_response = maccli.dao.api_configuration.search_public_configuration(keywords) mock.assert_called_once_with("GET", "/configuration/search?keyword=one&keyword=two") self.assertEqual(json_response, MOCK_CONFIGURATION_SEARCH_JSON) self.assertEqual(status_code, 200)
def test_iptables_raw(self, mock): table = 'filter' expected = ['/usr/bin/sudo', '-n', 'iptables', '--line-numbers', '-L', '-nv', '-t', '%s' % table] commands.iptables_raw(table) mock.assert_called_once_with(expected, **self.subprocess_args)
def test_instance_destroy_ok(self, mock): mock.return_value = (200, MOCK_RESPONSE_INSTANCE_DESTROY_JSON, MOCK_RESPONSE_INSTANCE_DESTROY_JSON_RAW) json_response = maccli.dao.api_instance.destroy(DEFAULT_SERVERNAME) mock.assert_called_once_with("DELETE", "/instance/%s" % DEFAULT_SERVERNAME) self.assertEqual(json_response, MOCK_RESPONSE_INSTANCE_DESTROY_JSON)
def _test_assert_called_once_with(self, mock, resource, chk_interval, time_out, **kwargs): """Method to replace repeatative asserts on resources :param mock: The mock to call assert with :param resource: The resource used in mock :param chk_interval: The interval used for polling the action :param time_out: Time out value for action :param kwargs: currently used for validating the is_ready attribute, can be extended as required """ isready = self.res_is.mock() if kwargs: if kwargs["is_ready"]: mock.assert_called_once_with( resource, update_resource=self.gfm(), is_ready=isready, check_interval=chk_interval, timeout=time_out) else: mock.assert_called_once_with( resource, update_resource=self.gfm(), check_interval=chk_interval, timeout=time_out)
def test_instance_destroy_error_not_found(self, mock): mock.return_value = (404, None, "Error Response") json_response = maccli.dao.api_instance.destroy(DEFAULT_SERVERNAME) mock.assert_called_once_with("DELETE", "/instance/%s" % DEFAULT_SERVERNAME) error = self.buf.getvalue() self.assertEqual(' '.join(MOCK_RESPONSE_INSTANCE_DESTROY_NOT_FOUND.split()), ' '.join(error.split())) self.assertEqual(json_response, None)
def test_hardwares_ok(self, mock): mock.return_value = (200, MOCK_HARDWARE_LIST_JSON, MOCK_HARDWARE_LIST_JSON_RAW) status_code, json_response = maccli.dao.api_provider.get_hardwares(DEFAULT_PROVIDER, DEFAULT_LOCATION, DEFAULT_CONFIGURATION, DEFAULT_RELEASE) mock.assert_called_once_with("GET", "/provider/hardware?provider=%s&location=%s&release=%s&cookbook_tag=%s" % ( DEFAULT_PROVIDER, DEFAULT_LOCATION, DEFAULT_RELEASE, DEFAULT_CONFIGURATION)) self.assertEqual(json_response, MOCK_HARDWARE_LIST_JSON) self.assertEqual(status_code, 200)
def test_locations(self, mock): mock.return_value = (200, MOCK_LOCATION_LIST_JSON, MOCK_LOCATION_LIST_JSON_RAW) status_code, json_response = maccli.dao.api_provider.get_locations(DEFAULT_CONFIGURATION, DEFAULT_PROVIDER) mock.assert_called_once_with("GET", "/provider/locations?cookbook_tag=%s&provider=%s" % ( DEFAULT_CONFIGURATION, DEFAULT_PROVIDER)) self.assertEqual(json_response, MOCK_LOCATION_LIST_JSON) self.assertEqual(status_code, 200)
def _test_assert_called_once_with(self, mock, resource, chk_interval, time_out, **kwargs): """Method to replace repeatative asserts on resources :param mock: The mock to call assert with :param resource: The resource used in mock :param chk_interval: The interval used for polling the action :param time_out: Time out value for action :param kwargs: currently used for validating the is_ready attribute, can be extended as required """ isready = self.res_is.mock() if kwargs: if kwargs["is_ready"]: mock.assert_called_once_with(resource, update_resource=self.gfm(), is_ready=isready, check_interval=chk_interval, timeout=time_out) else: mock.assert_called_once_with(resource, update_resource=self.gfm(), check_interval=chk_interval, timeout=time_out)
def test_is_tracing_returns_what_tracing_percent_method_returns_for_rest( mock, dummy_request ): dummy_request.zipkin_trace_id = '42' assert mock.return_value == request_helper.is_tracing(dummy_request) mock.assert_called_once_with( request_helper.DEFAULT_REQUEST_TRACING_PERCENT, '42' )
def test_is_tracing_returns_what_tracing_percent_method_returns_for_rest( mock, dummy_request ): dummy_request.zipkin_trace_id = '42' assert mock.return_value == request_helper.is_tracing(dummy_request) mock.assert_called_once_with( request_helper.DEFAULT_REQUEST_TRACING_PERCENT )
def test_ip_ns_route(self, mock): ns_list = commands.ip_ns_list() for ns in ns_list: expected = [ '/usr/bin/sudo', '-n', 'ip', 'netns', 'exec', ns, 'ip', 'r' ] commands.ip_ns_route(ns) mock.assert_called_once_with(expected, **self.subprocess_args)
def test_search_no_params(self, mock): mock.return_value = (200, MOCK_CONFIGURATION_SEARCH_JSON, MOCK_CONFIGURATION_SEARCH_JSON_RAW) status_code, json_response = maccli.dao.api_configuration.search_public_configuration( None) mock.assert_called_once_with("GET", "/configuration/search") self.assertEqual(json_response, MOCK_CONFIGURATION_SEARCH_JSON) self.assertEqual(status_code, 200)
def test_iptables_raw(self, mock): table = 'filter' expected = [ '/usr/bin/sudo', '-n', 'iptables', '-v', '-S', '-t', '%s' % table ] commands.iptables_raw(table) mock.assert_called_once_with(expected, **self.subprocess_args)
def test_iptables_ns(self, mock): table = 'filter' ns_list = commands.ip_ns_list() for ns in ns_list: expected = ['/usr/bin/sudo', '-n', 'ip', 'netns', 'exec', ns, 'iptables', '-v', '-S', '-t', table] commands.iptables_ns(ns, table) mock.assert_called_once_with(expected, **self.subprocess_args)
def test_hardwares_error(self, mock): mock.return_value = (400, None, "No credentials available") status_code, json_response = maccli.dao.api_provider.get_hardwares(DEFAULT_PROVIDER, DEFAULT_LOCATION, DEFAULT_CONFIGURATION, DEFAULT_RELEASE) mock.assert_called_once_with("GET", "/provider/hardware?provider=%s&location=%s&release=%s&cookbook_tag=%s" % ( DEFAULT_PROVIDER, DEFAULT_LOCATION, DEFAULT_RELEASE, DEFAULT_CONFIGURATION)) self.assertEqual(json_response, None) self.assertEqual(status_code, 400) out = self.buf.getvalue() self.assertEqual(' '.join(OUTPUT_HARDWARES_NO_CREDENTIALS.split()), ' '.join(out.split()))
def test_locations(self, mock): mock.return_value = (200, MOCK_LOCATION_LIST_JSON, MOCK_LOCATION_LIST_JSON_RAW) status_code, json_response = maccli.dao.api_provider.get_locations( DEFAULT_PROVIDER) mock.assert_called_once_with( "GET", "/provider/locations?provider=%s" % (DEFAULT_PROVIDER)) self.assertEqual(json_response, MOCK_LOCATION_LIST_JSON) self.assertEqual(status_code, 200)
def test_handle_exception_ignores_ignored_exceptions(self, mock, Client): mock.return_value = True mock_exc_info = [None, httpexceptions.HTTPNotFound()] client = mock.MagicMock() Client.return_value = client subscribers.handle_exception(self.request, mock_exc_info) mock.assert_called_once_with(self.request, mock_exc_info) client.capture_exception.assert_not_called()
def test_output_is_sourceable(mock, releases_dictionary): bash_script = '/foo/bar.sh' assert (write_releases_dictionary_to_bash(releases_dictionary, bash_script)) mock.assert_called_once_with(bash_script, 'w') handle = mock() args, _ = handle.write.call_args written_content = args[0] # TODO(Llorente): check environment variables assert (0 == os.system(written_content))
def test_iptables_ns(self, mock): table = 'filter' ns_list = commands.ip_ns_list() for ns in ns_list: expected = [ '/usr/bin/sudo', '-n', 'ip', 'netns', 'exec', ns, 'iptables', '-v', '-S', '-t', table ] commands.iptables_ns(ns, table) mock.assert_called_once_with(expected, **self.subprocess_args)
def test_instance_destroy_error_not_found(self, mock): mock.return_value = (404, None, "Error Response") json_response = maccli.dao.api_instance.destroy(DEFAULT_SERVERNAME) mock.assert_called_once_with("DELETE", "/instance/%s" % DEFAULT_SERVERNAME) error = self.buf.getvalue() self.assertEqual( ' '.join(MOCK_RESPONSE_INSTANCE_DESTROY_NOT_FOUND.split()), ' '.join(error.split())) self.assertEqual(json_response, None)
def test_hardwares_ok(self, mock): mock.return_value = (200, MOCK_HARDWARE_LIST_JSON, MOCK_HARDWARE_LIST_JSON_RAW) status_code, json_response = maccli.dao.api_provider.get_hardwares( DEFAULT_PROVIDER, DEFAULT_LOCATION, DEFAULT_RELEASE) mock.assert_called_once_with( "GET", "/provider/hardware?provider=%s&location=%s&release=%s" % (DEFAULT_PROVIDER, DEFAULT_LOCATION, DEFAULT_RELEASE)) self.assertEqual(json_response, MOCK_HARDWARE_LIST_JSON) self.assertEqual(status_code, 200)
def test_opbeat_tween_factory_returns_a_curried_tween_function(self, mock): handler = {} registry = self.request.registry result = subscribers.opbeat_tween_factory(handler, registry) mock.assert_not_called() result(self.request) mock.assert_called_once_with(handler, registry, self.request)
def test_search_several_param(self, mock): keywords = ['one', 'two'] mock.return_value = (200, MOCK_CONFIGURATION_SEARCH_JSON, MOCK_CONFIGURATION_SEARCH_JSON_RAW) status_code, json_response = maccli.dao.api_configuration.search_public_configuration( keywords) mock.assert_called_once_with( "GET", "/configuration/search?keyword=one&keyword=two") self.assertEqual(json_response, MOCK_CONFIGURATION_SEARCH_JSON) self.assertEqual(status_code, 200)
def test_all_running_options(self, mock): jid = "*****@*****.**" room = "*****@*****.**" password = "******" nick = "nick" args = ["test", "-j", jid, "-p", password, "-r", room, "-n", nick] self.assertIsNone(nonobot.main.main(args)) mock.assert_called_once_with(jid=jid, room=room, password=password, nick=nick, plugins={"help": []})
def test_instance_create_error(self, mock): mock.return_value = (400, None, "Error Response") json_response = maccli.dao.api_instance.create(DEFAULT_CONFIGURATION, DEFAULT_BOOTSTRAP, DEFAULT_DEPLOYMENT, DEFAULT_LOCATION, DEFAULT_SERVERNAME, DEFAULT_PROVIDER, DEFAULT_RELEASE, DEFAULT_RELEASE_VERSION, DEFAULT_BRANCH, DEFAULT_HARDWARE, DEFAULT_LIFESPAN, DEFAULT_ENVIRONMENT, DEFAULT_HD, DEFAULT_PORT, DEFAULT_NET, DEFAULT_METADATA, DEFAULT_APPLYCHANGES) mock.assert_called_once_with("POST", "/instance", data=MOCK_INSTANCE_CREATE_PARAMETERS_JSON_RAW) error = self.buf.getvalue() self.assertEqual(' '.join(MOCK_RESPONSE_INSTANCE_CREATE_ERROR.split()), ' '.join(error.split())) self.assertEqual(json_response, None)
def test_automatically_adds_errbacks_deferred(self, mock): d = Deferred() def return_deferred(u, c, t): return d util.command(command="mock")(return_deferred) f = Failure(ValueError("")) pluginmanager._handle_message("gandalf", "#channel", "!mock") d.errback(f) mock.assert_called_once_with("gandalf", "#channel", f)
def test_hardwares_error(self, mock): mock.return_value = (400, None, "No credentials available") status_code, json_response = maccli.dao.api_provider.get_hardwares( DEFAULT_PROVIDER, DEFAULT_LOCATION, DEFAULT_RELEASE) mock.assert_called_once_with( "GET", "/provider/hardware?provider=%s&location=%s&release=%s" % (DEFAULT_PROVIDER, DEFAULT_LOCATION, DEFAULT_RELEASE)) self.assertEqual(json_response, None) self.assertEqual(status_code, 400) out = self.buf.getvalue() self.assertEqual(' '.join(OUTPUT_HARDWARES_NO_CREDENTIALS.split()), ' '.join(out.split()))
def test_instance_create_error(self, mock): mock.return_value = (400, None, "Error Response") json_response = maccli.dao.api_instance.create( DEFAULT_CONFIGURATION, DEFAULT_BOOTSTRAP, DEFAULT_DEPLOYMENT, DEFAULT_LOCATION, DEFAULT_SERVERNAME, DEFAULT_PROVIDER, DEFAULT_RELEASE, DEFAULT_RELEASE_VERSION, DEFAULT_BRANCH, DEFAULT_HARDWARE, DEFAULT_LIFESPAN, DEFAULT_ENVIRONMENT, DEFAULT_HD, DEFAULT_PORT, DEFAULT_NET, DEFAULT_METADATA, DEFAULT_APPLYCHANGES) mock.assert_called_once_with( "POST", "/instance", data=MOCK_INSTANCE_CREATE_PARAMETERS_JSON_RAW) error = self.buf.getvalue() self.assertEqual(' '.join(MOCK_RESPONSE_INSTANCE_CREATE_ERROR.split()), ' '.join(error.split())) self.assertEqual(json_response, None)
def test_list_with_pagination(self, mock): self.call(execution_cmd.List) mock.assert_called_once_with(limit=None, marker='', sort_dirs='asc', sort_keys='created_at') self.call(execution_cmd.List, app_args=['--limit', '5', '--sort_dirs', 'id, Workflow', '--sort_keys', 'desc', '--marker', 'abc']) mock.assert_called_with(limit=5, marker='abc', sort_dirs='id, Workflow', sort_keys='desc')
def test_assert_called_once_with(self): mock = Mock() mock() # Will raise an exception if it fails mock.assert_called_once_with() mock() self.assertRaises(AssertionError, mock.assert_called_once_with) mock.reset_mock() self.assertRaises(AssertionError, mock.assert_called_once_with) mock("foo", "bar", baz=2) mock.assert_called_once_with("foo", "bar", baz=2) mock.reset_mock() mock("foo", "bar", baz=2) self.assertRaises(AssertionError, lambda: mock.assert_called_once_with("bob", "bar", baz=2))
def test_assert_called_once_with(self): mock = Mock() mock() # Will raise an exception if it fails mock.assert_called_once_with() mock() self.assertRaises(AssertionError, mock.assert_called_once_with) mock.reset_mock() self.assertRaises(AssertionError, mock.assert_called_once_with) mock('foo', 'bar', baz=2) mock.assert_called_once_with('foo', 'bar', baz=2) mock.reset_mock() mock('foo', 'bar', baz=2) self.assertRaises( AssertionError, lambda: mock.assert_called_once_with('bob', 'bar', baz=2))
def test_all_running_options(self, mock): jid = '*****@*****.**' room = '*****@*****.**' password = '******' nick = 'nick' args = ['test', '-j', jid, '-p', password, '-r', room, '-n', nick] self.assertIsNone(nonobot.main.main(args, plugins={})) mock.assert_called_once_with( jid=jid, room=room, password=password, nick=nick, plugins={} )
def test_assert_called_once_with_function_spec(self): def f(a, b, c, d=None): pass mock = Mock(spec=f) mock(1, b=2, c=3) mock.assert_called_once_with(1, 2, 3) mock.assert_called_once_with(a=1, b=2, c=3) self.assertRaises(AssertionError, mock.assert_called_once_with, 1, b=3, c=2) # Expected call doesn't match the spec's signature with self.assertRaises(AssertionError) as cm: mock.assert_called_once_with(e=8) if hasattr(cm.exception, '__cause__'): self.assertIsInstance(cm.exception.__cause__, TypeError) # Mock called more than once => always fails mock(4, 5, 6) self.assertRaises(AssertionError, mock.assert_called_once_with, 1, 2, 3) self.assertRaises(AssertionError, mock.assert_called_once_with, 4, 5, 6)
def test_instance_list(self, mock): mock.return_value = (200, MOCK_RESPONSE_INSTANCE_LIST_JSON, MOCK_RESPONSE_INSTANCE_LIST_JSON_RAW) json_response = maccli.dao.api_instance.get_list() mock.assert_called_once_with("GET", "/instances") self.assertEqual(json_response, MOCK_RESPONSE_INSTANCE_LIST_JSON)
def test_testfunc_calls_execute(self, mock): obj = self.FakeNegativeJSONTest("test_fake_negative") self.assertIn("test_fake_negative", dir(obj)) obj.test_fake_negative() mock.assert_called_once_with(self.FakeNegativeJSONTest._schema)
def test_ip_route_raw(self, mock): expected = ['/usr/bin/sudo', '-n', 'ip', 'r'] commands.ip_route_raw() mock.assert_called_once_with(expected, **self.subprocess_args)
def test_ip_ns_list(self, mock): expected = ['/usr/bin/sudo', '-n', 'ip', 'netns', 'list'] commands.ip_ns_list() mock.assert_called_once_with(expected, **self.subprocess_args)
def test_search_no_params(self, mock): mock.return_value = (200, MOCK_CONFIGURATION_SEARCH_JSON, MOCK_CONFIGURATION_SEARCH_JSON_RAW) status_code, json_response = maccli.dao.api_configuration.search_public_configuration(None) mock.assert_called_once_with("GET", "/configuration/search") self.assertEqual(json_response, MOCK_CONFIGURATION_SEARCH_JSON) self.assertEqual(status_code, 200)