Example #1
0
    def _doTestAddDiskGeneratesCorrectRequest(self, service_version):
        flag_values = copy.deepcopy(FLAGS)

        command = disk_cmds.AddDisk('adddisk', flag_values)

        expected_project = 'test_project'
        expected_disk = 'test_disk'
        expected_description = 'test disk'
        submitted_zone = 'copernicus-moon-base'
        expected_size = 20
        flag_values.service_version = service_version
        flag_values.zone = submitted_zone
        flag_values.project = expected_project
        flag_values.size_gb = expected_size
        flag_values.description = expected_description

        command.SetFlags(flag_values)
        command.SetApi(mock_api.MockApi())
        command._credential = mock_api.MockCredential()

        results, exceptions = command.Handle(expected_disk)
        self.assertEqual(len(results['items']), 1)
        self.assertEqual(exceptions, [])
        result = results['items'][0]

        expected_zone = command.NormalizeResourceName(expected_project,
                                                      'zones', submitted_zone)

        self.assertEqual(result['project'], expected_project)
        self.assertEqual(result['body']['name'], expected_disk)
        self.assertEqual(result['body']['description'], expected_description)
        self.assertEqual(result['body']['sizeGb'], expected_size)
        self.assertEqual(result['body']['zone'], expected_zone)
Example #2
0
  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
Example #3
0
  def _DoTestAddDiskFromImageGeneratesCorrectRequest(self, service_version):
    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_gb = 123
    submitted_zone = 'copernicus-moon-base'
    submitted_source_image = 'image1'
    submitted_size_gb = 123
    flag_values.zone = submitted_zone
    flag_values.project = expected_project
    flag_values.description = expected_description
    flag_values.source_image = submitted_source_image
    flag_values.size_gb = submitted_size_gb
    flag_values.service_version = service_version

    command.SetFlags(flag_values)
    command.SetApi(mock_api.MockApi())
    command._credential = mock_api.MockCredential()

    results, exceptions = command.Handle(expected_disk)
    self.assertEqual(len(results['items']), 1)
    self.assertEqual(exceptions, [])
    result = results['items'][0]

    expected_zone = command.NormalizeTopLevelResourceName(
        expected_project,
        'zones',
        submitted_zone)

    expected_source_image = command.NormalizeGlobalResourceName(
        expected_project,
        'images',
        submitted_source_image)

    self.assertEqual(result['project'], expected_project)
    self.assertEqual(result['body']['name'], expected_disk)
    self.assertEqual(result['body']['description'], expected_description)
    self.assertEqual(result['body']['sizeGb'], expected_size_gb)
    self.assertEqual(result['sourceImage'], expected_source_image)
    if command._IsUsingAtLeastApiVersion('v1beta14'):
      self.assertEqual(submitted_zone, result['zone'])
      self.assertFalse('zone' in result['body'])
    else:
      self.assertEqual(result['body']['zone'], expected_zone)
      self.assertFalse('zone' in result)
Example #4
0
  def testAddDiskDefaultSizeGb(self):
    flag_values = copy.deepcopy(FLAGS)

    command = disk_cmds.AddDisk('adddisk', flag_values)

    flag_values.zone = 'copernicus-moon-base'
    flag_values.project = 'test_project'

    command.SetFlags(flag_values)
    command.SetApi(mock_api.MockApi())
    command._credential = mock_api.MockCredential()

    results, exceptions = command.Handle('disk1')
    self.assertEqual(len(results['items']), 1)
    self.assertEqual(exceptions, [])
    result = results['items'][0]

    # We did not set the size, make sure it defaults to 10GB.
    self.assertEqual(10, result['body']['sizeGb'])
Example #5
0
  def _DoTestAddMultipleDisks(self, service_version):
    flag_values = copy.deepcopy(FLAGS)
    command = disk_cmds.AddDisk('adddisk', flag_values)

    expected_kind = command._GetResourceApiKind('disk')
    expected_project = 'test_project'
    expected_disks = ['test-disk-%02d' % i for i in xrange(100)]
    expected_description = 'test disk'
    submitted_zone = 'copernicus-moon-base'
    expected_size = 12

    flag_values.service_version = service_version
    flag_values.zone = submitted_zone
    flag_values.project = expected_project
    flag_values.size_gb = expected_size
    flag_values.description = expected_description

    command.SetFlags(flag_values)
    command.SetApi(mock_api.MockApi())
    command._credential = mock_api.MockCredential()

    expected_zone = command.NormalizeTopLevelResourceName(
        expected_project, 'zones', submitted_zone)

    results, exceptions = command.Handle(*expected_disks)
    self.assertEqual(exceptions, [])
    results = results['items']
    self.assertEqual(len(results), len(expected_disks))

    for expected_disk, result in zip(expected_disks, results):
      self.assertEqual(result['project'], expected_project)
      self.assertEqual(result['body']['kind'], expected_kind)
      self.assertEqual(result['body']['sizeGb'], expected_size)
      self.assertEqual(result['body']['name'], expected_disk)
      self.assertEqual(result['body']['description'], expected_description)
      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)
Example #6
0
  def testAddDiskFromImageDoesNotPassSizeGbUnlessExplicitlySet(self):
    flag_values = copy.deepcopy(FLAGS)

    command = disk_cmds.AddDisk('adddisk', flag_values)

    flag_values.zone = 'copernicus-moon-base'
    flag_values.project = 'test_project'
    flag_values.source_image = 'image1'
    flag_values.service_version = 'v1beta14'

    command.SetFlags(flag_values)
    command.SetApi(mock_api.MockApi())
    command._credential = mock_api.MockCredential()

    results, exceptions = command.Handle('disk1')
    self.assertEqual(len(results['items']), 1)
    self.assertEqual(exceptions, [])
    result = results['items'][0]

    # Make sure we did not pass 'sizeGb' in the body.
    self.assertFalse('sizeGb' in result['body'])
Example #7
0
 def testAddWithNoDisk(self):
   flag_values = copy.deepcopy(FLAGS)
   command = disk_cmds.AddDisk('adddisk', flag_values)
   self.assertRaises(app.UsageError, command.Handle)
Example #8
0
    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