Skip to content

Commit e156f0d

Browse files
authored
Switch back to original conftest test --update behavior (#1138)
* fix: Switch back to original --update behavior In conftest v0.61.0 the behavior of --update changed in order to fetch the policy URLs and stored them in a temporary directory. It broke the previous behavior that updated current policy directory and instead aggregated the current policy (under `policy`) with the one(s) used in `--update`. It also broke the behavior of a project that had no existing `policy`. This reverts commit ed0ff0a. Fixes #1136. Signed-off-by: Leonardo Taccari <[email protected]> * test: Test conftest test --update behavior Add an acceptance test in order to ensure that: - conftest test --update create/populate policy directory - conftest test --update update the policy directory with the now policies that are fetched remotely Fixes #1136. --------- Signed-off-by: Leonardo Taccari <[email protected]>
1 parent 8e47770 commit e156f0d

File tree

8 files changed

+62
-39
lines changed

8 files changed

+62
-39
lines changed

runner/test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,12 @@ func (t *TestRunner) Run(ctx context.Context, fileList []string) (output.CheckRe
5555
return nil, fmt.Errorf("parse configurations: %w", err)
5656
}
5757

58-
// When there are policies to download, they are placed in temporary
59-
// directory which is passed to the policy engine.
60-
// Downloaded policies are removed after the Run to keep the system intact.
58+
// When there are policies to download, they are currently placed in the first
59+
// directory that appears in the list of policies.
6160
if len(t.Update) > 0 {
62-
policyDir, err := os.MkdirTemp(os.TempDir(), "remote-policy-")
63-
if err != nil {
64-
return nil, fmt.Errorf("create temp dir: %w", err)
65-
}
66-
defer os.RemoveAll(policyDir)
67-
68-
if err := downloader.Download(ctx, policyDir, t.Update); err != nil {
61+
if err := downloader.Download(ctx, t.Policy[0], t.Update); err != nil {
6962
return nil, fmt.Errorf("update policies: %w", err)
7063
}
71-
72-
t.Policy = append(t.Policy, policyDir)
7364
}
7465

7566
capabilities, err := policy.LoadCapabilities(t.Capabilities)

tests/multiple-runs-with-update/file.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/multiple-runs-with-update/remote-policy/policy.rego

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/multiple-runs-with-update/test.bats

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/pull-update/file.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "a": "a", "b": "b" }

tests/multiple-runs-with-update/policy/main.rego renamed to tests/pull-update/remote-policy/a/main.rego

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package main
33
import rego.v1
44

55
deny contains msg if {
6-
input.bar == "baz"
7-
msg := "local-policy"
6+
input.a
7+
msg := "a should not be present"
88
}
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+
deny contains msg if {
6+
input.a
7+
msg := "a should not be present"
8+
}
9+
10+
deny contains msg if {
11+
input.b
12+
msg := "b should not be present"
13+
}

tests/pull-update/test.bats

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bats
2+
3+
setup_file() {
4+
# Create a temporary directory shared by all the tests
5+
export TEMP_DIR=$(mktemp -d)
6+
7+
# Copy all the files there
8+
cp -r . "${TEMP_DIR}"
9+
}
10+
11+
teardown_file() {
12+
# Cleanup temporary directory
13+
rm -rf "${TEMP_DIR}"
14+
}
15+
16+
@test "Ensure that policy do not exists" {
17+
run test -e "${TEMP_DIR}/policy"
18+
19+
[ "$status" -eq 1 ]
20+
}
21+
22+
@test "Pull and update first version policy" {
23+
run $CONFTEST test --policy "${TEMP_DIR}/policy" --update "file://${TEMP_DIR}/remote-policy/a" file.json
24+
25+
[ "$status" -eq 1 ]
26+
[[ "$output" =~ "a should not be present" ]]
27+
[[ "$output" =~ "1 test, 0 passed, 0 warnings, 1 failure, 0 exceptions" ]]
28+
}
29+
30+
@test "Ensure that policy directory exists" {
31+
run test -d "${TEMP_DIR}/policy"
32+
33+
[ "$status" -eq 0 ]
34+
}
35+
36+
@test "Pull and update second version policy" {
37+
run $CONFTEST test --policy "${TEMP_DIR}/policy" --update "file://${TEMP_DIR}/remote-policy/b" file.json
38+
39+
[ "$status" -eq 1 ]
40+
[[ "$output" =~ "a should not be present" ]]
41+
[[ "$output" =~ "b should not be present" ]]
42+
[[ "$output" =~ "2 tests, 0 passed, 0 warnings, 2 failures, 0 exceptions" ]]
43+
}

0 commit comments

Comments
 (0)