示例#1
0
 def test_get_id_from_id_then_name_empty(self):
     _id = str(uuid.uuid4())
     reses = {
         'networks': [
             {
                 'id': _id,
             },
         ],
     }
     resstr = self.client.serialize(reses)
     resstr1 = self.client.serialize({'networks': []})
     self.mox.StubOutWithMock(self.client.httpclient, "request")
     path = getattr(self.client, "networks_path")
     self.client.httpclient.request(
         test_cli20.end_url(path, "fields=id&id=" + _id),
         'GET',
         body=None,
         headers=mox.ContainsKeyValue('X-Auth-Token',
                                      test_cli20.TOKEN)).AndReturn(
                                          (test_cli20.MyResp(200), resstr1))
     self.client.httpclient.request(
         test_cli20.end_url(path, "fields=id&name=" + _id),
         'GET',
         body=None,
         headers=mox.ContainsKeyValue('X-Auth-Token',
                                      test_cli20.TOKEN)).AndReturn(
                                          (test_cli20.MyResp(200), resstr))
     self.mox.ReplayAll()
     returned_id = neutronV20.find_resourceid_by_name_or_id(
         self.client, 'network', _id)
     self.assertEqual(_id, returned_id)
    def _test_list_router_port(self, resources, cmd,
                               myid, detail=False, tags=[],
                               fields_1=[], fields_2=[]):
        self.mox.StubOutWithMock(cmd, "get_client")
        self.mox.StubOutWithMock(self.client.httpclient, "request")
        cmd.get_client().MultipleTimes().AndReturn(self.client)
        reses = {resources: [{'id': 'myid1', },
                             {'id': 'myid2', }, ], }

        resstr = self.client.serialize(reses)

        # url method body
        query = ""
        args = detail and ['-D', ] or []

        if fields_1:
            for field in fields_1:
                args.append('--fields')
                args.append(field)
        args.append(myid)
        if tags:
            args.append('--')
            args.append("--tag")
        for tag in tags:
            args.append(tag)
        if (not tags) and fields_2:
            args.append('--')
        if fields_2:
            args.append("--fields")
            for field in fields_2:
                args.append(field)
        fields_1.extend(fields_2)
        for field in fields_1:
            if query:
                query += "&fields=" + field
            else:
                query = "fields=" + field

        for tag in tags:
            if query:
                query += "&tag=" + tag
            else:
                query = "tag=" + tag
        if detail:
            query = query and query + '&verbose=True' or 'verbose=True'
        query = query and query + '&device_id=%s' or 'device_id=%s'
        path = getattr(self.client, resources + "_path")
        self.client.httpclient.request(
            test_cli20.end_url(path, query % myid), 'GET',
            body=None,
            headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)
        ).AndReturn((test_cli20.MyResp(200), resstr))
        self.mox.ReplayAll()
        cmd_parser = cmd.get_parser("list_" + resources)
        shell.run_command(cmd, cmd_parser, args)
        self.mox.VerifyAll()
        self.mox.UnsetStubs()
        _str = self.fake_stdout.make_string()

        self.assertTrue('myid1' in _str)
    def test_retrieve_pool_stats(self):
        """lb-pool-stats test_id."""
        resource = 'pool'
        cmd = pool.RetrievePoolStats(test_cli20.MyApp(sys.stdout), None)
        my_id = self.test_id
        fields = ['bytes_in', 'bytes_out']
        args = ['--fields', 'bytes_in', '--fields', 'bytes_out', my_id]

        self.mox.StubOutWithMock(cmd, "get_client")
        self.mox.StubOutWithMock(self.client.httpclient, "request")
        cmd.get_client().MultipleTimes().AndReturn(self.client)
        query = "&".join(["fields=%s" % field for field in fields])
        expected_res = {'stats': {'bytes_in': '1234', 'bytes_out': '4321'}}
        resstr = self.client.serialize(expected_res)
        path = getattr(self.client, "pool_path_stats")
        return_tup = (test_cli20.MyResp(200), resstr)
        self.client.httpclient.request(
            test_cli20.end_url(path % my_id, query),
            'GET',
            body=None,
            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()
        _str = self.fake_stdout.make_string()
        self.assertTrue('bytes_in' in _str)
        self.assertTrue('bytes_out' in _str)
示例#4
0
 def test_get_id_from_name_multiple(self):
     name = 'myname'
     reses = {
         'networks': [{
             'id': str(uuid.uuid4())
         }, {
             'id': str(uuid.uuid4())
         }]
     }
     resstr = self.client.serialize(reses)
     self.mox.StubOutWithMock(self.client.httpclient, "request")
     path = getattr(self.client, "networks_path")
     self.client.httpclient.request(
         test_cli20.end_url(path, "fields=id&name=" + name),
         'GET',
         body=None,
         headers=mox.ContainsKeyValue('X-Auth-Token',
                                      test_cli20.TOKEN)).AndReturn(
                                          (test_cli20.MyResp(200), resstr))
     self.mox.ReplayAll()
     try:
         neutronV20.find_resourceid_by_name_or_id(self.client, 'network',
                                                  name)
     except exceptions.NeutronClientException as ex:
         self.assertTrue('Multiple' in ex.message)
示例#5
0
    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=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_list_external_nets_empty_with_column(self):
     resources = "networks"
     cmd = network.ListExternalNetwork(test_cli20.MyApp(sys.stdout), None)
     self.mox.StubOutWithMock(cmd, "get_client")
     self.mox.StubOutWithMock(self.client.httpclient, "request")
     self.mox.StubOutWithMock(network.ListNetwork, "extend_list")
     network.ListNetwork.extend_list(mox.IsA(list), mox.IgnoreArg())
     cmd.get_client().MultipleTimes().AndReturn(self.client)
     reses = {resources: []}
     resstr = self.client.serialize(reses)
     # url method body
     query = "router%3Aexternal=True&id=myfakeid"
     args = ['-c', 'id', '--', '--id', 'myfakeid']
     path = getattr(self.client, resources + "_path")
     self.client.httpclient.request(
         test_cli20.end_url(path, query),
         'GET',
         body=None,
         headers=mox.ContainsKeyValue('X-Auth-Token',
                                      test_cli20.TOKEN)).AndReturn(
                                          (test_cli20.MyResp(200), resstr))
     self.mox.ReplayAll()
     cmd_parser = cmd.get_parser("list_" + resources)
     shell.run_command(cmd, cmd_parser, args)
     self.mox.VerifyAll()
     self.mox.UnsetStubs()
     _str = self.fake_stdout.make_string()
     self.assertEquals('\n', _str)
示例#7
0
    def test_disassociate_healthmonitor(self):
        cmd = healthmonitor.DisassociateHealthMonitor(
            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)

        path = getattr(self.client,
                       "disassociate_pool_health_monitors_path") % \
            {'pool': pool_id, 'health_monitor': health_monitor_id}
        return_tup = (test_cli20.MyResp(204), None)
        self.client.httpclient.request(
            test_cli20.end_url(path), 'DELETE',
            body=None,
            headers=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_list_external_nets_empty_with_column(self):
     resources = "networks"
     cmd = network.ListExternalNetwork(test_cli20.MyApp(sys.stdout), None)
     self.mox.StubOutWithMock(cmd, "get_client")
     self.mox.StubOutWithMock(self.client.httpclient, "request")
     self.mox.StubOutWithMock(network.ListNetwork, "extend_list")
     network.ListNetwork.extend_list(mox.IsA(list), mox.IgnoreArg())
     cmd.get_client().MultipleTimes().AndReturn(self.client)
     reses = {resources: []}
     resstr = self.client.serialize(reses)
     # url method body
     query = "router%3Aexternal=True&id=myfakeid"
     args = ['-c', 'id', '--', '--id', 'myfakeid']
     path = getattr(self.client, resources + "_path")
     self.client.httpclient.request(
         test_cli20.end_url(path, query), 'GET',
         body=None,
         headers=mox.ContainsKeyValue(
             'X-Auth-Token',
             test_cli20.TOKEN)).AndReturn(
                 (test_cli20.MyResp(200), resstr))
     self.mox.ReplayAll()
     cmd_parser = cmd.get_parser("list_" + resources)
     shell.run_command(cmd, cmd_parser, args)
     self.mox.VerifyAll()
     self.mox.UnsetStubs()
     _str = self.fake_stdout.make_string()
     self.assertEquals('\n', _str)
    def test_disassociate_healthmonitor(self):
        cmd = healthmonitor.DisassociateHealthMonitor(
            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)

        path = (
            getattr(self.client, "disassociate_pool_health_monitors_path") % {
                'pool': pool_id,
                'health_monitor': health_monitor_id
            })
        return_tup = (test_cli20.MyResp(204), None)
        self.client.httpclient.request(
            test_cli20.end_url(path),
            'DELETE',
            body=None,
            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_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()
示例#11
0
    def test_retrieve_pool_stats(self):
        """lb-pool-stats test_id."""
        resource = "pool"
        cmd = pool.RetrievePoolStats(test_cli20.MyApp(sys.stdout), None)
        my_id = self.test_id
        fields = ["bytes_in", "bytes_out"]
        args = ["--fields", "bytes_in", "--fields", "bytes_out", my_id]

        self.mox.StubOutWithMock(cmd, "get_client")
        self.mox.StubOutWithMock(self.client.httpclient, "request")
        cmd.get_client().MultipleTimes().AndReturn(self.client)
        query = "&".join(["fields=%s" % field for field in fields])
        expected_res = {"stats": {"bytes_in": "1234", "bytes_out": "4321"}}
        resstr = self.client.serialize(expected_res)
        path = getattr(self.client, "pool_path_stats")
        return_tup = (test_cli20.MyResp(200), resstr)
        self.client.httpclient.request(
            test_cli20.end_url(path % my_id, query),
            "GET",
            body=None,
            headers=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()
        _str = self.fake_stdout.make_string()
        self.assertTrue("bytes_in" in _str)
        self.assertTrue("bytes_out" in _str)
 def mox_calls(path, data):
     filters, response = self._build_test_data(data)
     self.client.httpclient.request(
         test_cli20.end_url(path, 'fields=id&fields=cidr' + filters),
         'GET',
         body=None,
         headers=mox.ContainsKeyValue(
             'X-Auth-Token', test_cli20.TOKEN)).AndReturn(response)
 def mox_calls(path, data):
     filters, response = self._build_test_data(data)
     self.client.httpclient.request(
         test_cli20.end_url(path, 'fields=id&fields=cidr' + filters),
         'GET',
         body=None,
         headers=mox.ContainsKeyValue(
             'X-Auth-Token', test_cli20.TOKEN)).AndReturn(response)
 def setup_list_stub(resources, data, query):
     reses = {resources: data}
     resstr = self.client.serialize(reses)
     resp = (test_cli20.MyResp(200), resstr)
     path = getattr(self.client, resources + '_path')
     self.client.httpclient.request(
         test_cli20.end_url(path, query), 'GET',
         body=None,
         headers=mox.ContainsKeyValue(
             'X-Auth-Token', test_cli20.TOKEN)).AndReturn(resp)
 def setup_list_stub(resources, data, query):
     reses = {resources: data}
     resstr = self.client.serialize(reses)
     resp = (test_cli20.MyResp(200), resstr)
     path = getattr(self.client, resources + '_path')
     self.client.httpclient.request(
         test_cli20.end_url(path, query), 'GET',
         body=None,
         headers=mox.ContainsKeyValue(
             'X-Auth-Token', test_cli20.TOKEN)).AndReturn(resp)
示例#16
0
 def mox_calls(path, data):
     sub_data_lists = [data[:len(data) - 1], data[len(data) - 1:]]
     filters, response = self._build_test_data(data)
     # 1 char of extra URI len will cause a split in 2 requests
     self.client.httpclient.request(
         test_cli20.end_url(path, 'fields=id&fields=cidr%s' % filters),
         'GET',
         body=None,
         headers=ContainsKeyValue(
             'X-Auth-Token', test_cli20.TOKEN)).AndRaise(
                 exceptions.RequestURITooLong(excess=1))
     for data in sub_data_lists:
         filters, response = self._build_test_data(data)
         self.client.httpclient.request(
             test_cli20.end_url(path,
                                'fields=id&fields=cidr%s' % filters),
             'GET',
             body=None,
             headers=ContainsKeyValue(
                 'X-Auth-Token', test_cli20.TOKEN)).AndReturn(response)
 def test_get_id_from_id_then_name_empty(self):
     _id = str(uuid.uuid4())
     reses = {'networks': [{'id': _id, }, ], }
     resstr = self.client.serialize(reses)
     resstr1 = self.client.serialize({'networks': []})
     self.mox.StubOutWithMock(self.client.httpclient, "request")
     path = getattr(self.client, "networks_path")
     self.client.httpclient.request(
         test_cli20.end_url(path, "fields=id&id=" + _id), 'GET',
         body=None,
         headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)
     ).AndReturn((test_cli20.MyResp(200), resstr1))
     self.client.httpclient.request(
         test_cli20.end_url(path, "fields=id&name=" + _id), 'GET',
         body=None,
         headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)
     ).AndReturn((test_cli20.MyResp(200), resstr))
     self.mox.ReplayAll()
     returned_id = neutronV20.find_resourceid_by_name_or_id(
         self.client, 'network', _id)
     self.assertEqual(_id, returned_id)
 def test_get_id_from_name_multiple(self):
     name = 'myname'
     reses = {'networks': [{'id': str(uuid.uuid4())},
                           {'id': str(uuid.uuid4())}]}
     resstr = self.client.serialize(reses)
     self.mox.StubOutWithMock(self.client.httpclient, "request")
     path = getattr(self.client, "networks_path")
     self.client.httpclient.request(
         test_cli20.end_url(path, "fields=id&name=" + name), 'GET',
         body=None,
         headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)
     ).AndReturn((test_cli20.MyResp(200), resstr))
     self.mox.ReplayAll()
     try:
         neutronV20.find_resourceid_by_name_or_id(
             self.client, 'network', name)
     except exceptions.NeutronClientNoUniqueMatch as ex:
         self.assertTrue('Multiple' in ex.message)
 def test_get_id_from_name_notfound(self):
     name = 'myname'
     reses = {'networks': []}
     resstr = self.client.serialize(reses)
     self.mox.StubOutWithMock(self.client.httpclient, "request")
     path = getattr(self.client, "networks_path")
     self.client.httpclient.request(
         test_cli20.end_url(path, "fields=id&name=" + name), 'GET',
         body=None,
         headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)
     ).AndReturn((test_cli20.MyResp(200), resstr))
     self.mox.ReplayAll()
     try:
         neutronV20.find_resourceid_by_name_or_id(
             self.client, 'network', name)
     except exceptions.NeutronClientException as ex:
         self.assertTrue('Unable to find' in ex.message)
         self.assertEqual(404, ex.status_code)
        def mox_calls(path, data):
            sub_data_lists = [data[:len(data) - 1], data[len(data) - 1:]]
            filters, response = self._build_test_data(data)

            # 1 char of extra URI len will cause a split in 2 requests
            self.mox.StubOutWithMock(self.client, "_check_uri_length")
            self.client._check_uri_length(mox.IgnoreArg()).AndRaise(
                exceptions.RequestURITooLong(excess=1))

            for data in sub_data_lists:
                filters, response = self._build_test_data(data)
                self.client._check_uri_length(mox.IgnoreArg()).AndReturn(None)
                self.client.httpclient.request(
                    test_cli20.end_url(path,
                                       'fields=id&fields=cidr%s' % filters),
                    'GET',
                    body=None,
                    headers=mox.ContainsKeyValue(
                        'X-Auth-Token', test_cli20.TOKEN)).AndReturn(response)
    def _test_list_router_port(self,
                               resources,
                               cmd,
                               myid,
                               detail=False,
                               tags=[],
                               fields_1=[],
                               fields_2=[]):
        self.mox.StubOutWithMock(cmd, "get_client")
        self.mox.StubOutWithMock(self.client.httpclient, "request")
        cmd.get_client().MultipleTimes().AndReturn(self.client)
        reses = {
            resources: [
                {
                    'id': 'myid1',
                },
                {
                    'id': 'myid2',
                },
            ],
        }

        resstr = self.client.serialize(reses)

        # url method body
        query = ""
        args = detail and [
            '-D',
        ] or []

        if fields_1:
            for field in fields_1:
                args.append('--fields')
                args.append(field)
        args.append(myid)
        if tags:
            args.append('--')
            args.append("--tag")
        for tag in tags:
            args.append(tag)
        if (not tags) and fields_2:
            args.append('--')
        if fields_2:
            args.append("--fields")
            for field in fields_2:
                args.append(field)
        fields_1.extend(fields_2)
        for field in fields_1:
            if query:
                query += "&fields=" + field
            else:
                query = "fields=" + field

        for tag in tags:
            if query:
                query += "&tag=" + tag
            else:
                query = "tag=" + tag
        if detail:
            query = query and query + '&verbose=True' or 'verbose=True'
        query = query and query + '&device_id=%s' or 'device_id=%s'
        path = getattr(self.client, resources + "_path")
        self.client.httpclient.request(
            test_cli20.end_url(path, query % myid),
            'GET',
            body=None,
            headers=mox.ContainsKeyValue('X-Auth-Token',
                                         test_cli20.TOKEN)).AndReturn(
                                             (test_cli20.MyResp(200), resstr))
        self.mox.ReplayAll()
        cmd_parser = cmd.get_parser("list_" + resources)
        shell.run_command(cmd, cmd_parser, args)
        self.mox.VerifyAll()
        self.mox.UnsetStubs()
        _str = self.fake_stdout.make_string()

        self.assertTrue('myid1' in _str)