• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 19377500035

14 Nov 2025 09:01PM UTC coverage: 80.078% (-0.2%) from 80.29%
19377500035

Pull #22890

github

web-flow
Merge 90397d509 into 42e1ebd41
Pull Request #22890: Updated all python subsystem constraints to 3.14

4 of 5 new or added lines in 5 files covered. (80.0%)

214 existing lines in 14 files now uncovered.

77661 of 96982 relevant lines covered (80.08%)

3.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.72
/src/python/pants/backend/helm/goals/deploy_test.py
1
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
1✔
5

6
from collections.abc import Iterable, Mapping
1✔
7
from textwrap import dedent
1✔
8

9
import pytest
1✔
10

11
from pants.backend.docker.target_types import DockerImageTarget
1✔
12
from pants.backend.helm.goals.deploy import DeployHelmDeploymentFieldSet
1✔
13
from pants.backend.helm.goals.deploy import rules as helm_deploy_rules
1✔
14
from pants.backend.helm.target_types import (
1✔
15
    HelmArtifactTarget,
16
    HelmChartTarget,
17
    HelmDeploymentTarget,
18
)
19
from pants.backend.helm.testutil import HELM_CHART_FILE
1✔
20
from pants.backend.helm.util_rules.tool import HelmBinary
1✔
21
from pants.core.goals import package
1✔
22
from pants.core.goals.deploy import DeployProcess
1✔
23
from pants.core.util_rules import source_files
1✔
24
from pants.engine.addresses import Address
1✔
25
from pants.engine.internals.scheduler import ExecutionError
1✔
26
from pants.testutil.rule_runner import PYTHON_BOOTSTRAP_ENV, QueryRule, RuleRunner
1✔
27

28

29
@pytest.fixture
1✔
30
def rule_runner() -> RuleRunner:
1✔
31
    rule_runner = RuleRunner(
1✔
32
        target_types=[HelmArtifactTarget, HelmChartTarget, HelmDeploymentTarget, DockerImageTarget],
33
        rules=[
34
            *helm_deploy_rules(),
35
            *source_files.rules(),
36
            *package.rules(),
37
            QueryRule(HelmBinary, ()),
38
            QueryRule(DeployProcess, (DeployHelmDeploymentFieldSet,)),
39
        ],
40
    )
41
    return rule_runner
1✔
42

43

44
def _run_deployment(
1✔
45
    rule_runner: RuleRunner,
46
    spec_path: str,
47
    target_name: str,
48
    *,
49
    args: Iterable[str] | None = None,
50
    env: Mapping[str, str] | None = None,
51
) -> DeployProcess:
52
    rule_runner.set_options(args or (), env=env, env_inherit=PYTHON_BOOTSTRAP_ENV)
1✔
53

54
    target = rule_runner.get_target(Address(spec_path, target_name=target_name))
1✔
55
    field_set = DeployHelmDeploymentFieldSet.create(target)
1✔
56

57
    return rule_runner.request(DeployProcess, [field_set])
1✔
58

59

