Exemplo n.º 1
0
 def test_create_network(self):
     network = fake_network_cache_model.new_network()
     self.assertEqual(network['id'], 1)
     self.assertEqual(network['bridge'], 'br0')
     self.assertEqual(network['label'], 'public')
     self.assertEqual(network['subnets'],
             [fake_network_cache_model.new_subnet(),
              fake_network_cache_model.new_subnet(
                     dict(cidr='255.255.255.255'))])
Exemplo n.º 2
0
 def test_add_subnet(self):
     network = fake_network_cache_model.new_network()
     network.add_subnet(fake_network_cache_model.new_subnet(
                 dict(cidr='0.0.0.0')))
     self.assertEqual(network['subnets'],
             [fake_network_cache_model.new_subnet(),
              fake_network_cache_model.new_subnet(
                     dict(cidr='255.255.255.255')),
              fake_network_cache_model.new_subnet(dict(cidr='0.0.0.0'))])
Exemplo n.º 3
0
 def test_create_network(self):
     network = fake_network_cache_model.new_network()
     self.assertEqual(network["id"], 1)
     self.assertEqual(network["bridge"], "br0")
     self.assertEqual(network["label"], "public")
     self.assertEqual(
         network["subnets"],
         [fake_network_cache_model.new_subnet(), fake_network_cache_model.new_subnet(dict(cidr="255.255.255.255"))],
     )
Exemplo n.º 4
0
    def _test_injected_network_template(self, should_inject, use_ipv6=False,
                                        gateway=True):
        """Check that netutils properly decides whether to inject based on
           whether the supplied subnet is static or dynamic.
        """
        network = fake_network_cache_model.new_network({'subnets': []})
        subnet_dict = {}
        if not gateway:
            subnet_dict['gateway'] = None

        if not should_inject:
            subnet_dict['dhcp_server'] = '10.10.0.1'

        network.add_subnet(fake_network_cache_model.new_subnet(subnet_dict))

        if should_inject and use_ipv6:
            gateway_ip = fake_network_cache_model.new_ip(dict(
                address='1234:567::1'))
            ip = fake_network_cache_model.new_ip(dict(
                address='1234:567::2'))
            ipv6_subnet_dict = dict(
                cidr='1234:567::/48',
                gateway=gateway_ip,
                ips=[ip])
            if not gateway:
                ipv6_subnet_dict['gateway'] = None
            network.add_subnet(fake_network_cache_model.new_subnet(
                ipv6_subnet_dict))

        # Behave as though CONF.flat_injected is True
        network['meta']['injected'] = True
        vif = fake_network_cache_model.new_vif({'network': network})
        ninfo = model.NetworkInfo([vif])

        template = netutils.get_injected_network_template(ninfo,
                                                          use_ipv6=use_ipv6)

        # will just ignore the improper behavior.
        if not should_inject:
            self.assertTrue(template is None)
        else:
            self.assertTrue('auto eth0' in template)
            self.assertTrue('iface eth0 inet static' in template)
            self.assertTrue('address 10.10.0.2' in template)
            self.assertTrue('netmask 255.255.255.0' in template)
            self.assertTrue('broadcast 10.10.0.255' in template)
            if gateway:
                self.assertTrue('gateway 10.10.0.1' in template)
            else:
                self.assertFalse('gateway' in template)
            self.assertTrue('dns-nameservers 1.2.3.4 2.3.4.5' in template)
            if use_ipv6:
                self.assertTrue('iface eth0 inet6 static' in template)
                self.assertTrue('address 1234:567::2' in template)
                self.assertTrue('netmask 48' in template)
                if gateway:
                    self.assertTrue('gateway 1234:567::1' in template)
