summaryrefslogtreecommitdiff
path: root/tempest/api/orchestration/stacks/test_neutron_resources.py
blob: ffadb1698fd0b81577ed78ea3f6521e6c781655b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#    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 logging

import netaddr

from tempest.api.orchestration import base
from tempest import clients
from tempest.common.utils import data_utils
from tempest import config
from tempest import exceptions
from tempest import test

CONF = config.CONF

LOG = logging.getLogger(__name__)


class NeutronResourcesTestJSON(base.BaseOrchestrationTest):

    @classmethod
    @test.safe_setup
    def setUpClass(cls):
        super(NeutronResourcesTestJSON, cls).setUpClass()
        if not CONF.orchestration.image_ref:
            raise cls.skipException("No image available to test")
        os = clients.Manager()
        if not CONF.service_available.neutron:
            raise cls.skipException("Neutron support is required")
        cls.neutron_basic_template = cls.load_template('neutron_basic')
        cls.network_client = os.network_client
        cls.stack_name = data_utils.rand_name('heat')
        template = cls.read_template('neutron_basic')
        cls.keypair_name = (CONF.orchestration.keypair_name or
                            cls._create_keypair()['name'])
        cls.external_network_id = CONF.network.public_network_id

        tenant_cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr)
        mask_bits = CONF.network.tenant_network_mask_bits
        cls.subnet_cidr = tenant_cidr.subnet(mask_bits).next()

        # create the stack
        cls.stack_identifier = cls.create_stack(
            cls.stack_name,
            template,
            parameters={
                'KeyName': cls.keypair_name,
                'InstanceType': CONF.orchestration.instance_type,
                'ImageId': CONF.orchestration.image_ref,
                'ExternalNetworkId': cls.external_network_id,
                'timeout': CONF.orchestration.build_timeout,
                'DNSServers': CONF.network.dns_servers,
                'SubNetCidr': str(cls.subnet_cidr)
            })
        cls.stack_id = cls.stack_identifier.split('/')[1]
        try:
            cls.client.wait_for_stack_status(cls.stack_id, 'CREATE_COMPLETE')
            _, resources = cls.client.list_resources(cls.stack_identifier)
        except exceptions.TimeoutException as e:
            if CONF.compute_feature_enabled.console_output:
                # attempt to log the server console to help with debugging
                # the cause of the server not signalling the waitcondition
                # to heat.
                _, body = cls.client.get_resource(cls.stack_identifier,
                                                  'Server')
                server_id = body['physical_resource_id']
                LOG.debug('Console output for %s', server_id)
                _, output = cls.servers_client.get_console_output(
                    server_id, None)
                LOG.debug(output)
            raise e

        cls.test_resources = {}
        for resource in resources:
            cls.test_resources[resource['logical_resource_id']] = resource

    @test.attr(type='slow')
    def test_created_resources(self):
        """Verifies created neutron resources."""
        resources = [('Network', self.neutron_basic_template['resources'][
                      'Network']['type']),
                     ('Subnet', self.neutron_basic_template['resources'][
                      'Subnet']['type']),
                     ('RouterInterface', self.neutron_basic_template[
                      'resources']['RouterInterface']['type']),
                     ('Server', self.neutron_basic_template['resources'][
                      'Server']['type'])]
        for resource_name, resource_type in resources:
            resource = self.test_resources.get(resource_name, None)
            self.assertIsInstance(resource, dict)
            self.assertEqual(resource_name, resource['logical_resource_id'])
            self.assertEqual(resource_type, resource['resource_type'])
            self.assertEqual('CREATE_COMPLETE', resource['resource_status'])

    @test.attr(type='slow')
    @test.services('network')
    def test_created_network(self):
        """Verifies created network."""
        network_id = self.test_resources.get('Network')['physical_resource_id']
        _, body = self.network_client.show_network(network_id)
        network = body['network']
        self.assertIsInstance(network, dict)
        self.assertEqual(network_id, network['id'])
        self.assertEqual(self.neutron_basic_template['resources'][
            'Network']['properties']['name'], network['name'])

    @test.attr(type='slow')
    @test.services('network')
    def test_created_subnet(self):
        """Verifies created subnet."""
        subnet_id = self.test_resources.get('Subnet')['physical_resource_id']
        _, body = self.network_client.show_subnet(subnet_id)
        subnet = body['subnet']
        network_id = self.test_resources.get('Network')['physical_resource_id']
        self.assertEqual(subnet_id, subnet['id'])
        self.assertEqual(network_id, subnet['network_id'])
        self.assertEqual(self.neutron_basic_template['resources'][
            'Subnet']['properties']['name'], subnet['name'])
        self.assertEqual(sorted(CONF.network.dns_servers),
                         sorted(subnet['dns_nameservers']))
        self.assertEqual(self.neutron_basic_template['resources'][
            'Subnet']['properties']['ip_version'], subnet['ip_version'])
        self.assertEqual(str(self.subnet_cidr), subnet['cidr'])

    @test.attr(type='slow')
    @test.services('network')
    def test_created_router(self):
        """Verifies created router."""
        router_id = self.test_resources.get('Router')['physical_resource_id']
        _, body = self.network_client.show_router(router_id)
        router = body['router']
        self.assertEqual(self.neutron_basic_template['resources'][
            'Router']['properties']['name'], router['name'])
        self.assertEqual(self.external_network_id,
                         router['external_gateway_info']['network_id'])
        self.assertEqual(True, router['admin_state_up'])

    @test.attr(type='slow')
    @test.services('network')
    def test_created_router_interface(self):
        """Verifies created router interface."""
        router_id = self.test_resources.get('Router')['physical_resource_id']
        network_id = self.test_resources.get('Network')['physical_resource_id']
        subnet_id = self.test_resources.get('Subnet')['physical_resource_id']
        _, body = self.network_client.list_ports()
        ports = body['ports']
        router_ports = filter(lambda port: port['device_id'] ==
                              router_id, ports)
        created_network_ports = filter(lambda port: port['network_id'] ==
                                       network_id, router_ports)
        self.assertEqual(1, len(created_network_ports))
        router_interface = created_network_ports[0]
        fixed_ips = router_interface['fixed_ips']
        subnet_fixed_ips = filter(lambda port: port['subnet_id'] ==
                                  subnet_id, fixed_ips)
        self.assertEqual(1, len(subnet_fixed_ips))
        router_interface_ip = subnet_fixed_ips[0]['ip_address']
        self.assertEqual(str(self.subnet_cidr.iter_hosts().next()),
                         router_interface_ip)

    @test.attr(type='slow')
    @test.services('compute', 'network')
    def test_created_server(self):
        """Verifies created sever."""
        server_id = self.test_resources.get('Server')['physical_resource_id']
        _, server = self.servers_client.get_server(server_id)
        self.assertEqual(self.keypair_name, server['key_name'])
        self.assertEqual('ACTIVE', server['status'])
        network = server['addresses'][self.neutron_basic_template['resources'][
                                      'Network']['properties']['name']][0]
        self.assertEqual(4, network['version'])
        self.assertIn(netaddr.IPAddress(network['addr']), self.subnet_cidr)