github.com/minio/console@v1.3.0/integration/admin_api_integration_test.go (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2021 MinIO, Inc. 3 // 4 // This program is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Affero General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 17 // These tests are for AdminAPI Tag based on swagger-console.yml 18 19 package integration 20 21 import ( 22 "bytes" 23 "encoding/json" 24 "fmt" 25 "io" 26 "log" 27 "mime/multipart" 28 "net/http" 29 "os" 30 "path" 31 "testing" 32 "time" 33 34 "github.com/minio/console/models" 35 36 "github.com/stretchr/testify/assert" 37 ) 38 39 func RestartService() (*http.Response, error) { 40 /* 41 Helper function to restart service 42 HTTP Verb: POST 43 URL: /api/v1/service/restart 44 */ 45 request, err := http.NewRequest( 46 "POST", 47 "http://localhost:9090/api/v1/service/restart", 48 nil, 49 ) 50 if err != nil { 51 log.Println(err) 52 } 53 request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) 54 request.Header.Add("Content-Type", "application/json") 55 client := &http.Client{ 56 Timeout: 2000 * time.Second, // increased timeout since restart takes time, more than other APIs. 57 } 58 response, err := client.Do(request) 59 return response, err 60 } 61 62 func GetNodes() (*http.Response, error) { 63 /* 64 Helper function to get nodes 65 HTTP Verb: GET 66 URL: /api/v1/nodes 67 */ 68 request, err := http.NewRequest( 69 "GET", 70 "http://localhost:9090/api/v1/nodes", 71 nil, 72 ) 73 if err != nil { 74 log.Println(err) 75 } 76 request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) 77 request.Header.Add("Content-Type", "application/json") 78 client := &http.Client{ 79 Timeout: 2000 * time.Second, // increased timeout since restart takes time, more than other APIs. 80 } 81 response, err := client.Do(request) 82 return response, err 83 } 84 85 func NotifyPostgres() (*http.Response, error) { 86 /* 87 Helper function to add Postgres Notification 88 HTTP Verb: PUT 89 URL: api/v1/configs/notify_postgres 90 Body: 91 { 92 "key_values":[ 93 { 94 "key":"connection_string", 95 "value":"user=postgres password=password host=localhost dbname=postgres port=5432 sslmode=disable" 96 }, 97 { 98 "key":"table", 99 "value":"accountsssss" 100 }, 101 { 102 "key":"format", 103 "value":"namespace" 104 }, 105 { 106 "key":"queue_limit", 107 "value":"10000" 108 }, 109 { 110 "key":"comment", 111 "value":"comment" 112 } 113 ] 114 } 115 */ 116 Body := models.SetConfigRequest{ 117 KeyValues: []*models.ConfigurationKV{ 118 { 119 Key: "connection_string", 120 Value: "user=postgres password=password host=173.18.0.4 dbname=postgres port=5432 sslmode=disable", 121 }, 122 { 123 Key: "table", 124 Value: "accountsssss", 125 }, 126 { 127 Key: "format", 128 Value: "namespace", 129 }, 130 { 131 Key: "queue_limit", 132 Value: "10000", 133 }, 134 { 135 Key: "comment", 136 Value: "comment", 137 }, 138 }, 139 } 140 141 requestDataJSON, _ := json.Marshal(Body) 142 requestDataBody := bytes.NewReader(requestDataJSON) 143 request, err := http.NewRequest( 144 "PUT", 145 "http://localhost:9090/api/v1/configs/notify_postgres", 146 requestDataBody, 147 ) 148 if err != nil { 149 log.Println(err) 150 } 151 request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) 152 request.Header.Add("Content-Type", "application/json") 153 client := &http.Client{ 154 Timeout: 2 * time.Second, 155 } 156 response, err := client.Do(request) 157 return response, err 158 } 159 160 func TestNotifyPostgres(t *testing.T) { 161 // Variables 162 asserter := assert.New(t) 163 164 // Test 165 response, err := NotifyPostgres() 166 finalResponse := inspectHTTPResponse(response) 167 asserter.Nil(err) 168 if err != nil { 169 log.Println(err) 170 asserter.Fail(finalResponse) 171 return 172 } 173 if response != nil { 174 asserter.Equal(200, response.StatusCode, finalResponse) 175 } 176 } 177 178 func TestRestartService(t *testing.T) { 179 asserter := assert.New(t) 180 restartResponse, restartError := RestartService() 181 asserter.Nil(restartError) 182 if restartError != nil { 183 log.Println(restartError) 184 return 185 } 186 addObjRsp := inspectHTTPResponse(restartResponse) 187 if restartResponse != nil { 188 asserter.Equal( 189 204, 190 restartResponse.StatusCode, 191 addObjRsp, 192 ) 193 } 194 } 195 196 func ListPoliciesWithBucket(bucketName string) (*http.Response, error) { 197 /* 198 Helper function to List Policies With Given Bucket 199 HTTP Verb: GET 200 URL: /bucket-policy/{bucket} 201 */ 202 request, err := http.NewRequest( 203 "GET", "http://localhost:9090/api/v1/bucket-policy/"+bucketName, nil) 204 if err != nil { 205 log.Println(err) 206 } 207 request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) 208 request.Header.Add("Content-Type", "application/json") 209 client := &http.Client{ 210 Timeout: 2 * time.Second, 211 } 212 response, err := client.Do(request) 213 return response, err 214 } 215 216 func TestListPoliciesWithBucket(t *testing.T) { 217 // Test Variables 218 bucketName := "testlistpolicieswithbucket" 219 asserter := assert.New(t) 220 221 // Test 222 response, err := ListPoliciesWithBucket(bucketName) 223 asserter.Nil(err) 224 if err != nil { 225 log.Println(err) 226 return 227 } 228 parsedResponse := inspectHTTPResponse(response) 229 if response != nil { 230 asserter.Equal( 231 200, 232 response.StatusCode, 233 parsedResponse, 234 ) 235 } 236 } 237 238 func ListUsersWithAccessToBucket(bucketName string) (*http.Response, error) { 239 /* 240 Helper function to List Users With Access to a Given Bucket 241 HTTP Verb: GET 242 URL: /bucket-users/{bucket} 243 */ 244 request, err := http.NewRequest( 245 "GET", "http://localhost:9090/api/v1/bucket-users/"+bucketName, nil) 246 if err != nil { 247 log.Println(err) 248 } 249 request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) 250 request.Header.Add("Content-Type", "application/json") 251 client := &http.Client{ 252 Timeout: 2 * time.Second, 253 } 254 response, err := client.Do(request) 255 return response, err 256 } 257 258 func TestListUsersWithAccessToBucket(t *testing.T) { 259 // Test Variables 260 bucketName := "testlistuserswithaccesstobucket1" 261 asserter := assert.New(t) 262 263 // Test 264 response, err := ListUsersWithAccessToBucket(bucketName) 265 asserter.Nil(err) 266 if err != nil { 267 log.Println(err) 268 return 269 } 270 parsedResponse := inspectHTTPResponse(response) 271 if response != nil { 272 asserter.Equal( 273 200, 274 response.StatusCode, 275 parsedResponse, 276 ) 277 } 278 } 279 280 func TestGetNodes(t *testing.T) { 281 asserter := assert.New(t) 282 getNodesResponse, getNodesError := GetNodes() 283 asserter.Nil(getNodesError) 284 if getNodesError != nil { 285 log.Println(getNodesError) 286 return 287 } 288 addObjRsp := inspectHTTPResponse(getNodesResponse) 289 if getNodesResponse != nil { 290 asserter.Equal( 291 200, 292 getNodesResponse.StatusCode, 293 addObjRsp, 294 ) 295 } 296 } 297 298 func ArnList() (*http.Response, error) { 299 /* 300 Helper function to get arn list 301 HTTP Verb: GET 302 URL: /api/v1/admin/arns 303 */ 304 request, err := http.NewRequest( 305 "GET", "http://localhost:9090/api/v1/admin/arns", nil) 306 if err != nil { 307 log.Println(err) 308 } 309 request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) 310 request.Header.Add("Content-Type", "application/json") 311 client := &http.Client{ 312 Timeout: 2 * time.Second, 313 } 314 response, err := client.Do(request) 315 return response, err 316 } 317 318 func TestArnList(t *testing.T) { 319 asserter := assert.New(t) 320 resp, err := ArnList() 321 asserter.Nil(err) 322 if err != nil { 323 log.Println(err) 324 return 325 } 326 objRsp := inspectHTTPResponse(resp) 327 if resp != nil { 328 asserter.Equal( 329 200, 330 resp.StatusCode, 331 objRsp, 332 ) 333 } 334 } 335 336 func ExportConfig() (*http.Response, error) { 337 request, err := http.NewRequest( 338 "GET", "http://localhost:9090/api/v1/configs/export", nil) 339 if err != nil { 340 log.Println(err) 341 } 342 request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) 343 client := &http.Client{ 344 Timeout: 2 * time.Second, 345 } 346 response, err := client.Do(request) 347 return response, err 348 } 349 350 func ImportConfig() (*http.Response, error) { 351 body := &bytes.Buffer{} 352 writer := multipart.NewWriter(body) 353 formFile, _ := writer.CreateFormFile("file", "sample-import-config.txt") 354 fileDir, _ := os.Getwd() 355 fileName := "sample-import-config.txt" 356 filePath := path.Join(fileDir, fileName) 357 file, _ := os.Open(filePath) 358 io.Copy(formFile, file) 359 writer.Close() 360 request, err := http.NewRequest( 361 "POST", "http://localhost:9090/api/v1/configs/import", 362 bytes.NewReader(body.Bytes()), 363 ) 364 if err != nil { 365 log.Println(err) 366 } 367 request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) 368 request.Header.Set("Content-Type", writer.FormDataContentType()) 369 370 client := &http.Client{ 371 Timeout: 2 * time.Second, 372 } 373 374 rsp, _ := client.Do(request) 375 if rsp.StatusCode != http.StatusOK { 376 log.Printf("Request failed with response code: %d", rsp.StatusCode) 377 } 378 return rsp, err 379 } 380 381 func TestExportConfig(t *testing.T) { 382 asserter := assert.New(t) 383 resp, err := ExportConfig() 384 asserter.Nil(err) 385 objRsp := inspectHTTPResponse(resp) 386 if resp != nil { 387 asserter.Equal( 388 200, 389 resp.StatusCode, 390 objRsp, 391 ) 392 } 393 } 394 395 func TestImportConfig(t *testing.T) { 396 asserter := assert.New(t) 397 resp, err := ImportConfig() 398 asserter.Nil(err) 399 objRsp := inspectHTTPResponse(resp) 400 if resp != nil { 401 asserter.Equal( 402 200, 403 resp.StatusCode, 404 objRsp, 405 ) 406 } 407 }