Exemplo n.º 5
0
    def _setup_injected_network_scenario(self,
                                         should_inject=True,
                                         use_ipv4=True,
                                         use_ipv6=False,
                                         gateway=True,
                                         dns=True,
                                         two_interfaces=False,
                                         libvirt_virt_type=None):
        """Check that netutils properly decides whether to inject based on
           whether the supplied subnet is static or dynamic.
        """
        network = fake_network_cache_model.new_network({'subnets': []})

        subnet_dict = {}
        if not gateway:
            subnet_dict['gateway'] = None

        if not dns:
            subnet_dict['dns'] = None

        if not should_inject:
            subnet_dict['dhcp_server'] = '10.10.0.1'

        if use_ipv4:
            network.add_subnet(
                fake_network_cache_model.new_subnet(subnet_dict))

        if should_inject and use_ipv6:
            gateway_ip = fake_network_cache_model.new_ip(
                dict(address='1234:567::1'))
            ip = fake_network_cache_model.new_ip(dict(address='1234:567::2'))
            ipv6_subnet_dict = dict(
                cidr='1234:567::/48',
                gateway=gateway_ip,
                dns=[
                    fake_network_cache_model.new_ip(
                        dict(address='2001:4860:4860::8888')),
                    fake_network_cache_model.new_ip(
                        dict(address='2001:4860:4860::8844'))
                ],
                ips=[ip])
            if not gateway:
                ipv6_subnet_dict['gateway'] = None
            network.add_subnet(
                fake_network_cache_model.new_subnet(ipv6_subnet_dict))

        # Behave as though CONF.flat_injected is True
        network['meta']['injected'] = True
        vif = fake_network_cache_model.new_vif({'network': network})
        vifs = [vif]
        if two_interfaces:
            vifs.append(vif)

        nwinfo = model.NetworkInfo(vifs)
        return netutils.get_injected_network_template(
            nwinfo, use_ipv6=use_ipv6, libvirt_virt_type=libvirt_virt_type)
Exemplo n.º 6
0
 def test_add_subnet(self):
     network = fake_network_cache_model.new_network()
     network.add_subnet(fake_network_cache_model.new_subnet(dict(cidr="0.0.0.0")))
     self.assertEqual(
         network["subnets"],
         [
             fake_network_cache_model.new_subnet(),
             fake_network_cache_model.new_subnet(dict(cidr="255.255.255.255")),
             fake_network_cache_model.new_subnet(dict(cidr="0.0.0.0")),
         ],
     )
Exemplo n.º 7
0
    def _test_injected_network_template(self, should_inject, use_ipv6=False,
                                        legacy=False):
        """Check that netutils properly decides whether to inject based on
           whether the supplied subnet is static or dynamic.
        """
        network = fake_network_cache_model.new_network({'subnets': []})
        if should_inject:
            network.add_subnet(fake_network_cache_model.new_subnet())
            if use_ipv6:
                gateway_ip = fake_network_cache_model.new_ip(dict(
                                                        address='1234:567::1'))
                ip = fake_network_cache_model.new_ip(dict(
                                                        address='1234:567::2'))
                subnet_dict = dict(
                        cidr='1234:567::/48',
                        gateway=gateway_ip,
                        ips=[ip])
                network.add_subnet(fake_network_cache_model.new_subnet(
                        subnet_dict))
        else:
            subnet_dict = dict(dhcp_server='10.10.0.1')
            network.add_subnet(fake_network_cache_model.new_subnet(
                    subnet_dict))
        # Behave as though CONF.flat_injected is True
        network['meta']['injected'] = True
        vif = fake_network_cache_model.new_vif({'network': network})
        ninfo = model.NetworkInfo([vif])
        if legacy:
            ninfo = ninfo.legacy()

        template = netutils.get_injected_network_template(ninfo,
                                                          use_ipv6=use_ipv6)

        # NOTE(bnemec): There is a bug with legacy network info that causes
        # it to inject regardless of whether the network is static or dynamic.
        # This can't be fixed without changes that would potentially break
        # existing code, so until legacy network info goes away this test
        # will just ignore the improper behavior.
        if not should_inject and not legacy:
            self.assertTrue(template is None)
        else:
            self.assertTrue('auto eth0' in template)
            self.assertTrue('iface eth0 inet static' in template)
            self.assertTrue('address 10.10.0.2' in template)
            self.assertTrue('netmask 255.255.255.0' in template)
            self.assertTrue('broadcast 10.10.0.255' in template)
            self.assertTrue('gateway 10.10.0.1' in template)
            self.assertTrue('dns-nameservers 1.2.3.4 2.3.4.5' in template)
            if use_ipv6:
                self.assertTrue('iface eth0 inet6 static' in template)
                self.assertTrue('address 1234:567::2' in template)
                self.assertTrue('netmask 48' in template)
                self.assertTrue('gateway 1234:567::1' in template)
