Skip to content

Commit

Permalink
ci: Provider Build Caching
Browse files Browse the repository at this point in the history
This centralising the caching for the provider builds, along with making the
workflow a shared one.
  • Loading branch information
techman83 committed Mar 21, 2024
1 parent d226e47 commit 96a867b
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 65 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/build-providers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Build / Cache Providers

on:
workflow_call:

concurrency:
group: provider-builds

jobs:
build-providers:
strategy:
matrix:
include:
- provider: random
version: 3.6.0
name: CallyProvidersRandom
- provider: google
version: 5.21.0
name: CallyProvidersGoogle
runs-on: ubuntu-latest
steps:
- name: Restore Provider Packages
id: cache-providers
uses: actions/cache@v4
with:
path: build/random/dist/${ matrix.name }}-${{ matrix.version }}.tar.gz
key: cally-provider-${{ matrix.provider }}-${{ matrix.version }}
- uses: actions/checkout@v4
if: steps.cache-providers.outputs.cache-hit != 'true'
- name: Setup Python
if: steps.cache-providers.outputs.cache-hit != 'true'
uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: pip
- name: Install Cally
if: steps.cache-providers.outputs.cache-hit != 'true'
run: pip install .[provider_build]
- uses: actions/setup-node@v4
if: steps.cache-providers.outputs.cache-hit != 'true'
with:
node-version: "20"
- name: Install cdktf-cli and build
if: steps.cache-providers.outputs.cache-hit != 'true'
run: |
npm install cdktf-cli
echo "$(pwd)/node_modules/.bin/" >> $GITHUB_PATH
cally provider build --provider ${{ matrix.provider }} --version ${{ matrix.version }}
(cd build/${{ matrix.provider }} && python -m build)
27 changes: 8 additions & 19 deletions .github/workflows/coverage-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,21 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: .github/workflows/build-providers.yaml
- uses: actions/cache/restore@v3
id: cache
with:
path: build/random/dist/CallyProvidersRandom-3.6.0.tar.gz
key: cally-provider-random-3.6.0
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: pip
- name: Install Cally test dependencies
run: pip install .[test]
- name: Restore Provider Packages
id: cache-providers
uses: actions/cache@v4
with:
path: build/random/dist/CallyProvidersRandom-3.6.0.tar.gz
key: cally-provider-random-3.6.0
- uses: actions/setup-node@v4
if: steps.cache-providers.outputs.cache-hit != 'true'
with:
node-version: "20"
- name: Install cdktf-cli and build
if: steps.cache-providers.outputs.cache-hit != 'true'
run: |
npm install cdktf-cli
echo "$(pwd)/node_modules/.bin/" >> $GITHUB_PATH
cally provider build --provider random --version 3.6.0
(cd build/random && python -m build)
- name: Install Provider Pacakge
run: pip install build/random/dist/CallyProvidersRandom-3.6.0.tar.gz
- name: Install Cally test dependencies
run: pip install .[test]
- name: Run Coverage
run: |
coverage run -m pytest
Expand Down
28 changes: 8 additions & 20 deletions .github/workflows/test-cally.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,23 @@ jobs:
matrix:
python: ["3.11"]
steps:
# Restore Provider Packages
- uses: actions/checkout@v4
- uses: .github/workflows/build-providers.yaml
- uses: actions/cache/restore@v3
id: cache
with:
path: build/random/dist/CallyProvidersRandom-3.6.0.tar.gz
key: cally-provider-random-3.6.0

# Cally Testing
# Cally Install
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
cache: pip
- name: Install Cally test dependencies
run: pip install .[test]

# Provider Packages
- name: Restore Provider Packages
id: cache-providers
uses: actions/cache@v4
with:
path: build/random/dist/CallyProvidersRandom-3.6.0.tar.gz
key: cally-provider-random-3.6.0
- uses: actions/setup-node@v4
if: steps.cache-providers.outputs.cache-hit != 'true'
with:
node-version: "20"
- name: Install cdktf-cli and build
if: steps.cache-providers.outputs.cache-hit != 'true'
run: |
npm install cdktf-cli
echo "$(pwd)/node_modules/.bin/" >> $GITHUB_PATH
cally provider build --provider random --version 3.6.0
(cd build/random && python -m build)
- name: Install Provider Pacakge
run: pip install build/random/dist/CallyProvidersRandom-3.6.0.tar.gz

Expand Down
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ test = [
"ruff",
"types-PyYAML",
]
provider_build = [
"build"
]

[project.scripts]
cally = "cally.cli:cally"
Expand All @@ -53,8 +56,15 @@ cally = "cally.cli:cally"
skip-string-normalization = true

[tool.coverage.run]
branch = true
omit = ["tests/*"]

[tool.coverage.report]
exclude_lines = [
"if TYPE_CHECKING:",
" pass",
]

