summaryrefslogtreecommitdiff
path: root/cloudinit/net/tests/test_network_state.py
blob: 07d726e2638ca2e85a4ba93e6fe00e48906b860f (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
# This file is part of cloud-init. See LICENSE file for license information.

from unittest import mock

from cloudinit.net import network_state
from cloudinit.tests.helpers import CiTestCase

netstate_path = 'cloudinit.net.network_state'


class TestNetworkStateParseConfig(CiTestCase):

    def setUp(self):
        super(TestNetworkStateParseConfig, self).setUp()
        nsi_path = netstate_path + '.NetworkStateInterpreter'
        self.add_patch(nsi_path, 'm_nsi')

    def test_missing_version_returns_none(self):
        ncfg = {}
        self.assertEqual(None, network_state.parse_net_config_data(ncfg))

    def test_unknown_versions_returns_none(self):
        ncfg = {'version': 13.2}
        self.assertEqual(None, network_state.parse_net_config_data(ncfg))

    def test_version_2_passes_self_as_config(self):
        ncfg = {'version': 2, 'otherconfig': {}, 'somemore': [1, 2, 3]}
        network_state.parse_net_config_data(ncfg)
        self.assertEqual([mock.call(version=2, config=ncfg)],
                         self.m_nsi.call_args_list)

    def test_valid_config_gets_network_state(self):
        ncfg = {'version': 2, 'otherconfig': {}, 'somemore': [1, 2, 3]}
        result = network_state.parse_net_config_data(ncfg)
        self.assertNotEqual(None, result)

    def test_empty_v1_config_gets_network_state(self):
        ncfg = {'version': 1, 'config': []}
        result = network_state.parse_net_config_data(ncfg)
        self.assertNotEqual(None, result)

    def test_empty_v2_config_gets_network_state(self):
        ncfg = {'version': 2}
        result = network_state.parse_net_config_data(ncfg)
        self.assertNotEqual(None, result)


class TestNetworkStateParseConfigV2(CiTestCase):

    def test_version_2_ignores_renderer_key(self):
        ncfg = {'version': 2, 'renderer': 'networkd', 'ethernets': {}}
        nsi = network_state.NetworkStateInterpreter(version=ncfg['version'],
                                                    config=ncfg)
        nsi.parse_config(skip_broken=False)
        self.assertEqual(ncfg, nsi.as_dict()['config'])


# vi: ts=4 expandtab