Exemplo n.º 8
0
    def _setup_injected_network_scenario(self, should_inject=True,
                                        use_ipv4=True, use_ipv6=False,
                                        gateway=True, dns=True,
                                        two_interfaces=False,
                                        libvirt_virt_type=None):
        """Check that netutils properly decides whether to inject based on
           whether the supplied subnet is static or dynamic.
        """
        network = fake_network_cache_model.new_network({'subnets': []})

        subnet_dict = {}
        if not gateway:
            subnet_dict['gateway'] = None

        if not dns:
            subnet_dict['dns'] = None

        if not should_inject:
            subnet_dict['dhcp_server'] = '10.10.0.1'

        if use_ipv4:
            network.add_subnet(
                fake_network_cache_model.new_subnet(subnet_dict))

        if should_inject and use_ipv6:
            gateway_ip = fake_network_cache_model.new_ip(dict(
                address='1234:567::1'))
            ip = fake_network_cache_model.new_ip(dict(
                address='1234:567::2'))
            ipv6_subnet_dict = dict(
                cidr='1234:567::/48',
                gateway=gateway_ip,
                dns=[fake_network_cache_model.new_ip(
                        dict(address='2001:4860:4860::8888')),
                     fake_network_cache_model.new_ip(
                         dict(address='2001:4860:4860::8844'))],
                ips=[ip])
            if not gateway:
                ipv6_subnet_dict['gateway'] = None
            network.add_subnet(fake_network_cache_model.new_subnet(
                ipv6_subnet_dict))

        # Behave as though CONF.flat_injected is True
        network['meta']['injected'] = True
        vif = fake_network_cache_model.new_vif({'network': network})
        vifs = [vif]
        if two_interfaces:
            vifs.append(vif)

        nwinfo = model.NetworkInfo(vifs)
        return netutils.get_injected_network_template(
                nwinfo, use_ipv6=use_ipv6, libvirt_virt_type=libvirt_virt_type)
Exemplo n.º 9
0
 def test_add_route_a_lot(self):
     subnet = fake_network_cache_model.new_subnet()
     route1 = fake_network_cache_model.new_route()
     route2 = fake_network_cache_model.new_route({'cidr': '1.1.1.1/24'})
     for i in xrange(10):
         subnet.add_route(route2)
     self.assertEqual(subnet['routes'], [route1, route2])
Exemplo n.º 10
0
 def test_add_route_a_lot(self):
     subnet = fake_network_cache_model.new_subnet()
     route1 = fake_network_cache_model.new_route()
     route2 = fake_network_cache_model.new_route({"cidr": "1.1.1.1/24"})
     for i in xrange(10):
         subnet.add_route(route2)
     self.assertEqual(subnet["routes"], [route1, route2])
Exemplo n.º 11
0
 def test_add_dns(self):
     subnet = fake_network_cache_model.new_subnet()
     dns = fake_network_cache_model.new_ip(dict(address='9.9.9.9'))
     subnet.add_dns(dns)
     self.assertEqual(subnet['dns'],
             [fake_network_cache_model.new_ip(dict(address='1.2.3.4')),
              fake_network_cache_model.new_ip(dict(address='2.3.4.5')),
              fake_network_cache_model.new_ip(dict(address='9.9.9.9'))])
