Skip to content

Commit bbdebd7

Browse files
committed
Add test coverage for top level data key merging
The motivation is to make sure PR #2656 doesn't change this behavior and potentially break things. The two things being verified: - If the value of the top level key is a map, then the map keys from each data source get merged - If the value of the top level key is not a map, then we fail hard with an error. Ref: https://issues.redhat.com/browse/EC-1418
1 parent 45de707 commit bbdebd7

File tree

8 files changed

+125
-1
lines changed

8 files changed

+125
-1
lines changed

acceptance/cli/cli.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ func matchFileSnapshot(ctx context.Context, file string) error {
764764
return snaps.MatchSnapshot(ctx, file, string(content), status.vars)
765765
}
766766

767-
func createTrackBundleFile(ctx context.Context, name string, content *godog.DocString) (context.Context, error) {
767+
func createGenericFile(ctx context.Context, name string, content *godog.DocString) (context.Context, error) {
768768
ctx, _, vars, err := variables(ctx)
769769
if err != nil {
770770
return ctx, err
@@ -783,6 +783,10 @@ func createTrackBundleFile(ctx context.Context, name string, content *godog.DocS
783783
return ctx, os.WriteFile(file, []byte(data), 0o600)
784784
}
785785

786+
func createTrackBundleFile(ctx context.Context, name string, content *godog.DocString) (context.Context, error) {
787+
return createGenericFile(ctx, name, content)
788+
}
789+
786790
// AddStepsTo adds Gherkin steps to the godog ScenarioContext
787791
func AddStepsTo(sc *godog.ScenarioContext) {
788792
sc.Step(`^ec command is run with "(.+)"$`, ecCommandIsRunWith)
@@ -793,6 +797,7 @@ func AddStepsTo(sc *godog.ScenarioContext) {
793797
sc.Step(`^the environment variable is set "([^"]*)"$`, theEnvironmentVarilableIsSet)
794798
sc.Step(`^the output should match the snapshot$`, matchSnapshot)
795799
sc.Step(`^the "([^"]*)" file should match the snapshot$`, matchFileSnapshot)
800+
sc.Step(`^a file named "([^"]*)" containing$`, createGenericFile)
796801
sc.Step(`^a track bundle file named "([^"]*)" containing$`, createTrackBundleFile)
797802
sc.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) {
798803
logExecution(ctx)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
# Used in secenario "multiple data source top level key
3+
# map merging" in features/validate_input.features
4+
some_top_level_key:
5+
john: "rhythm"
6+
paul: "bass"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
# Used in secenario "multiple data source top level key
3+
# map merging" in features/validate_input.features
4+
some_top_level_key:
5+
george: "lead"
6+
ringo: "drums"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# Used in secenario "multiple data source top level key
3+
# clash" in features/validate_input.features
4+
# (We don't test this explicitly, but it would behave
5+
# the same if it was a scalar value instead of a list.)
6+
some_top_level_key:
7+
- john
8+
- paul
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# Used in secenario "multiple data source top level key
3+
# clash" in features/validate_input.features
4+
# (We don't test this explicitly, but it would behave
5+
# the same if it was a scalar value instead of a list.)
6+
some_top_level_key:
7+
- george
8+
- ringo
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import rego.v1
4+
5+
# The acceptance test that uses this is about verifying the behavior
6+
# when multiple data sources define the same top level data key.
7+
# For this test we don't particularly care about the warning, but
8+
# we're using the result msg to expose what the data looks like.
9+
warn contains result if {
10+
result := {
11+
"msg": json.marshal(data.some_top_level_key),
12+
}
13+
}

features/__snapshots__/validate_input.snap

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,38 @@ Error: error validating file pipeline_definition.yaml: evaluating policy: no reg
8787
Error: success criteria not met
8888

8989
---
90+
91+
[multiple data source top level key map merging:stdout - 1]
92+
ec-version: ${EC_VERSION}
93+
effective-time: "${TIMESTAMP}"
94+
filepaths:
95+
- filepath: input.json
96+
success: true
97+
success-count: 0
98+
successes: null
99+
violations: []
100+
warnings:
101+
- msg: '{"george":"lead","john":"rhythm","paul":"bass","ringo":"drums"}'
102+
policy:
103+
sources:
104+
- data:
105+
- file::acceptance/examples/data-merges/data-1
106+
- file::acceptance/examples/data-merges/data-2
107+
policy:
108+
- file::acceptance/examples/data-merges/policy
109+
success: true
110+
111+
---
112+
113+
[multiple data source top level key map merging:stderr - 1]
114+
115+
---
116+
117+
[multiple data source top level key clash:stdout - 1]
118+
119+
---
120+
121+
[multiple data source top level key clash:stderr - 1]
122+
Error: error validating file input.json: evaluating policy: load: load documents: 1 error occurred during loading: ${TEMP}/ec-work-${RANDOM}/dat${RANDOM}/${RANDOM}/data.yaml: merge error
123+
124+
---

features/validate_input.feature

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,46 @@ Feature: validate input
6666
When ec command is run with "validate input --file input.yaml --policy git::https://${GITHOST}/git/multiple-sources-config.git"
6767
Then the exit status should be 1
6868
Then the output should match the snapshot
69+
70+
# In this example the same top level key is defined in
71+
# two different data sources, but its value a map.
72+
# In this situation a merge happens and we get second
73+
# level keys from both sources.
74+
Scenario: multiple data source top level key map merging
75+
Given a file named "policy.yaml" containing
76+
"""
77+
sources:
78+
- data:
79+
- "file::acceptance/examples/data-merges/data-1"
80+
- "file::acceptance/examples/data-merges/data-2"
81+
policy:
82+
- "file::acceptance/examples/data-merges/policy"
83+
"""
84+
Given a file named "input.json" containing
85+
"""
86+
{}
87+
"""
88+
When ec command is run with "validate input --file input.json --policy policy.yaml -o yaml"
89+
Then the exit status should be 0
90+
Then the output should match the snapshot
91+
92+
# In this example the same top level key is defined in
93+
# two different data sources, but its value is not a map.
94+
# In this situation ec throws a "merge error" error.
95+
Scenario: multiple data source top level key clash
96+
Given a file named "policy.yaml" containing
97+
"""
98+
sources:
99+
- data:
100+
- "file::acceptance/examples/data-merges/data-3"
101+
- "file::acceptance/examples/data-merges/data-4"
102+
policy:
103+
- "file::acceptance/examples/data-merges/policy"
104+
"""
105+
Given a file named "input.json" containing
106+
"""
107+
{}
108+
"""
109+
When ec command is run with "validate input --file input.json --policy policy.yaml -o yaml"
110+
Then the exit status should be 1
111+
Then the output should match the snapshot

0 commit comments

Comments
 (0)