summaryrefslogtreecommitdiffstats
path: root/tests/test_apex_network_settings.py
blob: a1dbaf1ce47104dac35f2b8b70e8be5fb6f08a9c (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
##############################################################################
# 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
##############################################################################

from apex.common.constants import (
    EXTERNAL_NETWORK,
    STORAGE_NETWORK,
    ADMIN_NETWORK,
)

from apex.network_settings import (
    NetworkSettings,
    NetworkSettingsException,
)

from nose.tools import (
    assert_equal,
    assert_is_instance,
    assert_raises
)

files_dir = '../config/network/'


class TestNetworkSettings(object):
    @classmethod
    def setup_class(klass):
        """This method is run once for each class before any tests are run"""

    @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_is_instance(
            NetworkSettings(files_dir+'network_settings.yaml'),
            NetworkSettings)

    def test_init_vlans(self):
        assert_is_instance(
            NetworkSettings(files_dir+'network_settings_vlans.yaml'),
            NetworkSettings)

# TODO, v6 test is stuck
    # def test_init_v6(self):
    #     assert_is_instance(
    #         NetworkSettings(files_dir+'network_settings_v6.yaml', True),
    #         NetworkSettings)

    def test_init_admin_disabled_or_missing(self):
        ns = NetworkSettings(files_dir+'network_settings.yaml')
        # remove admin, apex section will re-add it
        ns['networks'].pop('admin', None)
        assert_raises(NetworkSettingsException, NetworkSettings, ns)
        # remove admin and apex
        ns.pop('apex', None)
        ns['networks'].pop('admin', None)
        assert_raises(NetworkSettingsException, NetworkSettings, ns)

    def test_init_collapse_storage(self):
        ns = NetworkSettings(files_dir+'network_settings.yaml')
        # remove storage
        ns['networks'].pop('storage', None)
        assert_is_instance(NetworkSettings(ns), NetworkSettings)

    def test_init_missing_dns_domain(self):
        ns = NetworkSettings(files_dir+'network_settings.yaml')
        # remove storage
        ns.pop('dns-domain', None)
        assert_is_instance(NetworkSettings(ns), NetworkSettings)

    def test_dump_bash(self):
        ns = NetworkSettings('../config/network/network_settings.yaml')
        assert_equal(ns.dump_bash(), None)
        assert_equal(ns.dump_bash(path='/dev/null'), None)

    def test_get_network_settings(self):
        ns = NetworkSettings('../config/network/network_settings.yaml')
        assert_is_instance(ns, NetworkSettings)
        for role in ['controller', 'compute']:
            nic_index = 0
            print(ns.nics)
            for network in ns.enabled_network_list:
                nic = 'eth' + str(nic_index)
                assert_equal(ns.nics[role][network], nic)
                nic_index += 1

    def test_get_enabled_networks(self):
        ns = NetworkSettings('../config/network/network_settings.yaml')
        assert_is_instance(ns.enabled_network_list, list)

    def test_invalid_nic_members(self):
        ns = NetworkSettings(files_dir+'network_settings.yaml')
        storage_net_nicmap = ns['networks'][STORAGE_NETWORK]['nic_mapping']
        # set duplicate nic
        storage_net_nicmap['controller']['members'][0] = 'eth0'
        assert_raises(NetworkSettingsException, NetworkSettings, ns)
        # remove nic members
        storage_net_nicmap['controller']['members'] = []
        assert_raises(NetworkSettingsException, NetworkSettings, ns)

    def test_missing_vlan(self):
        ns = NetworkSettings(files_dir+'network_settings.yaml')
        storage_net_nicmap = ns['networks'][STORAGE_NETWORK]['nic_mapping']
        # remove vlan from storage net
        storage_net_nicmap['compute'].pop('vlan', None)
        assert_is_instance(NetworkSettings(ns), NetworkSettings)

# TODO
# need to manipulate interfaces some how
# maybe for ip_utils to return something to pass this
#    def test_admin_auto_detect(self):
#        ns = NetworkSettings(files_dir+'network_settings.yaml')
#        # remove cidr to force autodetection
#        ns['networks'][ADMIN_NETWORK].pop('cidr', None)
#        assert_is_instance(NetworkSettings(ns), NetworkSettings)

    def test_admin_fail_auto_detect(self):
        ns = NetworkSettings(files_dir+'network_settings.yaml')
        # remove cidr and installer_vm to fail autodetect
        ns['networks'][ADMIN_NETWORK].pop('cidr', None)
        ns['networks'][ADMIN_NETWORK].pop('installer_vm', None)
        assert_raises(NetworkSettingsException, NetworkSettings, ns)

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

    def test_config_ip(self):
        ns = NetworkSettings(files_dir+'network_settings.yaml')
        # set the provisioner ip to None to force _gen_ip to generate one
        ns['networks'][ADMIN_NETWORK]['installer_vm']['ip'] = None
        ns['networks'][EXTERNAL_NETWORK][0]['installer_vm']['ip'] = None
        # Now rebuild network settings object and check for repopulated values
        ns = NetworkSettings(ns)
        assert_equal(ns['networks'][ADMIN_NETWORK]['installer_vm']['ip'],
                     '192.0.2.1')
        assert_equal(ns['networks'][EXTERNAL_NETWORK][0]['installer_vm']['ip'],
                     '192.168.37.1')

    def test_config_gateway(self):
        ns = NetworkSettings(files_dir+'network_settings.yaml')
        # set the gateway ip to None to force _config_gateway to generate one
        ns['networks'][EXTERNAL_NETWORK][0]['gateway'] = None
        # Now rebuild network settings object and check for a repopulated value
        ns = NetworkSettings(ns)
        assert_equal(ns['networks'][EXTERNAL_NETWORK][0]['gateway'],
                     '192.168.37.1')