60
def test_run_helm_deploy(rule_runner: RuleRunner) -> None:
1✔
61
    rule_runner.write_files(
1✔
62
        {
63
            "src/chart/BUILD": """helm_chart(registries=["oci://www.example.com/external"])""",
64
            "src/chart/Chart.yaml": HELM_CHART_FILE,
65
            "src/deployment/BUILD": dedent(
66
                """\
67
              helm_deployment(
68
                name="foo",
69
                description="Foo deployment",
70
                namespace=f"uat-{env('NS_SUFFIX')}",
71
                skip_crds=True,
72
                no_hooks=True,
73
                chart="//src/chart",
74
                dependencies=["//src/docker/myimage"],
75
                sources=["common.yaml", "*.yaml", "*-override.yaml", "subdir/*.yaml", "subdir/*-override.yaml", "subdir/last.yaml"],
76
                values={
77
                    "key": "foo",
78
                    "amount": "300",
79
                    "long_string": "This is a long string",
80
                    "build_number": f"{env('BUILD_NUMBER')}",
81
                },
82
                timeout=150,
83
              )
84
              """
85
            ),
86
            "src/deployment/common.yaml": "",
87
            "src/deployment/bar-override.yaml": "",
88
            "src/deployment/foo.yaml": "",
89
            "src/deployment/bar.yaml": "",
90
            "src/deployment/subdir/foo.yaml": "",
91
            "src/deployment/subdir/foo-override.yaml": "",
92
            "src/deployment/subdir/bar.yaml": "",
93
            "src/deployment/subdir/last.yaml": "",
94
            "src/docker/myimage/BUILD": dedent(
95
                """\
96
                docker_image(registries=["https://wwww.example.com"], repository="myimage")
97
                """
98
            ),
99
            "src/docker/myimage/Dockerfile": dedent(
100
                """\
101
                FROM busybox
102
                """
103
            ),
104
        }
105
    )
106

107
    expected_build_number = "34"
1✔
108
    expected_ns_suffix = "quxx"
1✔
109
    expected_value_files_order = [
1✔
110
        "common.yaml",
111
        "bar.yaml",
112
        "foo.yaml",
113
        "bar-override.yaml",
114
        "subdir/bar.yaml",
115
        "subdir/foo.yaml",
116
        "subdir/foo-override.yaml",
117
        "subdir/last.yaml",
118
    ]
119

120
    deploy_args = ["--kubeconfig", "./kubeconfig", "--create-namespace"]
1✔
121
    deploy_process = _run_deployment(
1✔
122
        rule_runner,
123
        "src/deployment",
124
        "foo",
125
        args=[f"--helm-args={repr(deploy_args)}"],
126
        env={"BUILD_NUMBER": expected_build_number, "NS_SUFFIX": expected_ns_suffix},
127
    )
128

UNCOV
129
    helm = rule_runner.request(HelmBinary, [])
×
130

UNCOV
131
    assert deploy_process.process
×
UNCOV
132
    assert deploy_process.process.process.argv == (
×
133
        helm.path,
134
        "upgrade",
135
        "foo",
136
        "mychart",
137
        "--description",
138
        '"Foo deployment"',
139
        "--namespace",
140
        f"uat-{expected_ns_suffix}",
141
        "--skip-crds",
142
        "--no-hooks",
143
        "--post-renderer",
144
        "./post_renderer_wrapper.sh",
145
        "--values",
146
        ",".join(
147
            [f"__values/src/deployment/{filename}" for filename in expected_value_files_order]
148
        ),
149
        "--set",
150
        "key=foo",
151
        "--set",
152
        "amount=300",
153
        "--set",
154
        'long_string="This is a long string"',
155
        "--set",
156
        f"build_number={expected_build_number}",
157
        "--install",
158
        "--timeout",
159
        "150s",
160
        "--kubeconfig",
161
        "./kubeconfig",
162
        "--create-namespace",
163
    )
164

165

166
@pytest.mark.parametrize("invalid_passthrough_args", [["--namespace", "foo"], ["--dry-run"]])
1✔
167
def test_raises_error_when_using_invalid_passthrough_args(
1✔
168
    rule_runner: RuleRunner, invalid_passthrough_args: list[str]
169
) -> None:
170
    rule_runner.write_files(
1✔
171
        {
172
            "src/chart/BUILD": """helm_chart(registries=["oci://www.example.com/external"])""",
173
            "src/chart/Chart.yaml": HELM_CHART_FILE,
174
            "src/deployment/BUILD": dedent(
175
                """\
176
              helm_deployment(
177
                name="bar",
178
                namespace="uat",
179
                chart="//src/chart",
180
                sources=["*.yaml", "subdir/*.yml"]
181
              )
182
              """
183
            ),
184
        }
185
    )
