Skip to content

Example 9

Carlos edited this page Aug 4, 2017 · 1 revision

Using vcdriver for testing

The initial purpose of vcdriver always was testing, and with that in mind a couple of handy context managers were introduced in the package.

The first one ensures a context where a virtual machine is being created and deleted at the end, like:

>>> from vcdriver.vm import VirtualMachine, virtual machines
>>> from vcdriver.config import load
>>> load('path/to/vcdriver.ini')
>>> vm = VirtualMachine(name='vm_name', template='vm_template')
>>> with virtual_machines([vm]):
...     vm.ssh('ls')
...
Vcenter session opened with ID 52d75977-360d-0923-39ff-b914fd323d61
Waiting for [Create virtual machine "vm_name" from template "vm_template"] ... 0:00:25.209813
Waiting for [Get IP] ... 0:01:03.292489
[admin@10.0.0.1] run: ls
Waiting for [Power off virtual machine "vm_name"] ... 0:00:01.084497
Waiting for [Destroy virtual machine "vm_name"] ... 0:00:01.081120

The second context manager works at the snapshot level, so it takes and existing virtual machine, snapshot it, and revert and delete the snapshot at the end.

>>> from vcdriver.vm import VirtualMachine, snapshot
>>> vm = VirtualMachine(name='vm_name_to_be_found')
>>> vm.find()
Vcenter session opened with ID 52d75977-360d-0923-39ff-b914fd323d61
>>> with snapshot(vm):
...     vm.ssh('ls')
...
Vcenter session opened with ID 52d75977-360d-0923-39ff-b914fd323d61
Waiting for [Creating snapshot "8a589a01-de97-4591-a07a-1807c99d7767" on "vm_name_to_be_found"] ... 0:01:18.016550
[admin@10.0.0.1] run: ls
Waiting for [Restoring snapshot "8a589a01-de97-4591-a07a-1807c99d7767" on "vm_name_to_be_found"] ... 0:00:03.177706
Waiting for [Delete snapshot "8a589a01-de97-4591-a07a-1807c99d7767" from "vm_name_to_be_found"] ... 0:00:01.080298