def patch_critical_objects(stdout='',
                           stderr='',
                           return_code=0,
                           flags=mock_flags.MockFlags()):
    with contextlib2.ExitStack() as stack:
        flags.gcloud_path = 'gcloud'
        flags.run_uri = _RUN_URI
        flags.data_search_paths = ''

        stack.enter_context(mock_flags.PatchFlags(flags))
        stack.enter_context(mock.patch('__builtin__.open'))
        stack.enter_context(mock.patch(vm_util.__name__ + '.PrependTempDir'))
        stack.enter_context(
            mock.patch(vm_util.__name__ + '.NamedTemporaryFile'))
        stack.enter_context(
            mock.patch(util.__name__ + '.GetDefaultProject',
                       return_value='fakeproject'))
        stack.enter_context(
            mock.patch(util.__name__ + '.GetDefaultUser',
                       return_value='fakeuser'))

        retval = (stdout, stderr, return_code)
        issue_command = stack.enter_context(
            mock.patch(vm_util.__name__ + '.IssueCommand',
                       return_value=retval))
        yield issue_command
Exemple #2
0
    def setUp(self):
        self.flags = mock_flags.MockFlags()
        self.flags['run_uri'].parse('123')

        self.full_spec = {
            'cloud': 'GCP',
            'engine': 'mysql',
            'database_name': 'fake_name',
            'database_password': '******',
            'backup_enabled': True,
            'backup_start_time': '07:00',
            'vm_spec': {
                'GCP': {
                    'machine_type': 'n1-standard-1',
                    'zone': 'us-west1-a',
                }
            },
            'disk_spec': {
                'GCP': {
                    'disk_size': 500,
                }
            }
        }

        managed_relational_db._MANAGED_RELATIONAL_DB_REGISTRY = {
            'GCP': FakeManagedRelationalDb(None)
        }
Exemple #3
0
 def testNonPresentFlagsDoNotOverrideConfigs(self):
     flags = mock_flags.MockFlags()
     flags['aws_provisioned_iops'].value = 2000
     flags['data_disk_size'].value = 100
     spec = aws_disk.AwsDiskSpec(_COMPONENT, flags, disk_size=75, iops=1000)
     self.assertEqual(spec.disk_size, 75)
     self.assertEqual(spec.iops, 1000)
Exemple #4
0
 def testPresentFlagsOverrideConfigs(self):
     flags = mock_flags.MockFlags()
     flags['aws_provisioned_iops'].Parse(2000)
     flags['data_disk_size'].Parse(100)
     spec = aws_disk.AwsDiskSpec(_COMPONENT, flags, disk_size=75, iops=1000)
     self.assertEqual(spec.disk_size, 100)
     self.assertEqual(spec.iops, 2000)
Exemple #5
0
def patch_critical_objects(stdout='', stderr='', return_code=0):
    with contextlib2.ExitStack() as stack:
        retval = (stdout, stderr, return_code)

        mflags = mock_flags.MockFlags()
        mflags.gcloud_path = 'gcloud'
        mflags.run_uri = _RUN_URI
        mflags.kubectl = _KUBECTL
        mflags.kubeconfig = _KUBECONFIG

        stack.enter_context(mock_flags.PatchFlags(mflags))
        stack.enter_context(mock.patch('__builtin__.open'))
        stack.enter_context(mock.patch(vm_util.__name__ + '.PrependTempDir'))

        # Save and return the temp_file mock here so that we can access the write()
        # call on the instance that the mock returned. This allows us to verify
        # that the body of the file is what we expect it to be (useful for
        # verifying that the pod.yml body was written correctly).
        temp_file = stack.enter_context(
            mock.patch(vm_util.__name__ + '.NamedTemporaryFile'))

        issue_command = stack.enter_context(
            mock.patch(vm_util.__name__ + '.IssueCommand',
                       return_value=retval))

        yield issue_command, temp_file
