-
Notifications
You must be signed in to change notification settings - Fork 65
/
policy_test.go
163 lines (127 loc) · 4.07 KB
/
policy_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
package chef
import (
"fmt"
"net/http"
"os"
"testing"
)
const policyListResponseFile = "test/policies_response.json"
const policyRevisionResponseFile = "test/policy_revision_response.json"
func TestListPolicies(t *testing.T) {
setup()
defer teardown()
file, err := os.ReadFile(policyListResponseFile)
if err != nil {
t.Error(err)
}
mux.HandleFunc("/policies", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, string(file))
})
data, err := client.Policies.List()
if err != nil {
t.Error(err)
}
if data == nil {
t.Fatal("We should have some data")
}
if len(data) != 2 {
t.Error("Mismatch in expected policies count. Expected 2, Got: ", len(data))
}
if _, ok := data["aar"]; !ok {
t.Error("aar policy should be listed")
}
if _, ok := data["jenkins"]; !ok {
t.Error("jenkins policy should be listed")
}
}
func TestGetPolicy(t *testing.T) {
setup()
defer teardown()
policyGetJSON := `{
"revisions": {
"8228b0e381fe1de3ee39bf51e93029dbbdcecc61fb5abea4ca8c82591c0b529b": {
}
}
}`
mux.HandleFunc("/policies/base", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, policyGetJSON)
})
mux.HandleFunc("/policies/bad", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", 404)
})
data, err := client.Policies.Get("base")
if err != nil {
t.Error(err)
}
if _, ok := data["revisions"]["8228b0e381fe1de3ee39bf51e93029dbbdcecc61fb5abea4ca8c82591c0b529b"]; !ok {
t.Error("Missing expected revision for this policy")
}
_, err = client.Policies.Get("bad")
if err == nil {
t.Error("We expected this bad request to error", err)
}
}
func TestGetPolicyRevision(t *testing.T) {
setup()
defer teardown()
const policyName = "base"
const policyRevision = "8228b0e381fe1de3ee39bf51e93029dbbdcecc61fb5abea4ca8c82591c0b529b"
file, err := os.ReadFile(policyRevisionResponseFile)
if err != nil {
t.Error(err)
}
mux.HandleFunc(fmt.Sprintf("/policies/%s/revisions/%s", policyName, policyRevision), func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, string(file))
})
data, err := client.Policies.GetRevisionDetails(policyName, policyRevision)
if err != nil {
t.Error(err)
}
if data.Name != policyName {
t.Errorf("Unexpected policy name: %+v", data.Name)
}
if data.RevisionID != policyRevision {
t.Errorf("Unexpected policy revision ID: %+v", data.RevisionID)
}
if data.RunList[0] != "recipe[base::default]" {
t.Errorf("Unexpected policy run list: %+v", data.RevisionID)
}
if val, ok := data.NamedRunList["os"]; !ok {
t.Error("Expected os NamedRunList policy to be present in the policy information")
} else if val[0] != "recipe[hardening::default]" {
t.Error("Expected named run list for the policy, got: ", val[0])
}
if data.IncludedPolicyLocks[0].Name != "other" {
t.Error("Expected included policy name to be present in the policy information")
} else if data.IncludedPolicyLocks[0].RevisionID != "7b40995ad1150ec56950c757872d6732aa00e76382dfcd2fddeb3a971e57ba9c" {
t.Error("Expected included policy revision ID to be present in the policy information")
}
if val, ok := data.CookbookLocks["hardening"]; !ok {
t.Error("Expected hardening policy to be present in the policy information")
} else if val.Version != "0.1.0" {
t.Error("Expected hardening policy version to be 0.1.0, got: ", val.Version)
}
}
func TestDeletePolicyRevision(t *testing.T) {
setup()
defer teardown()
const policyName = "base"
const policyRevision = "8228b0e381fe1de3ee39bf51e93029dbbdcecc61fb5abea4ca8c82591c0b529b"
file, err := os.ReadFile(policyRevisionResponseFile)
if err != nil {
t.Error(err)
}
mux.HandleFunc(fmt.Sprintf("/policies/%s/revisions/%s", policyName, policyRevision), func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, string(file))
})
data, err := client.Policies.DeleteRevision(policyName, policyRevision)
if err != nil {
t.Error(err)
}
if data.Name != policyName {
t.Errorf("Unexpected policy name: %+v", data.Name)
}
if data.RevisionID != policyRevision {
t.Errorf("Unexpected policy revision ID: %+v", data.RevisionID)
}
}