<NME> test_list_server_filters.py
<BEF> # Copyright 2012 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
import testtools

from tempest.api.compute import base
from tempest.common import waiters
from tempest import config
from tempest.lib.common import fixed_network
from tempest.lib.common.utils import data_utils
from tempest.lib import decorators


CONF = config.CONF


    @classmethod
    @test.safe_setup
    def setUpClass(cls):
        super(ListServerFiltersTestJSON, cls).setUpClass()
        cls.client = cls.servers_client


    @classmethod
    def setup_clients(cls):
        super(ListServerFiltersTestJSON, cls).setup_clients()
        cls.client = cls.servers_client

    @classmethod
    def resource_setup(cls):
        super(ListServerFiltersTestJSON, cls).resource_setup()

        network = cls.get_tenant_network()
        if network:
            cls.fixed_network_name = network.get('name')
        else:
            cls.fixed_network_name = None
        network_kwargs = fixed_network.set_networks_kwarg(network)
        cls.s1_name = data_utils.rand_name(cls.__name__ + '-instance')
        cls.s1 = cls.create_test_server(name=cls.s1_name, **network_kwargs)

        cls.s2_name = data_utils.rand_name(cls.__name__ + '-instance')
        # If image_ref_alt is "" or None then we still want to boot a server
        # but we rely on `testtools.skipUnless` decorator to actually skip
        # the irrelevant tests.
        cls.s2 = cls.create_test_server(
            name=cls.s2_name, image_id=cls.image_ref_alt or cls.image_ref)

        cls.s3_name = data_utils.rand_name(cls.__name__ + '-instance')
        cls.s3 = cls.create_test_server(name=cls.s3_name,
                                        flavor=cls.flavor_ref_alt,
                                        wait_until='ACTIVE')

        waiters.wait_for_server_status(cls.client, cls.s1['id'],
                                       'ACTIVE')
        waiters.wait_for_server_status(cls.client, cls.s2['id'],
                                       'ACTIVE')
        resp, cls.s3 = cls.create_test_server(name=cls.s3_name,
                                              flavor=cls.flavor_ref_alt,
                                              wait_until='ACTIVE')

        cls.fixed_network_name = CONF.compute.fixed_network_name

    @utils.skip_unless_attr('multiple_images', 'Only one image found')
    @test.attr(type='gate')
        self.assertIn(self.s3['id'], map(lambda x: x['id'], servers))

    @decorators.idempotent_id('573637f5-7325-47bb-9144-3476d0416908')
    def test_list_servers_filter_by_flavor(self):
        """Filter the list of servers by flavor"""
        params = {'flavor': self.flavor_ref_alt}
        body = self.client.list_servers(**params)
        servers = body['servers']

        self.assertNotIn(self.s1['id'], map(lambda x: x['id'], servers))
        self.assertNotIn(self.s2['id'], map(lambda x: x['id'], servers))
        self.assertIn(self.s3['id'], map(lambda x: x['id'], servers))

    @decorators.idempotent_id('9b067a7b-7fee-4f6a-b29c-be43fe18fc5a')
    def test_list_servers_filter_by_server_name(self):
        """Filter the list of servers by server name"""
        params = {'name': self.s1_name}
        body = self.client.list_servers(**params)
        servers = body['servers']

        self.assertIn(self.s1_name, map(lambda x: x['name'], servers))
        self.assertNotIn(self.s2_name, map(lambda x: x['name'], servers))
        self.assertNotIn(self.s3_name, map(lambda x: x['name'], servers))

    @decorators.idempotent_id('ca78e20e-fddb-4ce6-b7f7-bcbf8605e66e')
    def test_list_servers_filter_by_active_status(self):
        """Filter the list of servers by server active status"""
        params = {'status': 'active'}
        body = self.client.list_servers(**params)
        servers = body['servers']

        self.assertIn(self.s1['id'], map(lambda x: x['id'], servers))
        self.assertIn(self.s2['id'], map(lambda x: x['id'], servers))
        self.assertIn(self.s3['id'], map(lambda x: x['id'], servers))

    @decorators.idempotent_id('451dbbb2-f330-4a9f-b0e1-5f5d2cb0f34c')
    def test_list_servers_filter_by_shutoff_status(self):
        """Filter the list of servers by server shutoff status"""
        params = {'status': 'shutoff'}
        self.client.stop_server(self.s1['id'])
        waiters.wait_for_server_status(self.client, self.s1['id'],
                                       'SHUTOFF')
        body = self.client.list_servers(**params)
        self.client.start_server(self.s1['id'])
        waiters.wait_for_server_status(self.client, self.s1['id'],
                                       'ACTIVE')
        servers = body['servers']

        self.assertIn(self.s1['id'], map(lambda x: x['id'], servers))
        self.assertNotIn(self.s2['id'], map(lambda x: x['id'], servers))
        self.assertNotIn(self.s3['id'], map(lambda x: x['id'], servers))

    @decorators.idempotent_id('614cdfc1-d557-4bac-915b-3e67b48eee76')
    def test_list_servers_filter_by_limit(self):
        """Filter the list of servers by limit 1

        Verify only the expected number of servers are returned (one server)
        """
        params = {'limit': 1}
        servers = self.client.list_servers(**params)
        self.assertEqual(1, len([x for x in servers['servers'] if 'id' in x]))

    @decorators.idempotent_id('b1495414-2d93-414c-8019-849afe8d319e')
    def test_list_servers_filter_by_zero_limit(self):
        """Filter the list of servers by limit 0

        Verify only the expected number of servers are returned (no server)
        """
        params = {'limit': 0}
        servers = self.client.list_servers(**params)
        self.assertEmpty(servers['servers'])

    @decorators.idempotent_id('37791bbd-90c0-4de0-831e-5f38cba9c6b3')
    def test_list_servers_filter_by_exceed_limit(self):
        """Filter the list of servers by exceeded limit

        Verify only the expected number of servers are returned (all servers)
        """
        params = {'limit': 100000}
        servers = self.client.list_servers(**params)
        all_servers = self.client.list_servers()
        self.assertEqual(len([x for x in all_servers['servers'] if 'id' in x]),
                         len([x for x in servers['servers'] if 'id' in x]))

    @decorators.idempotent_id('b3304c3b-97df-46d2-8cd3-e2b6659724e7')
    @testtools.skipUnless(CONF.compute.image_ref != CONF.compute.image_ref_alt,
                          "Need distinct images to run this test")
    def test_list_servers_detailed_filter_by_image(self):
        """"Filter the detailed list of servers by image"""
        params = {'image': self.image_ref}
        body = self.client.list_servers(detail=True, **params)
        servers = body['servers']

        self.assertIn(self.s1['id'], map(lambda x: x['id'], servers))
        self.assertNotIn(self.s2['id'], map(lambda x: x['id'], servers))
        self.assertIn(self.s3['id'], map(lambda x: x['id'], servers))

    @decorators.idempotent_id('80c574cc-0925-44ba-8602-299028357dd9')
    def test_list_servers_detailed_filter_by_flavor(self):
        """Filter the detailed list of servers by flavor"""
        params = {'flavor': self.flavor_ref_alt}
        body = self.client.list_servers(detail=True, **params)
        servers = body['servers']

        self.assertNotIn(self.s1['id'], map(lambda x: x['id'], servers))
        self.assertNotIn(self.s2['id'], map(lambda x: x['id'], servers))
        self.assertIn(self.s3['id'], map(lambda x: x['id'], servers))

    @decorators.idempotent_id('f9eb2b70-735f-416c-b260-9914ac6181e4')
    def test_list_servers_detailed_filter_by_server_name(self):
        """Filter the detailed list of servers by server name"""
        params = {'name': self.s1_name}
        body = self.client.list_servers(detail=True, **params)
        servers = body['servers']

        self.assertIn(self.s1_name, map(lambda x: x['name'], servers))
        self.assertNotIn(self.s2_name, map(lambda x: x['name'], servers))
        self.assertNotIn(self.s3_name, map(lambda x: x['name'], servers))

    @decorators.idempotent_id('de2612ab-b7dd-4044-b0b1-d2539601911f')
    def test_list_servers_detailed_filter_by_server_status(self):
        """Filter the detailed list of servers by server status"""
        params = {'status': 'active'}
        body = self.client.list_servers(detail=True, **params)
        servers = body['servers']
        test_ids = [s['id'] for s in (self.s1, self.s2, self.s3)]

        self.assertIn(self.s1['id'], map(lambda x: x['id'], servers))
        self.assertIn(self.s2['id'], map(lambda x: x['id'], servers))
        self.assertIn(self.s3['id'], map(lambda x: x['id'], servers))
        self.assertEqual(['ACTIVE'] * 3, [x['status'] for x in servers
                                          if x['id'] in test_ids])

    @decorators.idempotent_id('e9f624ee-92af-4562-8bec-437945a18dcb')
    def test_list_servers_filtered_by_name_wildcard(self):
        """Filter the list of servers by part of server name"""
        # List all servers that contains '-instance' in name
        params = {'name': '-instance'}
        body = self.client.list_servers(**params)
        servers = body['servers']

        self.assertIn(self.s1_name, map(lambda x: x['name'], servers))
        self.assertIn(self.s2_name, map(lambda x: x['name'], servers))
        self.assertIn(self.s3_name, map(lambda x: x['name'], servers))

        # Let's take random part of name and try to search it
        part_name = self.s1_name[6:-1]

        params = {'name': part_name}
        self.assertNotIn(self.s2_name, map(lambda x: x['name'], servers))
        self.assertNotIn(self.s3_name, map(lambda x: x['name'], servers))

    @test.skip_because(bug="1170718")
    @test.attr(type='gate')
    def test_list_servers_filtered_by_ip(self):
        # Filter servers by ip

    @decorators.idempotent_id('24a89b0c-0d55-4a28-847f-45075f19b27b')
    def test_list_servers_filtered_by_name_regex(self):
        """Filter the list of servers by server name regular expression"""
        # list of regex that should match s1, s2 and s3
        regexes = [r'^.*\-instance\-[0-9]+$', r'^.*\-instance\-.*$']
        for regex in regexes:
            params = {'name': regex}
            body = self.client.list_servers(**params)
            servers = body['servers']

            self.assertIn(self.s1_name, map(lambda x: x['name'], servers))
            self.assertIn(self.s2_name, map(lambda x: x['name'], servers))
            self.assertIn(self.s3_name, map(lambda x: x['name'], servers))

        # Let's take random part of name and try to search it
        part_name = self.s1_name[-10:]

        params = {'name': part_name}
        body = self.client.list_servers(**params)
        servers = body['servers']

        self.assertIn(self.s1_name, map(lambda x: x['name'], servers))
        self.assertNotIn(self.s2_name, map(lambda x: x['name'], servers))
        self.assertNotIn(self.s3_name, map(lambda x: x['name'], servers))

    @decorators.idempotent_id('43a1242e-7b31-48d1-88f2-3f72aa9f2077')
    def test_list_servers_filtered_by_ip(self):
        """Filter the list of servers by server ip address"""
        # Here should be listed 1 server
        if not self.fixed_network_name:
            msg = 'fixed_network_name needs to be configured to run this test'
            raise self.skipException(msg)

        # list servers filter by ip is something "regexp match", i.e,
        # filter by "10.1.1.1" will return both "10.1.1.1" and "10.1.1.10".
        # so here look for the longest server ip, and filter by that ip,
        # so as to ensure only one server is returned.
        ip_list = {}
        self.s1 = self.client.show_server(self.s1['id'])['server']
        # Get first ip address in spite of v4 or v6
        ip_addr = self.s1['addresses'][self.fixed_network_name][0]['addr']
        ip_list[ip_addr] = self.s1['id']

        self.s2 = self.client.show_server(self.s2['id'])['server']
        ip_addr = self.s2['addresses'][self.fixed_network_name][0]['addr']
        ip_list[ip_addr] = self.s2['id']

        self.s3 = self.client.show_server(self.s3['id'])['server']
        ip_addr = self.s3['addresses'][self.fixed_network_name][0]['addr']
        ip_list[ip_addr] = self.s3['id']

        longest_ip = max([[len(ip), ip] for ip in ip_list])[1]
        params = {'ip': longest_ip}
        body = self.client.list_servers(**params)
        servers = body['servers']

        self.assertIn(ip_list[longest_ip], map(lambda x: x['id'], servers))
        del ip_list[longest_ip]
        for ip in ip_list:
            self.assertNotIn(ip_list[ip], map(lambda x: x['id'], servers))

    @decorators.idempotent_id('a905e287-c35e-42f2-b132-d02b09f3654a')
    def test_list_servers_filtered_by_ip_regex(self):
        """Filter the list of servers by part of server ip address"""
        if not self.fixed_network_name:
            msg = 'fixed_network_name needs to be configured to run this test'
            raise self.skipException(msg)
        # query addresses of the 3 servers
        addrs = []
        for s in [self.s1, self.s2, self.s3]:
            s_show = self.client.show_server(s['id'])['server']
            addr_spec = s_show['addresses'][self.fixed_network_name][0]
            addrs.append(addr_spec['addr'])
        # find common part of the 3 ip addresses
        prefix = ''
        addrs_len = [len(a) for a in addrs]
        addrs_len.sort()
        # iterate over the smallest length of an ip
        for i in range(addrs_len[0]):
            if not addrs[0][i] == addrs[1][i] == addrs[2][i]:
                break
            prefix += addrs[0][i]

        if addr_spec['version'] == 4:
            params = {'ip': prefix}
        else:
            params = {'ip6': prefix}
        # capture all servers in case something goes wrong
        all_servers = self.client.list_servers(detail=True)
        body = self.client.list_servers(**params)
        servers = body['servers']

        self.assertIn(self.s1_name, map(lambda x: x['name'], servers),
                      "%s not found in %s, all servers %s" %
                      (self.s1_name, servers, all_servers))
        self.assertIn(self.s2_name, map(lambda x: x['name'], servers),
                      "%s not found in %s, all servers %s" %
                      (self.s2_name, servers, all_servers))
        self.assertIn(self.s3_name, map(lambda x: x['name'], servers),
                      "%s not found in %s, all servers %s" %
                      (self.s3_name, servers, all_servers))

    @decorators.idempotent_id('67aec2d0-35fe-4503-9f92-f13272b867ed')
    def test_list_servers_detailed_limit_results(self):
        """Filter the detailed list of servers by limit 1

        Verify only the expected number of servers are returned (one server)
        """
        params = {'limit': 1}
        servers = self.client.list_servers(detail=True, **params)
        self.assertEqual(1, len(servers['servers']))

