-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrender_fidelity_test.go
More file actions
97 lines (88 loc) · 2.96 KB
/
Copy pathrender_fidelity_test.go
File metadata and controls
97 lines (88 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// SPDX-License-Identifier: Apache-2.0
package git
import (
"context"
"os"
"path/filepath"
"testing"
gogit "github.com/go-git/go-git/v6"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
v1alpha3 "github.com/ConfigButler/gitops-reverser/api/v1alpha3"
"github.com/ConfigButler/gitops-reverser/internal/manifestanalyzer"
"github.com/ConfigButler/gitops-reverser/internal/types"
)
const postBuildTokenManifest = `apiVersion: v1
kind: ConfigMap
metadata:
name: region
namespace: default
data:
value: ${REGION}
`
func postBuildTokenEvent() Event {
return Event{
Object: &unstructured.Unstructured{Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{"name": "region", "namespace": "default"},
"data": map[string]interface{}{"value": "us-east"},
}},
Identifier: types.ResourceIdentifier{
Version: "v1", Resource: "configmaps", Namespace: "default", Name: "region",
},
Operation: "UPDATE",
}
}
func seedPostBuildTokenManifest(t *testing.T, root string) string {
t.Helper()
path := filepath.Join(root, "configmap.yaml")
require.NoError(t, os.WriteFile(path, []byte(postBuildTokenManifest), 0o600))
return path
}
// Both mutation paths reach the same boundary. In particular, resync must not be a back door
// that turns an externally resolved ${REGION} value into a source-file edit.
func TestRenderFidelityRefusal_BlocksLiveAndResyncWrites(t *testing.T) {
for _, test := range []struct {
name string
run func(worker *BranchWorker, worktree *gogit.Worktree) error
}{
{
name: "live event",
run: func(worker *BranchWorker, worktree *gogit.Worktree) error {
_, err := worker.flushEventsToWorktree(
context.Background(), worktree, "", []Event{postBuildTokenEvent()}, nil, v1alpha3.PruneOnEvent)
return err
},
},
{
name: "scoped resync",
run: func(worker *BranchWorker, worktree *gogit.Worktree) error {
_, _, err := worker.applyResyncToWorktree(
context.Background(), worktree, "",
ResolvedTargetMetadata{PruneMode: v1alpha3.PruneAlways},
[]manifestanalyzer.DesiredResource{{
Resource: postBuildTokenEvent().Identifier,
Object: postBuildTokenEvent().Object,
}}, nil)
return err
},
},
} {
t.Run(test.name, func(t *testing.T) {
worktree := newWorktreeForTest(t)
path := seedPostBuildTokenManifest(t, worktree.Filesystem().Root())
worker := &BranchWorker{
contentWriter: newContentWriter(types.SensitiveResourcePolicy{}),
mapper: configMapMapper(),
}
err := test.run(worker, worktree)
var refused *manifestanalyzer.AcceptanceRefusedError
require.ErrorAs(t, err, &refused)
assert.True(t, refused.AllIssuesOfKinds(manifestanalyzer.IssueRenderDoesNotMatchLive))
assert.Contains(t, refused.Error(), "${REGION}")
assertFileBytes(t, path, postBuildTokenManifest, "a fidelity refusal must not change Git")
})
}
}