-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
73 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters