4e3ae111e1ef1fbcbcfe4fd29c75b0585235e37f
[apex.git] / apex / tests / test_apex_network_environment.py
1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (Red Hat)
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import os
11
12 from copy import copy
13
14 from nose.tools import (
15     assert_equal,
16     assert_is_instance,
17     assert_not_equal,
18     assert_raises)
19
20 from apex.common.constants import (
21     EXTERNAL_NETWORK,
22     TENANT_NETWORK,
23     STORAGE_NETWORK,
24     API_NETWORK,
25     NET_ENV_FILE)
26 from apex import NetworkEnvironment
27 from apex.network.network_environment import NetworkEnvException
28 from apex import NetworkSettings
29 from apex.tests.constants import TEST_CONFIG_DIR
30 from apex.tests.constants import TEST_BUILD_DIR
31
32
33 class TestNetworkEnvironment:
34     @classmethod
35     def setup_class(cls):
36         """This method is run once for each class before any tests are run"""
37         cls.ns = NetworkSettings(
38             os.path.join(TEST_CONFIG_DIR, 'network/network_settings.yaml'))
39         cls.ns_vlans = NetworkSettings(
40             os.path.join(TEST_CONFIG_DIR,
41                          'network/network_settings_vlans.yaml'))
42         cls.ns_ipv6 = NetworkSettings(
43             os.path.join(TEST_CONFIG_DIR, 'network/network_settings_v6.yaml'))
44
45     @classmethod
46     def teardown_class(klass):
47         """This method is run once for each class _after_ all tests are run"""
48
49     def setup(self):
50         """This method is run once before _each_ test method is executed"""
51
52     def teardown(self):
53         """This method is run once after _each_ test method is executed"""
54
55     def test_init(self):
56         assert_raises(NetworkEnvException, NetworkEnvironment,
57                       None, os.path.join(TEST_BUILD_DIR, NET_ENV_FILE))
58
59     def test_netenv_settings_external_network_vlans(self):
60         # test vlans
61         ne = NetworkEnvironment(self.ns_vlans,
62                                 os.path.join(TEST_BUILD_DIR, NET_ENV_FILE))
63         assert_equal(ne['parameter_defaults']['NeutronExternalNetworkBridge'],
64                      '""')
65         assert_equal(ne['parameter_defaults']['ExternalNetworkVlanID'], 501)
66
67     def test_netenv_settings_external_network_ipv6(self):
68         # Test IPv6
69         ne = NetworkEnvironment(self.ns_ipv6,
70                                 os.path.join(TEST_BUILD_DIR, NET_ENV_FILE))
71         regstr = ne['resource_registry']['OS::TripleO::Network::External']
72         assert_equal(regstr.split('/')[-1], 'external_v6.yaml')
73
74     def test_netenv_settings_external_network_removed(self):
75         ns = copy(self.ns)
76         # Test removing EXTERNAL_NETWORK
77         ns.enabled_network_list.remove(EXTERNAL_NETWORK)
78         ne = NetworkEnvironment(ns, os.path.join(TEST_BUILD_DIR, NET_ENV_FILE))
79         regstr = ne['resource_registry']['OS::TripleO::Network::External']
80         assert_equal(regstr.split('/')[-1], 'OS::Heat::None')
81
82     def test_netenv_settings_tenant_network_vlans(self):
83         # test vlans
84         ne = NetworkEnvironment(self.ns_vlans,
85                                 os.path.join(TEST_BUILD_DIR, NET_ENV_FILE))
86         assert_equal(ne['parameter_defaults']['TenantNetworkVlanID'], 401)
87
88 # Apex is does not support v6 tenant networks
89 # Though there is code that would fire if a
90 # v6 cidr was passed in, just uncomment this to
91 # cover that code
92 #    def test_netenv_settings_tenant_network_v6(self):
93 #        # Test IPv6
94 #        ne = NetworkEnvironment(self.ns_ipv6,
95 #                                '../build/network-environment.yaml')
96 #        regstr = ne['resource_registry'][next(iter(TENANT_RESOURCES.keys()))]
97 #        assert_equal(regstr.split('/')[-1], 'tenant_v6.yaml')
98
99     def test_netenv_settings_tenant_network_removed(self):
100         ns = copy(self.ns)
101         # Test removing TENANT_NETWORK
102         ns.enabled_network_list.remove(TENANT_NETWORK)
103         ne = NetworkEnvironment(ns, os.path.join(TEST_BUILD_DIR, NET_ENV_FILE))
104         regstr = ne['resource_registry']['OS::TripleO::Network::Tenant']
105         assert_equal(regstr.split('/')[-1], 'OS::Heat::None')
106
107     def test_netenv_settings_storage_network_vlans(self):
108         # test vlans
109         ne = NetworkEnvironment(self.ns_vlans,
110                                 os.path.join(TEST_BUILD_DIR, NET_ENV_FILE))
111         assert_equal(ne['parameter_defaults']['StorageNetworkVlanID'], 201)
112
113     def test_netenv_settings_storage_network_v6(self):
114         # Test IPv6
115         ne = NetworkEnvironment(self.ns_ipv6,
116                                 os.path.join(TEST_BUILD_DIR, NET_ENV_FILE))
117         regstr = ne['resource_registry']['OS::TripleO::Network::Storage']
118         assert_equal(regstr.split('/')[-1], 'storage_v6.yaml')
119
120     def test_netenv_settings_storage_network_removed(self):
121         ns = copy(self.ns)
122         # Test removing STORAGE_NETWORK
123         ns.enabled_network_list.remove(STORAGE_NETWORK)
124         ne = NetworkEnvironment(ns, os.path.join(TEST_BUILD_DIR, NET_ENV_FILE))
125         regstr = ne['resource_registry']['OS::TripleO::Network::Storage']
126         assert_equal(regstr.split('/')[-1], 'OS::Heat::None')
127
128     def test_netenv_settings_api_network_v4(self):
129         ns = copy(self.ns_vlans)
130         ns['networks'][API_NETWORK]['enabled'] = True
131         ns['networks'][API_NETWORK]['cidr'] = '10.11.12.0/24'
132         ns = NetworkSettings(ns)
133         # test vlans
134         ne = NetworkEnvironment(ns, os.path.join(TEST_BUILD_DIR, NET_ENV_FILE))
135         assert_equal(ne['parameter_defaults']['InternalApiNetworkVlanID'], 101)
136
137     def test_netenv_settings_api_network_vlans(self):
138         ns = copy(self.ns_vlans)
139         ns['networks'][API_NETWORK]['enabled'] = True
140         ns = NetworkSettings(ns)
141         # test vlans
142         ne = NetworkEnvironment(ns, os.path.join(TEST_BUILD_DIR, NET_ENV_FILE))
143         assert_equal(ne['parameter_defaults']['InternalApiNetworkVlanID'], 101)
144
145     def test_netenv_settings_api_network_v6(self):
146         # Test IPv6
147         ne = NetworkEnvironment(self.ns_ipv6,
148                                 os.path.join(TEST_BUILD_DIR, NET_ENV_FILE))
149         regstr = ne['resource_registry']['OS::TripleO::Network::InternalApi']
150         assert_equal(regstr.split('/')[-1], 'internal_api_v6.yaml')
151
152     def test_netenv_settings_api_network_removed(self):
153         ns = copy(self.ns)
154         # API_NETWORK is not in the default network settings file
155         ne = NetworkEnvironment(ns, os.path.join(TEST_BUILD_DIR, NET_ENV_FILE))
156         regstr = ne['resource_registry']['OS::TripleO::Network::InternalApi']
157         assert_equal(regstr.split('/')[-1], 'OS::Heat::None')
158
159     def test_numa_configs(self):
160         ne = NetworkEnvironment(self.ns,
161                                 os.path.join(TEST_BUILD_DIR, NET_ENV_FILE),
162                                 compute_pre_config=True,
163                                 controller_pre_config=True)
164         assert_is_instance(ne, dict)
165         assert_not_equal(ne, {})
166
167     def test_exception(self):
168         e = NetworkEnvException("test")
169         print(e)
170         assert_is_instance(e, NetworkEnvException)