コード例 #1
0
 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'))
コード例 #2
0
ファイル: test_winazurearm.py プロジェクト: mjs/juju
 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)
コード例 #3
0
 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)
コード例 #4
0
ファイル: test_winazurearm.py プロジェクト: mjs/juju
 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)
コード例 #5
0
 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)
コード例 #6
0
 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()
コード例 #7
0
ファイル: test_winazurearm.py プロジェクト: mjs/juju
 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()
コード例 #8
0
ファイル: test_winazurearm.py プロジェクト: mjs/juju
 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)
コード例 #9
0
 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)
コード例 #10
0
ファイル: test_winazurearm.py プロジェクト: mjs/juju
 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)
コード例 #11
0
 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)
コード例 #12
0
 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)
コード例 #13
0
ファイル: test_winazurearm.py プロジェクト: mjs/juju
 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)
コード例 #14
0
 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)
コード例 #15
0
ファイル: test_winazurearm.py プロジェクト: mjs/juju
 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)
コード例 #16
0
ファイル: test_winazurearm.py プロジェクト: mjs/juju
 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)
コード例 #17
0
 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)
コード例 #18
0
 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')
コード例 #19
0
ファイル: test_winazurearm.py プロジェクト: mjs/juju
 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)
コード例 #20
0
 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)
コード例 #21
0
 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)
コード例 #22
0
 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)
コード例 #23
0
 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)
コード例 #24
0
ファイル: test_winazurearm.py プロジェクト: mjs/juju
 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'))