Exemplo n.º 1
0
  def testNonexistentDomain(self):
    """Test that loadDomainState can deal with nonexistent domains."""
    doms = {}
    for i in xrange(6):
      doms[('debmarshal-%d' % i, 'qemu')] = 500

    self.mox.StubOutWithMock(utils, '_clearLibvirtError')
    utils._clearLibvirtError()

    self.mox.StubOutWithMock(utils, 'loadState')
    utils.loadState('debmarshal-domains').AndReturn(dict(doms))

    self.mox.StubOutWithMock(hypervisors.qemu.QEMU, 'open')
    qemu_con = self.mox.CreateMock(libvirt.virConnect)
    hypervisors.qemu.QEMU.open().AndReturn(qemu_con)

    qemu_con.lookupByName('debmarshal-0').InAnyOrder()
    qemu_con.lookupByName('debmarshal-1').InAnyOrder()
    qemu_con.lookupByName('debmarshal-2').InAnyOrder().AndRaise(
        libvirt.libvirtError("Domain doesn't exist"))
    qemu_con.lookupByName('debmarshal-3').InAnyOrder()
    qemu_con.lookupByName('debmarshal-4').InAnyOrder().AndRaise(
        libvirt.libvirtError("Domain doesn't exist"))
    qemu_con.lookupByName('debmarshal-5').InAnyOrder()

    self.mox.ReplayAll()

    del doms[('debmarshal-2', 'qemu')]
    del doms[('debmarshal-4', 'qemu')]

    self.assertEqual(domains.loadDomainState(), doms)
Exemplo n.º 2
0
  def testAcquiringConnections(self):
    """Test acquiring libvirt connections when loading domain state.

    Also check that loadDomainState can reuse a connection it already
    has open.
    """
    doms = {('debmarshal-1', 'qemu'): 500,
            ('debmarshal-2', 'qemu'): 500}

    self.mox.StubOutWithMock(utils, '_clearLibvirtError')
    utils._clearLibvirtError()

    self.mox.StubOutWithMock(utils, 'loadState')
    utils.loadState('debmarshal-domains').AndReturn(doms)

    self.mox.StubOutWithMock(hypervisors.qemu.QEMU, 'open')
    qemu_con = self.mox.CreateMock(libvirt.virConnect)
    hypervisors.qemu.QEMU.open().AndReturn(qemu_con)

    virt_domain = self.mox.CreateMock(libvirt.virDomain)
    qemu_con.lookupByName('debmarshal-1').InAnyOrder().AndReturn(virt_domain)
    virt_domain = self.mox.CreateMock(libvirt.virDomain)
    qemu_con.lookupByName('debmarshal-2').InAnyOrder().AndReturn(virt_domain)

    self.mox.ReplayAll()

    self.assertEqual(domains.loadDomainState(), doms)
Exemplo n.º 3
0
  def testEmptyList(self):
    """Test the domain state file not existing."""
    self.mox.StubOutWithMock(utils, 'loadState')
    utils.loadState('debmarshal-domains').AndReturn(None)

    self.mox.ReplayAll()

    self.assertEqual(domains.loadDomainState(), {})
Exemplo n.º 4
0
    def testNetworkExistenceTest(self):
        """Make sure that networks get dropped from the list in the state
    file if they don't still exist. And that they're kept if they do"""
        self.mox.StubOutWithMock(utils, 'loadState')
        utils.loadState('debmarshal-networks').AndReturn({
            'foo': 500,
            'bar': 501
        })

        self.mox.StubOutWithMock(networks, '_listBridges')
        networks._listBridges().AndReturn(['virbr0', 'foo'])

        self.mox.ReplayAll()

        self.assertEqual(networks.loadNetworkState(), {'foo': 500})
Exemplo n.º 5
0
  def testNetworkExistenceTest(self):
    """Make sure that networks get dropped from the list in the state
    file if they don't still exist. And that they're kept if they do"""
    self.mox.StubOutWithMock(utils, 'loadState')
    utils.loadState('debmarshal-networks').AndReturn(
      {'foo': 500,
       'bar': 501})

    self.mox.StubOutWithMock(networks, '_listBridges')
    networks._listBridges().AndReturn(['virbr0', 'foo'])

    self.mox.ReplayAll()

    self.assertEqual(networks.loadNetworkState(),
                     {'foo': 500})
