Repository navigation
Expand file tree
/
Copy pathtest_image_ref.py
More file actions
117 lines (94 loc) · 4.28 KB
/
Copy pathtest_image_ref.py
File metadata and controls
117 lines (94 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""ch_cli_tools.model harness.deployment.image_ref support. An application
declaring it runs an image of its own build (a base/common image from
harness.dependencies.build, or one of its own task images) instead of
building or declaring a prebuilt one. Ported from legacy's
test_deployment_image_ref_* in tests/test_helm.py.
"""
import pytest
from conftest import CLOUDHARNESS_ROOT, RESOURCES
from test_util import write_tree
from ch_cli_tools.api import create_docker_compose_configuration, create_helm_chart
from ch_cli_tools.model import ValuesValidationError
def _image_ref_solution(tmp_path, image_ref="cloudharness-base", dockerfile=False, instance=False):
"""A solution with one application, refapp, running an image of the build through
image_ref - mirrors legacy's own `image_ref_solution` fixture builder."""
solution = tmp_path / "image_ref_solution"
values = f"""harness:
subdomain: refapp
deployment:
auto: true
image_ref: {image_ref}
dependencies:
build:
- cloudharness-base
"""
deploy = {"values.yaml": values}
if instance:
deploy["instances"] = {"one": {"values.yaml": "harness:\n subdomain: one\n"}}
refapp = {"deploy": deploy}
if dockerfile:
refapp["Dockerfile"] = "FROM scratch\n"
write_tree(solution, {"applications": {"refapp": refapp}})
return solution
def _chart(tmp_path, solution, **kwargs):
kwargs.setdefault("domain", "my.local")
kwargs.setdefault("namespace", "test")
kwargs.setdefault("local", False)
kwargs.setdefault("tag", "1")
kwargs.setdefault("registry", "reg")
return create_helm_chart(
[CLOUDHARNESS_ROOT, RESOURCES, solution], output_path=tmp_path, **kwargs
).to_dict()
def test_deployment_image_ref_runs_an_image_of_the_build(tmp_path):
solution = _image_ref_solution(tmp_path)
values = _chart(tmp_path, solution, include=["refapp"])
base_image = values["task-images"]["cloudharness-base"]
app = values["apps"]["refapp"]
assert app["build"] is False
assert app["image"] == base_image
assert app["harness"]["deployment"]["image"] == base_image
def test_deployment_image_ref_without_include(tmp_path):
solution = _image_ref_solution(tmp_path)
values = _chart(tmp_path, solution)
app = values["apps"]["refapp"]
assert app["build"] is False
assert app["harness"]["deployment"]["image"] == values["task-images"]["cloudharness-base"]
def test_deployment_image_ref_wins_over_a_dockerfile(tmp_path):
solution = _image_ref_solution(tmp_path, dockerfile=True)
values = _chart(tmp_path, solution, include=["refapp"])
app = values["apps"]["refapp"]
assert app["build"] is False
assert app["harness"]["deployment"]["image"] == values["task-images"]["cloudharness-base"]
def test_deployment_image_ref_reaches_its_own_instance(tmp_path):
solution = _image_ref_solution(tmp_path, instance=True)
values = _chart(tmp_path, solution, include=["refapp"], env="test")
base_image = values["task-images"]["cloudharness-base"]
for app_key in ("refapp", "refapp-one"):
app = values["apps"][app_key]
assert app["image"] == base_image
assert app["harness"]["deployment"]["image"] == base_image
def test_deployment_image_ref_to_an_image_not_built_fails(tmp_path):
solution = _image_ref_solution(tmp_path, image_ref="not-built")
with pytest.raises(ValuesValidationError, match="not-built"):
_chart(tmp_path, solution, include=["refapp"])
@pytest.mark.parametrize("include", [["refapp"], []], ids=["include", "noinclude"])
def test_deployment_image_ref_runs_an_image_of_the_build_through_dockercompose(
tmp_path, include
):
"""Same resolution (CHApp.is_buildable/buildable_image) as the helm path above,
reached through the dockercompose entrypoint instead."""
solution = _image_ref_solution(tmp_path)
values = create_docker_compose_configuration(
[CLOUDHARNESS_ROOT, RESOURCES, solution],
output_path=tmp_path / "out",
include=include,
domain="my.local",
namespace="test",
local=False,
tag="1",
registry="reg",
).to_dict()
base_image = values["task-images"]["cloudharness-base"]
app = values["apps"]["refapp"]
assert app["image"] == base_image
assert app["harness"]["deployment"]["image"] == base_image