github.com/minio/console@v1.4.1/replication/replication_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 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  package replication
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"fmt"
    23  	"log"
    24  	"net/http"
    25  	"os"
    26  	"strconv"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/go-openapi/loads"
    31  	"github.com/minio/console/api"
    32  	"github.com/minio/console/api/operations"
    33  )
    34  
    35  var token string
    36  
    37  func initConsoleServer() (*api.Server, error) {
    38  	// os.Setenv("CONSOLE_MINIO_SERVER", "localhost:9000")
    39  
    40  	swaggerSpec, err := loads.Embedded(api.SwaggerJSON, api.FlatSwaggerJSON)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	noLog := func(string, ...interface{}) {
    46  		// nothing to log
    47  	}
    48  
    49  	// Initialize MinIO loggers
    50  	api.LogInfo = noLog
    51  	api.LogError = noLog
    52  
    53  	consoleAPI := operations.NewConsoleAPI(swaggerSpec)
    54  	consoleAPI.Logger = noLog
    55  
    56  	server := api.NewServer(consoleAPI)
    57  	// register all APIs
    58  	server.ConfigureAPI()
    59  
    60  	// api.GlobalRootCAs, api.GlobalPublicCerts, api.GlobalTLSCertsManager = globalRootCAs, globalPublicCerts, globalTLSCerts
    61  
    62  	consolePort, _ := strconv.Atoi("9090")
    63  
    64  	server.Host = "0.0.0.0"
    65  	server.Port = consolePort
    66  	api.Port = "9090"
    67  	api.Hostname = "0.0.0.0"
    68  
    69  	return server, nil
    70  }
    71  
    72  func TestMain(m *testing.M) {
    73  	// start console server
    74  	go func() {
    75  		fmt.Println("start server")
    76  		srv, err := initConsoleServer()
    77  		if err != nil {
    78  			log.Println(err)
    79  			log.Println("init fail")
    80  			return
    81  		}
    82  		srv.Serve()
    83  	}()
    84  
    85  	fmt.Println("sleeping")
    86  	time.Sleep(2 * time.Second)
    87  
    88  	client := &http.Client{
    89  		Timeout: 2 * time.Second,
    90  	}
    91  	// get login credentials
    92  
    93  	requestData := map[string]string{
    94  		"accessKey": "minioadmin",
    95  		"secretKey": "minioadmin",
    96  	}
    97  
    98  	requestDataJSON, _ := json.Marshal(requestData)
    99  
   100  	requestDataBody := bytes.NewReader(requestDataJSON)
   101  
   102  	request, err := http.NewRequest("POST", "http://localhost:9090/api/v1/login", requestDataBody)
   103  	if err != nil {
   104  		log.Println(err)
   105  		return
   106  	}
   107  
   108  	request.Header.Add("Content-Type", "application/json")
   109  
   110  	response, err := client.Do(request)
   111  	if err != nil {
   112  		log.Println(err)
   113  		return
   114  	}
   115  
   116  	if response != nil {
   117  		for _, cookie := range response.Cookies() {
   118  			if cookie.Name == "token" {
   119  				token = cookie.Value
   120  				break
   121  			}
   122  		}
   123  	}
   124  
   125  	if token == "" {
   126  		log.Println("authentication token not found in cookies response")
   127  		return
   128  	}
   129  
   130  	code := m.Run()
   131  	os.Exit(code)
   132  }