Exemplo n.º 6
0
def loadNetworkState():
    """Load state for any networks previously created by debmarshal.

  State is written to /var/run/debmarshal-networks as a pickle. Ubuntu
  makes /var/run a tmpfs, so state vanishes after reboots - which is
  good, because the networks debmarshal has created do as well.

  Not all distributions do this, though, so we loop over the networks
  in the pickle and see which ones still exist. If a network no longer
  exists, we assume that it was deleted outside of debmarshal, and we
  erase our record of it.

  Returns:
    A list of networks. Each network is a tuple of (network_name,
      owner_uid, gateway_ip_address)
  """
    networks = utils.loadState('debmarshal-networks')
    if not networks:
        networks = {}

    bridges = set(_listBridges())

    for n in networks.keys():
        if n not in bridges:
            del networks[n]

    return networks
Exemplo n.º 7
0
  def testTwoBadNetworks(self):
    """Test finding two nonexistent networks when loading state."""
    nets = {'foo': 500,
            'bar': 500,
            'baz': 500,
            'quux': 500,
            'spam': 500,
            'eggs': 500}

    self.mox.StubOutWithMock(utils, 'loadState')
    utils.loadState('debmarshal-networks').AndReturn(dict(nets))

    self.mox.StubOutWithMock(networks, '_listBridges')
    networks._listBridges().AndReturn(['foo', 'bar', 'quux', 'eggs'])

    self.mox.ReplayAll()

    del nets['baz']
    del nets['spam']

    self.assertEqual(networks.loadNetworkState(),
                     nets)
Exemplo n.º 8
0
    def testTwoBadNetworks(self):
        """Test finding two nonexistent networks when loading state."""
        nets = {
            'foo': 500,
            'bar': 500,
            'baz': 500,
            'quux': 500,
            'spam': 500,
            'eggs': 500
        }

        self.mox.StubOutWithMock(utils, 'loadState')
        utils.loadState('debmarshal-networks').AndReturn(dict(nets))

        self.mox.StubOutWithMock(networks, '_listBridges')
        networks._listBridges().AndReturn(['foo', 'bar', 'quux', 'eggs'])

        self.mox.ReplayAll()

        del nets['baz']
        del nets['spam']

        self.assertEqual(networks.loadNetworkState(), nets)
Exemplo n.º 9
0
def loadNetworkState(virt_con=None):
    """Load state for any networks previously created by debmarshal.

  State is written to /var/run/debmarshal-networks as a pickle. Ubuntu
  makes /var/run a tmpfs, so state vanishes after reboots - which is
  good, because the networks debmarshal has created do as well.

  Not all distributions do this, though, so we loop over the networks
  in the pickle and see which ones still exist. If a network no longer
  exists, we assume that it was deleted outside of debmarshal, and we
  erase our record of it.

  Args:
    virt_con: A non-read-only libvirt.virConnect instance. If one
      isn't passed in, we'll open one of our own. It doesn't really
      matter which libvirt driver you connect to, because all of them
      share virtual networks.

  Returns:
    A list of networks. Each network is a tuple of (network_name,
      owner_uid, gateway_ip_address)
  """
    networks = utils.loadState("debmarshal-networks")
    if not networks:
        networks = {}

    if not virt_con:
        virt_con = libvirt.open("qemu:///system")

    for n in networks.keys():
        try:
            virt_con.networkLookupByName(n)
        except libvirt.libvirtError:
            del networks[n]

    return networks
Exemplo n.º 10
0
def loadDomainState():
  """Load stored state for domains previously created by debmarshal.

  State is stored in /var/run/debmarshal-domains. State is generally
  lost after reboots - which is good, since running domains tend to go
  away after reboots as well.

  Because not all distributions do this, and because domains can stop
  independent of debmarshal, we loop over the domains and erase our
  record of any domains that don't still exist.

  Each hypervisor has its own domain namespace. We'll need to open
  connections to multiple hypervisors, so there's no point passing a
  libvirt connection object in.

  Returns:
    A list of domains. Each domain is a tuple of (domain_name, owner
      hypervisor)
  """
  connections = {}

  domains = utils.loadState('debmarshal-domains')
  if not domains:
    return {}

  for dom, hypervisor in domains.keys():
    if hypervisor not in connections:
      hyper_class = hypervisors.base.hypervisors[hypervisor]
      connections[hypervisor] = hyper_class.open()

    try:
      connections[hypervisor].lookupByName(dom)
    except libvirt.libvirtError:
      del domains[(dom, hypervisor)]

  return domains