From 0cfdcb52dd426fbfc7714678f38822e9e4e43d99 Mon Sep 17 00:00:00 2001 From: David Kopp Date: Wed, 22 Nov 2023 17:09:20 +0100 Subject: [PATCH 1/4] Allow services to be omitted --- lib/schema_checker.py | 3 +-- runner.py | 13 ++++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/schema_checker.py b/lib/schema_checker.py index 8eff447ee..3dd980f72 100644 --- a/lib/schema_checker.py +++ b/lib/schema_checker.py @@ -129,8 +129,7 @@ def check_usage_scenario(self, usage_scenario): if 'networks' in usage_scenario: self.validate_networks_no_invalid_chars(usage_scenario['networks']) - for service_name in usage_scenario.get('services'): - service = usage_scenario['services'][service_name] + for service in usage_scenario.get('services', {}): if 'image' not in service and 'build' not in service: raise SchemaError("The 'image' key under services is required when 'build' key is not present.") diff --git a/runner.py b/runner.py index 4f4ebc14e..a07153874 100755 --- a/runner.py +++ b/runner.py @@ -338,8 +338,9 @@ def merge_dicts(dict1, dict2): # creating benchmarking scripts and you want to have all options in the compose but not in each benchmark. # The cleaner way would be to handle an empty service key throughout the code but would make it quite messy # so we chose to remove it right at the start. - for key in [sname for sname, content in yml_obj['services'].items() if content is None]: - del yml_obj['services'][key] + if 'services' in yml_obj: + for key in [sname for sname, content in yml_obj['services'].items() if content is None]: + del yml_obj['services'][key] self._usage_scenario = yml_obj @@ -379,7 +380,7 @@ def check_running_containers(self): raise PermissionError(f"Container '{container_name}' is already running on system. Please close it before running the tool.") def populate_image_names(self): - for service_name, service in self._usage_scenario.get('services', []).items(): + for service_name, service in self._usage_scenario.get('services', {}).items(): if not service.get('image', None): # image is a non essential field. But we need it, so we tmp it if self._dev_repeat_run: service['image'] = f"{service_name}" @@ -541,7 +542,7 @@ def build_docker_images(self): # technically the usage_scenario needs no services and can also operate on an empty list # This use case is when you have running containers on your host and want to benchmark some code running in them - for _, service in self._usage_scenario.get('services', []).items(): + for _, service in self._usage_scenario.get('services', {}).items(): # minimal protection from possible shell escapes. # since we use subprocess without shell we should be safe though if re.findall(r'(\.\.|\$|\'|"|`|!)', service['image']): @@ -640,7 +641,7 @@ def setup_networks(self): def setup_services(self): # technically the usage_scenario needs no services and can also operate on an empty list # This use case is when you have running containers on your host and want to benchmark some code running in them - for service_name in self._usage_scenario.get('services', []): + for service_name, service in self._usage_scenario.get('services', {}).items(): print(TerminalColors.HEADER, '\nSetting up containers', TerminalColors.ENDC) if 'container_name' in self._usage_scenario['services'][service_name]: @@ -648,8 +649,6 @@ def setup_services(self): else: container_name = service_name - service = self._usage_scenario['services'][service_name] - print('Resetting container') # By using the -f we return with 0 if no container is found # we always reset container without checking if something is running, as we expect that a user understands From 7bee90796aa644ae68be2f0b569e18885ac0edf6 Mon Sep 17 00:00:00 2001 From: David Kopp Date: Wed, 22 Nov 2023 23:00:33 +0100 Subject: [PATCH 2/4] Fix schema checker --- lib/schema_checker.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/schema_checker.py b/lib/schema_checker.py index 3dd980f72..a5d04e904 100644 --- a/lib/schema_checker.py +++ b/lib/schema_checker.py @@ -129,9 +129,11 @@ def check_usage_scenario(self, usage_scenario): if 'networks' in usage_scenario: self.validate_networks_no_invalid_chars(usage_scenario['networks']) - for service in usage_scenario.get('services', {}): - if 'image' not in service and 'build' not in service: - raise SchemaError("The 'image' key under services is required when 'build' key is not present.") + if 'services' in usage_scenario: + for service_name in usage_scenario.get('services'): + service = usage_scenario['services'][service_name] + if 'image' not in service and 'build' not in service: + raise SchemaError("The 'image' key under services is required when 'build' key is not present.") usage_scenario_schema.validate(usage_scenario) From 6e9af3582e7cef0f97b3fe3ecbd13ca4af804701 Mon Sep 17 00:00:00 2001 From: David Kopp Date: Wed, 22 Nov 2023 23:01:17 +0100 Subject: [PATCH 3/4] Add hint to README to install dev requirements before running tests --- tests/README.MD | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/README.MD b/tests/README.MD index ff1ca19ac..344fcbf97 100644 --- a/tests/README.MD +++ b/tests/README.MD @@ -1,6 +1,8 @@ ## Pre-reqs -Make sure you have `pytest` and `pydantic` installed: -`pip install pytest pydantic` +Make sure you have the required dev dependencies installed: +```sh +python3 -m pip install -r ../requirements-dev.txt +``` We assume that your green-metrics-tool is already set up to work on your machine, as it will use your local config.yml file to determine which metric providers can be utilized in the tests. From 6f7137cb503c0094423d6d88002c0159b9fa0cd0 Mon Sep 17 00:00:00 2001 From: David Kopp Date: Wed, 29 Nov 2023 21:30:57 +0100 Subject: [PATCH 4/4] Revert "Add hint to README to install dev requirements before running tests" This reverts commit 6e9af3582e7cef0f97b3fe3ecbd13ca4af804701. --- tests/README.MD | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/README.MD b/tests/README.MD index 344fcbf97..ff1ca19ac 100644 --- a/tests/README.MD +++ b/tests/README.MD @@ -1,8 +1,6 @@ ## Pre-reqs -Make sure you have the required dev dependencies installed: -```sh -python3 -m pip install -r ../requirements-dev.txt -``` +Make sure you have `pytest` and `pydantic` installed: +`pip install pytest pydantic` We assume that your green-metrics-tool is already set up to work on your machine, as it will use your local config.yml file to determine which metric providers can be utilized in the tests.