github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/common/honeycomb/honeycomb_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 honeycomb
    16  
    17  import (
    18  	"net/http/httptest"
    19  	"net/url"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	util "k8s.io/client-go/util/testing"
    25  )
    26  
    27  func TestHoneycombClientWrite(t *testing.T) {
    28  	handler := util.FakeHandler{
    29  		StatusCode:   202,
    30  		ResponseBody: "",
    31  		T:            t,
    32  	}
    33  	server := httptest.NewServer(&handler)
    34  	defer server.Close()
    35  
    36  	stubURL, err := url.Parse("?writekey=testkey&dataset=testdataset&apihost=" + server.URL)
    37  
    38  	assert.NoError(t, err)
    39  
    40  	config, err := BuildConfig(stubURL)
    41  
    42  	assert.Equal(t, config.WriteKey, "testkey")
    43  	assert.Equal(t, config.APIHost, server.URL)
    44  	assert.Equal(t, config.Dataset, "testdataset")
    45  
    46  	assert.NoError(t, err)
    47  
    48  	client, _ := NewClient(stubURL)
    49  
    50  	testBatchPoint := &BatchPoint{
    51  		Data:      "test",
    52  		Timestamp: time.Now(),
    53  	}
    54  
    55  	err = client.SendBatch([]*BatchPoint{testBatchPoint})
    56  
    57  	assert.NoError(t, err)
    58  
    59  	batch2 := []*BatchPoint{}
    60  
    61  	for i := 0; i < maxBatchSize*2; i++ {
    62  		batch2 = append(batch2, testBatchPoint)
    63  	}
    64  
    65  	err = client.SendBatch(batch2)
    66  
    67  	assert.NoError(t, err)
    68  
    69  	handler.ValidateRequestCount(t, 3)
    70  }