cloud-init-dev team mailing list archive
-
cloud-init-dev team
-
Mailing list archive
-
Message #03923
Re: [Merge] ~rjschwei/cloud-init:baseNetConfTestSUSE into cloud-init:master
I think if we re-factor the code just a bit we can make this much easier to test.
Heres an example:
http://paste.ubuntu.com/26164253/
The result then is that we can test 'write_network_from_eni' by simply calling:
tmpd = self.temp_dir()
devs = write_network_from_eni("your_eni_blob", target=tmpd)
self.assertEqual(expected_files_in_tmpd, dir2dict(self.tempd))
There are some comments inline, but I think if you go this route its easier to get
test coverage.
Diff comments:
> diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py
> index 8d0b263..9a62b71 100644
> --- a/tests/unittests/test_distros/test_netconfig.py
> +++ b/tests/unittests/test_distros/test_netconfig.py
> @@ -175,7 +176,7 @@ class WriteBuffer(object):
> return self.buffer.getvalue()
>
>
> -class TestNetCfgDistro(TestCase):
> +class TestNetCfgDistro(FilesystemMockingTestCase):
Since none of the other tests in this class use FileSystemMockingTestCase, if you need to use that, then I think we might as well just make a 'TestNetCfgOpenSuse' class that does that rather than changing this class to extend FilesystemMockingTestCase.
I think the only other f unction you're using is '_get_distro', and that doesn't even modify any 'self' vars, we can just split it out to a function.
basically, lots of ways to do this, but i didnt like changing the base class here.
>
> frbsd_ifout = """\
> hn0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
> @@ -771,4 +772,46 @@ ifconfig_vtnet0="DHCP"
> self.assertCfgEquals(expected_buf, str(write_buf))
> self.assertEqual(write_buf.mode, 0o644)
>
> + def test_simple_write_oopensuse(self):
> + """Opensuse network rendering writes appropriate sysconfg files."""
> + tmpdir = self.tmp_dir()
> + self.patchOS(tmpdir)
call reRoot rather than patchOS and patchUtils individually.
> + self.patchUtils(tmpdir)
> + distro = self._get_distro('opensuse')
> +
> + distro.apply_network(BASE_NET_CFG, False)
> +
> + lo_path = os.path.join(tmpdir, 'etc/sysconfig/network/ifcfg-lo')
> + eth0_path = os.path.join(tmpdir, 'etc/sysconfig/network/ifcfg-eth0')
> + eth1_path = os.path.join(tmpdir, 'etc/sysconfig/network/ifcfg-eth1')
> + expected_cfgs = {
> + lo_path: dedent('''
> + STARTMODE="auto"
> + USERCONTROL="no"
> + FIREWALL="no"
> + '''),
> + eth0_path: dedent('''
> + BOOTPROTO="static"
> + BROADCAST="192.168.1.0"
> + GATEWAY="192.168.1.254"
> + IPADDR="192.168.1.5"
> + NETMASK="255.255.255.0"
> + STARTMODE="auto"
> + USERCONTROL="no"
> + ETHTOOL_OPTIONS=""
> + '''),
> + eth1_path: dedent('''
> + BOOTPROTO="dhcp"
> + STARTMODE="auto"
> + USERCONTROL="no"
> + ETHTOOL_OPTIONS=""
> + ''')
> + }
> + for cfgpath in (lo_path, eth0_path, eth1_path):
you can use 'dir2dict' for this from helpers.
dir2dict(tmpdir)
will give you a dictioary with:
'etc/sysconfig/network/ifcfg-lo': 'content-of-file'
> + self.assertCfgEquals(
> + expected_cfgs[cfgpath],
> + util.load_file(cfgpath))
> + file_stat = os.stat(cfgpath)
> + self.assertEqual(0o644, stat.S_IMODE(file_stat.st_mode))
> +
> # vi: ts=4 expandtab
--
https://code.launchpad.net/~rjschwei/cloud-init/+git/cloud-init/+merge/333772
Your team cloud-init commiters is requested to review the proposed merge of ~rjschwei/cloud-init:baseNetConfTestSUSE into cloud-init:master.
References