Exemplo n.º 12
0
    def test_hydrate(self):
        new_network = dict(
            id=1,
            bridge='br0',
            label='public',
            subnets=[fake_network_cache_model.new_subnet(),
                fake_network_cache_model.new_subnet(
                        dict(cidr='255.255.255.255'))])
        network = model.Network.hydrate(fake_network_cache_model.new_network())

        self.assertEqual(network['id'], 1)
        self.assertEqual(network['bridge'], 'br0')
        self.assertEqual(network['label'], 'public')
        self.assertEqual(network['subnets'],
                [fake_network_cache_model.new_subnet(),
                 fake_network_cache_model.new_subnet(
                        dict(cidr='255.255.255.255'))])
Exemplo n.º 13
0
    def _setup_injected_network_scenario(
        self,
        should_inject=True,
        use_ipv4=True,
        use_ipv6=False,
        gateway=True,
        dns=True,
        two_interfaces=False,
        libvirt_virt_type=None,
    ):
        """Check that netutils properly decides whether to inject based on
           whether the supplied subnet is static or dynamic.
        """
        network = fake_network_cache_model.new_network({"subnets": []})

        subnet_dict = {}
        if not gateway:
            subnet_dict["gateway"] = None

        if not dns:
            subnet_dict["dns"] = None

        if not should_inject:
            subnet_dict["dhcp_server"] = "10.10.0.1"

        if use_ipv4:
            network.add_subnet(fake_network_cache_model.new_subnet(subnet_dict))

        if should_inject and use_ipv6:
            gateway_ip = fake_network_cache_model.new_ip(dict(address="1234:567::1"))
            ip = fake_network_cache_model.new_ip(dict(address="1234:567::2"))
            ipv6_subnet_dict = dict(cidr="1234:567::/48", gateway=gateway_ip, ips=[ip])
            if not gateway:
                ipv6_subnet_dict["gateway"] = None
            network.add_subnet(fake_network_cache_model.new_subnet(ipv6_subnet_dict))

        # Behave as though CONF.flat_injected is True
        network["meta"]["injected"] = True
        vif = fake_network_cache_model.new_vif({"network": network})
        vifs = [vif]
        if two_interfaces:
            vifs.append(vif)

        nwinfo = model.NetworkInfo(vifs)
        return netutils.get_injected_network_template(nwinfo, use_ipv6=use_ipv6, libvirt_virt_type=libvirt_virt_type)
Exemplo n.º 14
0
 def test_add_dns_a_lot(self):
     subnet = fake_network_cache_model.new_subnet()
     for i in xrange(10):
         subnet.add_dns(fake_network_cache_model.new_ip(
                 dict(address='9.9.9.9')))
     self.assertEqual(subnet['dns'],
             [fake_network_cache_model.new_ip(dict(address='1.2.3.4')),
              fake_network_cache_model.new_ip(dict(address='2.3.4.5')),
              fake_network_cache_model.new_ip(dict(address='9.9.9.9'))])
Exemplo n.º 15
0
 def test_add_ip(self):
     subnet = fake_network_cache_model.new_subnet()
     subnet.add_ip(
         fake_network_cache_model.new_ip(dict(address='192.168.1.102')))
     self.assertEqual(subnet['ips'], [
         fake_network_cache_model.new_ip(dict(address='192.168.1.100')),
         fake_network_cache_model.new_ip(dict(address='192.168.1.101')),
         fake_network_cache_model.new_ip(dict(address='192.168.1.102'))
     ])
Exemplo n.º 16
0
 def test_add_ip_a_lot(self):
     subnet = fake_network_cache_model.new_subnet()
     for i in xrange(10):
         subnet.add_ip(
             fake_network_cache_model.new_ip(dict(address='192.168.1.102')))
     self.assertEqual(subnet['ips'], [
         fake_network_cache_model.new_ip(dict(address='10.10.0.2')),
         fake_network_cache_model.new_ip(dict(address='10.10.0.3')),
         fake_network_cache_model.new_ip(dict(address='192.168.1.102'))
     ])
