forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_check_test.go
200 lines (157 loc) · 6.11 KB
/
policy_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
200
package tfe
import (
"context"
"io/ioutil"
"testing"
"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, _ := createUploadedPolicy(t, client, true, orgTest)
pTest2, _ := createUploadedPolicy(t, client, true, orgTest)
wTest, _ := createWorkspace(t, client, orgTest)
createPolicySet(t, client, orgTest, []*Policy{pTest1, pTest2}, []*Workspace{wTest})
rTest, _ := createPlannedRun(t, client, wTest)
t.Run("without list options", func(t *testing.T) {
pcl, err := client.PolicyChecks.List(ctx, rTest.ID, PolicyCheckListOptions{})
require.NoError(t, err)
require.Equal(t, 1, len(pcl.Items))
t.Run("pagination is properly decoded", func(t *testing.T) {
t.Skip("paging not supported yet in API")
assert.Equal(t, 1, pcl.CurrentPage)
assert.Equal(t, 1, pcl.TotalCount)
})
t.Run("permissions are properly decoded", func(t *testing.T) {
assert.NotEmpty(t, pcl.Items[0].Permissions)
})
t.Run("result is properly decoded", func(t *testing.T) {
require.NotEmpty(t, pcl.Items[0].Result)
assert.Equal(t, 2, pcl.Items[0].Result.Passed)
})
t.Run("timestamps are properly decoded", func(t *testing.T) {
assert.NotEmpty(t, pcl.Items[0].StatusTimestamps)
})
})
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("without a valid run ID", func(t *testing.T) {
pcl, err := client.PolicyChecks.List(ctx, badIdentifier, PolicyCheckListOptions{})
assert.Nil(t, pcl)
assert.EqualError(t, err, "invalid value for run ID")
})
}
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})
rTest, _ := createPlannedRun(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)
assert.NotEmpty(t, pc.Permissions)
assert.Equal(t, PolicyScopeOrganization, pc.Scope)
assert.Equal(t, PolicyPasses, pc.Status)
assert.NotEmpty(t, pc.StatusTimestamps)
t.Run("result is properly decoded", func(t *testing.T) {
require.NotEmpty(t, pc.Result)
assert.Equal(t, 1, pc.Result.Passed)
})
})
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.EqualError(t, err, "invalid value for policy check ID")
})
}
func TestPolicyChecksOverride(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
t.Run("when the policy failed", func(t *testing.T) {
pTest, pTestCleanup := createUploadedPolicy(t, client, false, orgTest)
defer pTestCleanup()
wTest, _ := createWorkspace(t, client, orgTest)
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest})
rTest, _ := createPlannedRun(t, client, wTest)
pcl, err := client.PolicyChecks.List(ctx, rTest.ID, PolicyCheckListOptions{})
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) {
pTest, pTestCleanup := createUploadedPolicy(t, client, true, orgTest)
defer pTestCleanup()
wTest, _ := createWorkspace(t, client, orgTest)
createPolicySet(t, client, orgTest, []*Policy{pTest}, []*Workspace{wTest})
rTest, _ := createPlannedRun(t, client, wTest)
pcl, err := client.PolicyChecks.List(ctx, rTest.ID, PolicyCheckListOptions{})
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.EqualError(t, err, "invalid value for policy check ID")
})
}
func TestPolicyChecksLogs(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})
rTest, _ := createPlannedRun(t, client, wTest)
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 := ioutil.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)
})
}