Skip to content

Commit

Permalink
Add folder functionality and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lantero committed Jan 24, 2017
1 parent e9f751c commit 7a1bfba
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ You can run some integration tests to check it works fine for your Vcenter insta
1. Read through the configuration section on the `wiki <https://github.com/Lantero/vcdriver/wiki>`_.
2. Provide some extra environment variables:

- `VCDRIVER_TEST_TEMPLATE`: The name of the virtual machine template (UNIX-like) that will be used for the tests.
- `VCDRIVER_TEST_FOLDER`: The name of the folder that will be used for the tests (Vms will be deleted inside this folder).
- `VCDRIVER_TEST_TEMPLATE`: The name of the virtual machine template (UNIX-like) that will be cloned for the tests.
- `VCDRIVER_TEST_SSH_USERNAME`: The username that will ssh into that virtual machine.
- `VCDRIVER_TEST_SSH_PASSWORD`: The password for the ssh user.

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


setup(
version='1.3.9',
version='1.4.0',
name='vcdriver',
description='A vcenter driver based on pyvmomi and fabric',
url='https://github.com/Lantero/vcdriver',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@
SshError
)
from vcdriver.vm import VirtualMachine, virtual_machines
from vcdriver.folder import destroy_virtual_machines


class TestIntegrationProvisioning(unittest.TestCase):
class TestProvisioning(unittest.TestCase):
def setUp(self):
self.vm = VirtualMachine(
name='test-integration-vcdriver',
template=os.getenv('VCDRIVER_TEST_TEMPLATE')
template=os.getenv('VCDRIVER_TEST_TEMPLATE'),
folder=os.getenv('VCDRIVER_TEST_FOLDER')
)
self.another_vm = VirtualMachine(
name='another-test-integration-vcdriver',
template=os.getenv('VCDRIVER_TEST_TEMPLATE')
template=os.getenv('VCDRIVER_TEST_TEMPLATE'),
folder=os.getenv('VCDRIVER_TEST_FOLDER')
)

def tearDown(self):
self.vm.destroy()
self.another_vm.destroy()
try:
self.vm.destroy()
self.another_vm.destroy()
except:
pass

def test_idempotent_methods(self):
with self.assertRaises(NoObjectFound):
Expand Down Expand Up @@ -57,8 +63,17 @@ def test_context_manager(self):
with self.assertRaises(NoObjectFound):
self.another_vm.find()

def test_destroy_virtual_machines(self):
self.vm.create()
self.another_vm.create()
vms = destroy_virtual_machines(os.getenv('VCDRIVER_TEST_FOLDER'))
with self.assertRaises(NoObjectFound):
vms[0].find()
with self.assertRaises(NoObjectFound):
vms[1].find()


class TestIntegrationNetworking(unittest.TestCase):
class TestNetworking(unittest.TestCase):
@staticmethod
def touch(file_name):
open(file_name, 'wb').close()
Expand Down
27 changes: 27 additions & 0 deletions test/unit/test_folder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import mock
import unittest

from pyVmomi import vim

from vcdriver.folder import destroy_virtual_machines
from vcdriver.vm import VirtualMachine


class TestFolder(unittest.TestCase):
@mock.patch('vcdriver.vm.Session')
@mock.patch('vcdriver.folder.Session')
@mock.patch('vcdriver.folder.get_vcenter_object')
@mock.patch.object(VirtualMachine, 'destroy')
def test_destroy_virtual_machines(
self, destroy, get_vcenter_object, folder_session, vm_session
):
vm1 = mock.MagicMock(spec=vim.VirtualMachine)
vm1.summary.config.name = ''
vm2 = mock.MagicMock(spec=vim.VirtualMachine)
vm2.summary.config.name = ''
other = mock.MagicMock()
folder_mock = mock.MagicMock()
folder_mock.childEntity = [vm1, vm2, other]
get_vcenter_object.return_value = folder_mock
self.assertEqual(len(destroy_virtual_machines('wrong folder')), 2)
self.assertEqual(destroy.call_count, 2)
21 changes: 21 additions & 0 deletions vcdriver/folder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pyVmomi import vim

from vcdriver.auth import Session
from vcdriver.helpers import get_vcenter_object
from vcdriver.vm import VirtualMachine


def destroy_virtual_machines(folder_name):
"""
Destroy all the virtual machines in the folder with the given name
:param folder_name: The folder name
"""
folder = get_vcenter_object(Session().connection, vim.Folder, folder_name)
destroyed_vms = []
for entity in folder.childEntity:
if isinstance(entity, vim.VirtualMachine):
vm = VirtualMachine(name=entity.summary.config.name)
vm.__setattr__('_vm_object', entity)
vm.destroy()
destroyed_vms.append(vm)
return destroyed_vms
2 changes: 1 addition & 1 deletion vcdriver/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ def print_summary(self):
for element in [
['\033[94mName\033[0m', self.name],
['\033[94mTemplate\033[0m', self.template],
['\033[94mData store\033[0m', self.data_store],
['\033[94mResource pool\033[0m', self.resource_pool],
['\033[94mData store\033[0m', self.data_store],
['\033[94mFolder\033[0m', self.folder],
['\033[94mSSH username\033[0m', self.ssh_username],
['\033[94mSSH password\033[0m', self.ssh_password],
Expand Down

0 comments on commit 7a1bfba

Please sign in to comment.