-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcustom_check_test.go
141 lines (130 loc) · 3.74 KB
/
custom_check_test.go
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package checks
import (
"errors"
"testing"
"github.com/DataDog/datadog-agent/pkg/compliance"
"github.com/DataDog/datadog-agent/pkg/compliance/checks/custom"
"github.com/DataDog/datadog-agent/pkg/compliance/checks/env"
"github.com/DataDog/datadog-agent/pkg/compliance/eval"
"github.com/DataDog/datadog-agent/pkg/compliance/mocks"
assert "github.com/stretchr/testify/require"
)
func TestNewCustomCheck(t *testing.T) {
assert := assert.New(t)
const ruleID = "rule-id"
expectCheckReport := &compliance.Report{Passed: true}
expectCheckError := errors.New("check failed")
customCheckFunc := func(report *compliance.Report, err error) custom.CheckFunc {
return func(e env.Env, ruleID string, vars map[string]string, expr *eval.IterableExpression) (*compliance.Report, error) {
return report, err
}
}
tests := []struct {
name string
resource compliance.Resource
checkFactory checkFactoryFunc
expectError error
expectCheckReport *compliance.Report
}{
{
name: "wrong resource kind",
resource: compliance.Resource{
ResourceCommon: compliance.ResourceCommon{
File: &compliance.File{
Path: "/etc/bitsy/spider",
},
},
},
expectError: errors.New("expecting custom resource in custom check"),
},
{
name: "missing check name",
resource: compliance.Resource{
ResourceCommon: compliance.ResourceCommon{
Custom: &compliance.Custom{},
},
},
expectError: errors.New("missing check name in custom check"),
},
{
name: "allowed empty condition",
resource: compliance.Resource{
ResourceCommon: compliance.ResourceCommon{
Custom: &compliance.Custom{
Name: "check-name",
},
},
},
checkFactory: func(_ string) custom.CheckFunc {
return customCheckFunc(expectCheckReport, nil)
},
expectCheckReport: expectCheckReport,
},
{
name: "custom check error",
resource: compliance.Resource{
ResourceCommon: compliance.ResourceCommon{
Custom: &compliance.Custom{
Name: "check-name",
},
},
},
checkFactory: func(_ string) custom.CheckFunc {
return customCheckFunc(nil, expectCheckError)
},
expectCheckReport: &compliance.Report{
Passed: false,
Error: expectCheckError,
},
},
{
name: "condition expression failure",
resource: compliance.Resource{
ResourceCommon: compliance.ResourceCommon{
Custom: &compliance.Custom{
Name: "check-name",
},
},
Condition: "~",
},
expectError: errors.New(`1:1: unexpected token "~"`),
},
{
name: "cannot find check by name",
resource: compliance.Resource{
ResourceCommon: compliance.ResourceCommon{
Custom: &compliance.Custom{
Name: "check-name",
},
},
},
checkFactory: func(_ string) custom.CheckFunc {
return nil
},
expectError: errors.New("custom check with name: check-name does not exist"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
customCheckFactory = test.checkFactory
check, err := newCustomCheck(ruleID, test.resource)
if test.expectError != nil {
assert.EqualError(err, test.expectError.Error())
assert.Nil(check)
} else {
assert.NotNil(check)
env := &mocks.Env{}
reports := check.check(env)
if test.expectCheckReport.Error != nil {
assert.EqualError(reports[0].Error, test.expectCheckReport.Error.Error())
}
assert.Equal(test.expectCheckReport.Passed, reports[0].Passed)
assert.Equal(test.expectCheckReport.Data, reports[0].Data)
}
})
}
}