-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathinit_test.go
More file actions
232 lines (203 loc) · 5.55 KB
/
init_test.go
File metadata and controls
232 lines (203 loc) · 5.55 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
// Copyright 2025 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package policydevel
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInitialize(t *testing.T) {
tempDir := t.TempDir()
t.Run("embedded rego", func(t *testing.T) {
opts := &InitOptions{
Directory: tempDir,
Embedded: true,
Name: "test-policy",
Description: "test description",
}
err := Initialize(opts)
require.NoError(t, err)
policyPath := filepath.Join(tempDir, "test-policy.yaml")
assert.FileExists(t, policyPath)
})
t.Run("standalone rego file", func(t *testing.T) {
opts := &InitOptions{
Directory: tempDir,
Embedded: false,
Name: "standalone-rego",
}
err := Initialize(opts)
require.NoError(t, err)
assert.FileExists(t, filepath.Join(tempDir, "standalone-rego.yaml"))
assert.FileExists(t, filepath.Join(tempDir, "standalone-rego.rego"))
})
t.Run("file exists and no force", func(t *testing.T) {
opts := &InitOptions{
Directory: tempDir,
Name: "duplicate",
}
// First time should succeed
err := Initialize(opts)
require.NoError(t, err)
// Second time should fail
err = Initialize(opts)
require.Error(t, err)
assert.Contains(t, err.Error(), "already exists")
// With force it should succeed
opts.Force = true
err = Initialize(opts)
require.NoError(t, err)
})
t.Run("name and description are properly set", func(t *testing.T) {
customName := "custom-policy-name"
customDesc := "This is a custom policy description"
opts := &InitOptions{
Directory: tempDir,
Name: customName,
Description: customDesc,
Embedded: true,
}
err := Initialize(opts)
require.NoError(t, err)
policyPath := filepath.Join(tempDir, customName+".yaml")
assert.FileExists(t, policyPath)
content, err := os.ReadFile(policyPath)
require.NoError(t, err)
policyContent := string(content)
assert.Contains(t, policyContent, "name: "+customName)
assert.Contains(t, policyContent, "description: "+customDesc)
assert.FileExists(t, filepath.Join(tempDir, customName+".yaml"))
})
}
func TestExecuteTemplate(t *testing.T) {
testCases := []struct {
name string
template string
data *TemplateData
expected string
}{
{
name: "basic interpolation",
template: "Hello {{.Name}}!",
data: &TemplateData{Name: "world"},
expected: "Hello world!",
},
{
name: "sanitize function",
template: "{{.Name | sanitize}}",
data: &TemplateData{Name: "My Policy"},
expected: "my-policy",
},
{
name: "indent function",
template: "{{indent 2 \"hello\"}}",
expected: " hello",
},
{
name: "multiple fields interpolation",
template: "Name: {{.Name}}, Desc: {{.Description}}",
data: &TemplateData{Name: "test", Description: "description"},
expected: "Name: test, Desc: description",
},
{
name: "trimSpace function",
template: "{{.Name | trimSpace}}",
data: &TemplateData{Name: " spaced "},
expected: "spaced",
},
{
name: "combined functions",
template: "{{.Name | trimSpace | sanitize}}",
data: &TemplateData{Name: " My Policy 123 "},
expected: "my-policy-123",
},
{
name: "empty template",
template: "",
data: &TemplateData{Name: "test"},
expected: "",
},
{
name: "embedded rego flag",
template: "Embedded: {{.Embedded}}",
data: &TemplateData{Embedded: true},
expected: "Embedded: true",
},
{
name: "material kind",
template: "Material: {{.MaterialKind}}",
data: &TemplateData{MaterialKind: "SBOM_CYCLONEDX_JSON"},
expected: "Material: SBOM_CYCLONEDX_JSON",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := executeTemplate(tc.template, tc.data)
require.NoError(t, err)
assert.Equal(t, tc.expected, result)
})
}
errorCases := []struct {
name string
template string
data *TemplateData
errMsg string
}{
{
name: "invalid template syntax",
template: "{{.Name",
data: &TemplateData{Name: "test"},
errMsg: "template parsing error",
},
{
name: "missing field",
template: "{{.MissingField}}",
data: &TemplateData{Name: "test"},
errMsg: "template execution error",
},
{
name: "invalid function",
template: "{{.Name | invalidFunc}}",
data: &TemplateData{Name: "test"},
errMsg: "template parsing error",
},
}
for _, tc := range errorCases {
t.Run(tc.name, func(t *testing.T) {
_, err := executeTemplate(tc.template, tc.data)
require.Error(t, err)
assert.Contains(t, err.Error(), tc.errMsg)
})
}
}
func TestSanitizeName(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{"My Policy", "my-policy"},
{" Trim Spaces ", "trim-spaces"},
{"UPPER CASE", "upper-case"},
{"Special!@#Chars", "special!@#chars"},
{"", ""},
}
for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
assert.Equal(t, tc.expected, sanitizeName(tc.input))
})
}
}