Exemplo n.º 17
0
 def test_add_ip(self):
     subnet = fake_network_cache_model.new_subnet()
     subnet.add_ip(fake_network_cache_model.new_ip(dict(address="192.168.1.102")))
     self.assertEqual(
         subnet["ips"],
         [
             fake_network_cache_model.new_fixed_ip(dict(address="10.10.0.2")),
             fake_network_cache_model.new_fixed_ip(dict(address="10.10.0.3")),
             fake_network_cache_model.new_ip(dict(address="192.168.1.102")),
         ],
     )
Exemplo n.º 18
0
 def test_add_ip(self):
     subnet = fake_network_cache_model.new_subnet()
     subnet.add_ip(fake_network_cache_model.new_ip(
             dict(address='192.168.1.102')))
     self.assertEqual(subnet['ips'],
             [fake_network_cache_model.new_ip(
                     dict(address='10.10.0.2')),
              fake_network_cache_model.new_ip(
                     dict(address='10.10.0.3')),
              fake_network_cache_model.new_ip(
                     dict(address='192.168.1.102'))])
Exemplo n.º 19
0
 def test_add_dns_a_lot(self):
     subnet = fake_network_cache_model.new_subnet()
     for i in xrange(10):
         subnet.add_dns(fake_network_cache_model.new_ip(dict(address="9.9.9.9")))
     self.assertEqual(
         subnet["dns"],
         [
             fake_network_cache_model.new_ip(dict(address="1.2.3.4")),
             fake_network_cache_model.new_ip(dict(address="2.3.4.5")),
             fake_network_cache_model.new_ip(dict(address="9.9.9.9")),
         ],
     )
Exemplo n.º 20
0
 def test_add_dns(self):
     subnet = fake_network_cache_model.new_subnet()
     dns = fake_network_cache_model.new_ip(dict(address="9.9.9.9"))
     subnet.add_dns(dns)
     self.assertEqual(
         subnet["dns"],
         [
             fake_network_cache_model.new_ip(dict(address="1.2.3.4")),
             fake_network_cache_model.new_ip(dict(address="2.3.4.5")),
             fake_network_cache_model.new_ip(dict(address="9.9.9.9")),
         ],
     )
Exemplo n.º 21
0
 def test_add_ip_a_lot(self):
     subnet = fake_network_cache_model.new_subnet()
     for i in xrange(10):
         subnet.add_ip(fake_network_cache_model.new_fixed_ip(
                     dict(address='192.168.1.102')))
     self.assertEqual(subnet['ips'],
             [fake_network_cache_model.new_fixed_ip(
                     dict(address='10.10.0.2')),
              fake_network_cache_model.new_fixed_ip(
                     dict(address='10.10.0.3')),
              fake_network_cache_model.new_fixed_ip(
                     dict(address='192.168.1.102'))])
Exemplo n.º 22
0
    def test_create_subnet_with_attrs(self):
        subnet = fake_network_cache_model.new_subnet()

        route1 = fake_network_cache_model.new_route()

        self.assertEqual(subnet['cidr'], '10.10.0.0/24')
        self.assertEqual(subnet['dns'],
                [fake_network_cache_model.new_ip(dict(address='1.2.3.4')),
                 fake_network_cache_model.new_ip(dict(address='2.3.4.5'))])
        self.assertEqual(subnet['gateway']['address'], '10.10.0.1')
        self.assertEqual(subnet['ips'],
                [fake_network_cache_model.new_ip(
                        dict(address='10.10.0.2')),
                 fake_network_cache_model.new_ip(
                            dict(address='10.10.0.3'))])
        self.assertEqual(subnet['routes'], [route1])
        self.assertEqual(subnet['version'], 4)
Exemplo n.º 23
0
    def test_hydrate(self):
        fake_network_cache_model.new_subnet()
        fake_network_cache_model.new_subnet(dict(cidr="255.255.255.255"))
        network = model.Network.hydrate(fake_network_cache_model.new_network())

        self.assertEqual(network["id"], 1)
        self.assertEqual(network["bridge"], "br0")
        self.assertEqual(network["label"], "public")
        self.assertEqual(
            network["subnets"],
            [fake_network_cache_model.new_subnet(), fake_network_cache_model.new_subnet(dict(cidr="255.255.255.255"))],
        )
