Skip to content

Commit 1f0f20f

Browse files
committed
add an integration test so we don't break things or whatever
1 parent 8ca1e34 commit 1f0f20f

13 files changed

+393
-7
lines changed

.coveragerc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[run]
2+
branch = True
3+
source =
4+
fireconfig
5+
omit =
6+
fireconfig/k8s/*
7+
8+
[report]
9+
exclude_lines =
10+
# Have to re-enable the standard pragma
11+
\#\s*pragma: no cover
12+
13+
# Don't complain if tests don't hit defensive assertion code:
14+
^\s*raise AssertionError\b
15+
^\s*raise NotImplementedError\b
16+
^\s*return NotImplemented\b
17+
^\s*raise$
18+
19+
# Don't complain if non-runnable code isn't run:
20+
^if __name__ == ['"]__main__['"]:$
21+
22+
# vim:ft=dosini

.flake8

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[flake8]
22
max-line-length = 121
3-
ignore = E121,E123,E126,E226,E24,E704,W503,W504,E702,E703,E741,W605
3+
extend-ignore = E702,E703,E741,W605,E124,E128
4+
extend-exclude = fireconfig/k8s/*
5+
6+
# vim:ft=dosini

.github/workflows/test.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Run tests
2+
3+
on:
4+
push:
5+
6+
jobs:
7+
test:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- name: Check out master
12+
uses: actions/checkout@v4
13+
14+
- name: Install Python
15+
uses: actions/setup-python@v5
16+
with:
17+
python-version: '3.11'
18+
19+
- name: Install Poetry
20+
uses: snok/install-poetry@v1
21+
22+
- name: Run tests
23+
run: |
24+
poetry install
25+
make test

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
__pycache__
22
.*sw[op]
3+
.coverage

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ repos:
66
hooks:
77
- id: end-of-file-fixer
88
- id: check-yaml
9+
args: ["--allow-multiple-documents"]
910
- id: trailing-whitespace
1011
- repo: https://github.com/pycqa/isort
1112
rev: 5.13.2

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.PHONY: test
2+
3+
test:
4+
poetry run coverage erase
5+
poetry run coverage run -m pytest -svv itests
6+
poetry run coverage report --show-missing

fireconfig/__init__.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ def compile(self, app: Construct):
4242
...
4343

4444

45-
def compile(pkgs: T.Dict[str, T.List[AppPackage]], dag_filename: T.Optional[str] = None) -> T.Tuple[str, str]:
46-
app = App()
45+
def compile(
46+
pkgs: T.Dict[str, T.List[AppPackage]],
47+
dag_filename: T.Optional[str] = None,
48+
cdk8s_outdir: T.Optional[str] = None,
49+
dry_run: bool = False,
50+
) -> T.Tuple[str, str]:
51+
app = App(outdir=cdk8s_outdir)
4752
gl = Chart(app, GLOBAL_CHART_NAME, disable_resource_name_hashes=True)
4853

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

77-
# app.synth()
82+
if not dry_run:
83+
app.synth()
7884

7985
return graph_str, diff_str

itests/output/0000-global.k8s.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: the-namespace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: deployment1-svc
5+
namespace: the-namespace
6+
spec:
7+
ports:
8+
- port: 8086
9+
targetPort: 8086
10+
selector:
11+
fireconfig.io/app: deployment1
12+
---
13+
apiVersion: v1
14+
kind: ServiceAccount
15+
metadata:
16+
name: fc-test-package-sa
17+
namespace: the-namespace
18+
---
19+
apiVersion: rbac.authorization.k8s.io/v1
20+
kind: ClusterRoleBinding
21+
metadata:
22+
name: fc-test-package-crb
23+
roleRef:
24+
apiGroup: rbac.authorization.k8s.io
25+
kind: ClusterRole
26+
name: cluster-admin
27+
subjects:
28+
- kind: ServiceAccount
29+
name: fc-test-package-sa
30+
namespace: the-namespace
31+
---
32+
apiVersion: v1
33+
kind: ConfigMap
34+
metadata:
35+
name: fc-test-package-the-volume-name
36+
namespace: the-namespace
37+
data:
38+
foo.yml: bar
39+
---
40+
apiVersion: v1
41+
kind: ConfigMap
42+
metadata:
43+
name: fc-test-package-other_name
44+
namespace: the-namespace
45+
data:
46+
bar.yml: asdf
47+
---
48+
apiVersion: apps/v1
49+
kind: Deployment
50+
metadata:
51+
name: fc-test-package-depl
52+
namespace: the-namespace
53+
spec:
54+
replicas: 1
55+
selector:
56+
matchLabels:
57+
fireconfig.io/app: deployment1
58+
template:
59+
metadata:
60+
labels:
61+
fireconfig.io/app: deployment1
62+
spec:
63+
containers:
64+
- args:
65+
- /run.sh
66+
env:
67+
- name: POD_OWNER
68+
value: fc-test-package-depl
69+
image: test:latest
70+
name: container1
71+
ports:
72+
- containerPort: 8086
73+
securityContext:
74+
capabilities:
75+
add:
76+
- SYS_PTRACE
77+
volumeMounts:
78+
- mountPath: /mount/path
79+
name: the-volume-name
80+
- mountPath: /mount/path
81+
name: other_name
82+
nodeSelector:
83+
type: kind-worker
84+
serviceAccountName: fc-test-package-sa
85+
volumes:
86+
- configMap:
87+
items:
88+
- key: foo.yml
89+
path: foo.yml
90+
name: fc-test-package-the-volume-name
91+
name: the-volume-name
92+
- configMap:
93+
items:
94+
- key: bar.yml
95+
path: bar.yml
96+
name: fc-test-package-other_name
97+
name: other_name

itests/output/dag.mermaid

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
```mermaid
2+
%%{init: {'themeVariables': {'mainBkg': '#ddd'}}}%%
3+
graph LR
4+
5+
classDef default color:#000
6+
subgraph global
7+
direction LR
8+
global/the-namespace[<b>Namespace</b><br>the-namespace]
9+
%% DELETED OBJECTS START
10+
%% DELETED OBJECTS END
11+
end
12+
13+
subgraph fc-test-package
14+
direction LR
15+
the-namespace/deployment1-svc[<b>Service</b><br>deployment1-svc]
16+
the-namespace/fc-test-package-depl[<b>Deployment</b><br>fc-test-package-depl]
17+
the-namespace/fc-test-package-sa[<b>ServiceAccount</b><br>fc-test-package-sa]
18+
fc-test-package/fc-test-package-crb[<b>ClusterRoleBinding</b><br>fc-test-package-crb]
19+
the-namespace/fc-test-package-the-volume-name[<b>ConfigMap</b><br>fc-test-package-the-volume-name]
20+
the-namespace/fc-test-package-other_name[<b>ConfigMap</b><br>fc-test-package-other_name]
21+
the-namespace/fc-test-package-sa--->the-namespace/fc-test-package-depl
22+
fc-test-package/fc-test-package-crb--->the-namespace/fc-test-package-depl
23+
the-namespace/fc-test-package-the-volume-name--->the-namespace/fc-test-package-depl
24+
the-namespace/fc-test-package-other_name--->the-namespace/fc-test-package-depl
25+
%% DELETED OBJECTS START
26+
%% DELETED OBJECTS END
27+
end
28+
29+
global--->fc-test-package
30+
31+
%% STYLE DEFINITIONS START
32+
%% STYLE DEFINITIONS END
33+
```

itests/test_deployment.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from constructs import Construct
2+
3+
import fireconfig as fire
4+
from fireconfig.types import Capability
5+
6+
GRPC_PORT = 8086
7+
OUTPUT_DIR = "itests/output"
8+
9+
10+
def _make_deployment():
11+
volumes = (fire.VolumesBuilder()
12+
.with_config_map("the-volume-name", "/mount/path", {"foo.yml": "bar"})
13+
.with_config_map("other_name", "/mount/path", {"bar.yml": "asdf"})
14+
)
15+
16+
container = fire.ContainerBuilder(
17+
name="container1",
18+
image="test:latest",
19+
args=["/run.sh"],
20+
).with_ports(GRPC_PORT).with_security_context(Capability.DEBUG).with_volumes(volumes)
21+
22+
return (fire.DeploymentBuilder(app_label="deployment1")
23+
.with_containers(container)
24+
.with_service()
25+
.with_service_account_and_role_binding("cluster-admin", True)
26+
.with_node_selector("type", "kind-worker")
27+
)
28+
29+
30+
class FCTestPackage(fire.AppPackage):
31+
def __init__(self):
32+
self._depl = _make_deployment()
33+
34+
def compile(self, chart: Construct):
35+
self._depl.build(chart)
36+
37+
@property
38+
def id(self):
39+
return "fc-test-package"
40+
41+
42+
def test_deployment():
43+
old_dag_filename = f"{OUTPUT_DIR}/dag.mermaid"
44+
dag, diff = fire.compile(
45+
{"the-namespace": [FCTestPackage()]},
46+
dag_filename=old_dag_filename,
47+
cdk8s_outdir=OUTPUT_DIR
48+
)
49+
50+
assert diff == ""
51+
with open(old_dag_filename) as f:
52+
# the resulting dag file has a blank newline which gets stripped by pre-commit,
53+
# so compare everything except for that very last character
54+
assert dag[:-1] == f.read()

0 commit comments

Comments
 (0)