github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearch_secure_integration_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  //
     3  // The OpenSearch Contributors require contributions made to
     4  // this file be licensed under the Apache-2.0 license or a
     5  // compatible open source license.
     6  //
     7  // Modifications Copyright OpenSearch Contributors. See
     8  // GitHub history for details.
     9  
    10  // +build integration,secure
    11  
    12  package opensearch_test
    13  
    14  import (
    15  	"crypto/tls"
    16  	"encoding/json"
    17  	"log"
    18  	"net/http"
    19  	"testing"
    20  
    21  	"github.com/opensearch-project/opensearch-go/v2"
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func getSecuredClient() (*opensearch.Client, error) {
    26  
    27  	return opensearch.NewClient(opensearch.Config{
    28  		Transport: &http.Transport{
    29  			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    30  		},
    31  		Addresses: []string{"https://localhost:9200"},
    32  		Username:  "admin",
    33  		Password:  "admin",
    34  	})
    35  }
    36  
    37  type clusterVersion struct {
    38  	Number       string `json:"number"`
    39  	BuildFlavor  string `json:"build_flavor"`
    40  	Distribution string `json:"distribution"`
    41  }
    42  
    43  type Info struct {
    44  	Version clusterVersion `json:"version"`
    45  	Tagline string         `json:"tagline"`
    46  }
    47  
    48  func TestSecuredClientAPI(t *testing.T) {
    49  	t.Run("Check Info", func(t *testing.T) {
    50  		client, err := getSecuredClient()
    51  		if err != nil {
    52  			log.Fatalf("Error creating the client: %s\n", err)
    53  		}
    54  		res, err := client.Info()
    55  		if err != nil {
    56  			log.Fatalf("Error getting the response: %s\n", err)
    57  		}
    58  		defer res.Body.Close()
    59  
    60  		var infoResponse Info
    61  		err = json.NewDecoder(res.Body).Decode(&infoResponse)
    62  		if err != nil {
    63  			log.Fatalf("Error parsing the response: %s\n", err)
    64  		}
    65  		assert.True(t, len(infoResponse.Version.Number) > 0, "version number should not be empty")
    66  		assert.True(t, len(infoResponse.Tagline) > 0, "tagline should not be empty")
    67  		assert.True(t, len(infoResponse.Version.Distribution) > 0 || len(infoResponse.Version.BuildFlavor) > 0,
    68  			"Either distribution or build flavor should not be empty")
    69  	})
    70  }