-
Notifications
You must be signed in to change notification settings - Fork 3
/
metrics_test.go
129 lines (112 loc) · 3.13 KB
/
metrics_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
package cloudscale
import (
"fmt"
"net/http"
"net/url"
"reflect"
"testing"
"time"
)
func TestMetrics_GetBucketMetrics(t *testing.T) {
setup()
defer teardown()
metricsRequest := &BucketMetricsRequest{
Start: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
End: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
BucketNames: []string{},
ObjectsUserIDs: []string{},
}
mux.HandleFunc("/v1/metrics/buckets", func(w http.ResponseWriter, r *http.Request) {
expected := map[string][]string{
"start": {"2020-01-01"},
"end": {"2020-01-01"},
}
assertEqual(t, url.Values(expected), r.URL.Query())
jsonStr := `
{
"start": "2019-12-31T23:00:00Z",
"end": "2020-01-01T23:00:00Z",
"data": [
{
"subject": {
"name": "Carmichael",
"objects_user_id": "2b44ef90ff15f010dc013f8d32612e07d7734e5f5581d0c94be56780b0dcd58a"
},
"time_series": [
{
"start": "2019-12-31T23:00:00Z",
"end": "2020-01-01T23:00:00Z",
"usage": {
"requests": 561,
"object_count": 1105,
"storage_bytes": 1729,
"received_bytes": 2465,
"sent_bytes": 2821
}
}
]
}
]
}`
fmt.Fprintf(w, jsonStr)
})
metrics, err := client.Metrics.GetBucketMetrics(ctx, metricsRequest)
if err != nil {
t.Errorf("Metrics.GetBucketMetrics returned error: %v", err)
return
}
expectedMetrics := BucketMetrics{
Start: time.Date(2019, 12, 31, 23, 0, 0, 0, time.UTC),
End: time.Date(2020, 1, 1, 23, 0, 0, 0, time.UTC),
Data: []BucketMetricsData{
{
Subject: BucketMetricsDataSubject{
BucketName: "Carmichael",
ObjectsUserID: "2b44ef90ff15f010dc013f8d32612e07d7734e5f5581d0c94be56780b0dcd58a",
},
TimeSeries: []BucketMetricsInterval{
{
Start: time.Date(2019, 12, 31, 23, 0, 0, 0, time.UTC),
End: time.Date(2020, 1, 1, 23, 0, 0, 0, time.UTC),
Usage: BucketMetricsIntervalUsage{
Requests: 561,
ObjectCount: 1105,
StorageBytes: 1729,
ReceivedBytes: 2465,
SentBytes: 2821,
},
},
},
},
},
}
if !reflect.DeepEqual(*metrics, expectedMetrics) {
t.Errorf("expected metrics '%v', received '%v'", expectedMetrics, metrics)
}
}
func TestMetrics_GetBucketMetricsAdditionalArgs(t *testing.T) {
setup()
defer teardown()
metricsRequest := &BucketMetricsRequest{
Start: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
End: time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC),
BucketNames: []string{"guete", "morge"},
ObjectsUserIDs: []string{"hallo", "tschüss"},
}
mux.HandleFunc("/v1/metrics/buckets", func(w http.ResponseWriter, r *http.Request) {
expected := map[string][]string{
"start": {"2020-01-01"},
"end": {"2020-01-02"},
"bucket_name": {"guete", "morge"},
"objects_user_id": {"hallo", "tschüss"},
}
assertEqual(t, url.Values(expected), r.URL.Query())
// Dummy response.
fmt.Fprintf(w, "{}")
})
_, err := client.Metrics.GetBucketMetrics(ctx, metricsRequest)
if err != nil {
t.Errorf("Metrics.GetBucketMetrics returned error: %v", err)
return
}
}