Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run tests in parallel using pytest-xdist #881

Merged
merged 13 commits into from
Oct 14, 2022
44 changes: 40 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ jobs:
terraform_version: ${{ env.TF_VERSION }}
terraform_wrapper: false

- name: Config Terraform plugin cache
run: |
echo 'plugin_cache_dir="$HOME/.terraform.d/plugin-cache"' >~/.terraformrc
mkdir --parents ~/.terraform.d/plugin-cache

- name: Initialize providers
run: |
terraform init

# avoid conflicts with user-installed providers on local machines
- name: Pin provider versions
run: |
Expand All @@ -66,7 +75,7 @@ jobs:
run: |
mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }}
pip install -r tests/requirements.txt
pytest -vv tests/examples
pytest -vv --numprocesses auto --dist loadgroup tests/examples

examples:
runs-on: ubuntu-latest
Expand All @@ -89,6 +98,15 @@ jobs:
terraform_version: ${{ env.TF_VERSION }}
terraform_wrapper: false

- name: Config Terraform plugin cache
run: |
echo 'plugin_cache_dir="$HOME/.terraform.d/plugin-cache"' >~/.terraformrc
mkdir --parents ~/.terraform.d/plugin-cache

- name: Initialize providers
run: |
terraform init

# avoid conflicts with user-installed providers on local machines
- name: Pin provider versions
run: |
Expand All @@ -101,7 +119,7 @@ jobs:
run: |
mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }}
pip install -r tests/requirements.txt
pytest -vv tests/blueprints
pytest -vv --numprocesses auto --dist loadgroup tests/blueprints

modules:
runs-on: ubuntu-latest
Expand All @@ -124,6 +142,15 @@ jobs:
terraform_version: ${{ env.TF_VERSION }}
terraform_wrapper: false

- name: Config Terraform plugin cache
run: |
echo 'plugin_cache_dir="$HOME/.terraform.d/plugin-cache"' >~/.terraformrc
mkdir --parents ~/.terraform.d/plugin-cache

- name: Initialize providers
run: |
terraform init

# avoid conflicts with user-installed providers on local machines
- name: Pin provider versions
run: |
Expand All @@ -136,7 +163,7 @@ jobs:
run: |
mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }}
pip install -r tests/requirements.txt
pytest -vv tests/modules
pytest -vv --numprocesses auto --dist loadgroup tests/modules

fast:
runs-on: ubuntu-latest
Expand All @@ -159,6 +186,15 @@ jobs:
terraform_version: ${{ env.TF_VERSION }}
terraform_wrapper: false

- name: Config Terraform plugin cache
run: |
echo 'plugin_cache_dir="$HOME/.terraform.d/plugin-cache"' >~/.terraformrc
mkdir --parents ~/.terraform.d/plugin-cache

- name: Initialize providers
run: |
terraform init

# avoid conflicts with user-installed providers on local machines
- name: Pin provider versions
run: |
Expand All @@ -171,4 +207,4 @@ jobs:
run: |
mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }}
pip install -r tests/requirements.txt
pytest -vv tests/fast
pytest -vv --numprocesses auto --dist loadgroup tests/fast
4 changes: 4 additions & 0 deletions default-versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ terraform {
source = "hashicorp/google-beta"
version = ">= 4.36.0" # tftest
}
# used in fast
local = {
source = "hashicorp/local"
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/blueprints/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest


def pytest_collection_modifyitems(config, items):
for item in items:
item.add_marker(
pytest.mark.xdist_group(name='/'.join(item.path.parent.parts[-2:])))
4 changes: 3 additions & 1 deletion tests/examples/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pathlib import Path

import marko
import pytest

FABRIC_ROOT = Path(__file__).parents[2]
MODULES_PATH = FABRIC_ROOT / 'modules/'
Expand All @@ -36,13 +37,14 @@ def pytest_generate_tests(metafunc):
doc = marko.parse(readme.read_text())
index = 0
last_header = None
mark = pytest.mark.xdist_group(name=module.name)
for child in doc.children:
if isinstance(child, marko.block.FencedCode) and child.lang == 'hcl':
index += 1
code = child.children[0].children
if 'tftest skip' in code:
continue
examples.append(code)
examples.append(pytest.param(code, marks=mark))
path = module.relative_to(FABRIC_ROOT)
name = f'{path}:{last_header}'
if index > 1:
Expand Down
20 changes: 20 additions & 0 deletions tests/fast/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest


def pytest_collection_modifyitems(config, items):
for item in items:
item.add_marker(pytest.mark.xdist_group(name=item.path.parent.name))
11 changes: 2 additions & 9 deletions tests/modules/api_gateway/test_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest


@pytest.fixture
def resources(plan_runner):
_, resources = plan_runner()
return resources


def test_resource_count(resources):
def test_resource_count(plan_runner):
"Test number of resources created."
_, resources = plan_runner()
assert len(resources) == 5
20 changes: 20 additions & 0 deletions tests/modules/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest


def pytest_collection_modifyitems(config, items):
for item in items:
item.add_marker(pytest.mark.xdist_group(name=item.path.parent.name))
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pytest>=6.2.5
pytest-xdist
PyYAML>=6.0
tftest>=1.6.3
marko>=1.2.0
Expand Down