Exemple #6
0
 def setUp(self):
   super(CloudRedisSpecTestCase, self).setUp()
   self._spec_class = benchmark_config_spec._CloudRedisSpec
   self.flags = mock_flags.MockFlags()
   self.flags['cloud'].value = providers.GCP
   self.flags['cloud'].present = True
   self.flags['run_uri'].value = 'test'
 def testMissingGpuCount(self):
   flags = mock_flags.MockFlags()
   with self.assertRaises(errors.Config.MissingOption) as cm:
     virtual_machine.BaseVmSpec(_COMPONENT,
                                flag_values=flags,
                                gpu_type='k80')
   self.assertEqual(str(cm.exception), (
       'gpu_count must be specified if gpu_type is set'))
 def testDetach(self):
     vm = mock.Mock()
     flags = mock_flags.MockFlags()
     flags['scratch_dir'].parse('/mnt')
     nfs_disk = _NfsDisk(flags)
     nfs_disk.Attach(vm)  # to set the vm on the disk
     nfs_disk.Detach()
     vm.RemoteCommand.assert_called_with('sudo umount /mnt')
Exemple #9
0
 def setUp(self):
     self._mocked_flags = mock_flags.MockFlags()
     self._mocked_flags.cloud = providers.GCP
     self._mocked_flags.os_type = os_types.DEBIAN
     p = mock.patch(util.__name__ + '.GetDefaultProject')
     p.start()
     self.addCleanup(p.stop)
     self.addCleanup(context.SetThreadBenchmarkSpec, None)
Exemple #10
0
 def testCustomMachineTypeFlagOverride(self):
   flags = mock_flags.MockFlags()
   flags['machine_type'].Parse('{cpus: 1, memory: 7.5GiB}')
   result = gce_virtual_machine.GceVmSpec(
       _COMPONENT, flag_values=flags, machine_type='n1-standard-8')
   self.assertEqual(result.machine_type, None)
   self.assertEqual(result.cpus, 1)
   self.assertEqual(result.memory, 7680)
Exemple #11
0
 def testDiskSpecFlag(self):
   flags = mock_flags.MockFlags()
   flags['scratch_dir'].value = '/path/from/flag'
   flags['scratch_dir'].present = True
   result = self._decoder.Decode(
       {'disk_specs': [{'mount_point': '/path/from/spec'}]},
       _COMPONENT, flags)
   self.assertEqual(result.disk_specs[0].mount_point, '/path/from/flag')
def CreateManagedDbFromSpec(spec_dict):
  mock_db_spec = mock.Mock(
      spec=benchmark_config_spec._ManagedRelationalDbSpec)
  mock_db_spec.configure_mock(**spec_dict)
  mocked_flags = mock_flags.MockFlags()
  mocked_flags.run_uri = 'mock-run-uri'
  with mock_flags.PatchFlags(mocked_flags):
    db_class = gcp_managed_relational_db.GCPManagedRelationalDb(mock_db_spec)
  return db_class
Exemple #13
0
 def testStringMachineTypeFlagOverride(self):
   flags = mock_flags.MockFlags()
   flags['machine_type'].Parse('n1-standard-8')
   result = gce_virtual_machine.GceVmSpec(
       _COMPONENT, flag_values=flags,
       machine_type={'cpus': 1, 'memory': '7.5GiB'})
   self.assertEqual(result.machine_type, 'n1-standard-8')
   self.assertEqual(result.cpus, None)
   self.assertEqual(result.memory, None)
    def setUp(self):
        self.flags = mock_flags.MockFlags()
        self.flags['run_uri'].parse('123')

        self.minimal_spec = {
            'cloud': 'GCP',
        }

        cloud_tpu._CLOUD_TPU_REGISTRY = {'GCP': FakeCloudTpu(None)}
