summaryrefslogtreecommitdiffstats
path: root/tests/test_apex_network_environment.py
blob: 7c9b922fd12520f6acf22b915d9408e5d5ab8d25 (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
##############################################################################
# Copyright (c) 2016 Dan Radez (Red Hat)
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import ipaddress

from copy import copy

from apex.common.constants import (
    EXTERNAL_NETWORK,
    TENANT_NETWORK,
    STORAGE_NETWORK,
    API_NETWORK,
    CONTROLLER)
from apex.network_settings import NetworkSettings
from apex.network_environment import (
    NetworkEnvironment,
    NetworkEnvException,
    EXTERNAL_RESOURCES,
    TENANT_RESOURCES,
    STORAGE_RESOURCES,
    API_RESOURCES)

from nose.tools import assert_equal
from nose.tools import assert_raises
from nose.tools import assert_is_instance
from nose.tools import assert_not_equal


class TestNetworkEnvironment(object):
    @classmethod
    def setup_class(klass):
        """This method is run once for each class before any tests are run"""
        klass.ns = NetworkSettings(
            '../config/network/network_settings.yaml', True)
        klass.ns_vlans = NetworkSettings(
            '../config/network/network_settings_vlans.yaml', True)
        klass.ns_ipv6 = NetworkSettings(
            '../config/network/network_settings_v6.yaml', True)

    @classmethod
    def teardown_class(klass):
        """This method is run once for each class _after_ all tests are run"""

    def setUp(self):
        """This method is run once before _each_ test method is executed"""

    def teardown(self):
        """This method is run once after _each_ test method is executed"""

    def test_init(self):
        assert_raises(NetworkEnvException, NetworkEnvironment,
                      None, '../build/network-environment.yaml')

    def test_netenv_settings_external_network_vlans(self):
        # test vlans
        ne = NetworkEnvironment(self.ns_vlans,
                                '../build/network-environment.yaml')
        assert_equal(ne['parameter_defaults']['NeutronExternalNetworkBridge'],
                     '""')
        assert_equal(ne['parameter_defaults']['ExternalNetworkVlanID'], 501)

    def test_netenv_settings_external_network_ipv6(self):
        # Test IPv6
        ne = NetworkEnvironment(self.ns_ipv6,
                                '../build/network-environment.yaml')
        regstr = ne['resource_registry'][next(iter(EXTERNAL_RESOURCES.keys()))]
        assert_equal(regstr.split('/')[-1], 'external_v6.yaml')

    def test_netenv_settings_external_network_removed(self):
        ns = copy(self.ns)
        # Test removing EXTERNAL_NETWORK
        ns.enabled_network_list.remove(EXTERNAL_NETWORK)
        ne = NetworkEnvironment(ns, '../build/network-environment.yaml')
        regstr = ne['resource_registry'][next(iter(EXTERNAL_RESOURCES.keys()))]
        assert_equal(regstr.split('/')[-1], 'noop.yaml')

    def test_netenv_settings_tenant_network_vlans(self):
        # test vlans
        ne = NetworkEnvironment(self.ns_vlans,
                                '../build/network-environment.yaml')
        assert_equal(ne['parameter_defaults']['TenantNetworkVlanID'], 401)

# Apex is does not support v6 tenant networks
# Though there is code that would fire if a
# v6 cidr was passed in, just uncomment this to
# cover that code
#    def test_netenv_settings_tenant_network_v6(self):
#        # Test IPv6
#        ne = NetworkEnvironment(self.ns_ipv6,
#                                '../build/network-environment.yaml')
#        regstr = ne['resource_registry'][next(iter(TENANT_RESOURCES.keys()))]
#        assert_equal(regstr.split('/')[-1], 'tenant_v6.yaml')

    def test_netenv_settings_tenant_network_removed(self):
        ns = copy(self.ns)
        # Test removing TENANT_NETWORK
        ns.enabled_network_list.remove(TENANT_NETWORK)
        ne = NetworkEnvironment(ns, '../build/network-environment.yaml')
        regstr = ne['resource_registry'][next(iter(TENANT_RESOURCES.keys()))]
        assert_equal(regstr.split('/')[-1], 'noop.yaml')

    def test_netenv_settings_storage_network_vlans(self):
        # test vlans
        ne = NetworkEnvironment(self.ns_vlans,
                                '../build/network-environment.yaml')
        assert_equal(ne['parameter_defaults']['StorageNetworkVlanID'], 201)

    def test_netenv_settings_storage_network_v6(self):
        # Test IPv6
        ne = NetworkEnvironment(self.ns_ipv6,
                                '../build/network-environment.yaml')
        regstr = ne['resource_registry'][next(iter(STORAGE_RESOURCES.keys()))]
        assert_equal(regstr.split('/')[-1], 'storage_v6.yaml')

    def test_netenv_settings_storage_network_removed(self):
        ns = copy(self.ns)
        # Test removing STORAGE_NETWORK
        ns.enabled_network_list.remove(STORAGE_NETWORK)
        ne = NetworkEnvironment(ns, '../build/network-environment.yaml')
        regstr = ne['resource_registry'][next(iter(STORAGE_RESOURCES.keys()))]
        assert_equal(regstr.split('/')[-1], 'noop.yaml')

    def test_netenv_settings_api_network_v4(self):
        ns = copy(self.ns_vlans)
        ns['networks'][API_NETWORK]['enabled'] = True
        ns['networks'][API_NETWORK]['cidr'] = '10.11.12.0/24'
        ns = NetworkSettings(ns, True)
        # test vlans
        ne = NetworkEnvironment(ns, '../build/network-environment.yaml')
        assert_equal(ne['parameter_defaults']['InternalApiNetworkVlanID'], 101)

    def test_netenv_settings_api_network_vlans(self):
        ns = copy(self.ns_vlans)
        ns['networks'][API_NETWORK]['enabled'] = True
        ns = NetworkSettings(ns, True)
        # test vlans
        ne = NetworkEnvironment(ns, '../build/network-environment.yaml')
        assert_equal(ne['parameter_defaults']['InternalApiNetworkVlanID'], 101)

    def test_netenv_settings_api_network_v6(self):
        # Test IPv6
        ne = NetworkEnvironment(self.ns_ipv6,
                                '../build/network-environment.yaml')
        regstr = ne['resource_registry'][next(iter(API_RESOURCES.keys()))]
        assert_equal(regstr.split('/')[-1], 'internal_api_v6.yaml')

    def test_netenv_settings_api_network_removed(self):
        ns = copy(self.ns)
        # API_NETWORK is not in the default network settings file
        ne = NetworkEnvironment(ns, '../build/network-environment.yaml')
        regstr = ne['resource_registry'][next(iter(API_RESOURCES.keys()))]
        assert_equal(regstr.split('/')[-1], 'noop.yaml')

    def test_numa_configs(self):
        ne = NetworkEnvironment(self.ns, '../build/network-environment.yaml',
                                compute_pre_config=True,
                                controller_pre_config=True)
        assert_is_instance(ne, dict)
        assert_not_equal(ne, {})

    def test_exception(self):
        e = NetworkEnvException("test")
        print(e)
        assert_is_instance(e, NetworkEnvException)