def testAddDiskRequiresZone(self): flag_values = copy.deepcopy(FLAGS) command = disk_cmds.AddDisk('adddisk', flag_values) expected_project = 'test_project' expected_disk = 'test_disk' expected_description = 'test disk' expected_size = 20 submitted_version = command_base.CURRENT_VERSION submitted_zone = 'us-east-a' flag_values.service_version = submitted_version flag_values.project = expected_project flag_values.size_gb = expected_size flag_values.description = expected_description command.SetFlags(flag_values) zones = {'items': [{'name': 'us-east-a'}, {'name': 'us-east-b'}, {'name': 'us-east-c'}, {'name': 'us-west-a'}]} class MockZonesApi(object): def list(self, **unused_kwargs): return mock_api.MockRequest(zones) api = mock_api.MockApi() api.zones = MockZonesApi command.SetApi(api) command._credential = mock_api.MockCredential() expected_zone = command.NormalizeTopLevelResourceName( expected_project, 'zones', submitted_zone) mock_output = mock_api.MockOutput() mock_input = mock_api.MockInput('1\n\r') oldin = sys.stdin sys.stdin = mock_input oldout = sys.stdout sys.stdout = mock_output results, exceptions = command.Handle(expected_disk) self.assertEqual(len(results['items']), 1) self.assertEqual(exceptions, []) result = results['items'][0] if command._IsUsingAtLeastApiVersion('v1beta14'): self.assertEqual(submitted_zone, result['zone']) self.assertFalse('zone' in result['body']) else: self.assertFalse('zone' in result) self.assertEqual(result['body']['zone'], expected_zone) sys.stdin = oldin sys.stdout = oldout
def testAddSnapshotRequiresSourceDisk(self): flag_values = copy.deepcopy(FLAGS) command = snapshot_cmds.AddSnapshot('addsnapshot', flag_values) expected_project = 'test_project' expected_snapshot = 'test_snapshot' expected_description = 'test snapshot' submitted_version = command_base.CURRENT_VERSION submitted_source_disk = 'disk1' flag_values.service_version = submitted_version flag_values.project = expected_project flag_values.description = expected_description command.SetFlags(flag_values) def GetDiskPath(disk_name): disk_path = 'projects/test_project/disks/%s' % (disk_name) if command._IsUsingAtLeastApiVersion('v1beta14'): disk_path = 'projects/test_project/zones/zone-a/disks/%s' % (disk_name) return disk_path disks = { 'items': [ {'name': GetDiskPath('disk1')}, {'name': GetDiskPath('disk2')}, {'name': GetDiskPath('disk3')}]} class MockDisksApi(object): def list(self, **unused_kwargs): return mock_api.MockRequest(disks) api = mock_api.MockApi() api.disks = MockDisksApi command.SetApi(api) expected_disk = command.NormalizePerZoneResourceName( expected_project, 'zone-a', 'disks', submitted_source_disk) mock_output = mock_api.MockOutput() mock_input = mock_api.MockInput('1\n\r') oldin = sys.stdin sys.stdin = mock_input oldout = sys.stdout sys.stdout = mock_output result = command.Handle(expected_snapshot) self.assertEqual(result['body']['sourceDisk'], expected_disk) sys.stdin = oldin sys.stdout = oldout
def testAddDiskRequiresSharedFateZone(self): flag_values = copy.deepcopy(FLAGS) command = disk_cmds.AddDisk('adddisk', flag_values) expected_project = 'test_project' expected_disk = 'test_disk' expected_description = 'test disk' expected_size = 20 submitted_version = command_base.CURRENT_VERSION submitted_zone = 'us-east-a' flag_values.service_version = submitted_version flag_values.project = expected_project flag_values.size_gb = expected_size flag_values.description = expected_description command.SetFlags(flag_values) def GetZonePath(part_one, part_two, part_three): zone_path = 'projects/test_project/zones/%s-%s-%s' % ( part_one, part_two, part_three) return zone_path zones = { 'items': [{ 'name': GetZonePath('us', 'east', 'a') }, { 'name': GetZonePath('us', 'east', 'b') }, { 'name': GetZonePath('us', 'east', 'c') }, { 'name': GetZonePath('us', 'west', 'a') }] } class MockSharedFateZonesApi(object): def list(self, **unused_kwargs): return mock_api.MockRequest(zones) class MockZonesApi(object): def list(self, **unused_kwargs): return mock_api.MockRequest(zones) api = mock_api.MockApi() api.sharedFateZones = MockSharedFateZonesApi api.zones = MockZonesApi command.SetApi(api) command._credential = mock_api.MockCredential() expected_zone = command.NormalizeResourceName(expected_project, 'zones', submitted_zone) mock_output = mock_api.MockOutput() mock_input = mock_api.MockInput('1\n\r') oldin = sys.stdin sys.stdin = mock_input oldout = sys.stdout sys.stdout = mock_output results, exceptions = command.Handle(expected_disk) self.assertEqual(len(results['items']), 1) self.assertEqual(exceptions, []) result = results['items'][0] self.assertEqual(result['body']['zone'], expected_zone) sys.stdin = oldin sys.stdout = oldout