Skip to content

Commit f11cb7e

Browse files
authored
Merge pull request #2664 from cuipinghuo/add-ut-for-internal-opa
test: add comprehensive unit tests for internal/opa package
2 parents d1b786e + 4d5753e commit f11cb7e

File tree

2 files changed

+241
-0
lines changed

2 files changed

+241
-0
lines changed

internal/opa/inspect_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
hd "github.com/MakeNowJust/heredoc"
2929
"github.com/gkampitakis/go-snaps/snaps"
30+
"github.com/open-policy-agent/opa/v1/ast"
3031
"github.com/spf13/afero"
3132
"github.com/stretchr/testify/assert"
3233
"github.com/stretchr/testify/require"
@@ -242,3 +243,45 @@ func TestCheckRules(t *testing.T) {
242243
})
243244
}
244245
}
246+
247+
func TestHasAnnotations(t *testing.T) {
248+
cases := []struct {
249+
name string
250+
rule *ast.AnnotationsRef
251+
expectedResult bool
252+
description string
253+
}{
254+
{
255+
name: "rule with annotations",
256+
rule: &ast.AnnotationsRef{
257+
Annotations: &ast.Annotations{
258+
Scope: "rule",
259+
Title: "Test Rule",
260+
},
261+
},
262+
expectedResult: true,
263+
description: "Should return true when rule has annotations",
264+
},
265+
{
266+
name: "rule with empty annotations",
267+
rule: &ast.AnnotationsRef{
268+
Annotations: &ast.Annotations{},
269+
},
270+
expectedResult: true,
271+
description: "Should return true when rule has empty annotations object",
272+
},
273+
{
274+
name: "rule without annotations",
275+
rule: &ast.AnnotationsRef{},
276+
expectedResult: false,
277+
description: "Should return false when rule has no annotations",
278+
},
279+
}
280+
281+
for _, c := range cases {
282+
t.Run(c.name, func(t *testing.T) {
283+
result := hasAnnotations(c.rule)
284+
assert.Equal(t, c.expectedResult, result, c.description)
285+
})
286+
}
287+
}

