-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcommand_check_test.go
199 lines (181 loc) · 5.01 KB
/
command_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
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
// 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.
//go:build !windows
// +build !windows
package checks
import (
"context"
"errors"
"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"
)
type commandFixture struct {
name string
resource compliance.Resource
commandExitCode int
commandOutput string
commandError error
expectCommandName string
expectCommandArgs []string
expectReport *compliance.Report
}
func (f *commandFixture) mockRunCommand(t *testing.T) commandRunnerFunc {
return func(ctx context.Context, name string, args []string, captureStdout bool) (int, []byte, error) {
assert.Equal(t, f.expectCommandName, name)
assert.Equal(t, f.expectCommandArgs, args)
return f.commandExitCode, []byte(f.commandOutput), f.commandError
}
}
func (f *commandFixture) run(t *testing.T) {
t.Helper()
assert := assert.New(t)
commandRunner = f.mockRunCommand(t)
env := &mocks.Env{}
defer env.AssertExpectations(t)
commandCheck, err := newResourceCheck(env, "rule-id", f.resource)
assert.NoError(err)
reports := commandCheck.check(env)
assert.Equal(f.expectReport.Passed, reports[0].Passed)
assert.Equal(f.expectReport.Data, reports[0].Data)
if f.expectReport.Error != nil {
assert.EqualError(f.expectReport.Error, reports[0].Error.Error())
}
}
func TestCommandCheck(t *testing.T) {
tests := []commandFixture{
{
name: "binary run",
resource: compliance.Resource{
ResourceCommon: compliance.ResourceCommon{
Command: &compliance.Command{
BinaryCmd: &compliance.BinaryCmd{
Name: "myCommand",
Args: []string{"--foo=bar", "--baz"},
},
},
},
Condition: `command.stdout == "output"`,
},
commandExitCode: 0,
commandOutput: "output",
commandError: nil,
expectCommandName: "myCommand",
expectCommandArgs: []string{"--foo=bar", "--baz"},
expectReport: &compliance.Report{
Passed: true,
Data: event.Data{
"command.exitCode": 0,
},
},
},
{
name: "shell run",
resource: compliance.Resource{
ResourceCommon: compliance.ResourceCommon{
Command: &compliance.Command{
ShellCmd: &compliance.ShellCmd{
Run: "my command --foo=bar --baz",
},
},
},
Condition: `command.stdout == "output"`,
},
commandExitCode: 0,
commandOutput: "output",
commandError: nil,
expectCommandName: getDefaultShell().Name,
expectCommandArgs: append(getDefaultShell().Args, "my command --foo=bar --baz"),
expectReport: &compliance.Report{
Passed: true,
Data: event.Data{
"command.exitCode": 0,
},
},
},
{
name: "custom shell run",
resource: compliance.Resource{
ResourceCommon: compliance.ResourceCommon{
Command: &compliance.Command{
ShellCmd: &compliance.ShellCmd{
Run: "my command --foo=bar --baz",
Shell: &compliance.BinaryCmd{
Name: "zsh",
Args: []string{"-someoption", "-c"},
},
},
},
},
Condition: `command.stdout == "output"`,
},
commandExitCode: 0,
commandOutput: "output",
commandError: nil,
expectCommandName: "zsh",
expectCommandArgs: []string{"-someoption", "-c", "my command --foo=bar --baz"},
expectReport: &compliance.Report{
Passed: true,
Data: event.Data{
"command.exitCode": 0,
},
},
},
{
name: "execution failure",
resource: compliance.Resource{
ResourceCommon: compliance.ResourceCommon{
Command: &compliance.Command{
BinaryCmd: &compliance.BinaryCmd{
Name: "myCommand",
Args: []string{"--foo=bar", "--baz"},
},
},
},
Condition: `command.stdout == "output"`,
},
commandExitCode: -1,
commandError: errors.New("some failure"),
expectCommandName: "myCommand",
expectCommandArgs: []string{"--foo=bar", "--baz"},
expectReport: &compliance.Report{
Passed: false,
Error: errors.New("command 'Binary command: myCommand, args: [--foo=bar --baz]' execution failed, error: some failure"),
},
},
{
name: "non-zero return code",
resource: compliance.Resource{
ResourceCommon: compliance.ResourceCommon{
Command: &compliance.Command{
BinaryCmd: &compliance.BinaryCmd{
Name: "myCommand",
Args: []string{"--foo=bar", "--baz"},
},
},
},
Condition: `command.exitCode == 2`,
},
commandExitCode: 2,
commandOutput: "output",
commandError: nil,
expectCommandName: "myCommand",
expectCommandArgs: []string{"--foo=bar", "--baz"},
expectReport: &compliance.Report{
Passed: true,
Data: event.Data{
"command.exitCode": 2,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.run(t)
})
}
}