Exemplo n.º 24
0
    def test_create_subnet_with_attrs(self):
        subnet = fake_network_cache_model.new_subnet()

        route1 = fake_network_cache_model.new_route()

        self.assertEqual(subnet["cidr"], "10.10.0.0/24")
        self.assertEqual(
            subnet["dns"],
            [
                fake_network_cache_model.new_ip(dict(address="1.2.3.4")),
                fake_network_cache_model.new_ip(dict(address="2.3.4.5")),
            ],
        )
        self.assertEqual(subnet["gateway"]["address"], "10.10.0.1")
        self.assertEqual(
            subnet["ips"],
            [
                fake_network_cache_model.new_fixed_ip(dict(address="10.10.0.2")),
                fake_network_cache_model.new_fixed_ip(dict(address="10.10.0.3")),
            ],
        )
        self.assertEqual(subnet["routes"], [route1])
        self.assertEqual(subnet["version"], 4)
Exemplo n.º 25
0
 def test_subnet_equal(self):
     subnet1 = fake_network_cache_model.new_subnet()
     subnet2 = fake_network_cache_model.new_subnet()
     self.assertEqual(subnet1, subnet2)
Exemplo n.º 26
0
 def test_subnet_equal(self):
     subnet1 = fake_network_cache_model.new_subnet()
     subnet2 = fake_network_cache_model.new_subnet()
     self.assertEqual(subnet1, subnet2)
Exemplo n.º 27
0
    def _test_injected_network_template(self,
                                        should_inject,
                                        use_ipv4=True,
                                        use_ipv6=False,
                                        gateway=True):
        """Check that netutils properly decides whether to inject based on
           whether the supplied subnet is static or dynamic.
        """
        network = fake_network_cache_model.new_network({'subnets': []})
        subnet_dict = {}
        if not gateway:
            subnet_dict['gateway'] = None

        if not should_inject:
            subnet_dict['dhcp_server'] = '10.10.0.1'

        if use_ipv4:
            network.add_subnet(
                fake_network_cache_model.new_subnet(subnet_dict))

        if should_inject and use_ipv6:
            gateway_ip = fake_network_cache_model.new_ip(
                dict(address='1234:567::1'))
            ip = fake_network_cache_model.new_ip(dict(address='1234:567::2'))
            ipv6_subnet_dict = dict(cidr='1234:567::/48',
                                    gateway=gateway_ip,
                                    ips=[ip])
            if not gateway:
                ipv6_subnet_dict['gateway'] = None
            network.add_subnet(
                fake_network_cache_model.new_subnet(ipv6_subnet_dict))

        # Behave as though CONF.flat_injected is True
        network['meta']['injected'] = True
        vif = fake_network_cache_model.new_vif({'network': network})
        ninfo = model.NetworkInfo([vif])

        template = netutils.get_injected_network_template(ninfo,
                                                          use_ipv6=use_ipv6)

        # will just ignore the improper behavior.
        if not should_inject:
            self.assertTrue(template is None)
        else:
            if use_ipv4:
                self.assertIn('auto eth0', template)
                self.assertIn('iface eth0 inet static', template)
                self.assertIn('address 10.10.0.2', template)
                self.assertIn('netmask 255.255.255.0', template)
                self.assertIn('broadcast 10.10.0.255', template)
                if gateway:
                    self.assertIn('gateway 10.10.0.1', template)
                else:
                    self.assertNotIn('gateway', template)
                self.assertIn('dns-nameservers 1.2.3.4 2.3.4.5', template)
            if use_ipv6:
                self.assertIn('iface eth0 inet6 static', template)
                self.assertIn('address 1234:567::2', template)
                self.assertIn('netmask 48', template)
                if gateway:
                    self.assertIn('gateway 1234:567::1', template)
            if not use_ipv4 and not use_ipv6:
                self.assertTrue(template is None)