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

     1  // Copyright 2017 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 librato
    16  
    17  import (
    18  	"net/http/httptest"
    19  	"net/url"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	util "k8s.io/client-go/util/testing"
    24  )
    25  
    26  func TestLibratoClientWrite(t *testing.T) {
    27  	handler := util.FakeHandler{
    28  		StatusCode:   200,
    29  		ResponseBody: "",
    30  		T:            t,
    31  	}
    32  	server := httptest.NewServer(&handler)
    33  	defer server.Close()
    34  
    35  	stubLibratoURL, err := url.Parse("?username=stub&token=stub&api=" + server.URL)
    36  
    37  	assert.NoError(t, err)
    38  
    39  	config, err := BuildConfig(stubLibratoURL)
    40  
    41  	assert.NoError(t, err)
    42  
    43  	client := NewClient(*config)
    44  
    45  	err = client.Write([]Measurement{
    46  		{
    47  			Name:  "test",
    48  			Value: 1.4,
    49  		},
    50  	})
    51  
    52  	assert.NoError(t, err)
    53  
    54  	handler.ValidateRequestCount(t, 1)
    55  
    56  	expectedBody := `{"measurements":[{"name":"test","value":1.4}]}`
    57  
    58  	handler.ValidateRequest(t, "/v1/measurements", "POST", &expectedBody)
    59  }
    60  
    61  func TestLibratoClientWriteWithTags(t *testing.T) {
    62  	handler := util.FakeHandler{
    63  		StatusCode:   200,
    64  		ResponseBody: "",
    65  		T:            t,
    66  	}
    67  	server := httptest.NewServer(&handler)
    68  	defer server.Close()
    69  
    70  	stubLibratoURL, err := url.Parse("?username=stub&token=stub&tags=a,b&tag_a=test&api=" + server.URL)
    71  
    72  	assert.NoError(t, err)
    73  
    74  	config, err := BuildConfig(stubLibratoURL)
    75  
    76  	assert.NoError(t, err)
    77  
    78  	client := NewClient(*config)
    79  
    80  	err = client.Write([]Measurement{
    81  		{
    82  			Name:  "test",
    83  			Value: 1.4,
    84  			Tags: map[string]string{
    85  				"test": "tag",
    86  			},
    87  		},
    88  	})
    89  
    90  	assert.NoError(t, err)
    91  
    92  	handler.ValidateRequestCount(t, 1)
    93  
    94  	expectedBody := `{"tags":{"a":"test"},"measurements":[{"name":"test","value":1.4,"tags":{"test":"tag"}}]}`
    95  
    96  	handler.ValidateRequest(t, "/v1/measurements", "POST", &expectedBody)
    97  }