-
Notifications
You must be signed in to change notification settings - Fork 102
/
policy_check_integration_test.go
281 lines (234 loc) · 8.65 KB
/
policy_check_integration_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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"bytes"
"context"
"encoding/json"
"io"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPolicyChecksList(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
pTest1, policyCleanup1 := createUploadedPolicy(t, client, true, orgTest)
defer policyCleanup1()
pTest2, policyCleanup2 := createUploadedPolicy(t, client, true, orgTest)
defer policyCleanup2()
wTest, wsCleanup := createWorkspace(t, client, orgTest)
defer wsCleanup()
createPolicySet(t, client, orgTest, []*Policy{pTest1, pTest2}, []*Workspace{wTest}, nil, nil, "")
rTest, runCleanup := createPolicyCheckedRun(t, client, wTest)
defer runCleanup()
t.Run("without list options", func(t *testing.T) {
pcl, err := client.PolicyChecks.List(ctx, rTest.ID, nil)
require.NoError(t, err)
require.Equal(t, 1, len(pcl.Items))
assert.NotEmpty(t, pcl.Items[0].Permissions)
require.NotEmpty(t, pcl.Items[0].Result)
assert.Equal(t, 2, pcl.Items[0].Result.Passed)
assert.NotEmpty(t, pcl.Items[0].StatusTimestamps)
assert.NotNil(t, pcl.Items[0].StatusTimestamps.QueuedAt)
})
t.Run("with list options", func(t *testing.T) {
t.Skip("paging not supported yet in API")
// Request a page number which is out of range. The result should
// be successful, but return no results if the paging options are
// properly passed along.
pcl, err := client.PolicyChecks.List(ctx, rTest.ID, &PolicyCheckListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
assert.Empty(t, pcl.Items)
assert.Equal(t, 999, pcl.CurrentPage)
assert.Equal(t, 1, pcl.TotalCount)
})
t.Run("with Include option", func(t *testing.T) {
pcl, err := client.PolicyChecks.List(ctx, rTest.ID, &PolicyCheckListOptions{
Include: []PolicyCheckIncludeOpt{PolicyCheckRun},
})
require.NoError(t, err)
require.NotEmpty(t, pcl.Items)
require.NotNil(t, pcl.Items[0])
require.NotNil(t, pcl.Items[0].Run)
assert.NotEmpty(t, pcl.Items[0].Run.Status)
})
t.Run("without a valid run ID", func(t *testing.T) {
pcl, err := client.PolicyChecks.List(ctx, badIdentifier, nil)
assert.Nil(t, pcl)
assert.EqualError(t, err, ErrInvalidRunID.Error())
})
}
func TestPolicyChecksRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
pTest, _ := createUploadedPolicy(t, client, true, orgTest)
wTest, _ := createWorkspace(t, client, orgTest)
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest}, nil, nil, "")
rTest, _ := createPolicyCheckedRun(t, client, wTest)
require.Equal(t, 1, len(rTest.PolicyChecks))
t.Run("when the policy check exists", func(t *testing.T) {
pc, err := client.PolicyChecks.Read(ctx, rTest.PolicyChecks[0].ID)
require.NoError(t, err)
require.NotEmpty(t, pc.Result)
assert.NotEmpty(t, pc.Permissions)
assert.Equal(t, PolicyScopeOrganization, pc.Scope)
assert.Equal(t, PolicyPasses, pc.Status)
assert.NotEmpty(t, pc.StatusTimestamps)
assert.Equal(t, 1, pc.Result.Passed)
assert.NotEmpty(t, pc.Run)
assert.NotEmpty(t, pc.Result.Sentinel)
if reflect.TypeOf(pc.Result.Sentinel) != reflect.TypeOf(map[string]interface{}{}) {
assert.Fail(t, "Sentinel is not a map[string]interface{}")
}
})
t.Run("when the policy check does not exist", func(t *testing.T) {
pc, err := client.PolicyChecks.Read(ctx, "nonexisting")
assert.Nil(t, pc)
assert.Equal(t, ErrResourceNotFound, err)
})
t.Run("without a valid policy check ID", func(t *testing.T) {
pc, err := client.PolicyChecks.Read(ctx, badIdentifier)
assert.Nil(t, pc)
assert.Equal(t, err, ErrInvalidPolicyCheckID)
})
}
func TestPolicyChecksOverride(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("when the policy failed", func(t *testing.T) {
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
pTest, pTestCleanup := createUploadedPolicy(t, client, false, orgTest)
defer pTestCleanup()
wTest, wTestCleanup := createWorkspace(t, client, orgTest)
defer wTestCleanup()
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest}, nil, nil, "")
rTest, tTestCleanup := createPolicyCheckedRun(t, client, wTest)
defer tTestCleanup()
pcl, err := client.PolicyChecks.List(ctx, rTest.ID, nil)
require.NoError(t, err)
require.Equal(t, 1, len(pcl.Items))
require.Equal(t, PolicySoftFailed, pcl.Items[0].Status)
pc, err := client.PolicyChecks.Override(ctx, pcl.Items[0].ID)
require.NoError(t, err)
assert.NotEmpty(t, pc.Result)
assert.Equal(t, PolicyOverridden, pc.Status)
})
t.Run("when the policy passed", func(t *testing.T) {
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
pTest, pTestCleanup := createUploadedPolicy(t, client, true, orgTest)
defer pTestCleanup()
wTest, wTestCleanup := createWorkspace(t, client, orgTest)
defer wTestCleanup()
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest}, nil, nil, "")
rTest, rTestCleanup := createPolicyCheckedRun(t, client, wTest)
defer rTestCleanup()
pcl, err := client.PolicyChecks.List(ctx, rTest.ID, nil)
require.NoError(t, err)
require.Equal(t, 1, len(pcl.Items))
require.Equal(t, PolicyPasses, pcl.Items[0].Status)
_, err = client.PolicyChecks.Override(ctx, pcl.Items[0].ID)
assert.Error(t, err)
})
t.Run("without a valid policy check ID", func(t *testing.T) {
p, err := client.PolicyChecks.Override(ctx, badIdentifier)
assert.Nil(t, p)
assert.Equal(t, err, ErrInvalidPolicyCheckID)
})
}
func TestPolicyChecksLogs(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
pTest, pTestCleanup := createUploadedPolicy(t, client, true, orgTest)
defer pTestCleanup()
wTest, wTestCleanup := createWorkspace(t, client, orgTest)
defer wTestCleanup()
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest}, nil, nil, "")
rTest, rTestCleanup := createPolicyCheckedRun(t, client, wTest)
defer rTestCleanup()
require.Equal(t, 1, len(rTest.PolicyChecks))
t.Run("when the log exists", func(t *testing.T) {
pc, err := client.PolicyChecks.Read(ctx, rTest.PolicyChecks[0].ID)
require.NoError(t, err)
logReader, err := client.PolicyChecks.Logs(ctx, pc.ID)
require.NoError(t, err)
logs, err := io.ReadAll(logReader)
require.NoError(t, err)
assert.Contains(t, string(logs), "1 policies evaluated")
})
t.Run("when the log does not exist", func(t *testing.T) {
logs, err := client.PolicyChecks.Logs(ctx, "nonexisting")
assert.Nil(t, logs)
assert.Error(t, err)
})
}
func TestPolicyCheck_Unmarshal(t *testing.T) {
data := map[string]interface{}{
"data": map[string]interface{}{
"type": "policy-checks",
"id": "1",
"attributes": map[string]interface{}{
"actions": map[string]interface{}{
"is-overridable": true,
},
"permissions": map[string]interface{}{
"can-override": true,
},
"result": map[string]interface{}{
"advisory-failed": 1,
"duration": 1,
"hard-failed": 1,
"passed": 1,
"result": true,
"soft-failed": 1,
"total-failed": 1,
},
"scope": PolicyScopeOrganization,
"status": PolicyOverridden,
"status-timestamps": map[string]string{
"queued-at": "2020-03-16T23:15:59+00:00",
"errored-at": "2019-03-16T23:23:59+00:00",
},
},
},
}
byteData, err := json.Marshal(data)
require.NoError(t, err)
responseBody := bytes.NewReader(byteData)
pc := &PolicyCheck{}
err = unmarshalResponse(responseBody, pc)
require.NoError(t, err)
queuedParsedTime, err := time.Parse(time.RFC3339, "2020-03-16T23:15:59+00:00")
require.NoError(t, err)
erroredParsedTime, err := time.Parse(time.RFC3339, "2019-03-16T23:23:59+00:00")
require.NoError(t, err)
assert.Equal(t, pc.ID, "1")
assert.Equal(t, pc.Actions.IsOverridable, true)
assert.Equal(t, pc.Permissions.CanOverride, true)
assert.Equal(t, pc.Result.AdvisoryFailed, 1)
assert.Equal(t, pc.Result.Duration, 1)
assert.Equal(t, pc.Result.HardFailed, 1)
assert.Equal(t, pc.Result.Passed, 1)
assert.Equal(t, pc.Result.Result, true)
assert.Equal(t, pc.Result.SoftFailed, 1)
assert.Equal(t, pc.Result.TotalFailed, 1)
assert.Equal(t, pc.Scope, PolicyScopeOrganization)
assert.Equal(t, pc.Status, PolicyOverridden)
assert.Equal(t, pc.StatusTimestamps.QueuedAt, queuedParsedTime)
assert.Equal(t, pc.StatusTimestamps.ErroredAt, erroredParsedTime)
}