Skip to content

Commit

Permalink
Improve handling of additional files in Helm unit tests (#19263)
Browse files Browse the repository at this point in the history
Fixes handling of additional files in Helm unit tests
  • Loading branch information
alonsodomin authored Jun 7, 2023
1 parent 2103025 commit e02cd57
Show file tree
Hide file tree
Showing 4 changed files with 369 additions and 17 deletions.
95 changes: 95 additions & 0 deletions docs/markdown/Helm/helm-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,101 @@ pants test ::
✓ testprojects/src/helm/example/tests/env-configmap_test.yaml succeeded in 0.75s.
```
### Feeding additional files to unit tests
In some cases we may want our tests to have access to additional files which are not part of the chart. This can be achieved by setting a dependency between our unit test targets and a `resources` target as follows:
```python src/helm/example/tests/BUILD
helm_unittest_tests(dependencies=[":extra-values"])
resources(name="extra-values", sources=["extra-values.yml"])
```
```yaml src/helm/example/templates/env-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap
data:
{{- range $key, $val := .Values.data }}
{{ $key | upper }}: {{ $val | quote }}
{{- end }}
```
```yaml src/helm/example/tests/extra-values.yml
data:
VAR1_NAME: var1Value
var2_name: var2Value
```
```yaml src/helm/example/tests/env-configmap_test.yaml
suite: test env-configmap
templates:
- env-configmap.yaml
values:
- extra-values.yml
tests:
- it: should contain the env map variables
asserts:
- equal:
path: data.VAR1_NAME
value: "var1Value"
- equal:
path: data.VAR2_NAME
value: "var2Value"
```
Additional files can be referenced from any location inside your workspace. Note that the actual path to the additional files will be relative to the source roots configured in Pants.
In this example, since Helm charts define their source root at the location of the `Chart.yaml` file and the `extra-values.yml` file is inside the `tests` folder relative to the chart, the test suite can access it as being local to it.

However, in the following case, we need to reference the extra file relative to the chart root. Note the `../data/extra-values.yml` path in the test suite.

```toml pants.toml
[source]
root_patterns=["src/extra"]
```
```python src/extra/data/BUILD
resources(name="extra-values", sources=["extra-values.yml"])
```
```yaml src/extra/data/extra-values.yml
data:
VAR1_NAME: var1Value
var2_name: var2Value
```
```python src/helm/example/tests/BUILD
helm_unittest_tests(dependencies=["src/extra/data:extra-values"])
```
```yaml src/helm/example/templates/env-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap
data:
{{- range $key, $val := .Values.data }}
{{ $key | upper }}: {{ $val | quote }}
{{- end }}
```
```yaml src/helm/example/tests/env-configmap_test.yaml
suite: test env-configmap
templates:
- env-configmap.yaml
values:
- ../data/extra-values.yml
tests:
- it: should contain the env map variables
asserts:
- equal:
path: data.VAR1_NAME
value: "var1Value"
- equal:
path: data.VAR2_NAME
value: "var2Value"
```

> 🚧 Using `file`, `files` and `relocated_files` targets
>
> Other file-centric targets are also supported, just be aware that `file` and `files` targets are
> not affected by the source roots setting. When using `relocated_files`, the files will be relative
> to the value set in the `dest` field.

### Timeouts

Pants can cancel tests that take too long, which is useful to prevent tests from hanging indefinitely.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async def first_party_helm_deployment_mapping(
docker_target_addresses = {tgt.address.spec: tgt.address for tgt in docker_targets}

def lookup_docker_addreses(image_ref: str) -> tuple[str, Address] | None:
addr = docker_target_addresses.get(str(image_ref), None)
addr = docker_target_addresses.get(image_ref, None)
if addr:
return image_ref, addr
return None
Expand Down
22 changes: 10 additions & 12 deletions src/python/pants/backend/helm/test/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
from pants.backend.helm.util_rules.tool import HelmProcess
from pants.base.deprecated import warn_or_error
from pants.core.goals.test import TestFieldSet, TestRequest, TestResult, TestSubsystem
from pants.core.target_types import ResourceSourceField
from pants.core.target_types import FileSourceField, ResourceSourceField
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
from pants.core.util_rules.stripped_source_files import StrippedSourceFiles
from pants.engine.addresses import Address
from pants.engine.fs import AddPrefix, Digest, MergeDigests, RemovePrefix, Snapshot
from pants.engine.process import FallibleProcessResult, ProcessCacheScope
Expand Down Expand Up @@ -89,21 +90,18 @@ async def run_helm_unittest(
raise MissingUnitTestChartDependency(field_set.address)

chart_target = chart_targets[0]
chart, chart_root, test_files = await MultiGet(
chart, chart_root, test_files, extra_files = await MultiGet(
Get(HelmChart, HelmChartRequest, HelmChartRequest.from_target(chart_target)),
Get(HelmChartRoot, HelmChartRootRequest(chart_target[HelmChartMetaSourceField])),
Get(
SourceFiles,
SourceFilesRequest(sources_fields=[field_set.source]),
),
Get(
StrippedSourceFiles,
SourceFilesRequest(
sources_fields=[
field_set.source,
*(
tgt.get(SourcesField)
for tgt in transitive_targets.dependencies
if not HelmChartFieldSet.is_applicable(tgt)
),
],
for_sources_types=(HelmUnitTestSourceField, ResourceSourceField),
sources_fields=[tgt.get(SourcesField) for tgt in transitive_targets.dependencies],
for_sources_types=(ResourceSourceField, FileSourceField),
enable_codegen=True,
),
),
Expand All @@ -118,7 +116,7 @@ async def run_helm_unittest(

merged_digests = await Get(
Digest,
MergeDigests([chart.snapshot.digest, stripped_test_files]),
MergeDigests([chart.snapshot.digest, stripped_test_files, extra_files.snapshot.digest]),
)
input_digest = await Get(Digest, AddPrefix(merged_digests, chart.name))

Expand Down
Loading

0 comments on commit e02cd57

Please sign in to comment.