github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/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  	"gopkg.in/olivere/elastic.v3"
    25  )
    26  
    27  func TestCreateElasticSearchService(t *testing.T) {
    28  	clusterName := "sandbox"
    29  	esURI := fmt.Sprintf("?nodes=https://foo.com:20468&nodes=https://bar.com:20468&"+
    30  		"esUserName=test&esUserSecret=password&maxRetries=10&startupHealthcheckTimeout=30&"+
    31  		"sniff=false&healthCheck=false&cluster_name=%s", clusterName)
    32  
    33  	url, err := url.Parse(esURI)
    34  	if err != nil {
    35  		t.Fatalf("Error when parsing URL: %s", err.Error())
    36  	}
    37  
    38  	esSvc, err := CreateElasticSearchService(url)
    39  	if err != nil {
    40  		t.Fatalf("Error when creating config: %s", err.Error())
    41  	}
    42  
    43  	expectedClient, err := elastic.NewClient(
    44  		elastic.SetURL("https://foo.com:20468", "https://bar.com:20468"),
    45  		elastic.SetBasicAuth("test", "password"),
    46  		elastic.SetMaxRetries(10),
    47  		elastic.SetHealthcheckTimeoutStartup(30*time.Second),
    48  		elastic.SetSniff(false), elastic.SetHealthcheck(false))
    49  
    50  	if err != nil {
    51  		t.Fatalf("Error when creating client: %s", err.Error())
    52  	}
    53  
    54  	actualClientRefl := reflect.ValueOf(esSvc.EsClient).Elem()
    55  	expectedClientRefl := reflect.ValueOf(expectedClient).Elem()
    56  
    57  	if actualClientRefl.FieldByName("basicAuthUsername").String() != expectedClientRefl.FieldByName("basicAuthUsername").String() {
    58  		t.Fatal("basicAuthUsername is not equal")
    59  	}
    60  	if actualClientRefl.FieldByName("basicAuthUsername").String() != expectedClientRefl.FieldByName("basicAuthUsername").String() {
    61  		t.Fatal("basicAuthUsername is not equal")
    62  	}
    63  	if actualClientRefl.FieldByName("maxRetries").Int() != expectedClientRefl.FieldByName("maxRetries").Int() {
    64  		t.Fatal("maxRetries is not equal")
    65  	}
    66  	if actualClientRefl.FieldByName("healthcheckTimeoutStartup").Int() != expectedClientRefl.FieldByName("healthcheckTimeoutStartup").Int() {
    67  		t.Fatal("healthcheckTimeoutStartup is not equal")
    68  	}
    69  	if actualClientRefl.FieldByName("snifferEnabled").Bool() != expectedClientRefl.FieldByName("snifferEnabled").Bool() {
    70  		t.Fatal("snifferEnabled is not equal")
    71  	}
    72  	if actualClientRefl.FieldByName("healthcheckEnabled").Bool() != expectedClientRefl.FieldByName("healthcheckEnabled").Bool() {
    73  		t.Fatal("healthcheckEnabled is not equal")
    74  	}
    75  	if esSvc.ClusterName != clusterName {
    76  		t.Fatal("cluster name is not equal")
    77  	}
    78  }
    79  
    80  func TestCreateElasticSearchServiceForDefaultClusterName(t *testing.T) {
    81  	esURI := "?nodes=https://foo.com:20468&nodes=https://bar.com:20468&" +
    82  		"esUserName=test&esUserSecret=password&maxRetries=10&startupHealthcheckTimeout=30&" +
    83  		"sniff=false&healthCheck=false"
    84  
    85  	url, err := url.Parse(esURI)
    86  	if err != nil {
    87  		t.Fatalf("Error when parsing URL: %s", err.Error())
    88  	}
    89  
    90  	esSvc, err := CreateElasticSearchService(url)
    91  	if err != nil {
    92  		t.Fatalf("Error when creating config: %s", err.Error())
    93  	}
    94  
    95  	if esSvc.ClusterName != ESClusterName {
    96  		t.Fatalf("cluster name is not equal. Expected: %s, Got: %s", ESClusterName, esSvc.ClusterName)
    97  	}
    98  }