def test_associate_healthmonitor(self): cmd = healthmonitor.AssociateHealthMonitor( test_cli20.MyApp(sys.stdout), None) resource = 'health_monitor' health_monitor_id = 'hm-id' pool_id = 'p_id' args = [health_monitor_id, pool_id] self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) body = {resource: {'id': health_monitor_id}} result = { resource: { 'id': health_monitor_id }, } result_str = self.client.serialize(result) path = getattr(self.client, "associate_pool_health_monitors_path") % pool_id return_tup = (test_cli20.MyResp(200), result_str) self.client.httpclient.request( test_cli20.end_url(path), 'POST', body=test_cli20.MyComparator(body, self.client), headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn(return_tup) self.mox.ReplayAll() cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(args) cmd.run(parsed_args) self.mox.VerifyAll() self.mox.UnsetStubs()
def test_remove_firewall_rule(self): # firewall-policy-remove-rule myid ruleid resource = 'firewall_policy' cmd = firewallpolicy.FirewallPolicyRemoveRule( test_cli20.MyApp(sys.stdout), None) myid = 'myid' args = ['myid', 'removerule'] extrafields = { 'firewall_rule_id': 'removerule', } body = extrafields path = getattr(self.client, resource + "_remove_path") cmd_parser = cmd.get_parser(resource + "_remove_rule") resp = (test_cli20.MyResp(204), None) with mock.patch.object(cmd, "get_client", return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, "request", return_value=resp) as mock_request: shell.run_command(cmd, cmd_parser, args) self.assert_mock_multiple_calls_with_same_arguments( mock_get_client, mock.call(), 2) mock_request.assert_called_once_with( test_cli20.MyUrlComparator(test_cli20.end_url(path % myid), self.client), 'PUT', body=test_cli20.MyComparator(body, self.client), headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))
def test_associate_healthmonitor(self): cmd = healthmonitor.AssociateHealthMonitor( test_cli20.MyApp(sys.stdout), None) resource = 'health_monitor' health_monitor_id = 'hm-id' pool_id = 'p_id' args = [health_monitor_id, pool_id] body = {resource: {'id': health_monitor_id}} result = { resource: { 'id': health_monitor_id }, } result_str = self.client.serialize(result) path = getattr(self.client, "associate_pool_health_monitors_path") % pool_id return_tup = (test_cli20.MyResp(200), result_str) cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(args) with mock.patch.object(cmd, "get_client", return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, "request", return_value=return_tup) as mock_request: cmd.run(parsed_args) mock_get_client.assert_called_once_with() mock_request.assert_called_once_with( test_cli20.end_url(path), 'POST', body=test_cli20.MyComparator(body, self.client), headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))
def test_remove_firewall_rule(self): """firewall-policy-remove-rule myid ruleid """ resource = 'firewall_policy' cmd = firewallpolicy.FirewallPolicyRemoveRule( test_cli20.MyApp(sys.stdout), None) myid = 'myid' args = ['myid', 'removerule'] extrafields = { 'firewall_rule_id': 'removerule', } self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) body = extrafields path = getattr(self.client, resource + "_remove_path") self.client.httpclient.request( test_cli20.MyUrlComparator( test_cli20.end_url(path % myid, format=self.format), self.client), 'PUT', body=test_cli20.MyComparator(body, self.client), headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)).AndReturn( (test_cli20.MyResp(204), None)) args.extend(['--request-format', self.format]) self.mox.ReplayAll() cmd_parser = cmd.get_parser(resource + "_remove_rule") shell.run_command(cmd, cmd_parser, args) self.mox.VerifyAll() self.mox.UnsetStubs()
def _test_tag_operation(self, cmd, path, method, args, prog_name, body=None): self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) if body: body = test_cli20.MyComparator(body, self.client) self.client.httpclient.request(test_cli20.MyUrlComparator( test_cli20.end_url(path, format=self.format), self.client), method, body=body, headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn( (test_cli20.MyResp(204), None)) self.mox.ReplayAll() cmd_parser = cmd.get_parser(prog_name) shell.run_command(cmd, cmd_parser, args) self.mox.VerifyAll() self.mox.UnsetStubs()
def test_insert_firewall_rule(self): # firewall-policy-insert-rule myid newruleid --insert-before ruleAid # --insert-after ruleBid resource = 'firewall_policy' cmd = firewallpolicy.FirewallPolicyInsertRule( test_cli20.MyApp(sys.stdout), None) myid = 'myid' args = [ 'myid', 'newrule', '--insert-before', 'rule2', '--insert-after', 'rule1' ] extrafields = { 'firewall_rule_id': 'newrule', 'insert_before': 'rule2', 'insert_after': 'rule1' } self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) body = extrafields path = getattr(self.client, resource + "_insert_path") self.client.httpclient.request( test_cli20.MyUrlComparator(test_cli20.end_url(path % myid), self.client), 'PUT', body=test_cli20.MyComparator(body, self.client), headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)).AndReturn( (test_cli20.MyResp(204), None)) self.mox.ReplayAll() cmd_parser = cmd.get_parser(resource + "_insert_rule") shell.run_command(cmd, cmd_parser, args) self.mox.VerifyAll() self.mox.UnsetStubs()
def _test_assoc_with_cfg_agent(self, resource, cmd, cmd_args, destination, body, result): path = ((scheduler.ConfigAgentHandlingHostingDevice.resource_path + destination) % cmd_args[0]) result_str = self.client.serialize(result) return_tup = (test_cli20.MyResp(200), result_str) cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(cmd_args) if getattr(self, 'mox', None): self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) self.client.httpclient.request( test_cli20.end_url(path), 'POST', body=test_cli20.MyComparator(body, self.client), headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn(return_tup) self.mox.ReplayAll() cmd.run(parsed_args) self.mox.VerifyAll() self.mox.UnsetStubs() else: mock_request_calls = [ mock.call(test_cli20.end_url(path), 'POST', body=test_cli20.MyComparator(body, self.client), headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})) ] with mock.patch.object( cmd, "get_client", return_value=self.client) as mock_get_client: with mock.patch.object( self.client.httpclient, "request", return_value=return_tup) as mock_request: cmd.run(parsed_args) mock_request.assert_has_calls(mock_request_calls) self.assert_mock_multiple_calls_with_same_arguments( mock_get_client, mock.call(), None)
def _test_tag_operation(self, cmd, path, method, args, prog_name, body=None): with mock.patch.object(cmd, 'get_client', return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, 'request', return_value=(test_cli20.MyResp(204), None) ) as mock_request: if body: body = test_cli20.MyComparator(body, self.client) cmd_parser = cmd.get_parser(prog_name) shell.run_command(cmd, cmd_parser, args) mock_get_client.assert_called_once_with() mock_request.assert_called_once_with( test_cli20.MyUrlComparator(test_cli20.end_url(path), self.client), method, body=body, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))
def _test_add_to_hosting_device(self, resource, cmd, cmd_args, destination, body, result): path = ((hostingdevice.HostingDevice.resource_path + destination) % cmd_args[0]) self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) result_str = self.client.serialize(result) return_tup = (test_cli20.MyResp(200), result_str) self.client.httpclient.request( test_cli20.end_url(path), 'POST', body=test_cli20.MyComparator(body, self.client), headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn(return_tup) self.mox.ReplayAll() cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(cmd_args) cmd.run(parsed_args) self.mox.VerifyAll() self.mox.UnsetStubs()
def _test_add_to_agent(self, resource, cmd, cmd_args, destination, body, result): path = ((self.client.agent_path + destination) % cmd_args[0]) result_str = self.client.serialize(result) return_tup = (test_cli20.MyResp(200), result_str) cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(cmd_args) with mock.patch.object(cmd, "get_client", return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, "request", return_value=return_tup) as mock_request: cmd.run(parsed_args) mock_get_client.assert_called_once_with() mock_request.assert_called_once_with( test_cli20.end_url(path), 'POST', body=test_cli20.MyComparator(body, self.client), headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))