-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathgroup_check_test.go
94 lines (82 loc) · 2.27 KB
/
group_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
// 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 (
"testing"
"github.com/DataDog/datadog-agent/pkg/compliance"
"github.com/DataDog/datadog-agent/pkg/compliance/event"
"github.com/DataDog/datadog-agent/pkg/compliance/mocks"
assert "github.com/stretchr/testify/require"
)
func TestGroupCheck(t *testing.T) {
tests := []struct {
name string
etcGroupFile string
resource compliance.Resource
expectReport *compliance.Report
expectError error
}{
{
name: "docker group user found",
etcGroupFile: "./testdata/group/etc-group",
resource: compliance.Resource{
ResourceCommon: compliance.ResourceCommon{
Group: &compliance.Group{
Name: "docker",
},
},
Condition: `"carlos" in group.users`,
},
expectReport: &compliance.Report{
Passed: true,
Data: event.Data{
"group.name": "docker",
"group.id": 412,
"group.users": []string{"alice", "bob", "carlos", "dan", "eve"},
},
Resource: compliance.ReportResource{
ID: "docker",
Type: "group",
},
},
},
{
name: "docker group user not found",
etcGroupFile: "./testdata/group/etc-group",
resource: compliance.Resource{
ResourceCommon: compliance.ResourceCommon{
Group: &compliance.Group{
Name: "docker",
},
},
Condition: `"carol" in group.users`,
},
expectReport: &compliance.Report{
Passed: false,
Data: event.Data{
"group.name": "docker",
"group.id": 412,
"group.users": []string{"alice", "bob", "carlos", "dan", "eve"},
},
Resource: compliance.ReportResource{
ID: "docker",
Type: "group",
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert := assert.New(t)
env := &mocks.Env{}
env.On("EtcGroupPath").Return(test.etcGroupFile)
groupCheck, err := newResourceCheck(env, "rule-id", test.resource)
assert.NoError(err)
reports := groupCheck.check(env)
assert.Equal(test.expectReport, reports[0])
assert.Equal(test.expectError, reports[0].Error)
})
}
}