Skip to content

Commit 9663dc7

Browse files
authored
Merge pull request #2681 from simonbaird/rego-data-merge-tests
Add test coverage for top level data key merging
2 parents 139b68e + 2e98f3e commit 9663dc7

File tree

10 files changed

+128
-4
lines changed

10 files changed

+128
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ ci: test lint-fix acceptance ## Run the usual required CI tasks
184184

185185
LICENSE_IGNORE=\
186186
-ignore 'dist/cli-reference/*.yaml' \
187-
-ignore 'acceptance/examples/*.yaml' \
187+
-ignore 'acceptance/examples/**/*.yaml' \
188188
-ignore 'configs/*/*.yaml' \
189189
-ignore 'node_modules/**' \
190190
-ignore 'hack/**/charts/**' \

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

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/gkampitakis/go-snaps v0.5.7
1515
github.com/go-git/go-git/v5 v5.13.2
1616
github.com/go-logr/logr v1.4.3
17+
github.com/go-openapi/strfmt v0.23.0
1718
github.com/google/go-cmp v0.7.0
1819
github.com/google/go-containerregistry v0.20.7
1920
github.com/google/safearchive v0.0.0-20241025131057-f7ce9d7b6f9c
@@ -31,6 +32,7 @@ require (
3132
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
3233
github.com/secure-systems-lab/go-securesystemslib v0.9.0
3334
github.com/sigstore/cosign/v2 v2.4.1
35+
github.com/sigstore/rekor v1.3.6
3436
github.com/sigstore/sigstore v1.8.9
3537
github.com/sirupsen/logrus v1.9.3
3638
github.com/smarty/cproxy/v2 v2.1.1
@@ -190,7 +192,6 @@ require (
190192
github.com/go-openapi/loads v0.22.0 // indirect
191193
github.com/go-openapi/runtime v0.28.0 // indirect
192194
github.com/go-openapi/spec v0.21.0 // indirect
193-
github.com/go-openapi/strfmt v0.23.0 // indirect
194195
github.com/go-openapi/swag v0.23.0 // indirect
195196
github.com/go-openapi/validate v0.24.0 // indirect
196197
github.com/go-viper/mapstructure/v2 v2.3.0 // indirect
@@ -289,7 +290,6 @@ require (
289290
github.com/shteou/go-ignore v0.3.1 // indirect
290291
github.com/sigstore/fulcio v1.6.3 // indirect
291292
github.com/sigstore/protobuf-specs v0.3.2 // indirect
292-
github.com/sigstore/rekor v1.3.6 // indirect
293293
github.com/sigstore/timestamp-authority v1.2.2 // indirect
294294
github.com/skeema/knownhosts v1.3.0 // indirect
295295
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect

0 commit comments

Comments
 (0)