diff --git a/perfkitbenchmarker/benchmark_spec.py b/perfkitbenchmarker/benchmark_spec.py index c5b4341d56..f6f7f8e7e6 100644 --- a/perfkitbenchmarker/benchmark_spec.py +++ b/perfkitbenchmarker/benchmark_spec.py @@ -129,15 +129,20 @@ def UnPickleLock(locked: bool, *args): ) # pyformat: disable # TODO(user): Delete this flag after fulling updating gcl. -flags.DEFINE_enum('benchmark_compatibility_checking', SUPPORTED, - [SUPPORTED, NOT_EXCLUDED, SKIP_CHECK], - 'Method used to check compatibility between the benchmark ' - ' and the cloud. ' + SUPPORTED + ' runs the benchmark only' - ' if the cloud provider has declared it supported. ' + - NOT_EXCLUDED + ' runs the benchmark unless it has been' - ' declared not supported by the cloud provider. ' + SKIP_CHECK - + ' does not do the compatibility' - ' check.') +flags.DEFINE_enum( + 'benchmark_compatibility_checking', + SUPPORTED, + [SUPPORTED, NOT_EXCLUDED, SKIP_CHECK], + 'Method used to check compatibility between the benchmark and the cloud. ' + + SUPPORTED + + ' runs the benchmark only' + ' if the cloud provider has declared it supported. ' + + NOT_EXCLUDED + + ' runs the benchmark unless it has been' + ' declared not supported by the cloud provider. ' + + SKIP_CHECK + + ' does not do the compatibility check.', +) # pyformat: enable @@ -282,8 +287,7 @@ def vm_groups(self) -> dict[str, list[virtual_machine.BaseVirtualMachine]]: """Returns the vm groups in the benchmark.""" vm_groups = dict(self.unmanaged_vm_groups) vm_groups.update({ - name: list(group.vms) - for name, group in self.managed_vm_groups.items() + name: list(group.vms) for name, group in self.managed_vm_groups.items() }) return vm_groups @@ -877,7 +881,9 @@ def ConstructCapacityReservations(self): providers.LoadProvider(cloud) capacity_reservation_class = capacity_reservation.GetResourceClass(cloud) self.capacity_reservations.append( - capacity_reservation_class(vm_group) # pytype: disable=not-instantiable + capacity_reservation_class( + vm_group + ) # pytype: disable=not-instantiable ) def _CheckBenchmarkSupport(self, cloud): @@ -939,9 +945,9 @@ def ConstructVirtualMachines(self): jujuvm.units.extend(vms) # pytype: disable=attribute-error if jujuvm and jujuvm not in self.vms: - self.unmanaged_vm_groups[ - '%s_juju_controller' % group_spec.cloud - ] = [jujuvm] + self.unmanaged_vm_groups['%s_juju_controller' % group_spec.cloud] = [ + jujuvm + ] self.unmanaged_vm_groups[group_name] = vms @@ -1247,7 +1253,7 @@ def Delete(self): if self.managed_vm_groups: background_tasks.RunThreaded( lambda vm_group: vm_group.Delete(), - list(self.managed_vm_groups.values()) + list(self.managed_vm_groups.values()), ) if self.vms: @@ -1338,9 +1344,9 @@ def _GetResourceDict(self, time_format, timeout_minutes=None): tags = { resource_type.TIMEOUT_METADATA_KEY: timeout_utc.strftime(time_format), 'create_time_utc': now_utc.strftime(time_format), - 'benchmark': self.name, + 'benchmark': self._SafeLabelKeyOrValue(self.name), 'perfkit_uuid': self.uuid, - 'owner': FLAGS.owner, + 'owner': self._SafeLabelKeyOrValue(FLAGS.owner), 'benchmark_uid': self.uid, } diff --git a/tests/benchmark_spec_test.py b/tests/benchmark_spec_test.py index 4c19de4967..dffc348f80 100644 --- a/tests/benchmark_spec_test.py +++ b/tests/benchmark_spec_test.py @@ -20,6 +20,7 @@ from absl.testing import flagsaver import mock from perfkitbenchmarker import benchmark_spec +from perfkitbenchmarker import flags as pkb_flags from perfkitbenchmarker import context from perfkitbenchmarker import flag_alias from perfkitbenchmarker import pkb # pylint: disable=unused-import # noqa @@ -125,6 +126,15 @@ def setUp(self): self.addCleanup(context.SetThreadBenchmarkSpec, None) +class GetResourceTagsTestCase(_BenchmarkSpecTestCase): + + @mock.patch('getpass.getuser', return_value='Name_Surname') + def testOwnerFromGetuserIsSanitized(self, _): + FLAGS.owner = pkb_flags.GetCurrentUser() + spec = pkb_common_test_case.CreateBenchmarkSpecFromYaml(SIMPLE_CONFIG) + self.assertEqual('name_surname', spec.GetResourceTags()['owner']) + + class GenericTestCase(_BenchmarkSpecTestCase): def testGetSamples(self):