def setUp(self):
        """The only two interesting conditions to test here are whether
    storeState raises an exception or not, so let's commonize
    everything else"""
        super(TestCreateNetwork, self).setUp()

        self.networks = {
            'debmarshal-0': 500,
            'debmarshal-3': 500,
            'debmarshal-4': 500,
            'debmarshal-4': 500
        }
        self.name = 'debmarshal-1'
        self.gateway = '169.254.3.1'
        self.hosts = ['wiki.company.com', 'login.company.com']
        self.host_dict = {
            'wiki.company.com': ('169.254.3.2', '00:00:00:00:00:00'),
            'login.company.com': ('169.254.3.3', '00:00:00:00:00:00')
        }

        self.mox.StubOutWithMock(utils, 'getCaller')
        utils.getCaller().AndReturn(1000)

        self.mox.StubOutWithMock(debmarshal.utils, 'acquireLock')
        debmarshal.utils.acquireLock('debmarshal-netlist', fcntl.LOCK_EX)

        self.mox.StubOutWithMock(networks, '_validateHostname')
        networks._validateHostname(mox.IgnoreArg()).MultipleTimes()

        self.mox.StubOutWithMock(libvirt, 'open')
        self.virt_con = self.mox.CreateMock(libvirt.virConnect)
        libvirt.open(mox.IgnoreArg()).AndReturn(self.virt_con)

        self.mox.StubOutWithMock(networks, '_findUnusedName')
        networks._findUnusedName().AndReturn(self.name)

        self.mox.StubOutWithMock(networks, '_findUnusedNetwork')
        networks._findUnusedNetwork(len(self.hosts)).\
            AndReturn((self.gateway, '255.255.255.0'))

        self.mox.StubOutWithMock(networks, 'loadNetworkState')
        networks.loadNetworkState(self.virt_con).AndReturn(dict(self.networks))

        self.mox.StubOutWithMock(virtinst.util, 'randomMAC')
        virtinst.util.randomMAC().MultipleTimes().AndReturn(
            '00:00:00:00:00:00')

        self.mox.StubOutWithMock(networks, '_genNetworkXML')
        networks._genNetworkXML(self.name, self.gateway, '255.255.255.0',
                                self.host_dict).AndReturn('<fake_xml />')

        self.virt_net = self.mox.CreateMock(libvirt.virNetwork)
        self.virt_con.networkDefineXML('<fake_xml />').AndReturn(self.virt_net)
        self.virt_net.create()
Exemple #2
0
  def setUp(self):
    """The only two interesting conditions to test here are whether
    storeState raises an exception or not, so let's commonize
    everything else"""
    super(TestCreateNetwork, self).setUp()

    self.networks = {'debmarshal-0': 500,
                     'debmarshal-3': 500,
                     'debmarshal-4': 500,
                     'debmarshal-4': 500}
    self.name = 'debmarshal-1'
    self.gateway = '169.254.3.1'
    self.hosts = ['wiki.company.com', 'login.company.com']
    self.host_dict = {'wiki.company.com':
                      ('169.254.3.2', '00:00:00:00:00:00'),
                      'login.company.com':
                      ('169.254.3.3', '00:00:00:00:00:00')}

    self.mox.StubOutWithMock(utils, 'getCaller')
    utils.getCaller().AndReturn(1000)

    self.mox.StubOutWithMock(debmarshal.utils, 'acquireLock')
    debmarshal.utils.acquireLock('debmarshal-netlist', fcntl.LOCK_EX)

    self.mox.StubOutWithMock(networks, '_validateHostname')
    networks._validateHostname(mox.IgnoreArg()).MultipleTimes()

    self.mox.StubOutWithMock(libvirt, 'open')
    self.virt_con = self.mox.CreateMock(libvirt.virConnect)
    libvirt.open(mox.IgnoreArg()).AndReturn(self.virt_con)

    self.mox.StubOutWithMock(networks, '_findUnusedName')
    networks._findUnusedName().AndReturn(self.name)

    self.mox.StubOutWithMock(networks, '_findUnusedNetwork')
    networks._findUnusedNetwork(len(self.hosts)).\
        AndReturn((self.gateway, '255.255.255.0'))

    self.mox.StubOutWithMock(networks, 'loadNetworkState')
    networks.loadNetworkState(self.virt_con).AndReturn(dict(self.networks))

    self.mox.StubOutWithMock(virtinst.util, 'randomMAC')
    virtinst.util.randomMAC().MultipleTimes().AndReturn('00:00:00:00:00:00')

    self.mox.StubOutWithMock(networks, '_genNetworkXML')
    networks._genNetworkXML(self.name, self.gateway, '255.255.255.0',
                           self.host_dict).AndReturn('<fake_xml />')

    self.virt_net = self.mox.CreateMock(libvirt.virNetwork)
    self.virt_con.networkDefineXML('<fake_xml />').AndReturn(self.virt_net)
    self.virt_net.create()