Exemple #15
0
 def setUp(self):
     mocked_flags = mock_flags.MockFlags()
     mocked_flags.project = 'project'
     mock_spec = mock.Mock(spec=benchmark_config_spec._CloudRedisSpec)
     mock_spec.redis_name = 'foobar'
     mock_spec.redis_tier = 'tier'
     mock_spec.redis_size_gb = 5
     mock_spec.redis_version = 'version'
     with mock_flags.PatchFlags(mocked_flags):
         self.redis = gcp_cloud_redis.CloudRedis(mock_spec)
  def testInvalidGpuCount(self):
    flags = mock_flags.MockFlags()
    with self.assertRaises(errors.Config.InvalidValue) as cm:
      virtual_machine.BaseVmSpec(_COMPONENT,
                                 flag_values=flags,
                                 gpu_count=0,
                                 gpu_type='k80')

    self.assertEqual(str(cm.exception), (
        'Invalid test_component.gpu_count value: "0". '
        'Value must be at least 1.'))
    def testCreateCustomVersion(self):
        spec = self.create_container_engine_spec()
        flags = mock_flags.MockFlags()
        flags.container_cluster_version = 'fake-version'
        with patch_critical_objects(flags=flags) as issue_command:
            cluster = google_container_engine.GkeCluster(spec)
            cluster._Create()
            command_string = ' '.join(issue_command.call_args[0][0])

            self.assertEqual(issue_command.call_count, 1)
            self.assertIn('--cluster-version fake-version', command_string)
Exemple #18
0
    def testDownloadPreprovisionedDataGcp(self):
        spec = self.create_virtual_machine_spec()
        flags = mock_flags.MockFlags()
        flags.container_cluster_cloud = 'GCP'
        with patch_critical_objects(flags=flags) as (issue_command, _):
            kub_vm = (kubernetes_virtual_machine.
                      DebianBasedKubernetesVirtualMachine(spec))
            kub_vm.DownloadPreprovisionedData('path', 'name', 'filename')

            command = issue_command.call_args[0][0]
            command_string = ' '.join(command)
            self.assertIn('gsutil', command_string)
Exemple #19
0
 def setUp(self):
     mocked_flags = mock_flags.MockFlags()
     mocked_flags.project = 'project'
     mocked_flags.redis_failover_style = cloud_redis.Failover.FAILOVER_NONE
     mock_spec = mock.Mock(spec=benchmark_config_spec._CloudRedisSpec)
     mock_spec.redis_name = 'foobar'
     mock_spec.redis_tier = 'tier'
     mock_spec.redis_size_gb = 5
     mock_spec.redis_version = 'version'
     mock_spec.client_vm = mock.Mock()
     with mock_flags.PatchFlags(mocked_flags):
         self.redis = gcp_cloud_redis.CloudRedis(mock_spec)
  def testInvalidGpuType(self):
    flags = mock_flags.MockFlags()
    with self.assertRaises(errors.Config.InvalidValue) as cm:
      virtual_machine.BaseVmSpec(_COMPONENT,
                                 flag_values=flags,
                                 gpu_count=1,
                                 gpu_type='bad_type')

    self.assertIn((
        'Invalid test_component.gpu_type value: "bad_type". '
        'Value must be one of the following:'), str(cm.exception))

    self.assertIn('k80', str(cm.exception))
    self.assertIn('p100', str(cm.exception))
    def setUp(self):
        self.flags = mock_flags.MockFlags()
        self.flags['run_uri'].parse('123')

        self.full_spec = {
            'cloud': 'GCP',
            'tpu_name': 'pkb-tpu-123',
            'tpu_cidr_range': '192.168.0.0/29',
            'tpu_accelerator_type': 'tpu-v2',
            'tpu_description': 'My TF Node',
            'tpu_network': 'default',
            'tpu_tf_version': 'nightly',
            'tpu_zone': 'us-central1-a'
        }

        cloud_tpu._CLOUD_TPU_REGISTRY = {'GCP': FakeCloudTpu(None)}
Exemple #22
0
 def testNonPresentFlagsDoNotOverrideConfigs(self):
   flags = mock_flags.MockFlags()
   flags['data_disk_size'].value = 100
   flags['data_disk_type'].value = 'flag_disk_type'
   flags['num_striped_disks'].value = 3
   flags['scratch_dir'].value = '/flag_scratch_dir'
   spec = disk.BaseDiskSpec(
       _COMPONENT, flags, device_path='config_device_path', disk_number=1,
       disk_size=75, disk_type='config_disk_type', mount_point='/mountpoint',
       num_striped_disks=2)
   self.assertEqual(spec.device_path, 'config_device_path')
   self.assertEqual(spec.disk_number, 1)
   self.assertEqual(spec.disk_size, 75)
   self.assertEqual(spec.disk_type, 'config_disk_type')
   self.assertEqual(spec.mount_point, '/mountpoint')
   self.assertEqual(spec.num_striped_disks, 2)
