Ejemplo n.º 1
0
    def test_basic_static(self):
        """Just the most basic static config.

        note 'lo' should not be rendered as an interface."""
        entries = {
            'eth0': {
                'auto': True,
                'dns-nameservers': ['8.8.8.8'],
                'bootproto': 'static',
                'address': '10.0.0.2',
                'gateway': '10.0.0.1',
                'netmask': '255.255.255.0'
            },
            'lo': {
                'auto': True
            }
        }
        target = self.tmp_dir()
        devs = _render_network(entries, target=target)
        files = dir2dict(target, prefix=target)
        self.assertEqual(['eth0'], devs)
        self.assertEqual(
            {
                '/etc/netctl/eth0':
                '\n'.join([
                    "Address=10.0.0.2/255.255.255.0", "Connection=ethernet",
                    "DNS=('8.8.8.8')", "Gateway=10.0.0.1", "IP=static",
                    "Interface=eth0", ""
                ]),
                '/etc/resolv.conf':
                'nameserver 8.8.8.8\n'
            }, files)
 def test_run_hook_up_dhcp4_prefix(self):
     """Test run_hook filters correctly with older DHCP4_ data."""
     nic = "eth0"
     dhc.run_hook(nic, "up", data_d=self.tmp, env=self.ex_env_dhcp4)
     found = dir2dict(self.tmp + os.path.sep)
     self.assertEqual([nic + ".json"], list(found.keys()))
     self.assertEqual(self.expected, json.loads(found[nic + ".json"]))
    def _apply_and_verify(
        self,
        apply_fn,
        config,
        expected_cfgs=None,
        bringup=False,
        with_netplan=False,
    ):
        if not expected_cfgs:
            raise ValueError("expected_cfg must not be None")

        tmpd = None
        with mock.patch("cloudinit.net.netplan.available",
                        return_value=with_netplan):
            with self.reRooted(tmpd) as tmpd:
                apply_fn(config, bringup)

        results = dir2dict(tmpd)
        for cfgpath, expected in expected_cfgs.items():
            print("----------")
            print(expected)
            print("^^^^ expected | rendered VVVVVVV")
            print(results[cfgpath])
            print("----------")
            self.assertEqual(expected, results[cfgpath])
            self.assertEqual(0o644, get_mode(cfgpath, tmpd))
 def test_run_hook_up(self):
     """Test expected use of run_hook_up."""
     nic = "eth0"
     dhc.run_hook(nic, "up", data_d=self.tmp, env=self.ex_env)
     found = dir2dict(self.tmp + os.path.sep)
     self.assertEqual([nic + ".json"], list(found.keys()))
     self.assertEqual(self.expected, json.loads(found[nic + ".json"]))
    def _apply_and_verify_freebsd(self,
                                  apply_fn,
                                  config,
                                  expected_cfgs=None,
                                  bringup=False):
        if not expected_cfgs:
            raise ValueError("expected_cfg must not be None")

        tmpd = None
        with mock.patch("cloudinit.net.freebsd.available") as m_avail:
            m_avail.return_value = True
            with self.reRooted(tmpd) as tmpd:
                util.ensure_dir("/etc")
                util.ensure_file("/etc/rc.conf")
                util.ensure_file("/etc/resolv.conf")
                apply_fn(config, bringup)

        results = dir2dict(tmpd)
        for cfgpath, expected in expected_cfgs.items():
            print("----------")
            print(expected)
            print("^^^^ expected | rendered VVVVVVV")
            print(results[cfgpath])
            print("----------")
            self.assertEqual(set(expected.split("\n")),
                             set(results[cfgpath].split("\n")))
            self.assertEqual(0o644, get_mode(cfgpath, tmpd))
 def test_run_hook_up_creates_dir(self):
     """If dir does not exist, run_hook should create it."""
     subd = self.tmp_path("subdir", self.tmp)
     nic = "eth1"
     dhc.run_hook(nic, "up", data_d=subd, env=self.ex_env)
     self.assertEqual(
         set([nic + ".json"]), set(dir2dict(subd + os.path.sep))
     )
 def test_handle_args(self):
     """quick test of call to handle_args."""
     nic = "eth0"
     args = argparse.Namespace(event=dhc.UP, interface=nic)
     with mock.patch.dict("os.environ", clear=True, values=self.ex_env):
         dhc.handle_args(dhc.NAME, args, data_d=self.tmp)
     found = dir2dict(self.tmp + os.path.sep)
     self.assertEqual([nic + ".json"], list(found.keys()))
     self.assertEqual(self.expected, json.loads(found[nic + ".json"]))
 def test_run_hook_down_deletes(self):
     """down should delete the created json file."""
     nic = "eth1"
     populate_dir(
         self.tmp, {nic + ".json": "{'abcd'}", "myfile.txt": "text"}
     )
     dhc.run_hook(nic, "down", data_d=self.tmp, env={"old_host_name": "x1"})
     self.assertEqual(
         set(["myfile.txt"]), set(dir2dict(self.tmp + os.path.sep))
     )
    def _apply_and_verify(self,
                          apply_fn,
                          config,
                          expected_cfgs=None,
                          bringup=False):
        if not expected_cfgs:
            raise ValueError("expected_cfg must not be None")

        tmpd = None
        with mock.patch("cloudinit.net.sysconfig.available") as m_avail:
            m_avail.return_value = True
            with self.reRooted(tmpd) as tmpd:
                apply_fn(config, bringup)

        results = dir2dict(tmpd)
        for cfgpath, expected in expected_cfgs.items():
            self.assertCfgEquals(expected, results[cfgpath])
            self.assertEqual(0o644, get_mode(cfgpath, tmpd))
    def _apply_and_verify(self,
                          apply_fn,
                          config,
                          expected_cfgs=None,
                          bringup=False):
        if not expected_cfgs:
            raise ValueError("expected_cfg must not be None")

        tmpd = None
        with mock.patch("cloudinit.net.networkd.available") as m_avail:
            m_avail.return_value = True
            with self.reRooted(tmpd) as tmpd:
                apply_fn(config, bringup)

        results = dir2dict(tmpd)
        for cfgpath, expected in expected_cfgs.items():
            actual = self.create_conf_dict(results[cfgpath].splitlines())
            self.compare_dicts(actual, expected)
            self.assertEqual(0o644, get_mode(cfgpath, tmpd))