Exemple #3
0
  def testXml(self):
    """Test an XML tree."""
    xml_string = networks._genNetworkXML(self.name,
                                        self.gateway,
                                        self.netmask,
                                        self.hosts)
    xml = etree.fromstring(xml_string)

    # These assertions are simply used to test that the element with
    # the right name exists
    self.assertNotEqual(xml.xpath('/network'), [])

    self.assertNotEqual(xml.xpath('/network/name'), [])
    self.assertEqual(xml.xpath('string(/network/name)'), self.name)

    self.assertNotEqual(xml.xpath('/network/ip'), [])
    self.assertEqual(xml.xpath('string(/network/ip/@address)'), self.gateway)
    self.assertEqual(xml.xpath('string(/network/ip/@netmask)'), self.netmask)

    self.assertNotEqual(xml.xpath('/network/ip/dhcp'), [])

    self.assertNotEqual(xml.xpath('/network/ip/dhcp/range'), [])
    self.assertEqual(xml.xpath('string(/network/ip/dhcp/range/@start)'),
                     '%s.2' % self.net)
    self.assertEqual(xml.xpath('string(/network/ip/dhcp/range/@end)'),
                     '%s.254' % self.net)

    self.assertEqual(len(xml.xpath('/network/ip/dhcp/host')), len(self.hosts))
    for h, hinfo in self.hosts.iteritems():
      host_node = '/network/ip/dhcp/host[@name = $name]'
      self.assertNotEqual(xml.xpath(host_node, name=h), [])
      self.assertEqual(xml.xpath('string(%s/@ip)' % host_node, name=h), hinfo[0])
      self.assertEqual(xml.xpath('string(%s/@mac)' % host_node, name=h), hinfo[1])
Exemple #4
0
    def testXml(self):
        """Test an XML tree."""
        xml_string = networks._genNetworkXML(self.name, self.gateway,
                                             self.netmask, self.hosts)
        xml = etree.fromstring(xml_string)

        # These assertions are simply used to test that the element with
        # the right name exists
        self.assertNotEqual(xml.xpath('/network'), [])

        self.assertNotEqual(xml.xpath('/network/name'), [])
        self.assertEqual(xml.xpath('string(/network/name)'), self.name)

        self.assertNotEqual(xml.xpath('/network/ip'), [])
        self.assertEqual(xml.xpath('string(/network/ip/@address)'),
                         self.gateway)
        self.assertEqual(xml.xpath('string(/network/ip/@netmask)'),
                         self.netmask)

        self.assertNotEqual(xml.xpath('/network/ip/dhcp'), [])

        self.assertNotEqual(xml.xpath('/network/ip/dhcp/range'), [])
        self.assertEqual(xml.xpath('string(/network/ip/dhcp/range/@start)'),
                         '%s.2' % self.net)
        self.assertEqual(xml.xpath('string(/network/ip/dhcp/range/@end)'),
                         '%s.254' % self.net)

        self.assertEqual(len(xml.xpath('/network/ip/dhcp/host')),
                         len(self.hosts))
        for h, hinfo in self.hosts.iteritems():
            host_node = '/network/ip/dhcp/host[@name = $name]'
            self.assertNotEqual(xml.xpath(host_node, name=h), [])
            self.assertEqual(xml.xpath('string(%s/@ip)' % host_node, name=h),
                             hinfo[0])
            self.assertEqual(xml.xpath('string(%s/@mac)' % host_node, name=h),
                             hinfo[1])