Exemple #23
0
 def testPresentFlagsOverrideConfigs(self):
   flags = mock_flags.MockFlags()
   flags['data_disk_size'].Parse(100)
   flags['data_disk_type'].Parse('flag_disk_type')
   flags['num_striped_disks'].Parse(3)
   flags['scratch_dir'].Parse('/flag_scratch_dir')
   spec = disk.BaseDiskSpec(
       _COMPONENT, flags, device_path='config_device_path', disk_number=1,
       disk_size=75, disk_type='config_disk_type', mount_point='/mountpoint',
       num_striped_disks=2)
   self.assertEqual(spec.device_path, 'config_device_path')
   self.assertEqual(spec.disk_number, 1)
   self.assertEqual(spec.disk_size, 100)
   self.assertEqual(spec.disk_type, 'flag_disk_type')
   self.assertEqual(spec.mount_point, '/flag_scratch_dir')
   self.assertEqual(spec.num_striped_disks, 3)
 def testNfsFlags(self):
     flags = mock_flags.MockFlags()
     flags['nfs_version'].parse('4.1')
     flags['nfs_rsize'].parse(1)
     flags['nfs_wsize'].parse(2)
     flags['nfs_timeout'].parse(3)
     flags['nfs_timeout_hard'].parse(False)
     flags['nfs_retries'].parse(4)
     nfs_disk = _NfsDisk(flags)
     mount_options = self.MountOptions(soft=None,
                                       retrans=4,
                                       rsize=1,
                                       timeo=30,
                                       wsize=2,
                                       nfsvers='4.1')
     mount_options.pop('hard')
     self.assertEqual(mount_options,
                      self.MountOptionsAsDict(nfs_disk.mount_options))
Exemple #25
0
    def setUp(self):
        self.flags = mock_flags.MockFlags()
        self.flags['run_uri'].parse('123')

        self.spec = {
            'cloud': 'GCP',
            'engine': 'mysql',
            'vm_spec': {
                'GCP': {
                    'machine_type': 'n1-standard-1'
                }
            },
            'disk_spec': {
                'GCP': {
                    'disk_size': 500
                }
            }
        }
Exemple #26
0
    def setUp(self):
        super(BenchmarkConfigSpecTestCase, self).setUp()
        self._flags = mock_flags.MockFlags()
        self._flags['cloud'].value = providers.GCP

        self._spec_class = benchmark_config_spec.BenchmarkConfigSpec
        self._description = 'Test description.'
        self._vm_groups = {
            'default': {
                'cloud': providers.GCP,
                'os_type': os_types.DEBIAN,
                'vm_spec': _GCP_AWS_VM_CONFIG
            }
        }
        self._kwargs = {
            'description': self._description,
            'vm_groups': self._vm_groups
        }
Exemple #27
0
    def setUp(self):
        self.flags = mock_flags.MockFlags()
        self.flags['run_uri'].parse('123')

        self.minimal_spec = {
            'cloud': 'GCP',
            'engine': 'mysql',
            'vm_spec': {
                'GCP': {
                    'machine_type': 'n1-standard-1'
                }
            },
            'disk_spec': {
                'GCP': {
                    'disk_size': 500
                }
            }
        }

        managed_relational_db._MANAGED_RELATIONAL_DB_REGISTRY = {
            'GCP': FakeManagedRelationalDb(None)
        }
Exemple #28
0
 def setUp(self):
   self._mocked_flags = mock_flags.MockFlags()
   self._mocked_flags.cloud = providers.GCP
   self._mocked_flags.os_type = os_types.DEBIAN
   self.addCleanup(context.SetThreadBenchmarkSpec, None)
Exemple #29
0
 def testVmSpecFlag(self):
   flags = mock_flags.MockFlags()
   flags['install_packages'].value = False
   flags['install_packages'].present = True
   result = self._decoder.Decode({}, _COMPONENT, flags)
   self.assertFalse(result.install_packages)
Exemple #30
0
 def createNonPresentFlags(self):
   result = mock_flags.MockFlags()
   result['cloud'].value = providers.AWS
   result['num_vms'].value = 3
   result['os_type'].value = os_types.WINDOWS
   return result