[[tool.mypy.overrides]]
module = "dynaconf.*"
ignore_missing_imports = true
Expand Down
6 changes: 6 additions & 0 deletions src/cally/cli/config/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import yaml
from dynaconf import LazySettings

try:
from cally.idp.defaults import DEFAULTS as IDP_DEFAULTS # type: ignore
except ModuleNotFoundError:
IDP_DEFAULTS: dict = {} # type: ignore[no-redef]


def load(obj: LazySettings, *args, **kwargs) -> None: # noqa: ARG001
"""
Expand Down Expand Up @@ -31,6 +36,7 @@ def load(obj: LazySettings, *args, **kwargs) -> None: # noqa: ARG001
loaded = yaml.safe_load(config_file.read_text())

# Defaults
obj.update(IDP_DEFAULTS)
obj.update(loaded.get('defaults', {}))
if obj.cally_env is not None:
obj.update(environment=obj.cally_env)
Expand Down
34 changes: 34 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import os
import sys
from importlib import import_module, reload
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import TestCase, mock
Expand Down Expand Up @@ -56,3 +58,35 @@ def synth_stack(self, stack: CallyStack) -> dict:
stack.synth_stack(self.working.name)
output_file = Path(self.working.name, 'stacks', stack.name, 'cdk.tf.json')
return json.loads(output_file.read_text())


class CallyIdpTestHarness(CallyTfTestHarness):
def setUp(self):
sys.path.append(Path(Path(__file__).parent, 'testdata/test_cls').as_posix())
self.reload_cally_modules()
super().setUp()

def tearDown(self):
super().tearDown()
sys.path.remove(Path(Path(__file__).parent, 'testdata/test_cls').as_posix())
for mod in [
'cally.idp',
'cally.idp.defaults',
'cally.idp.stacks',
'cally.idp.commands',
]:
if mod in sys.modules:
del sys.modules[mod]
self.reload_cally_modules()

@staticmethod
def reload_cally_modules():
modules = [
'cally.cli',
'cally.cdk.stacks',
'cally.cli.config.loader',
'cally.cli.config',
]
for module in modules:
mod = import_module(module)
reload(mod)
4 changes: 2 additions & 2 deletions tests/cli/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os
from unittest import mock

from cally.cli.config import CallyConfig
from cally.cli.config import types as cally_types
from dynaconf import ValidationError

from cally.cli.config import CallyConfig
from cally.cli.config import types as cally_types
from ... import CallyTestHarness


Expand Down
17 changes: 17 additions & 0 deletions tests/cli/config/test_idp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from cally.cli.config import CallyConfig

from ... import CallyIdpTestHarness


class CallyIdpConfigTests(CallyIdpTestHarness):

def test_idp_defaults(self):
config = CallyConfig(config_file='blah.yaml')
config.environment = 'harness'
config.service = 'idp-defaults-ya'
data = {
'ENVIRONMENT': 'harness',
'NAME': 'idp-defaults-ya',
'PROVIDERS': {'provido': {'location': 'some-place1'}},
}
self.assertDictEqual(config.settings.to_dict(), data)
1 change: 0 additions & 1 deletion tests/cli/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


class ConfigCliTests(CallyTestHarness):

@mock.patch.dict(os.environ, {"CALLY_STACK_TYPE": "CallyStack"})
def test_print_service_basic(self):
result = CliRunner().invoke(
Expand Down
13 changes: 2 additions & 11 deletions tests/cli/test_idp.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import sys
from importlib import reload
from pathlib import Path

sys.path.append(Path(Path(__file__).parent, '../testdata/test_cls').as_posix())

from cally import cli
from cally.cli.config import CallyConfig
from click.testing import CliRunner

from .. import CallyTestHarness
from .. import CallyIdpTestHarness


class CliTests(CallyTestHarness):
def setUp(self):
super().setUp()
reload(cli)
class CliTests(CallyIdpTestHarness):

def test_example(self):
result = CliRunner().invoke(
Expand Down
14 changes: 2 additions & 12 deletions tests/stacks/test_idp.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import sys
from importlib import reload
from pathlib import Path

sys.path.append(Path(Path(__file__).parent, '../testdata/test_cls').as_posix())

from cally.cdk import stacks

from .. import CallyTfTestHarness

from .. import CallyIdpTestHarness

class IdpStackTests(CallyTfTestHarness):
def setUp(self):
super().setUp()
reload(stacks)

class IdpStackTests(CallyIdpTestHarness):
def test_class_load(self):
self.assertEqual(stacks.MinimalStack.__name__, 'MinimalStack')

Expand Down
1 change: 1 addition & 0 deletions tests/testdata/test_cls/cally/idp/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEFAULTS = {'providers': {'provido': {'location': 'some-place1'}}}

0 comments on commit 96a867b

Please sign in to comment.