-
Notifications
You must be signed in to change notification settings - Fork 2
/
adminapi_test.go
198 lines (175 loc) · 5.75 KB
/
adminapi_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
package radosgwadmin
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/go-playground/validator/v10"
"github.com/stretchr/testify/suite"
)
var validate *validator.Validate
type ModelsSuite struct {
suite.Suite
dbags map[string][]byte
vs []interface{}
aa *AdminAPI
}
func (ms *ModelsSuite) SetupSuite() {
ms.dbags = make(map[string][]byte)
ms.vs = []interface{}{
"aGetRequest{},
&QuotaSetRequest{},
&UserCreateRequest{},
&UserCapsRequest{},
&UserModifyRequest{},
&SubUserCreateModifyRequest{},
&bucketRequest{},
&bucketRmRequest{},
&bucketLinkRequest{},
&bucketUnlinkRequest{},
&BucketIndexRequest{},
&bucketObjectRmRequest{},
}
datadir := os.Getenv("ADMINAPI_TEST_DATADIR")
if datadir == "" {
datadir = "./testdata"
}
files, err := ioutil.ReadDir(datadir)
if err != nil {
panic(fmt.Sprintf("Got error trying to open dir %s: %s", datadir, err))
}
for _, file := range files {
if file.IsDir() || !strings.HasSuffix(strings.ToLower(file.Name()), ".json") {
continue
}
path := datadir + "/" + file.Name()
key := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
ms.dbags[key], err = ioutil.ReadFile(path)
if err != nil {
panic(fmt.Sprintf("Got error trying to read file %s: %s", path, err))
}
}
ms.aa = &AdminAPI{}
validate = validator.New()
}
func (ms *ModelsSuite) Test01Validators() {
var structName string
defer func() {
if r := recover(); r != nil {
ms.Fail("Paniced in Validators()", "paniced validating %s: %#v\n", structName, r)
}
}()
for _, v := range ms.vs {
structName = reflect.TypeOf(v).Elem().Name()
err := validate.Struct(v)
if err != nil {
vierr, ok := err.(*validator.InvalidValidationError)
ms.False(ok, "Error is InvalidValidationError %s", vierr)
}
}
// test the UserCreateRequest validators
ucr := &UserCreateRequest{
UID: "whatever",
Email: "[email protected]",
KeyType: "swift",
UserCaps: []UserCap{
{
Type: "buckets",
Permission: "read,write",
},
{
Type: "asdf",
Permission: "invalid",
},
},
}
err := validate.Struct(ucr)
ms.Error(err, "did not fail validation")
_, ok := err.(validator.ValidationErrors)
ms.True(ok, "not coerce to ValidationErrors")
}
func (ms *ModelsSuite) Test02Usage() {
usagejson := ms.dbags["usage"]
resp := &UsageResponse{}
err := json.Unmarshal(usagejson, resp)
ms.NoError(err, "Error unmarshaling json")
ms.Len(resp.Entries, 1, "Expected number of entries not found")
ms.Len(resp.Entries[0].Buckets, 2, "Expected number of Buckets not found")
t, err := time.Parse(RadosTimeFormat, "2017-03-16 04:00:00.000000Z")
ms.NoError(err, "Error received when no error expected")
ms.Equal(time.Time(resp.Entries[0].Buckets[0].Time), t, "Time formats don't match")
ms.Len(resp.Summary, 1, "Expected summary size not found")
ms.Len(resp.Summary[0].Categories, 6, "Expected number of categories in the summary not found")
}
func (ms *ModelsSuite) Test03Bucket() {
bucketjson := ms.dbags["bucket"]
resp := &BucketStatsResponse{}
err := json.Unmarshal(bucketjson, resp)
ms.NoError(err, "Error unmarshaling bucket json")
ms.T().Logf("bucket response:\n%#v\n", resp)
ms.T().Logf("mktime: %s\n", time.Time(resp.Mtime).String())
bucketindjson := ms.dbags["bucketindex"]
bir := &BucketIndexResponse{}
err = bir.Decode(bytes.NewReader(bucketindjson))
ms.NoError(err, "Error unmarshaling bucket index json")
ms.Equal(bir.NewObjects[0], "key.json", "first element of NewObjects not as expected")
ms.Equal(len(bir.NewObjects), 3, "length of NewObjects not 3")
ms.Equal(bir.Headers.ExistingHeader.Usage.RGWMain.NumObjects, uint64(9), "rgwmain num objects not as expected")
ms.Equal(bir.Headers.ExistingHeader.Usage.RGWNone.SizeKb, uint64(5), "rgwnone num objects not as expected")
bucketindjsonNoFix := ms.dbags["bucketindex_nofix"]
bir = &BucketIndexResponse{}
err = bir.Decode(bytes.NewReader(bucketindjsonNoFix))
ms.NoError(err, "Error, could not read bucketindex_nofix")
bucketpoljson := ms.dbags["bucketpolicy"]
bpr := &BucketPolicyResponse{}
err = json.Unmarshal(bucketpoljson, bpr)
ms.NoError(err, "Error, could not read from bucket policy")
ms.T().Log(spew.Sdump(bpr))
}
func (ms *ModelsSuite) Test04Metadata() {
bucketjson := ms.dbags["mbucket"]
bresp := &MBucketResponse{}
err := json.Unmarshal(bucketjson, bresp)
ms.NoError(err, "Error unmarshaling mbucket json")
ms.T().Logf("mbucket response:\n%#v\n", bresp)
userjson := ms.dbags["muser"]
uresp := &MUserResponse{}
err = json.Unmarshal(userjson, uresp)
ms.NoError(err, "Error unmarshaling muser json")
ms.T().Logf("muser response:\n%#v\n", uresp)
bucketinstjson := ms.dbags["mbucketinstance"]
biresp := &MBucketInstanceResponse{}
err = json.Unmarshal(bucketinstjson, biresp)
ms.NoError(err, "Error unmarshaling mbucketinst json")
ms.T().Logf("mbucket response:\n%#v\n", biresp)
}
func (ms *ModelsSuite) Test05Quotas() {
quotasjson := ms.dbags["quotas"]
resp := &Quotas{}
err := json.Unmarshal(quotasjson, resp)
ms.NoError(err, "Error unmarshaling quotas json")
ms.Equal(resp.BucketQuota.MaxObjects, int64(-1), "Value not expected")
ms.Equal(resp.UserQuota.MaxSizeKb, int64(-1), "Value not expected")
}
func (ms *ModelsSuite) Test06User() {
userjson := ms.dbags["user"]
userstatjson := ms.dbags["userstat"]
resp := &UserInfoResponse{}
err := json.Unmarshal(userjson, resp)
ms.NoError(err, "Error unmarshaling user json")
ms.Nil(resp.Stats, "stats not nil as expected")
resp = &UserInfoResponse{}
err = json.Unmarshal(userstatjson, resp)
ms.NoError(err, "Error unmsrshaling userstat json")
ms.NotNil(resp.Stats, "userstat Stats is nil when it shouldn't be")
}
func TestAdminAPI(t *testing.T) {
suite.Run(t, new(ModelsSuite))
}