LibvirtConnection: Share underlying connections between instances

The current implementation of LibvirtConnection is to cache opened
connection for later use. Then multiple conn_inst.get() invocations
return the same underlying opened connection. In this way we can share
underlying connection as long as the caller require it from the same
LibvirtConnection instance.

However if different callers instantiate multiple LibvirtConnection
instances, the underlying connections are not shared among them.
As we know, libvirtd deal with incoming connections using a thread pool,
if each time different callers instantiate new LibvirtConnection
instances, the libvirtd will finally refuse to accept new RPC requests.

To re-produce this problem, you can try the following steps
  cd kimchi
  PYTHONPATH=src python
  >>> from kimchi.model.libvirtconnection import LibvirtConnection
  >>> for i in range(200):
  ...     print i
  ...     conn = LibvirtConnection('qemu:///system').get()
  ...     print conn

The effect of this problem is that "make check" would hang if we add
much test method in "tests/test_model.py". In "test_model.py", each test
method instantiates a new Model instance, and each new Model instance
would instantiate a new LibvirtConnection. It's true that Python can
garbage collect Model and LibvirtConnection instances after each test
method, but it usually garbage collect algorithms does not guarantee
to release all the object at once. Another problem affecting garbage
collection is that background threads started in the thread can hold
indirect reference to Model instances. It's a also know problem that
Python can not deal with circular reference if objects in the circle has
customized "__del__()" method, and unfortunately all objects in the
libvirt Python binding have customized "__del__()" methods.

So in this patch we make all the LibvirtConnection instances share a
common connection cache. The connection cache is a nested dict as
follow.

{
    'qemu:///system': {0: conn_obj0, 1: conn_obj1, ...},
    'test:///default': {0: conn_obj2, 1: conn_obj3, ...},
    ...
}

This means all LibvirtConnection object instanced with the same
connection URI share the same cache.

Signed-off-by: Zhou Zheng Sheng <zhshzhou@linux.vnet.ibm.com>
VMsModel: Make _update_guests_stats() robust against race condition

"VMsModel._update_guests_stats()" is passed to cherrypy BackgroundTask
to run in a background thread. The main responsibility of this method is
to collect and update VM running statistics. The problem is that a
virtual machine can be destroyed and deleted when the method is running,
so in any part that deal with virtual machine in this method, it's
possible to trigger exception.

If you notice sometimes the following exception when runing test_model
tests, the problem described above is the cause.

Exception in thread Thread-4:
Traceback (most recent call last):
  File "/usr/lib64/python2.7/threading.py", line 811, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/site-packages/cherrypy/process/plugins.py", line 475, in run
    self.function(*self.args, **self.kwargs)
  File "/home/edward/src/kimchi/src/kimchi/model/vms.py", line 79, in _update_guests_stats
    dom = VMModel.get_vm(name, self.conn)
  File "/home/edward/src/kimchi/src/kimchi/model/vms.py", line 457, in get_vm
    raise NotFoundError("KCHVM0002E", {'name': name})
NotFoundError: KCHVM0002E: Virtual machine kimchi-ide-bus-vm does not exist

The cherrypy BackgroundTask will stop running once it detects exception
in the task method. So if we want to keep the "_update_guests_stats()"
running, we should swallow all kinds of exceptions in the method body.

Signed-off-by: Zhou Zheng Sheng <zhshzhou@linux.vnet.ibm.com>
ModelTests: Improve leftover cleaning

In ModelTests we use RollbackContext to register undo actions for
cleaning leftovers. The general principle for using RollbackContext is
that we should register an undo action right after the successful
allocation of a resource, otherwise we leave a window between the
resource allocation and cleanup action registration. If it happened to
raise an exception in this window, the allocated resource is not
cleaned.

In ModelTests, you can find that some test methods is not properly using
RollbackContext, they place assertions between resource allocation and
cleanup registration. This patch tries to adjust the order peroperly.

Signed-off-by: Zhou Zheng Sheng <zhshzhou@linux.vnet.ibm.com>
ConfigTests: Fix novnc paths

In "config.py.in" we changed the way of determining novnc path, we
should also change it accordingly in "test_config.py.in".

Signed-off-by: Zhou Zheng Sheng <zhshzhou@linux.vnet.ibm.com>