<MSG> Merge "unskip test_list_servers_filtered_by_ip"
<DFF> @@ -28,6 +28,7 @@ class ListServerFiltersTestJSON(base.BaseV2ComputeTest):
     @classmethod
     @test.safe_setup
     def setUpClass(cls):
+        cls.set_network_resources(network=True, subnet=True, dhcp=True)
         super(ListServerFiltersTestJSON, cls).setUpClass()
         cls.client = cls.servers_client
 
@@ -69,8 +70,12 @@ class ListServerFiltersTestJSON(base.BaseV2ComputeTest):
         resp, cls.s3 = cls.create_test_server(name=cls.s3_name,
                                               flavor=cls.flavor_ref_alt,
                                               wait_until='ACTIVE')
-
-        cls.fixed_network_name = CONF.compute.fixed_network_name
+        if (CONF.service_available.neutron and
+                CONF.compute.allow_tenant_isolation):
+            network = cls.isolated_creds.get_primary_network()
+            cls.fixed_network_name = network['name']
+        else:
+            cls.fixed_network_name = CONF.compute.fixed_network_name
 
     @utils.skip_unless_attr('multiple_images', 'Only one image found')
     @test.attr(type='gate')
@@ -226,7 +231,6 @@ class ListServerFiltersTestJSON(base.BaseV2ComputeTest):
         self.assertNotIn(self.s2_name, map(lambda x: x['name'], servers))
         self.assertNotIn(self.s3_name, map(lambda x: x['name'], servers))
 
