forked from wavefrontHQ/go-wavefront-management-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_policy_test.go
164 lines (156 loc) · 5.07 KB
/
metrics_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
164
package wavefront
import (
"fmt"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/url"
"testing"
)
type MockMetricsPolicyClient struct {
Client
T *testing.T
}
func (m *MockMetricsPolicyClient) Do(req *http.Request) (io.ReadCloser, error) {
switch req.Method {
case "GET":
return testDo(m.T, req, "./fixtures/crud-metrics-policy-default-response.json", "GET", &MetricsPolicy{})
case "PUT":
return testDo(m.T, req, "./fixtures/crud-metrics-policy-response.json", "PUT", &UpdateMetricsPolicyRequest{})
default:
return nil, fmt.Errorf("unimplemented METHOD %s", req.Method)
}
}
func TestMetricsPolicy_Get(t *testing.T) {
baseurl, _ := url.Parse("http://testing.wavefront.com")
m := &MetricsPolicyAPI{
client: &MockMetricsPolicyClient{
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
T: t,
},
}
id := "8bcffe68-5fcb-47fa-b935-ba7bc102b9a7"
resp, err := m.Get()
assert.Nil(t, err)
assert.Equal(t, &MetricsPolicy{
PolicyRules: []PolicyRule{{
Accounts: []PolicyUser{},
UserGroups: []PolicyUserGroup{{ID: id, Name: "Everyone", Description: "System group which contains all users"}},
Roles: []Role{},
Name: "Allow All Metrics",
Tags: []PolicyTag{},
Description: "Predefined policy rule. Allows access to all metrics (timeseries, histograms, and counters) for all accounts. If this rule is removed, all accounts can access all metrics if there are no matching blocking rules.",
Prefixes: []string{"*"},
TagsAnded: false,
AccessType: "ALLOW",
}},
Customer: "example",
UpdaterId: "system",
UpdatedEpochMillis: 1603762170831,
}, resp)
}
func TestMetricsPolicy_Put(t *testing.T) {
baseurl, _ := url.Parse("http://testing.wavefront.com")
m := &MetricsPolicyAPI{
client: &MockMetricsPolicyClient{
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
T: t,
},
}
id := "8bcffe68-5fcb-47fa-b935-ba7bc102b9a7"
id2 := "7y6ffe68-5fcb-47fa-b935-ba7bc102b9a7"
resp, err := m.Update(&UpdateMetricsPolicyRequest{PolicyRules: []PolicyRuleRequest{{
AccountIds: []string{},
UserGroupIds: []string{id},
RoleIds: []string{},
Name: "Allow All Metrics",
Tags: []PolicyTag{},
Description: "Predefined policy rule. Allows access to all metrics (timeseries, histograms, and counters) for all accounts. If this rule is removed, all accounts can access all metrics if there are no matching blocking rules.",
Prefixes: []string{"*"},
TagsAnded: true,
AccessType: "ALLOW",
},
{
AccountIds: []string{},
UserGroupIds: []string{},
RoleIds: []string{"abc123", "poi567"},
Name: "BLOCK Some Metrics by role",
Tags: []PolicyTag{{Key: "Custom", Value: "Value"}},
Description: "Scoped filter for roles.",
Prefixes: []string{"aa.*", "bb.*"},
TagsAnded: true,
AccessType: "BLOCK",
},
{
AccountIds: []string{id2},
UserGroupIds: []string{},
RoleIds: []string{},
Name: "Allow Some Metrics by accounts",
Tags: []PolicyTag{{Key: "env", Value: "prod"}},
Description: "Scoped filter for users.",
Prefixes: []string{"*"},
TagsAnded: false,
AccessType: "ALLOW",
},
}})
assert.Nil(t, err)
assert.Equal(t, &MetricsPolicy{
PolicyRules: []PolicyRule{{
Accounts: []PolicyUser{},
UserGroups: []PolicyUserGroup{{
Name: "Everyone",
ID: id,
Description: "System group which contains all users",
}},
Roles: []Role{},
Name: "Allow All Metrics",
Tags: []PolicyTag{},
Description: "Predefined policy rule. Allows access to all metrics (timeseries, histograms, and counters) for all accounts. If this rule is removed, all accounts can access all metrics if there are no matching blocking rules.",
Prefixes: []string{"*"},
TagsAnded: false,
AccessType: "ALLOW",
},
{
Accounts: []PolicyUser{},
UserGroups: []PolicyUserGroup{},
Roles: []Role{
{Name: "test-role1", ID: "abc123", Description: "misc"},
{Name: "test-role2", ID: "poi567", Description: ""},
},
Name: "BLOCK Some Metrics by role",
Tags: []PolicyTag{{Key: "Custom", Value: "Value"}},
Description: "Scoped filter for roles.",
Prefixes: []string{"aa.*", "bb.*"},
TagsAnded: true,
AccessType: "BLOCK",
},
{
Accounts: []PolicyUser{
{ID: "[email protected]", Name: "[email protected]"},
{ID: "[email protected]", Name: "[email protected]"}},
UserGroups: []PolicyUserGroup{},
Roles: []Role{},
Name: "Allow Some Metrics by account",
Tags: []PolicyTag{{Key: "env", Value: "prod"}},
Description: "Scoped filter for users.",
Prefixes: []string{"*"},
TagsAnded: true,
AccessType: "BLOCK",
},
},
Customer: "example",
UpdaterId: "[email protected]",
UpdatedEpochMillis: 2603766170831,
},
resp)
}