github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/common/elasticsearch/elasticsearch_test.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package elasticsearch
    16  
    17  import (
    18  	"net/url"
    19  	"reflect"
    20  	"testing"
    21  	"time"
    22  
    23  	"fmt"
    24  	elastic2 "gopkg.in/olivere/elastic.v3"
    25  	elastic5 "gopkg.in/olivere/elastic.v5"
    26  )
    27  
    28  func TestCreateElasticSearchServiceV2(t *testing.T) {
    29  	clusterName := "sandbox"
    30  	esURI := fmt.Sprintf("?nodes=https://foo.com:20468&nodes=https://bar.com:20468&"+
    31  		"esUserName=test&esUserSecret=password&maxRetries=10&startupHealthcheckTimeout=30&"+
    32  		"sniff=false&healthCheck=false&ver=2&cluster_name=%s", clusterName)
    33  
    34  	url, err := url.Parse(esURI)
    35  	if err != nil {
    36  		t.Fatalf("Error when parsing URL: %s", err.Error())
    37  	}
    38  
    39  	esSvc, err := CreateElasticSearchService(url)
    40  	if err != nil {
    41  		t.Fatalf("Error when creating config: %s", err.Error())
    42  	}
    43  
    44  	expectedClient, err := elastic2.NewClient(
    45  		elastic2.SetURL("https://foo.com:20468", "https://bar.com:20468"),
    46  		elastic2.SetBasicAuth("test", "password"),
    47  		elastic2.SetMaxRetries(10),
    48  		elastic2.SetHealthcheckTimeoutStartup(30*time.Second),
    49  		elastic2.SetSniff(false), elastic2.SetHealthcheck(false))
    50  
    51  	if err != nil {
    52  		t.Fatalf("Error when creating client: %s", err.Error())
    53  	}
    54  
    55  	actualClientRefl := reflect.ValueOf(esSvc.EsClient).Elem().FieldByName("clientV2").Elem()
    56  	expectedClientRefl := reflect.ValueOf(expectedClient).Elem()
    57  
    58  	if actualClientRefl.FieldByName("basicAuthUsername").String() != expectedClientRefl.FieldByName("basicAuthUsername").String() {
    59  		t.Fatal("basicAuthUsername is not equal")
    60  	}
    61  	if actualClientRefl.FieldByName("basicAuthUsername").String() != expectedClientRefl.FieldByName("basicAuthUsername").String() {
    62  		t.Fatal("basicAuthUsername is not equal")
    63  	}
    64  	if actualClientRefl.FieldByName("maxRetries").Int() != expectedClientRefl.FieldByName("maxRetries").Int() {
    65  		t.Fatal("maxRetries is not equal")
    66  	}
    67  	if actualClientRefl.FieldByName("healthcheckTimeoutStartup").Int() != expectedClientRefl.FieldByName("healthcheckTimeoutStartup").Int() {
    68  		t.Fatal("healthcheckTimeoutStartup is not equal")
    69  	}
    70  	if actualClientRefl.FieldByName("snifferEnabled").Bool() != expectedClientRefl.FieldByName("snifferEnabled").Bool() {
    71  		t.Fatal("snifferEnabled is not equal")
    72  	}
    73  	if actualClientRefl.FieldByName("healthcheckEnabled").Bool() != expectedClientRefl.FieldByName("healthcheckEnabled").Bool() {
    74  		t.Fatal("healthcheckEnabled is not equal")
    75  	}
    76  	if esSvc.ClusterName != clusterName {
    77  		t.Fatal("cluster name is not equal")
    78  	}
    79  }
    80  func TestCreateElasticSearchServiceV5(t *testing.T) {
    81  	clusterName := "sandbox"
    82  	esURI := fmt.Sprintf("?nodes=https://foo.com:20468&nodes=https://bar.com:20468&"+
    83  		"esUserName=test&esUserSecret=password&startupHealthcheckTimeout=30&"+
    84  		"sniff=false&healthCheck=false&ver=5&cluster_name=%s", clusterName)
    85  
    86  	url, err := url.Parse(esURI)
    87  	if err != nil {
    88  		t.Fatalf("Error when parsing URL: %s", err.Error())
    89  	}
    90  
    91  	esSvc, err := CreateElasticSearchService(url)
    92  	if err != nil {
    93  		t.Fatalf("Error when creating config: %s", err.Error())
    94  	}
    95  
    96  	expectedClient, err := elastic5.NewClient(
    97  		elastic5.SetURL("https://foo.com:20468", "https://bar.com:20468"),
    98  		elastic5.SetBasicAuth("test", "password"),
    99  		elastic5.SetHealthcheckTimeoutStartup(30*time.Second),
   100  		elastic5.SetSniff(false), elastic5.SetHealthcheck(false))
   101  
   102  	if err != nil {
   103  		t.Fatalf("Error when creating client: %s", err.Error())
   104  	}
   105  
   106  	actualClientRefl := reflect.ValueOf(esSvc.EsClient).Elem().FieldByName("clientV5").Elem()
   107  	expectedClientRefl := reflect.ValueOf(expectedClient).Elem()
   108  
   109  	if actualClientRefl.FieldByName("basicAuthUsername").String() != expectedClientRefl.FieldByName("basicAuthUsername").String() {
   110  		t.Fatal("basicAuthUsername is not equal")
   111  	}
   112  	if actualClientRefl.FieldByName("basicAuthUsername").String() != expectedClientRefl.FieldByName("basicAuthUsername").String() {
   113  		t.Fatal("basicAuthUsername is not equal")
   114  	}
   115  	if actualClientRefl.FieldByName("healthcheckTimeoutStartup").Int() != expectedClientRefl.FieldByName("healthcheckTimeoutStartup").Int() {
   116  		t.Fatal("healthcheckTimeoutStartup is not equal")
   117  	}
   118  	if actualClientRefl.FieldByName("snifferEnabled").Bool() != expectedClientRefl.FieldByName("snifferEnabled").Bool() {
   119  		t.Fatal("snifferEnabled is not equal")
   120  	}
   121  	if actualClientRefl.FieldByName("healthcheckEnabled").Bool() != expectedClientRefl.FieldByName("healthcheckEnabled").Bool() {
   122  		t.Fatal("healthcheckEnabled is not equal")
   123  	}
   124  	if esSvc.ClusterName != clusterName {
   125  		t.Fatal("cluster name is not equal")
   126  	}
   127  }
   128  
   129  func TestCreateElasticSearchServiceForDefaultClusterName(t *testing.T) {
   130  	esURI := "?nodes=https://foo.com:20468&nodes=https://bar.com:20468&" +
   131  		"esUserName=test&esUserSecret=password&maxRetries=10&startupHealthcheckTimeout=30&" +
   132  		"sniff=false&healthCheck=false"
   133  
   134  	url, err := url.Parse(esURI)
   135  	if err != nil {
   136  		t.Fatalf("Error when parsing URL: %s", err.Error())
   137  	}
   138  
   139  	esSvc, err := CreateElasticSearchService(url)
   140  	if err != nil {
   141  		t.Fatalf("Error when creating config: %s", err.Error())
   142  	}
   143  
   144  	if esSvc.ClusterName != ESClusterName {
   145  		t.Fatalf("cluster name is not equal. Expected: %s, Got: %s", ESClusterName, esSvc.ClusterName)
   146  	}
   147  }
   148  
   149  func TestCreateElasticSearchServiceWithIngestPipeline(t *testing.T) {
   150  	const pipeline = "test"
   151  
   152  	esURI := "?nodes=https://foo.com:20468&nodes=https://bar.com:20468&" +
   153  		"pipeline=" + pipeline + "&" +
   154  		"sniff=false&healthCheck=false"
   155  
   156  	url, err := url.Parse(esURI)
   157  	if err != nil {
   158  		t.Fatalf("Error when parsing URL: %s", err.Error())
   159  	}
   160  
   161  	esSvc, err := CreateElasticSearchService(url)
   162  	if err != nil {
   163  		t.Fatalf("Error when creating config: %s", err.Error())
   164  	}
   165  
   166  	if esSvc.EsClient.pipeline != pipeline {
   167  		t.Fatalf("Ingest pipline is not equal. Expected: %s, Got: %s", pipeline, esSvc.EsClient.pipeline)
   168  	}
   169  
   170  	if esSvc.EsClient.version < 5 {
   171  		t.Fatalf("ElasticSearch client not using version 5+ (required for pipeline). Got: %d", esSvc.EsClient.version)
   172  	}
   173  }
   174  
   175  func TestCreateElasticSearchServiceSingleDnsEntrypointV5(t *testing.T) {
   176  	clusterName := "sandbox"
   177  	esURI := fmt.Sprintf("https://foo.com:9200?"+
   178  		"esUserName=test&esUserSecret=password&startupHealthcheckTimeout=30&"+
   179  		"sniff=false&healthCheck=false&cluster_name=%s", clusterName)
   180  
   181  	url, err := url.Parse(esURI)
   182  	if err != nil {
   183  		t.Fatalf("Error when parsing URL: %s", err.Error())
   184  	}
   185  
   186  	esSvc, err := CreateElasticSearchService(url)
   187  	if err != nil {
   188  		t.Fatalf("Error when creating config: %s", err.Error())
   189  	}
   190  
   191  	expectedClient, err := elastic5.NewClient(
   192  		elastic5.SetURL("https://foo.com:9200"),
   193  		elastic5.SetBasicAuth("test", "password"),
   194  		elastic5.SetHealthcheckTimeoutStartup(30*time.Second),
   195  		elastic5.SetSniff(false), elastic5.SetHealthcheck(false))
   196  
   197  	if err != nil {
   198  		t.Fatalf("Error when creating client: %s", err.Error())
   199  	}
   200  
   201  	actualClientRefl := reflect.ValueOf(esSvc.EsClient).Elem().FieldByName("clientV5").Elem()
   202  	expectedClientRefl := reflect.ValueOf(expectedClient).Elem()
   203  
   204  	if actualClientRefl.FieldByName("basicAuthUsername").String() != expectedClientRefl.FieldByName("basicAuthUsername").String() {
   205  		t.Fatal("basicAuthUsername is not equal")
   206  	}
   207  	if actualClientRefl.FieldByName("healthcheckTimeoutStartup").Int() != expectedClientRefl.FieldByName("healthcheckTimeoutStartup").Int() {
   208  		t.Fatal("healthcheckTimeoutStartup is not equal")
   209  	}
   210  	if actualClientRefl.FieldByName("snifferEnabled").Bool() != expectedClientRefl.FieldByName("snifferEnabled").Bool() {
   211  		t.Fatal("snifferEnabled is not equal")
   212  	}
   213  	if actualClientRefl.FieldByName("healthcheckEnabled").Bool() != expectedClientRefl.FieldByName("healthcheckEnabled").Bool() {
   214  		t.Fatal("healthcheckEnabled is not equal")
   215  	}
   216  	if esSvc.ClusterName != clusterName {
   217  		t.Fatal("cluster name is not equal")
   218  	}
   219  }
   220  func TestCreateElasticSearchServiceSingleDnsEntrypointV2(t *testing.T) {
   221  	clusterName := "sandbox"
   222  	esURI := fmt.Sprintf("https://foo.com:9200?"+
   223  		"esUserName=test&esUserSecret=password&startupHealthcheckTimeout=30&"+
   224  		"sniff=false&healthCheck=false&ver=2&cluster_name=%s", clusterName)
   225  
   226  	url, err := url.Parse(esURI)
   227  	if err != nil {
   228  		t.Fatalf("Error when parsing URL: %s", err.Error())
   229  	}
   230  	esSvc, err := CreateElasticSearchService(url)
   231  	if err != nil {
   232  		t.Fatalf("Error when creating config: %s", err.Error())
   233  	}
   234  
   235  	expectedClient, err := elastic2.NewClient(
   236  		elastic2.SetURL("https://foo.com:9200"),
   237  		elastic2.SetBasicAuth("test", "password"),
   238  		elastic2.SetHealthcheckTimeoutStartup(30*time.Second),
   239  		elastic2.SetSniff(false), elastic2.SetHealthcheck(false))
   240  
   241  	if err != nil {
   242  		t.Fatalf("Error when creating client: %s", err.Error())
   243  	}
   244  
   245  	actualClientRefl := reflect.ValueOf(esSvc.EsClient).Elem().FieldByName("clientV2").Elem()
   246  	expectedClientRefl := reflect.ValueOf(expectedClient).Elem()
   247  
   248  	if actualClientRefl.FieldByName("basicAuthUsername").String() != expectedClientRefl.FieldByName("basicAuthUsername").String() {
   249  		t.Fatal("basicAuthUsername is not equal")
   250  	}
   251  	if actualClientRefl.FieldByName("healthcheckTimeoutStartup").Int() != expectedClientRefl.FieldByName("healthcheckTimeoutStartup").Int() {
   252  		t.Fatal("healthcheckTimeoutStartup is not equal")
   253  	}
   254  	if actualClientRefl.FieldByName("snifferEnabled").Bool() != expectedClientRefl.FieldByName("snifferEnabled").Bool() {
   255  		t.Fatal("snifferEnabled is not equal")
   256  	}
   257  	if actualClientRefl.FieldByName("healthcheckEnabled").Bool() != expectedClientRefl.FieldByName("healthcheckEnabled").Bool() {
   258  		t.Fatal("healthcheckEnabled is not equal")
   259  	}
   260  	if esSvc.ClusterName != clusterName {
   261  		t.Fatal("cluster name is not equal")
   262  	}
   263  }