def _DoTestAddSnapshotGeneratesCorrectRequest(self, service_version): 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_source_disk = 'disk1' submitted_zone = 'myzone' flag_values.service_version = service_version flag_values.source_disk = submitted_source_disk flag_values.project = expected_project flag_values.description = expected_description command.SetFlags(flag_values) command.SetApi(mock_api.MockApi()) if command._IsUsingAtLeastApiVersion('v1beta14'): flag_values.zone = submitted_zone expected_source_disk = command.NormalizePerZoneResourceName( expected_project, submitted_zone, 'disks', submitted_source_disk) result = command.Handle(expected_snapshot) self.assertEqual(result['project'], expected_project) self.assertEqual(result['body']['name'], expected_snapshot) self.assertEqual(result['body']['description'], expected_description) self.assertEqual(result['body']['sourceDisk'], expected_source_disk)
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 _DoTestAddSnapshotWithoutZoneGeneratesCorrectRequest(self, service_version): 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_source_disk = 'disk1' disk_zone = 'us-east-a' api_base = 'https://www.googleapis.com/compute/%s' % service_version disk_self_link = '%s/projects/%s/zones/%s/disks/%s' % ( api_base, expected_project, disk_zone, submitted_source_disk) flag_values.service_version = service_version flag_values.source_disk = submitted_source_disk flag_values.project = expected_project flag_values.description = expected_description zones = {'items': [{'name': disk_zone}]} disks = {'items': [{'name': 'disk1', 'selfLink': disk_self_link}]} class MockZonesApi(object): def list(self, **unused_kwargs): return mock_api.MockRequest(zones) class MockDisksApi(object): def list(self, **unused_kwargs): return mock_api.MockRequest(disks) api = mock_api.MockApi() api.zones = MockZonesApi api.disks = MockDisksApi command.SetFlags(flag_values) command.SetApi(api) expected_source_disk = command.NormalizePerZoneResourceName( expected_project, disk_zone, 'disks', submitted_source_disk) result = command.Handle(expected_snapshot) self.assertEqual(result['project'], expected_project) self.assertEqual(result['body']['name'], expected_snapshot) self.assertEqual(result['body']['description'], expected_description) self.assertEqual(result['body']['sourceDisk'], expected_source_disk)
def _doTestAddSnapshotFailsWithUnsupportedApiVersion( self, service_version): 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_source_disk = 'disk1' flag_values.service_version = service_version flag_values.source_disk = submitted_source_disk flag_values.project = expected_project flag_values.description = expected_description command.SetFlags(flag_values) command.SetApi(mock_api.MockApi()) try: command.RunWithFlagsAndPositionalArgs(flag_values, [expected_snapshot]) self.fail('Expected exception') except command_base.CommandError: pass