internal/opa/output_test.go

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,201 @@ func TestTextOutputIsSorted(t *testing.T) {
218218
assert.NoError(t, err)
219219
assert.Equal(t, "# Source: A\n\n\n(No annotations found)\n--\n# Source: B\n\n\n(No annotations found)\n--\n# Source: C\n\n\n(No annotations found)\n--\n", buffy.String())
220220
}
221+
222+
func TestOutputText(t *testing.T) {
223+
cases := []struct {
224+
name string
225+
allData map[string][]*ast.AnnotationsRef
226+
template string
227+
expected string
228+
expectErr bool
229+
errMsg string
230+
}{
231+
{
232+
name: "successful output with multiple sources",
233+
allData: map[string][]*ast.AnnotationsRef{
234+
"source1": {
235+
{
236+
Path: ast.Ref{
237+
ast.VarTerm("data"),
238+
ast.StringTerm("policy"),
239+
ast.StringTerm("test"),
240+
ast.StringTerm("deny"),
241+
},
242+
Annotations: &ast.Annotations{
243+
Scope: "rule",
244+
Title: "Test Rule",
245+
Description: "Test Description",
246+
},
247+
},
248+
},
249+
"source2": {
250+
{
251+
Path: ast.Ref{
252+
ast.VarTerm("data"),
253+
ast.StringTerm("policy"),
254+
ast.StringTerm("warn"),
255+
},
256+
Annotations: &ast.Annotations{
257+
Scope: "rule",
258+
Title: "Warning Rule",
259+
},
260+
},
261+
},
262+
},
263+
template: "text",
264+
expected: "# Source: source1\n\npolicy.test. (deny)\nTest Rule\nTest Description\n--\n# Source: source2\n\npolicy. (warn)\nWarning Rule\n\n--\n",
265+
expectErr: false,
266+
},
267+
{
268+
name: "successful output with no annotations",
269+
allData: map[string][]*ast.AnnotationsRef{
270+
"source1": {
271+
{
272+
Path: ast.Ref{
273+
ast.VarTerm("data"),
274+
ast.StringTerm("policy"),
275+
ast.StringTerm("test"),
276+
ast.StringTerm("deny"),
277+
},
278+
// No annotations
279+
},
280+
},
281+
},
282+
template: "text",
283+
expected: "# Source: source1\n\npolicy.test.deny\n(No annotations found)\n--\n",
284+
expectErr: false,
285+
},
286+
{
287+
name: "failed output with invalid template",
288+
allData: map[string][]*ast.AnnotationsRef{
289+
"source1": {
290+
{
291+
Path: ast.Ref{
292+
ast.VarTerm("data"),
293+
ast.StringTerm("policy"),
294+
ast.StringTerm("test"),
295+
ast.StringTerm("deny"),
296+
},
297+
Annotations: &ast.Annotations{
298+
Scope: "rule",
299+
Title: "Test Rule",
300+
},
301+
},
302+
},
303+
},
304+
template: "invalid-template",
305+
expected: "",
306+
expectErr: true,
307+
errMsg: "no template",
308+
},
309+
}
310+
311+
for _, c := range cases {
312+
t.Run(c.name, func(t *testing.T) {
313+
buf := new(bytes.Buffer)
314+
err := OutputText(buf, c.allData, c.template)
315+
316+
if c.expectErr {
317+
assert.Error(t, err, "Expected error for invalid input")
318+
if c.errMsg != "" {
319+
assert.Contains(t, err.Error(), c.errMsg, "Error message should contain expected text")
320+
}
321+
} else {
322+
assert.NoError(t, err, "Expected no error for valid input")
323+
assert.Equal(t, c.expected, buf.String(), "Output should match expected")
324+
}
325+
})
326+
}
327+
}
328+
329+
func TestRenderAnn(t *testing.T) {
330+
cases := []struct {
331+
name string
332+
annotations *ast.AnnotationsRef
333+
tmplName string
334+
expected string
335+
expectErr bool
336+
errMsg string
337+
}{
338+
{
339+
name: "successful render with text template",
340+
annotations: &ast.AnnotationsRef{
341+
Path: ast.Ref{
342+
ast.VarTerm("data"),
343+
ast.StringTerm("policy"),
344+
ast.StringTerm("test"),
345+
ast.StringTerm("deny"),
346+
},
347+
Annotations: &ast.Annotations{
348+
Scope: "rule",
349+
Title: "Test Rule",
350+
Description: "Test Description",
351+
Custom: map[string]interface{}{
352+
"short_name": "test_rule",
353+
},
354+
},
355+
},
356+
tmplName: "text",
357+
expected: "policy.test.test_rule (deny)\nhttps://conforma.dev/docs/policy/packages/release_test.html#test__test_rule\nTest Rule\nTest Description\n--\n",
358+
expectErr: false,
359+
},
360+
{
361+
name: "successful render with names template",
362+
annotations: &ast.AnnotationsRef{
363+
Path: ast.Ref{
364+
ast.VarTerm("data"),
365+
ast.StringTerm("policy"),
366+
ast.StringTerm("test"),
367+
ast.StringTerm("warn"),
368+
},
369+
Annotations: &ast.Annotations{
370+
Scope: "rule",
371+
Title: "Warning Rule",
372+
Custom: map[string]interface{}{
373+
"short_name": "warning_rule",
374+
},
375+
},
376+
},
377+
tmplName: "names",
378+
expected: "policy.test.warning_rule\n",
379+
expectErr: false,
380+
},
381+
{
382+
name: "failed render with invalid template",
383+
annotations: &ast.AnnotationsRef{
384+
Path: ast.Ref{
385+
ast.VarTerm("data"),
386+
ast.StringTerm("policy"),
387+
ast.StringTerm("test"),
388+
ast.StringTerm("deny"),
389+
},
390+
Annotations: &ast.Annotations{
391+
Scope: "rule",
392+
Title: "Test Rule",
393+
},
394+
},
395+
tmplName: "invalid-template",
396+
expected: "",
397+
expectErr: true,
398+
errMsg: "no template",
399+
},
400+
}
401+
402+
for _, c := range cases {
403+
t.Run(c.name, func(t *testing.T) {
404+
buf := new(bytes.Buffer)
405+
err := renderAnn(buf, c.annotations, c.tmplName)
406+
407+
if c.expectErr {
408+
assert.Error(t, err, "Expected error for invalid template")
409+
if c.errMsg != "" {
410+
assert.Contains(t, err.Error(), c.errMsg, "Error message should contain expected text")
411+
}
412+
} else {
413+
assert.NoError(t, err, "Expected no error for valid template")
414+
assert.Equal(t, c.expected, buf.String(), "Output should match expected")
415+
}
416+
})
417+
}
418+
}

0 commit comments

Comments
 (0)