-    @test.skip_because(bug="1170718")
     @test.attr(type='gate')
     def test_list_servers_filtered_by_ip(self):
         # Filter servers by ip

{'commit': 'diff --git a/tempest/api/compute/servers/test_list_server_filters.py b/tempest/api/compute/servers/test_list_server_filters.py\nindex f0913f1e1..f66020ceb 100644\n--- a/tempest/api/compute/servers/test_list_server_filters.py\n+++ b/tempest/api/compute/servers/test_list_server_filters.py\n@@ -28,6 +28,7 @@ class ListServerFiltersTestJSON(base.BaseV2ComputeTest):\n     @classmethod\n     @test.safe_setup\n     def setUpClass(cls):\n+        cls.set_network_resources(network=True, subnet=True, dhcp=True)\n         super(ListServerFiltersTestJSON, cls).setUpClass()\n         cls.client = cls.servers_client\n \n@@ -69,8 +70,12 @@ class ListServerFiltersTestJSON(base.BaseV2ComputeTest):\n         resp, cls.s3 = cls.create_test_server(name=cls.s3_name,\n                                               flavor=cls.flavor_ref_alt,\n                                               wait_until=\'ACTIVE\')\n-\n-        cls.fixed_network_name = CONF.compute.fixed_network_name\n+        if (CONF.service_available.neutron and\n+                CONF.compute.allow_tenant_isolation):\n+            network = cls.isolated_creds.get_primary_network()\n+            cls.fixed_network_name = network[\'name\']\n+        else:\n+            cls.fixed_network_name = CONF.compute.fixed_network_name\n \n     @utils.skip_unless_attr(\'multiple_images\', \'Only one image found\')\n     @test.attr(type=\'gate\')\n@@ -226,7 +231,6 @@ class ListServerFiltersTestJSON(base.BaseV2ComputeTest):\n         self.assertNotIn(self.s2_name, map(lambda x: x[\'name\'], servers))\n         self.assertNotIn(self.s3_name, map(lambda x: x[\'name\'], servers))\n \n-    @test.skip_because(bug="1170718")\n     @test.attr(type=\'gate\')\n     def test_list_servers_filtered_by_ip(self):\n         # Filter servers by ip\ndiff --git a/tempest/api/compute/v3/servers/test_list_server_filters.py b/tempest/api/compute/v3/servers/test_list_server_filters.py\nindex 2cb176cca..778b033e4 100644\n--- a/tempest/api/compute/v3/servers/test_list_server_filters.py\n+++ b/tempest/api/compute/v3/servers/test_list_server_filters.py\n@@ -28,6 +28,7 @@ class ListServerFiltersV3Test(base.BaseV3ComputeTest):\n     @classmethod\n     @test.safe_setup\n     def setUpClass(cls):\n+        cls.set_network_resources(network=True, subnet=True, dhcp=True)\n         super(ListServerFiltersV3Test, cls).setUpClass()\n         cls.client = cls.servers_client\n \n@@ -70,7 +71,12 @@ class ListServerFiltersV3Test(base.BaseV3ComputeTest):\n                                               flavor=cls.flavor_ref_alt,\n                                               wait_until=\'ACTIVE\')\n \n-        cls.fixed_network_name = CONF.compute.fixed_network_name\n+        if (CONF.service_available.neutron and\n+                CONF.compute.allow_tenant_isolation):\n+            network = cls.isolated_creds.get_primary_network()\n+            cls.fixed_network_name = network[\'name\']\n+        else:\n+            cls.fixed_network_name = CONF.compute.fixed_network_name\n \n     @utils.skip_unless_attr(\'multiple_images\', \'Only one image found\')\n     @test.attr(type=\'gate\')\n@@ -226,7 +232,6 @@ class ListServerFiltersV3Test(base.BaseV3ComputeTest):\n         self.assertNotIn(self.s2_name, map(lambda x: x[\'name\'], servers))\n         self.assertNotIn(self.s3_name, map(lambda x: x[\'name\'], servers))\n \n-    @test.skip_because(bug="1170718")\n     @test.attr(type=\'gate\')\n     def test_list_servers_filtered_by_ip(self):\n         # Filter servers by ip\n', 'commit_subject': 'Merge "unskip test_list_servers_filtered_by_ip"', 'repo_name': 'openstack/tempest', 'committer': {'name': 'Gerrit Code Review', 'email': '61e62b213a1a56f7695845df4fc372a10cb0a73e@openstack.org', 'time_sec': '1398174866', 'tz_offset': '0', 'date': {'seconds': '1398174866'}}, 'commit_tree': '69042668e21685abdd6739a6ad8351bae4fab2fc', 'commit_message': 'Merge "unskip test_list_servers_filtered_by_ip"', 'language_name': 'Python', 'license': 'apache-2.0', 'hunks': '@@ -28,6 +28,7 @@ class ListServerFiltersTestJSON(base.BaseV2ComputeTest):\n     @classmethod\n     @test.safe_setup\n     def setUpClass(cls):\n+        cls.set_network_resources(network=True, subnet=True, dhcp=True)\n         super(ListServerFiltersTestJSON, cls).setUpClass()\n         cls.client = cls.servers_client\n \n@@ -69,8 +70,12 @@ class ListServerFiltersTestJSON(base.BaseV2ComputeTest):\n         resp, cls.s3 = cls.create_test_server(name=cls.s3_name,\n                                               flavor=cls.flavor_ref_alt,\n                                               wait_until=\'ACTIVE\')\n-\n-        cls.fixed_network_name = CONF.compute.fixed_network_name\n+        if (CONF.service_available.neutron and\n+                CONF.compute.allow_tenant_isolation):\n+            network = cls.isolated_creds.get_primary_network()\n+            cls.fixed_network_name = network[\'name\']\n+        else:\n+            cls.fixed_network_name = CONF.compute.fixed_network_name\n \n     @utils.skip_unless_attr(\'multiple_images\', \'Only one image found\')\n     @test.attr(type=\'gate\')\n@@ -226,7 +231,6 @@ class ListServerFiltersTestJSON(base.BaseV2ComputeTest):\n         self.assertNotIn(self.s2_name, map(lambda x: x[\'name\'], servers))\n         self.assertNotIn(self.s3_name, map(lambda x: x[\'name\'], servers))\n \n-    @test.skip_because(bug="1170718")\n     @test.attr(type=\'gate\')\n     def test_list_servers_filtered_by_ip(self):\n         # Filter servers by ip\n', 'addition_count': 7, 'deletion_count': 3, 'src_file': 'a/tempest/api/compute/servers/test_list_server_filters.py', 'tgt_file': 'b/tempest/api/compute/servers/test_list_server_filters.py', 'file_extension': '.py', 'before_file': '# Copyright 2012 OpenStack Foundation\n# All Rights Reserved.\n#\n#    Licensed under the Apache License, Version 2.0 (the "License"); you may\n#    not use this file except in compliance with the License. You may obtain\n#    a copy of the License at\n#\n#         http://www.apache.org/licenses/LICENSE-2.0\n#\n#    Unless required by applicable law or agreed to in writing, software\n#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\n#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n#    License for the specific language governing permissions and limitations\n#    under the License.\nimport testtools\n\nfrom tempest.api.compute import base\nfrom tempest.common import waiters\nfrom tempest import config\nfrom tempest.lib.common import fixed_network\nfrom tempest.lib.common.utils import data_utils\nfrom tempest.lib import decorators\n\n\nCONF = config.CONF\n\n\n    @classmethod\n    @test.safe_setup\n    def setUpClass(cls):\n        super(ListServerFiltersTestJSON, cls).setUpClass()\n        cls.client = cls.servers_client\n\n\n    @classmethod\n    def setup_clients(cls):\n        super(ListServerFiltersTestJSON, cls).setup_clients()\n        cls.client = cls.servers_client\n\n    @classmethod\n    def resource_setup(cls):\n        super(ListServerFiltersTestJSON, cls).resource_setup()\n\n        network = cls.get_tenant_network()\n        if network:\n            cls.fixed_network_name = network.get(\'name\')\n        else:\n            cls.fixed_network_name = None\n        network_kwargs = fixed_network.set_networks_kwarg(network)\n        cls.s1_name = data_utils.rand_name(cls.__name__ + \'-instance\')\n        cls.s1 = cls.create_test_server(name=cls.s1_name, **network_kwargs)\n\n        cls.s2_name = data_utils.rand_name(cls.__name__ + \'-instance\')\n        # If image_ref_alt is "" or None then we still want to boot a server\n        # but we rely on `testtools.skipUnless` decorator to actually skip\n        # the irrelevant tests.\n        cls.s2 = cls.create_test_server(\n            name=cls.s2_name, image_id=cls.image_ref_alt or cls.image_ref)\n\n        cls.s3_name = data_utils.rand_name(cls.__name__ + \'-instance\')\n        cls.s3 = cls.create_test_server(name=cls.s3_name,\n                                        flavor=cls.flavor_ref_alt,\n                                        wait_until=\'ACTIVE\')\n\n        waiters.wait_for_server_status(cls.client, cls.s1[\'id\'],\n                                       \'ACTIVE\')\n        waiters.wait_for_server_status(cls.client, cls.s2[\'id\'],\n                                       \'ACTIVE\')\n        resp, cls.s3 = cls.create_test_server(name=cls.s3_name,\n                                              flavor=cls.flavor_ref_alt,\n                                              wait_until=\'ACTIVE\')\n\n        cls.fixed_network_name = CONF.compute.fixed_network_name\n\n    @utils.skip_unless_attr(\'multiple_images\', \'Only one image found\')\n    @test.attr(type=\'gate\')\n        self.assertIn(self.s3[\'id\'], map(lambda x: x[\'id\'], servers))\n\n    @decorators.idempotent_id(\'573637f5-7325-47bb-9144-3476d0416908\')\n    def test_list_servers_filter_by_flavor(self):\n        """Filter the list of servers by flavor"""\n        params = {\'flavor\': self.flavor_ref_alt}\n        body = self.client.list_servers(**params)\n        servers = body[\'servers\']\n\n        self.assertNotIn(self.s1[\'id\'], map(lambda x: x[\'id\'], servers))\n        self.assertNotIn(self.s2[\'id\'], map(lambda x: x[\'id\'], servers))\n        self.assertIn(self.s3[\'id\'], map(lambda x: x[\'id\'], servers))\n\n    @decorators.idempotent_id(\'9b067a7b-7fee-4f6a-b29c-be43fe18fc5a\')\n    def test_list_servers_filter_by_server_name(self):\n        """Filter the list of servers by server name"""\n        params = {\'name\': self.s1_name}\n        body = self.client.list_servers(**params)\n        servers = body[\'servers\']\n\n        self.assertIn(self.s1_name, map(lambda x: x[\'name\'], servers))\n        self.assertNotIn(self.s2_name, map(lambda x: x[\'name\'], servers))\n        self.assertNotIn(self.s3_name, map(lambda x: x[\'name\'], servers))\n\n    @decorators.idempotent_id(\'ca78e20e-fddb-4ce6-b7f7-bcbf8605e66e\')\n    def test_list_servers_filter_by_active_status(self):\n        """Filter the list of servers by server active status"""\n        params = {\'status\': \'active\'}\n        body = self.client.list_servers(**params)\n        servers = body[\'servers\']\n\n        self.assertIn(self.s1[\'id\'], map(lambda x: x[\'id\'], servers))\n        self.assertIn(self.s2[\'id\'], map(lambda x: x[\'id\'], servers))\n        self.assertIn(self.s3[\'id\'], map(lambda x: x[\'id\'], servers))\n\n    @decorators.idempotent_id(\'451dbbb2-f330-4a9f-b0e1-5f5d2cb0f34c\')\n    def test_list_servers_filter_by_shutoff_status(self):\n        """Filter the list of servers by server shutoff status"""\n        params = {\'status\': \'shutoff\'}\n        self.client.stop_server(self.s1[\'id\'])\n        waiters.wait_for_server_status(self.client, self.s1[\'id\'],\n                                       \'SHUTOFF\')\n        body = self.client.list_servers(**params)\n        self.client.start_server(self.s1[\'id\'])\n        waiters.wait_for_server_status(self.client, self.s1[\'id\'],\n                                       \'ACTIVE\')\n        servers = body[\'servers\']\n\n        self.assertIn(self.s1[\'id\'], map(lambda x: x[\'id\'], servers))\n        self.assertNotIn(self.s2[\'id\'], map(lambda x: x[\'id\'], servers))\n        self.assertNotIn(self.s3[\'id\'], map(lambda x: x[\'id\'], servers))\n\n    @decorators.idempotent_id(\'614cdfc1-d557-4bac-915b-3e67b48eee76\')\n    def test_list_servers_filter_by_limit(self):\n        """Filter the list of servers by limit 1\n\n        Verify only the expected number of servers are returned (one server)\n        """\n        params = {\'limit\': 1}\n        servers = self.client.list_servers(**params)\n        self.assertEqual(1, len([x for x in servers[\'servers\'] if \'id\' in x]))\n\n    @decorators.idempotent_id(\'b1495414-2d93-414c-8019-849afe8d319e\')\n    def test_list_servers_filter_by_zero_limit(self):\n        """Filter the list of servers by limit 0\n\n        Verify only the expected number of servers are returned (no server)\n        """\n        params = {\'limit\': 0}\n        servers = self.client.list_servers(**params)\n        self.assertEmpty(servers[\'servers\'])\n\n    @decorators.idempotent_id(\'37791bbd-90c0-4de0-831e-5f38cba9c6b3\')\n    def test_list_servers_filter_by_exceed_limit(self):\n        """Filter the list of servers by exceeded limit\n\n        Verify only the expected number of servers are returned (all servers)\n        """\n        params = {\'limit\': 100000}\n        servers = self.client.list_servers(**params)\n        all_servers = self.client.list_servers()\n        self.assertEqual(len([x for x in all_servers[\'servers\'] if \'id\' in x]),\n                         len([x for x in servers[\'servers\'] if \'id\' in x]))\n\n    @decorators.idempotent_id(\'b3304c3b-97df-46d2-8cd3-e2b6659724e7\')\n    @testtools.skipUnless(CONF.compute.image_ref != CONF.compute.image_ref_alt,\n                          "Need distinct images to run this test")\n    def test_list_servers_detailed_filter_by_image(self):\n        """"Filter the detailed list of servers by image"""\n        params = {\'image\': self.image_ref}\n        body = self.client.list_servers(detail=True, **params)\n        servers = body[\'servers\']\n\n        self.assertIn(self.s1[\'id\'], map(lambda x: x[\'id\'], servers))\n        self.assertNotIn(self.s2[\'id\'], map(lambda x: x[\'id\'], servers))\n        self.assertIn(self.s3[\'id\'], map(lambda x: x[\'id\'], servers))\n\n    @decorators.idempotent_id(\'80c574cc-0925-44ba-8602-299028357dd9\')\n    def test_list_servers_detailed_filter_by_flavor(self):\n        """Filter the detailed list of servers by flavor"""\n        params = {\'flavor\': self.flavor_ref_alt}\n        body = self.client.list_servers(detail=True, **params)\n        servers = body[\'servers\']\n\n        self.assertNotIn(self.s1[\'id\'], map(lambda x: x[\'id\'], servers))\n        self.assertNotIn(self.s2[\'id\'], map(lambda x: x[\'id\'], servers))\n        self.assertIn(self.s3[\'id\'], map(lambda x: x[\'id\'], servers))\n\n    @decorators.idempotent_id(\'f9eb2b70-735f-416c-b260-9914ac6181e4\')\n    def test_list_servers_detailed_filter_by_server_name(self):\n        """Filter the detailed list of servers by server name"""\n        params = {\'name\': self.s1_name}\n        body = self.client.list_servers(detail=True, **params)\n        servers = body[\'servers\']\n\n        self.assertIn(self.s1_name, map(lambda x: x[\'name\'], servers))\n        self.assertNotIn(self.s2_name, map(lambda x: x[\'name\'], servers))\n        self.assertNotIn(self.s3_name, map(lambda x: x[\'name\'], servers))\n\n    @decorators.idempotent_id(\'de2612ab-b7dd-4044-b0b1-d2539601911f\')\n    def test_list_servers_detailed_filter_by_server_status(self):\n        """Filter the detailed list of servers by server status"""\n        params = {\'status\': \'active\'}\n        body = self.client.list_servers(detail=True, **params)\n        servers = body[\'servers\']\n        test_ids = [s[\'id\'] for s in (self.s1, self.s2, self.s3)]\n\n        self.assertIn(self.s1[\'id\'], map(lambda x: x[\'id\'], servers))\n        self.assertIn(self.s2[\'id\'], map(lambda x: x[\'id\'], servers))\n        self.assertIn(self.s3[\'id\'], map(lambda x: x[\'id\'], servers))\n        self.assertEqual([\'ACTIVE\'] * 3, [x[\'status\'] for x in servers\n                                          if x[\'id\'] in test_ids])\n\n    @decorators.idempotent_id(\'e9f624ee-92af-4562-8bec-437945a18dcb\')\n    def test_list_servers_filtered_by_name_wildcard(self):\n        """Filter the list of servers by part of server name"""\n        # List all servers that contains \'-instance\' in name\n        params = {\'name\': \'-instance\'}\n        body = self.client.list_servers(**params)\n        servers = body[\'servers\']\n\n        self.assertIn(self.s1_name, map(lambda x: x[\'name\'], servers))\n        self.assertIn(self.s2_name, map(lambda x: x[\'name\'], servers))\n        self.assertIn(self.s3_name, map(lambda x: x[\'name\'], servers))\n\n        # Let\'s take random part of name and try to search it\n        part_name = self.s1_name[6:-1]\n\n        params = {\'name\': part_name}\n        self.assertNotIn(self.s2_name, map(lambda x: x[\'name\'], servers))\n        self.assertNotIn(self.s3_name, map(lambda x: x[\'name\'], servers))\n\n    @test.skip_because(bug="1170718")\n    @test.attr(type=\'gate\')\n    def test_list_servers_filtered_by_ip(self):\n        # Filter servers by ip\n\n    @decorators.idempotent_id(\'24a89b0c-0d55-4a28-847f-45075f19b27b\')\n    def test_list_servers_filtered_by_name_regex(self):\n        """Filter the list of servers by server name regular expression"""\n        # list of regex that should match s1, s2 and s3\n        regexes = [r\'^.*\\-instance\\-[0-9]+$\', r\'^.*\\-instance\\-.*$\']\n        for regex in regexes:\n            params = {\'name\': regex}\n            body = self.client.list_servers(**params)\n            servers = body[\'servers\']\n\n            self.assertIn(self.s1_name, map(lambda x: x[\'name\'], servers))\n            self.assertIn(self.s2_name, map(lambda x: x[\'name\'], servers))\n            self.assertIn(self.s3_name, map(lambda x: x[\'name\'], servers))\n\n        # Let\'s take random part of name and try to search it\n        part_name = self.s1_name[-10:]\n\n        params = {\'name\': part_name}\n        body = self.client.list_servers(**params)\n        servers = body[\'servers\']\n\n        self.assertIn(self.s1_name, map(lambda x: x[\'name\'], servers))\n        self.assertNotIn(self.s2_name, map(lambda x: x[\'name\'], servers))\n        self.assertNotIn(self.s3_name, map(lambda x: x[\'name\'], servers))\n\n    @decorators.idempotent_id(\'43a1242e-7b31-48d1-88f2-3f72aa9f2077\')\n    def test_list_servers_filtered_by_ip(self):\n        """Filter the list of servers by server ip address"""\n        # Here should be listed 1 server\n        if not self.fixed_network_name:\n            msg = \'fixed_network_name needs to be configured to run this test\'\n            raise self.skipException(msg)\n\n        # list servers filter by ip is something "regexp match", i.e,\n        # filter by "10.1.1.1" will return both "10.1.1.1" and "10.1.1.10".\n        # so here look for the longest server ip, and filter by that ip,\n        # so as to ensure only one server is returned.\n        ip_list = {}\n        self.s1 = self.client.show_server(self.s1[\'id\'])[\'server\']\n        # Get first ip address in spite of v4 or v6\n        ip_addr = self.s1[\'addresses\'][self.fixed_network_name][0][\'addr\']\n        ip_list[ip_addr] = self.s1[\'id\']\n\n        self.s2 = self.client.show_server(self.s2[\'id\'])[\'server\']\n        ip_addr = self.s2[\'addresses\'][self.fixed_network_name][0][\'addr\']\n        ip_list[ip_addr] = self.s2[\'id\']\n\n        self.s3 = self.client.show_server(self.s3[\'id\'])[\'server\']\n        ip_addr = self.s3[\'addresses\'][self.fixed_network_name][0][\'addr\']\n        ip_list[ip_addr] = self.s3[\'id\']\n\n        longest_ip = max([[len(ip), ip] for ip in ip_list])[1]\n        params = {\'ip\': longest_ip}\n        body = self.client.list_servers(**params)\n        servers = body[\'servers\']\n\n        self.assertIn(ip_list[longest_ip], map(lambda x: x[\'id\'], servers))\n        del ip_list[longest_ip]\n        for ip in ip_list:\n            self.assertNotIn(ip_list[ip], map(lambda x: x[\'id\'], servers))\n\n    @decorators.idempotent_id(\'a905e287-c35e-42f2-b132-d02b09f3654a\')\n    def test_list_servers_filtered_by_ip_regex(self):\n        """Filter the list of servers by part of server ip address"""\n        if not self.fixed_network_name:\n            msg = \'fixed_network_name needs to be configured to run this test\'\n            raise self.skipException(msg)\n        # query addresses of the 3 servers\n        addrs = []\n        for s in [self.s1, self.s2, self.s3]:\n            s_show = self.client.show_server(s[\'id\'])[\'server\']\n            addr_spec = s_show[\'addresses\'][self.fixed_network_name][0]\n            addrs.append(addr_spec[\'addr\'])\n        # find common part of the 3 ip addresses\n        prefix = \'\'\n        addrs_len = [len(a) for a in addrs]\n        addrs_len.sort()\n        # iterate over the smallest length of an ip\n        for i in range(addrs_len[0]):\n            if not addrs[0][i] == addrs[1][i] == addrs[2][i]:\n                break\n            prefix += addrs[0][i]\n\n        if addr_spec[\'version\'] == 4:\n            params = {\'ip\': prefix}\n        else:\n            params = {\'ip6\': prefix}\n        # capture all servers in case something goes wrong\n        all_servers = self.client.list_servers(detail=True)\n        body = self.client.list_servers(**params)\n        servers = body[\'servers\']\n\n        self.assertIn(self.s1_name, map(lambda x: x[\'name\'], servers),\n                      "%s not found in %s, all servers %s" %\n                      (self.s1_name, servers, all_servers))\n        self.assertIn(self.s2_name, map(lambda x: x[\'name\'], servers),\n                      "%s not found in %s, all servers %s" %\n                      (self.s2_name, servers, all_servers))\n        self.assertIn(self.s3_name, map(lambda x: x[\'name\'], servers),\n                      "%s not found in %s, all servers %s" %\n                      (self.s3_name, servers, all_servers))\n\n    @decorators.idempotent_id(\'67aec2d0-35fe-4503-9f92-f13272b867ed\')\n    def test_list_servers_detailed_limit_results(self):\n        """Filter the detailed list of servers by limit 1\n\n        Verify only the expected number of servers are returned (one server)\n        """\n        params = {\'limit\': 1}\n        servers = self.client.list_servers(detail=True, **params)\n        self.assertEqual(1, len(servers[\'servers\']))\n', 'message': 'Merge "unskip test_list_servers_filtered_by_ip"', 'author_name': 'Gerrit Code Review', 'lang': 'py'}