|
| 1 | +# vi: ts=4 expandtab |
| 2 | +# |
| 3 | +# Copyright (C) 2015 Canonical Ltd. |
| 4 | +# Copyright (C) 2016 VMware INC. |
| 5 | +# |
| 6 | +# Author: Sankar Tanguturi <stanguturi@vmware.com> |
| 7 | +# |
| 8 | +# This program is free software: you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License version 3, as |
| 10 | +# published by the Free Software Foundation. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License |
| 18 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +import logging |
| 21 | +import sys |
| 22 | +import unittest |
| 23 | + |
| 24 | +from cloudinit.sources.helpers.vmware.imc.boot_proto import BootProtoEnum |
| 25 | +from cloudinit.sources.helpers.vmware.imc.config import Config |
| 26 | +from cloudinit.sources.helpers.vmware.imc.config_file import ConfigFile |
| 27 | + |
| 28 | +logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) |
| 29 | +logger = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +class TestVmwareConfigFile(unittest.TestCase): |
| 33 | + |
| 34 | + def test_utility_methods(self): |
| 35 | + cf = ConfigFile("tests/data/vmware/cust-dhcp-2nic.cfg") |
| 36 | + |
| 37 | + cf.clear() |
| 38 | + |
| 39 | + self.assertEqual(0, cf.size(), "clear size") |
| 40 | + |
| 41 | + cf._insertKey(" PASSWORD|-PASS ", " foo ") |
| 42 | + cf._insertKey("BAR", " ") |
| 43 | + |
| 44 | + self.assertEqual(2, cf.size(), "insert size") |
| 45 | + self.assertEqual('foo', cf["PASSWORD|-PASS"], "password") |
| 46 | + self.assertTrue("PASSWORD|-PASS" in cf, "hasPassword") |
| 47 | + self.assertFalse(cf.should_keep_current_value("PASSWORD|-PASS"), |
| 48 | + "keepPassword") |
| 49 | + self.assertFalse(cf.should_remove_current_value("PASSWORD|-PASS"), |
| 50 | + "removePassword") |
| 51 | + self.assertFalse("FOO" in cf, "hasFoo") |
| 52 | + self.assertTrue(cf.should_keep_current_value("FOO"), "keepFoo") |
| 53 | + self.assertFalse(cf.should_remove_current_value("FOO"), "removeFoo") |
| 54 | + self.assertTrue("BAR" in cf, "hasBar") |
| 55 | + self.assertFalse(cf.should_keep_current_value("BAR"), "keepBar") |
| 56 | + self.assertTrue(cf.should_remove_current_value("BAR"), "removeBar") |
| 57 | + |
| 58 | + def test_configfile_static_2nics(self): |
| 59 | + cf = ConfigFile("tests/data/vmware/cust-static-2nic.cfg") |
| 60 | + |
| 61 | + conf = Config(cf) |
| 62 | + |
| 63 | + self.assertEqual('myhost1', conf.host_name, "hostName") |
| 64 | + self.assertEqual('Africa/Abidjan', conf.timezone, "tz") |
| 65 | + self.assertTrue(conf.utc, "utc") |
| 66 | + |
| 67 | + self.assertEqual(['10.20.145.1', '10.20.145.2'], |
| 68 | + conf.name_servers, |
| 69 | + "dns") |
| 70 | + self.assertEqual(['eng.vmware.com', 'proxy.vmware.com'], |
| 71 | + conf.dns_suffixes, |
| 72 | + "suffixes") |
| 73 | + |
| 74 | + nics = conf.nics |
| 75 | + ipv40 = nics[0].staticIpv4 |
| 76 | + |
| 77 | + self.assertEqual(2, len(nics), "nics") |
| 78 | + self.assertEqual('NIC1', nics[0].name, "nic0") |
| 79 | + self.assertEqual('00:50:56:a6:8c:08', nics[0].mac, "mac0") |
| 80 | + self.assertEqual(BootProtoEnum.STATIC, nics[0].bootProto, "bootproto0") |
| 81 | + self.assertEqual('10.20.87.154', ipv40[0].ip, "ipv4Addr0") |
| 82 | + self.assertEqual('255.255.252.0', ipv40[0].netmask, "ipv4Mask0") |
| 83 | + self.assertEqual(2, len(ipv40[0].gateways), "ipv4Gw0") |
| 84 | + self.assertEqual('10.20.87.253', ipv40[0].gateways[0], "ipv4Gw0_0") |
| 85 | + self.assertEqual('10.20.87.105', ipv40[0].gateways[1], "ipv4Gw0_1") |
| 86 | + |
| 87 | + self.assertEqual(1, len(nics[0].staticIpv6), "ipv6Cnt0") |
| 88 | + self.assertEqual('fc00:10:20:87::154', |
| 89 | + nics[0].staticIpv6[0].ip, |
| 90 | + "ipv6Addr0") |
| 91 | + |
| 92 | + self.assertEqual('NIC2', nics[1].name, "nic1") |
| 93 | + self.assertTrue(not nics[1].staticIpv6, "ipv61 dhcp") |
| 94 | + |
| 95 | + def test_config_file_dhcp_2nics(self): |
| 96 | + cf = ConfigFile("tests/data/vmware/cust-dhcp-2nic.cfg") |
| 97 | + |
| 98 | + conf = Config(cf) |
| 99 | + nics = conf.nics |
| 100 | + self.assertEqual(2, len(nics), "nics") |
| 101 | + self.assertEqual('NIC1', nics[0].name, "nic0") |
| 102 | + self.assertEqual('00:50:56:a6:8c:08', nics[0].mac, "mac0") |
| 103 | + self.assertEqual(BootProtoEnum.DHCP, nics[0].bootProto, "bootproto0") |
0 commit comments