From 94787f52efbb14e610d2d8039199bcf76e0cdc96 Mon Sep 17 00:00:00 2001 From: Kiryl Filatau Date: Thu, 4 Jun 2026 19:58:02 +0200 Subject: [PATCH 1/2] Fix GCP labels by lowercasing MakeFormattedDefaultTags --- perfkitbenchmarker/providers/gcp/util.py | 2 +- tests/providers/gcp/util_test.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/perfkitbenchmarker/providers/gcp/util.py b/perfkitbenchmarker/providers/gcp/util.py index bfe124957f..d18e2619e5 100644 --- a/perfkitbenchmarker/providers/gcp/util.py +++ b/perfkitbenchmarker/providers/gcp/util.py @@ -620,7 +620,7 @@ def MakeFormattedDefaultTags(timeout_minutes: int | None = None): Returns: A string contains tags, contributed from the benchmark spec. """ - return FormatTags(GetDefaultTags(timeout_minutes)) + return FormatTags(GetDefaultTags(timeout_minutes)).lower() def GetAccessToken(application_default: bool = True) -> str: diff --git a/tests/providers/gcp/util_test.py b/tests/providers/gcp/util_test.py index cf72c230fe..43d3c3de4d 100644 --- a/tests/providers/gcp/util_test.py +++ b/tests/providers/gcp/util_test.py @@ -523,6 +523,21 @@ def testFormatAndSplitAreInverses(self): tags = {'a': 'b', 'c': 'd'} self.assertEqual(tags, util.SplitTags(util.FormatTags(tags))) + @mock.patch.object( + util, + 'GetDefaultTags', + return_value={ + 'owner': 'Name_Surname', + 'benchmark': 'kubernetes_scale', + }, + ) + def testMakeFormattedDefaultTagsLowercasesOwner(self, mock_get_default_tags): + del mock_get_default_tags + self.assertEqual( + 'benchmark=kubernetes_scale,owner=name_surname', + util.MakeFormattedDefaultTags(), + ) + if __name__ == '__main__': unittest.main() From b6d0f4ea71a375e79f5a53acf80e28f2706d435f Mon Sep 17 00:00:00 2001 From: Kiryl Filatau Date: Thu, 11 Jun 2026 16:27:40 +0200 Subject: [PATCH 2/2] pyink reformated --- perfkitbenchmarker/benchmark_spec.py | 42 ++++++++++++++---------- perfkitbenchmarker/providers/gcp/util.py | 2 +- tests/benchmark_spec_test.py | 10 ++++++ tests/providers/gcp/util_test.py | 15 --------- 4 files changed, 35 insertions(+), 34 deletions(-) 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/perfkitbenchmarker/providers/gcp/util.py b/perfkitbenchmarker/providers/gcp/util.py index d18e2619e5..bfe124957f 100644 --- a/perfkitbenchmarker/providers/gcp/util.py +++ b/perfkitbenchmarker/providers/gcp/util.py @@ -620,7 +620,7 @@ def MakeFormattedDefaultTags(timeout_minutes: int | None = None): Returns: A string contains tags, contributed from the benchmark spec. """ - return FormatTags(GetDefaultTags(timeout_minutes)).lower() + return FormatTags(GetDefaultTags(timeout_minutes)) def GetAccessToken(application_default: bool = True) -> str: 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): diff --git a/tests/providers/gcp/util_test.py b/tests/providers/gcp/util_test.py index 43d3c3de4d..cf72c230fe 100644 --- a/tests/providers/gcp/util_test.py +++ b/tests/providers/gcp/util_test.py @@ -523,21 +523,6 @@ def testFormatAndSplitAreInverses(self): tags = {'a': 'b', 'c': 'd'} self.assertEqual(tags, util.SplitTags(util.FormatTags(tags))) - @mock.patch.object( - util, - 'GetDefaultTags', - return_value={ - 'owner': 'Name_Surname', - 'benchmark': 'kubernetes_scale', - }, - ) - def testMakeFormattedDefaultTagsLowercasesOwner(self, mock_get_default_tags): - del mock_get_default_tags - self.assertEqual( - 'benchmark=kubernetes_scale,owner=name_surname', - util.MakeFormattedDefaultTags(), - ) - if __name__ == '__main__': unittest.main()