Exemple #5
0
  def createNetwork(self, hosts, _debmarshal_sender=None):
    """All of the networking config you need for a debmarshal test rig.

    createNetwork creates an isolated virtual network within
    libvirt. It picks an IP address space that is as-yet unused
    (within debmarshal), and assigns that to the network. It then
    allocates IP addresses and MAC addresses for each of the hostnames
    listed in hosts.

    createNetwork tracks which users created which networks, and
    debmarshal will only allow the user that created a network to
    attach VMs to it or destroy it.

    Args:
      hosts: A list of hostnames that will eventually be attached to
        this network

    Returns:
      A 4-tuple containing:
        Network name: This is used to reference the newly created
          network in the future. It is unique across the local
          workstation
        Gateway: The network address. Also the DNS server, if that
          information isn't being grabbed over DHCP
        Netmask: The netmask for the network
        VMs: A dict mapping hostnames in hosts to (IP address, MAC
          address), as assigned by createNetwork
    """
    utils.caller = _debmarshal_sender

    # First, input validation. Everything in hosts should be a valid
    # hostname
    for h in hosts:
      networks._validateHostname(h)

    # We don't really care which particular libvirt driver we connect
    # to, because they all share the same networking
    # config. libvirt.open() is supposed to take None to indicate a
    # default, but it doesn't seem to work, so we pass in what's
    # supposed to be the default for root.
    virt_con = libvirt.open('qemu:///system')

    net_name = networks._findUnusedName()
    net_gateway, net_mask = networks._findUnusedNetwork(len(hosts))

    net_hosts = {}
    host_addr = ip.IP(net_gateway) + 1
    for host in hosts:
      # Use the virtinst package's MAC address generator because it's
      # easier than writing another one for ourselves.
      #
      # This does mean that the MAC addresses are allocated from
      # Xensource's OUI, but whatever
      mac = virtinst.util.randomMAC()
      net_hosts[host] = (host_addr.ip_ext, mac)
      host_addr += 1

    xml = networks._genNetworkXML(net_name, net_gateway, net_mask, net_hosts)
    virt_net = virt_con.networkDefineXML(xml)
    virt_net.create()

    try:
      nets = networks.loadNetworkState(virt_con)
      nets[net_name] = utils.getCaller()
      utils.storeState(nets, 'debmarshal-networks')
    except:
      virt_con.networkLookupByName(net_name).destroy()
      virt_con.networkLookupByName(net_name).undefine()
      raise

    utils.caller = None

    return (net_name, net_gateway, net_mask, net_hosts)
Exemple #6
0
    def createNetwork(self, hosts, _debmarshal_sender=None):
        """All of the networking config you need for a debmarshal test rig.

    createNetwork creates an isolated virtual network within
    libvirt. It picks an IP address space that is as-yet unused
    (within debmarshal), and assigns that to the network. It then
    allocates IP addresses and MAC addresses for each of the hostnames
    listed in hosts.

    createNetwork tracks which users created which networks, and
    debmarshal will only allow the user that created a network to
    attach VMs to it or destroy it.

    Args:
      hosts: A list of hostnames that will eventually be attached to
        this network

    Returns:
      A 4-tuple containing:
        Network name: This is used to reference the newly created
          network in the future. It is unique across the local
          workstation
        Gateway: The network address. Also the DNS server, if that
          information isn't being grabbed over DHCP
        Netmask: The netmask for the network
        VMs: A dict mapping hostnames in hosts to (IP address, MAC
          address), as assigned by createNetwork
    """
        utils.caller = _debmarshal_sender

        # First, input validation. Everything in hosts should be a valid
        # hostname
        for h in hosts:
            networks._validateHostname(h)

        # We don't really care which particular libvirt driver we connect
        # to, because they all share the same networking
        # config. libvirt.open() is supposed to take None to indicate a
        # default, but it doesn't seem to work, so we pass in what's
        # supposed to be the default for root.
        virt_con = libvirt.open('qemu:///system')

        net_name = networks._findUnusedName()
        net_gateway, net_mask = networks._findUnusedNetwork(len(hosts))

        net_hosts = {}
        host_addr = ip.IP(net_gateway) + 1
        for host in hosts:
            # Use the virtinst package's MAC address generator because it's
            # easier than writing another one for ourselves.
            #
            # This does mean that the MAC addresses are allocated from
            # Xensource's OUI, but whatever
            mac = virtinst.util.randomMAC()
            net_hosts[host] = (host_addr.ip_ext, mac)
            host_addr += 1

        xml = networks._genNetworkXML(net_name, net_gateway, net_mask,
                                      net_hosts)
        virt_net = virt_con.networkDefineXML(xml)
        virt_net.create()

        try:
            nets = networks.loadNetworkState(virt_con)
            nets[net_name] = utils.getCaller()
            utils.storeState(nets, 'debmarshal-networks')
        except:
            virt_con.networkLookupByName(net_name).destroy()
            virt_con.networkLookupByName(net_name).undefine()
            raise

        utils.caller = None

        return (net_name, net_gateway, net_mask, net_hosts)