Ejemplo n.º 11
0
    def test_basic_static(self):
        """Just the most basic static config.

        note 'lo' should not be rendered as an interface."""
        entries = {
            "eth0": {
                "auto": True,
                "dns-nameservers": ["8.8.8.8"],
                "bootproto": "static",
                "address": "10.0.0.2",
                "gateway": "10.0.0.1",
                "netmask": "255.255.255.0",
            },
            "lo": {
                "auto": True
            },
        }
        target = self.tmp_dir()
        devs = _render_network(entries, target=target)
        files = dir2dict(target, prefix=target)
        self.assertEqual(["eth0"], devs)
        self.assertEqual(
            {
                "/etc/netctl/eth0":
                "\n".join([
                    "Address=10.0.0.2/255.255.255.0",
                    "Connection=ethernet",
                    "DNS=('8.8.8.8')",
                    "Gateway=10.0.0.1",
                    "IP=static",
                    "Interface=eth0",
                    "",
                ]),
                "/etc/resolv.conf":
                "nameserver 8.8.8.8\n",
            },
            files,
        )
Ejemplo n.º 12
0
    def _render_and_read(self,
                         network_config=None,
                         state=None,
                         netplan_path=None,
                         target=None):
        if target is None:
            target = self.tmp_dir()
            os.mkdir("%s/etc" % target)
            with open("%s/etc/rc.conf" % target, 'a') as fd:
                fd.write("# dummy rc.conf\n")
            with open("%s/etc/resolv.conf" % target, 'a') as fd:
                fd.write("# dummy resolv.conf\n")

        if network_config:
            ns = cloudinit.net.network_state.parse_net_config_data(
                network_config)
        elif state:
            ns = state
        else:
            raise ValueError("Expected data or state, got neither")

        renderer = cloudinit.net.freebsd.Renderer()
        renderer.render_network_state(ns, target=target)
        return dir2dict(target)