def test_find_vm_instance(self, is_mock): client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() rgd1 = ResourceGroupDetails(client, ResourceGroup('one'), vms=[ VirtualMachine('name-0', 'id-a'), VirtualMachine('name-1', 'id-b') ]) rgd2 = ResourceGroupDetails(client, ResourceGroup('two'), vms=[ VirtualMachine('name-0', 'id-y'), VirtualMachine('name-1', 'id-z') ]) resources = [rgd1, rgd2] self.assertEqual((None, None), find_vm_instance(resources, 'id-q', None)) self.assertEqual((rgd2, rgd2.vms[0]), find_vm_instance(resources, 'id-y', None)) self.assertEqual((rgd2, rgd2.vms[0]), find_vm_instance(resources, 'name-0', 'two')) self.assertEqual((None, None), find_vm_instance(resources, 'name-0', 'three')) self.assertEqual((None, None), find_vm_instance(resources, 'name-9', 'two'))
def test_list_resources_recursive(self, is_mock): client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() # For the call to find many groups. a_group = ResourceGroup('juju-bar-1') b_group = ResourceGroup('juju-foo-0') client.resource.resource_groups.list.return_value = [a_group, b_group] # For the call to load a ResourceGroupDetails instance. storage_account = StorageAccount('abcd-12', datetime.now(tz=pytz.UTC)) client.storage.storage_accounts.list_by_resource_group.return_value = [ storage_account] virtual_machine = VirtualMachine('admin-machine-0', 'bcde-1234') client.compute.virtual_machines.list.return_value = [virtual_machine] address = Address('machine-0-public-ip', '1.2.3.4') client.network.public_ip_addresses.list.return_value = [address] network = Network('juju-bar-network-1') client.network.virtual_networks.list.return_value = [network] # The addition of recursive=True will get the details of the # subordinate resources and set is_loaded to True. result = list_resources(client, 'juju-bar*', recursive=True) rgd = ResourceGroupDetails( client, a_group, storage_accounts=[storage_account], vms=[virtual_machine], addresses=[address], networks=[network]) rgd.is_loaded = True self.assertEqual([rgd], result)
def test_list_resources_recursive(self, is_mock): client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() # For the call to find many groups. a_group = ResourceGroup('juju-bar-1') b_group = ResourceGroup('juju-foo-0') client.resource.resource_groups.list.return_value = [a_group, b_group] # For the call to load a ResourceGroupDetails instance. storage_account = StorageAccount('abcd-12', datetime.now(tz=pytz.UTC)) client.storage.storage_accounts.list_by_resource_group.return_value = [ storage_account ] virtual_machine = VirtualMachine('admin-machine-0', 'bcde-1234') client.compute.virtual_machines.list.return_value = [virtual_machine] address = Address('machine-0-public-ip', '1.2.3.4') client.network.public_ip_addresses.list.return_value = [address] network = Network('juju-bar-network-1') client.network.virtual_networks.list.return_value = [network] # The addition of recursive=True will get the details of the # subordinate resources and set is_loaded to True. result = list_resources(client, 'juju-bar*', recursive=True) rgd = ResourceGroupDetails(client, a_group, storage_accounts=[storage_account], vms=[virtual_machine], addresses=[address], networks=[network]) rgd.is_loaded = True self.assertEqual([rgd], result)
def test_list_resources_ignore_default(self, is_mock): # Default resources are created by Azure. They should only be # seen via the UI. A glob for everything will still ignore defaults. client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() groups = [ResourceGroup('{}-network'.format(DEFAULT_RESOURCE_PREFIX))] client.resource.resource_groups.list.return_value = groups result = list_resources(client, '*') self.assertEqual([], result)
def test_list_resources(self, is_mock): client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() groups = [ResourceGroup('juju-foo-0'), ResourceGroup('juju-bar-1')] client.resource.resource_groups.list.return_value = groups result = list_resources(client, 'juju-bar*') rgd = ResourceGroupDetails(client, groups[-1]) self.assertEqual([rgd], result) client.resource.resource_groups.list.assert_called_once_with()
def test_delete_instance_without_match(self, is_mock): client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() vm1 = VirtualMachine('name-0', 'id-a') rgd1 = ResourceGroupDetails(client, ResourceGroup('one'), vms=[vm1]) with patch('winazurearm.list_resources', autospec=True, return_value=[rgd1]): # Passing an non-existent id bypasses the call to delete. with self.assertRaises(ValueError): delete_instance(client, 'id-z') self.assertEqual(0, client.compute.virtual_machines.delete.call_count)
def test_delete_resources_poller_is_none(self, is_mock): now = datetime.now(tz=pytz.utc) client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() group = ResourceGroup('juju-bar-1') client.resource.resource_groups.list.return_value = [group] # The resource_groups's storage_account is 4 hours old. storage_account = StorageAccount('abcd-12', now - timedelta(hours=4)) client.storage.storage_accounts.list_by_resource_group.return_value = [ storage_account] count = delete_resources(client, 'juju-bar*', old_age=2, now=now) self.assertEqual(1, count) self.assertEqual(1, client.resource.resource_groups.delete.call_count)
def xxx_test_delete_resources_only_network(self, is_mock): now = datetime.now(tz=pytz.utc) client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() group = ResourceGroup('juju-bar-1') client.resource.resource_groups.list.return_value = [group] # The resource_group has a network, but nothing else network = Network('juju-bar-network-1') client.network.virtual_networks.list.return_value = [network] poller = FakePoller() client.resource.resource_groups.delete.return_value = poller count = delete_resources(client, 'juju-bar*', old_age=2, now=now) self.assertEqual(1, count) self.assertEqual(1, client.resource.resource_groups.delete.call_count)
def test_delete_resources_poller_is_none(self, is_mock): now = datetime.now(tz=pytz.utc) client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() group = ResourceGroup('juju-bar-1') client.resource.resource_groups.list.return_value = [group] # The resource_groups's storage_account is 4 hours old. storage_account = StorageAccount('abcd-12', now - timedelta(hours=4)) client.storage.storage_accounts.list_by_resource_group.return_value = [ storage_account ] count = delete_resources(client, 'juju-bar*', old_age=2, now=now) self.assertEqual(1, count) self.assertEqual(1, client.resource.resource_groups.delete.call_count)
def test_delete_resources_old_age_0(self, is_mock): now = datetime.now(tz=pytz.utc) client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() a_group = ResourceGroup('juju-bar-1') b_group = ResourceGroup('juju-foo-0') client.resource.resource_groups.list.return_value = [a_group, b_group] poller = FakePoller() client.resource.resource_groups.delete.return_value = poller # Delete resource groups that are 0 hours old. # All matched resource_groups are deleted count = delete_resources(client, 'juju-bar*', old_age=0, now=now) self.assertEqual(1, count) self.assertEqual(1, client.resource.resource_groups.delete.call_count) self.assertIs(True, poller.is_done)
def test_delete_instance_with_name_and_resource_group(self, is_mock): client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() poller = FakePoller() client.compute.virtual_machines.delete.return_value = poller vm1 = VirtualMachine('name-0', 'id-a') rgd1 = ResourceGroupDetails(client, ResourceGroup('one'), vms=[vm1]) with patch('winazurearm.list_resources', autospec=True, return_value=[rgd1]) as lr_mock: # Passing just a name and group will take the group glob path. delete_instance(client, 'name-0', 'one') lr_mock.assert_called_once_with(client, glob='one', recursive=True) client.compute.virtual_machines.delete.assert_called_once_with( 'one', 'name-0') self.assertIs(True, poller.is_done)
def test_main_delete_instance_name_group(self, is_mock): client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') with patch('winazurearm.delete_instance', autospec=True) as di_mock: code = main(['winazurearm.py', 'delete-instance', 'name', 'group']) self.assertEqual(0, code) self.assertEqual(1, is_mock.call_count) di_mock.assert_called_once_with(client, 'name', resource_group='group')
def test_delete_resources_found_old(self, is_mock): now = datetime.now(tz=pytz.utc) client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() group = ResourceGroup('juju-bar-1') client.resource.resource_groups.list.return_value = [group] # The resource_groups's storage_account is 4 hours old. storage_account = StorageAccount('abcd-12', now - timedelta(hours=4)) client.storage.storage_accounts.list_by_resource_group.return_value = [ storage_account] poller = FakePoller() client.resource.resource_groups.delete.return_value = poller # Delete resource groups that are 2 hours old. count = delete_resources(client, 'juju-bar*', old_age=2, now=now) self.assertEqual(1, count) client.resource.resource_groups.delete.assert_called_once_with( 'juju-bar-1') self.assertIs(True, poller.is_done)
def test_main_delete_resources(self, is_mock): client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') with patch('winazurearm.delete_resources', autospec=True) as dr_mock: code = main(['winazurearm.py', 'delete-resources', 'juju-deploy*']) self.assertEqual(0, code) self.assertEqual(1, is_mock.call_count) dr_mock.assert_called_once_with(client, glob='juju-deploy*', old_age=OLD_MACHINE_AGE)
def test_delete_resources_found_old(self, is_mock): now = datetime.now(tz=pytz.utc) client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() group = ResourceGroup('juju-bar-1') client.resource.resource_groups.list.return_value = [group] # The resource_groups's storage_account is 4 hours old. storage_account = StorageAccount('abcd-12', now - timedelta(hours=4)) client.storage.storage_accounts.list_by_resource_group.return_value = [ storage_account ] poller = FakePoller() client.resource.resource_groups.delete.return_value = poller # Delete resource groups that are 2 hours old. count = delete_resources(client, 'juju-bar*', old_age=2, now=now) self.assertEqual(1, count) client.resource.resource_groups.delete.assert_called_once_with( 'juju-bar-1') self.assertIs(True, poller.is_done)
def test_main_list_resources(self, is_mock): client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') with patch('winazurearm.list_resources', autospec=True) as lr_mock: code = main(['winazurearm.py', 'list-resources', 'juju-deploy*']) self.assertEqual(0, code) self.assertEqual(1, is_mock.call_count) lr_mock.assert_called_once_with(client, glob='juju-deploy*', print_out=True, recursive=False)
def test_main_delete_instance_not_found(self, is_mock): client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') with patch('winazurearm.delete_instance', autospec=True, side_effect=ValueError) as di_mock: code = main(['winazurearm.py', 'delete-instance', 'instance-id']) self.assertEqual(1, code) self.assertEqual(1, is_mock.call_count) di_mock.assert_called_once_with(client, 'instance-id', resource_group=None)
def test_find_vm_instance(self, is_mock): client = ARMClient('subscription_id', 'client_id', 'secret', 'tenant') client.init_services() rgd1 = ResourceGroupDetails( client, ResourceGroup('one'), vms=[VirtualMachine('name-0', 'id-a'), VirtualMachine('name-1', 'id-b')]) rgd2 = ResourceGroupDetails( client, ResourceGroup('two'), vms=[VirtualMachine('name-0', 'id-y'), VirtualMachine('name-1', 'id-z')]) resources = [rgd1, rgd2] self.assertEqual( (None, None), find_vm_instance(resources, 'id-q', None)) self.assertEqual( (rgd2, rgd2.vms[0]), find_vm_instance(resources, 'id-y', None)) self.assertEqual( (rgd2, rgd2.vms[0]), find_vm_instance(resources, 'name-0', 'two')) self.assertEqual( (None, None), find_vm_instance(resources, 'name-0', 'three')) self.assertEqual( (None, None), find_vm_instance(resources, 'name-9', 'two'))