186

187
    source_root_patterns = ["/src/*"]
1✔
188
    deploy_args = ["--force", "--debug", "--kubeconfig=./kubeconfig", *invalid_passthrough_args]
1✔
189

190
    invalid_passthrough_args_as_string = " ".join(invalid_passthrough_args)
1✔
191
    with pytest.raises(
1✔
192
        ExecutionError,
193
        match=f"The following command line arguments are not valid: {invalid_passthrough_args_as_string}.",
194
    ):
195
        _run_deployment(
1✔
196
            rule_runner,
197
            "src/deployment",
198
            "bar",
199
            args=[
200
                f"--source-root-patterns={repr(source_root_patterns)}",
201
                f"--helm-args={repr(deploy_args)}",
202
            ],
203
        )
204

205

206
def test_can_deploy_3rd_party_chart(rule_runner: RuleRunner) -> None:
1✔
207
    rule_runner.write_files(
1✔
208
        {
209
            "3rdparty/helm/BUILD": dedent(
210
                """\
211
                helm_artifact(
212
                    name="prometheus-stack",
213
                    repository="https://prometheus-community.github.io/helm-charts",
214
                    artifact="kube-prometheus-stack",
215
                    version="^27.2.0"
216
                )
217
                """
218
            ),
219
            "BUILD": dedent(
220
                """\
221
              helm_deployment(
222
                name="deploy_3rd_party",
223
                chart="//3rdparty/helm:prometheus-stack",
224
              )
225
              """
226
            ),
227
        }
228
    )
229
    deploy_process = _run_deployment(
1✔
230
        rule_runner,
231
        "",
232
        "deploy_3rd_party",
233
        args=[
234
            "--helm-infer-external-docker-images=['quay.io/kiwigrid/k8s-sidecar','grafana/grafana','bats/bats','k8s.gcr.io/kube-state-metrics/kube-state-metrics','quay.io/prometheus/node-exporter','quay.io/prometheus-operator/prometheus-operator']"
235
        ],
236
    )
237

UNCOV
238
    assert deploy_process.process
×
UNCOV
239
    assert len(deploy_process.publish_dependencies) == 0
×
240

241

242
@pytest.mark.parametrize(
1✔
243
    "dry_run_args,expected",
244
    [
245
        ([], False),
246
        (["--experimental-deploy-dry-run=False"], False),
247
        (["--experimental-deploy-dry-run"], True),
248
    ],
249
)
250
def test_run_helm_deploy_adheres_to_dry_run_flag(
1✔
251
    rule_runner: RuleRunner, dry_run_args: list[str], expected: bool
252
) -> None:
253
    rule_runner.write_files(
1✔
254
        {
255
            "src/chart/BUILD": """helm_chart(registries=["oci://www.example.com/external"])""",
256
            "src/chart/Chart.yaml": HELM_CHART_FILE,
257
            "src/deployment/BUILD": dedent(
258
                """\
259
              helm_deployment(
260
                name="bar",
261
                namespace="uat",
262
                chart="//src/chart",
263
                sources=["*.yaml", "subdir/*.yml"]
264
              )
265
              """
266
            ),
267
        }
268
    )
269

270
    expected_build_number = "34"
1✔
271
    expected_ns_suffix = "quxx"
1✔
272

273
    deploy_args = ["--kubeconfig", "./kubeconfig", "--create-namespace"]
1✔
274
    deploy_process = _run_deployment(
1✔
275
        rule_runner,
276
        "src/deployment",
277
        "bar",
278
        args=[f"--helm-args={repr(deploy_args)}", *dry_run_args],
279
        env={"BUILD_NUMBER": expected_build_number, "NS_SUFFIX": expected_ns_suffix},
280
    )
281

UNCOV
282
    assert deploy_process.process
×
UNCOV
283
    assert ("--dry-run" in deploy_process.process.process.argv) == expected
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc