Site replication API (#1773)

Site replication API
tests in CI/CL environment
This commit is contained in:
Prakash Senthil Vel
2022-03-31 17:11:01 +00:00
committed by GitHub
parent fba9bd87df
commit 2eecabf5e6
35 changed files with 4037 additions and 1 deletions

View File

@@ -0,0 +1,205 @@
// This file is part of MinIO Console Server
// Copyright (c) 2022 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// These tests are for AdminAPI Tag based on swagger-console.yml
package replication
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"testing"
"time"
"github.com/minio/console/models"
"github.com/stretchr/testify/assert"
)
const apiURL = "http://localhost:9090/api/v1/admin/site-replication"
func makeExecuteReq(method string, body io.Reader) (*http.Response, error) {
client := &http.Client{
Timeout: 10 * time.Second,
}
request, err := http.NewRequest(
method,
apiURL,
body,
)
if err != nil {
return nil, err
}
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
request.Header.Add("Content-Type", "application/json")
response, err := client.Do(request)
if err != nil {
return nil, err
}
return response, nil
}
func AddSiteReplicationInfo() (*http.Response, error) {
sites := make([]map[string]interface{}, 2)
sites[0] = map[string]interface{}{
"accessKey": "minioadmin",
"endpoint": "http://localhost:9000",
"secretKey": "minioadmin",
"name": "sitellhost9000",
}
sites[1] = map[string]interface{}{
"accessKey": "minioadmin",
"endpoint": "http://minio1:9001",
"secretKey": "minioadmin",
"name": "sitellhost9001",
}
requestDataJSON, _ := json.Marshal(sites)
requestDataBody := bytes.NewReader(requestDataJSON)
return makeExecuteReq("POST", requestDataBody)
}
func EditSiteReplicationInfo() (*http.Response, error) {
getResponse, err := makeExecuteReq("GET", nil)
//defer response.Body.Close()
if err != nil {
return nil, err
}
getResObj := &models.SiteReplicationInfoResponse{}
json.NewDecoder(getResponse.Body).Decode(getResObj)
//fmt.Println("Edit Got::", getResObj, getResObj.Sites[0], getResObj.Sites[1])
var secondDeploymentID string
if getResObj != nil {
if len(getResObj.Sites) > 0 {
secondDeploymentID = getResObj.Sites[1].DeploymentID
}
} else {
return nil, nil
}
fmt.Println("Editing::", getResObj.Sites[1])
fmt.Println("Editing::", secondDeploymentID)
pSiteInfo := map[string]interface{}{
"deploymentID": secondDeploymentID,
"endpoint": "http://minio2:9002",
"name": "sitellhost9002",
}
requestDataJSON, _ := json.Marshal(pSiteInfo)
requestDataBody := bytes.NewReader(requestDataJSON)
return makeExecuteReq("PUT", requestDataBody)
}
func DeleteSiteReplicationInfo() (*http.Response, error) {
delReq := map[string]interface{}{
"all": true,
"sites": []string{
"sitellhost9000",
"sitellhost9001",
"sitellhost9002",
},
}
requestDataJSON, _ := json.Marshal(delReq)
requestDataBody := bytes.NewReader(requestDataJSON)
return makeExecuteReq("DELETE", requestDataBody)
}
func TestGetSiteReplicationInfo(t *testing.T) {
assert := assert.New(t)
response, err := makeExecuteReq("GET", nil)
//defer response.Body.Close()
tgt := &models.SiteReplicationInfoResponse{}
json.NewDecoder(response.Body).Decode(tgt)
if err != nil {
log.Println(err)
return
}
if response != nil {
assert.Equal(200, response.StatusCode, "Status Code is incorrect")
}
}
func TestAddSiteReplicationInfo(t *testing.T) {
assert := assert.New(t)
fmt.Println("Add Site Replication")
response, err := AddSiteReplicationInfo()
if err != nil {
log.Println(err)
return
}
if response != nil {
assert.Equal(200, response.StatusCode, "Status Code is incorrect")
}
fmt.Println("TestAddSiteReplicationInfo: ", response.StatusCode)
}
func TestEditSiteReplicationInfo(t *testing.T) {
assert := assert.New(t)
response, err := EditSiteReplicationInfo()
if err != nil {
log.Println(err)
return
}
if response != nil {
assert.NotEmpty(response)
assert.Equal(200, response.StatusCode, "Status Code is incorrect")
}
}
func TestDeleteSiteReplicationInfo(t *testing.T) {
assert := assert.New(t)
fmt.Println("Delete Site Replication")
response, err := DeleteSiteReplicationInfo()
if err != nil {
log.Println(err)
return
}
if response != nil {
assert.Equal(204, response.StatusCode, "Status Code is incorrect")
}
fmt.Println("TestDeleteReplicationInfo: ", response.StatusCode)
}

View File

@@ -0,0 +1,136 @@
// This file is part of MinIO Console Server
// Copyright (c) 2022 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package replication
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"testing"
"time"
"github.com/go-openapi/loads"
"github.com/minio/console/restapi"
"github.com/minio/console/restapi/operations"
)
var token string
func initConsoleServer() (*restapi.Server, error) {
//os.Setenv("CONSOLE_MINIO_SERVER", "localhost:9000")
swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
if err != nil {
return nil, err
}
noLog := func(string, ...interface{}) {
// nothing to log
}
// Initialize MinIO loggers
restapi.LogInfo = noLog
restapi.LogError = noLog
api := operations.NewConsoleAPI(swaggerSpec)
api.Logger = noLog
server := restapi.NewServer(api)
// register all APIs
server.ConfigureAPI()
//restapi.GlobalRootCAs, restapi.GlobalPublicCerts, restapi.GlobalTLSCertsManager = globalRootCAs, globalPublicCerts, globalTLSCerts
consolePort, _ := strconv.Atoi("9090")
server.Host = "0.0.0.0"
server.Port = consolePort
restapi.Port = "9090"
restapi.Hostname = "0.0.0.0"
return server, nil
}
func TestMain(m *testing.M) {
// start console server
go func() {
fmt.Println("start server")
srv, err := initConsoleServer()
if err != nil {
log.Println(err)
log.Println("init fail")
return
}
srv.Serve()
}()
fmt.Println("sleeping")
time.Sleep(2 * time.Second)
client := &http.Client{
Timeout: 2 * time.Second,
}
// get login credentials
requestData := map[string]string{
"accessKey": "minioadmin",
"secretKey": "minioadmin",
}
requestDataJSON, _ := json.Marshal(requestData)
requestDataBody := bytes.NewReader(requestDataJSON)
request, err := http.NewRequest("POST", "http://localhost:9090/api/v1/login", requestDataBody)
if err != nil {
log.Println(err)
return
}
request.Header.Add("Content-Type", "application/json")
response, err := client.Do(request)
if err != nil {
log.Println(err)
return
}
if response != nil {
for _, cookie := range response.Cookies() {
if cookie.Name == "token" {
token = cookie.Value
break
}
}
}
if token == "" {
log.Println("authentication token not found in cookies response")
return
}
code := m.Run()
os.Exit(code)
}