Skip to content

Commit

Permalink
add an integration test so we don't break things or whatever
Browse files Browse the repository at this point in the history
  • Loading branch information
drmorr0 committed Jan 26, 2024
1 parent 8ca1e34 commit 1575c6f
Show file tree
Hide file tree
Showing 13 changed files with 391 additions and 7 deletions.
22 changes: 22 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[run]
branch = True
source =
fireconfig
omit =
fireconfig/k8s/*

[report]
exclude_lines =
# Have to re-enable the standard pragma
\#\s*pragma: no cover

# Don't complain if tests don't hit defensive assertion code:
^\s*raise AssertionError\b
^\s*raise NotImplementedError\b
^\s*return NotImplemented\b
^\s*raise$

# Don't complain if non-runnable code isn't run:
^if __name__ == ['"]__main__['"]:$
# vim:ft=dosini
5 changes: 4 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[flake8]
max-line-length = 121
ignore = E121,E123,E126,E226,E24,E704,W503,W504,E702,E703,E741,W605
extend-ignore = E702,E703,E741,W605,E124,E128
extend-exclude = fireconfig/k8s/*

# vim:ft=dosini
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Run tests

on:
push:

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Check out master
uses: actions/checkout@v4

- name: Install Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install Poetry
uses: snok/install-poetry@v1

- name: Run tests
run: make test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__
.*sw[op]
.coverage
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ repos:
hooks:
- id: end-of-file-fixer
- id: check-yaml
args: ["--allow-multiple-documents"]
- id: trailing-whitespace
- repo: https://github.com/pycqa/isort
rev: 5.13.2
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.PHONY: test

test:
poetry run coverage erase
poetry run coverage run -m pytest -svv itests
poetry run coverage report --show-missing
12 changes: 9 additions & 3 deletions fireconfig/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ def compile(self, app: Construct):
...


def compile(pkgs: T.Dict[str, T.List[AppPackage]], dag_filename: T.Optional[str] = None) -> T.Tuple[str, str]:
app = App()
def compile(
pkgs: T.Dict[str, T.List[AppPackage]],
dag_filename: T.Optional[str] = None,
cdk8s_outdir: T.Optional[str] = None,
dry_run: bool = False,
) -> T.Tuple[str, str]:
app = App(outdir=cdk8s_outdir)
gl = Chart(app, GLOBAL_CHART_NAME, disable_resource_name_hashes=True)

subgraph_dag = defaultdict(list)
Expand Down Expand Up @@ -74,6 +79,7 @@ def compile(pkgs: T.Dict[str, T.List[AppPackage]], dag_filename: T.Optional[str]
graph_str = format_mermaid_graph(subgraph_dag, subgraphs, dag_filename, resource_changes)
diff_str = format_diff(resource_changes)

# app.synth()
if not dry_run:
app.synth()

return graph_str, diff_str
4 changes: 4 additions & 0 deletions itests/output/0000-global.k8s.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: the-namespace
97 changes: 97 additions & 0 deletions itests/output/0001-fc-test-package.k8s.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
apiVersion: v1
kind: Service
metadata:
name: deployment1-svc
namespace: the-namespace
spec:
ports:
- port: 8086
targetPort: 8086
selector:
fireconfig.io/app: deployment1
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: fc-test-package-sa
namespace: the-namespace
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: fc-test-package-crb
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: fc-test-package-sa
namespace: the-namespace
---
apiVersion: v1
kind: ConfigMap
metadata:
name: fc-test-package-the-volume-name
namespace: the-namespace
data:
foo.yml: bar
---
apiVersion: v1
kind: ConfigMap
metadata:
name: fc-test-package-other_name
namespace: the-namespace
data:
bar.yml: asdf
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: fc-test-package-depl
namespace: the-namespace
spec:
replicas: 1
selector:
matchLabels:
fireconfig.io/app: deployment1
template:
metadata:
labels:
fireconfig.io/app: deployment1
spec:
containers:
- args:
- /run.sh
env:
- name: POD_OWNER
value: fc-test-package-depl
image: test:latest
name: container1
ports:
- containerPort: 8086
securityContext:
capabilities:
add:
- SYS_PTRACE
volumeMounts:
- mountPath: /mount/path
name: the-volume-name
- mountPath: /mount/path
name: other_name
nodeSelector:
type: kind-worker
serviceAccountName: fc-test-package-sa
volumes:
- configMap:
items:
- key: foo.yml
path: foo.yml
name: fc-test-package-the-volume-name
name: the-volume-name
- configMap:
items:
- key: bar.yml
path: bar.yml
name: fc-test-package-other_name
name: other_name
33 changes: 33 additions & 0 deletions itests/output/dag.mermaid
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
```mermaid
%%{init: {'themeVariables': {'mainBkg': '#ddd'}}}%%
graph LR

classDef default color:#000
subgraph global
direction LR
global/the-namespace[<b>Namespace</b><br>the-namespace]
%% DELETED OBJECTS START
%% DELETED OBJECTS END
end

subgraph fc-test-package
direction LR
the-namespace/deployment1-svc[<b>Service</b><br>deployment1-svc]
the-namespace/fc-test-package-depl[<b>Deployment</b><br>fc-test-package-depl]
the-namespace/fc-test-package-sa[<b>ServiceAccount</b><br>fc-test-package-sa]
fc-test-package/fc-test-package-crb[<b>ClusterRoleBinding</b><br>fc-test-package-crb]
the-namespace/fc-test-package-the-volume-name[<b>ConfigMap</b><br>fc-test-package-the-volume-name]
the-namespace/fc-test-package-other_name[<b>ConfigMap</b><br>fc-test-package-other_name]
the-namespace/fc-test-package-sa--->the-namespace/fc-test-package-depl
fc-test-package/fc-test-package-crb--->the-namespace/fc-test-package-depl
the-namespace/fc-test-package-the-volume-name--->the-namespace/fc-test-package-depl
the-namespace/fc-test-package-other_name--->the-namespace/fc-test-package-depl
%% DELETED OBJECTS START
%% DELETED OBJECTS END
end

global--->fc-test-package

%% STYLE DEFINITIONS START
%% STYLE DEFINITIONS END
```
54 changes: 54 additions & 0 deletions itests/test_deployment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from constructs import Construct

import fireconfig as fire
from fireconfig.types import Capability

GRPC_PORT = 8086
OUTPUT_DIR = "itests/output"


def _make_deployment():
volumes = (fire.VolumesBuilder()
.with_config_map("the-volume-name", "/mount/path", {"foo.yml": "bar"})
.with_config_map("other_name", "/mount/path", {"bar.yml": "asdf"})
)

container = fire.ContainerBuilder(
name="container1",
image="test:latest",
args=["/run.sh"],
).with_ports(GRPC_PORT).with_security_context(Capability.DEBUG).with_volumes(volumes)

return (fire.DeploymentBuilder(app_label="deployment1")
.with_containers(container)
.with_service()
.with_service_account_and_role_binding("cluster-admin", True)
.with_node_selector("type", "kind-worker")
)


class FCTestPackage(fire.AppPackage):
def __init__(self):
self._depl = _make_deployment()

def compile(self, chart: Construct):
self._depl.build(chart)

@property
def id(self):
return "fc-test-package"


def test_deployment():
old_dag_filename = f"{OUTPUT_DIR}/dag.mermaid"
dag, diff = fire.compile(
{"the-namespace": [FCTestPackage()]},
dag_filename=old_dag_filename,
cdk8s_outdir=OUTPUT_DIR
)

assert diff == ""
with open(old_dag_filename) as f:
# the resulting dag file has a blank newline which gets stripped by pre-commit,
# so compare everything except for that very last character
assert dag[:-1] == f.read()
Loading

0 comments on commit 1575c6f

Please sign in to comment.