github.com/openshift/installer@v1.4.17/pkg/metrics/pushclient/pushclient_test.go (about)

     1  package pushclient
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/prometheus/client_golang/prometheus"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestPushMetricsToGateway(t *testing.T) {
    16  	promCollector := prometheus.NewCounter(
    17  		prometheus.CounterOpts{
    18  			Name:        "test",
    19  			Help:        "test metrics",
    20  			ConstLabels: map[string]string{"test1": "test1"},
    21  		},
    22  	)
    23  
    24  	promCollector.Add(10)
    25  
    26  	expectedOutput := `# HELP test test metrics
    27  # TYPE test counter
    28  test{test1="test1"} 10
    29  `
    30  
    31  	testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    32  		buf := new(strings.Builder)
    33  		_, err := io.Copy(buf, req.Body)
    34  		assert.NoError(t, err)
    35  		assert.EqualValues(t, expectedOutput, buf.String())
    36  		fmt.Println("all done")
    37  	}))
    38  	defer testServer.Close()
    39  
    40  	pushClient := PushClient{URL: testServer.URL, Client: &http.Client{}, JobName: "installer_metrics"}
    41  	pushClient.Push(promCollector)
    42  
    43  }
    44  
    45  func TestPushMetricsToAllGateway(t *testing.T) {
    46  	counter := prometheus.NewCounter(
    47  		prometheus.CounterOpts{
    48  			Name:        "test_counter",
    49  			Help:        "test metrics",
    50  			ConstLabels: map[string]string{"test1": "test1"},
    51  		},
    52  	)
    53  	counter.Add(10)
    54  
    55  	histogram := prometheus.NewHistogram(
    56  		prometheus.HistogramOpts{
    57  			Name:        "test_histogram",
    58  			Help:        "test metrics",
    59  			ConstLabels: map[string]string{"test1": "test1"},
    60  		},
    61  	)
    62  	histogram.Observe(10)
    63  
    64  	promCollector := []prometheus.Collector{counter, histogram}
    65  
    66  	expectedOutput := `# HELP test_counter test metrics
    67  # TYPE test_counter counter
    68  test_counter{test1="test1"} 10
    69  # HELP test_histogram test metrics
    70  # TYPE test_histogram histogram
    71  test_histogram_bucket{test1="test1",le="0.005"} 0
    72  test_histogram_bucket{test1="test1",le="0.01"} 0
    73  test_histogram_bucket{test1="test1",le="0.025"} 0
    74  test_histogram_bucket{test1="test1",le="0.05"} 0
    75  test_histogram_bucket{test1="test1",le="0.1"} 0
    76  test_histogram_bucket{test1="test1",le="0.25"} 0
    77  test_histogram_bucket{test1="test1",le="0.5"} 0
    78  test_histogram_bucket{test1="test1",le="1"} 0
    79  test_histogram_bucket{test1="test1",le="2.5"} 0
    80  test_histogram_bucket{test1="test1",le="5"} 0
    81  test_histogram_bucket{test1="test1",le="10"} 1
    82  test_histogram_bucket{test1="test1",le="+Inf"} 1
    83  test_histogram_sum{test1="test1"} 10
    84  test_histogram_count{test1="test1"} 1
    85  `
    86  
    87  	testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    88  		buf := new(strings.Builder)
    89  		_, err := io.Copy(buf, req.Body)
    90  		assert.NoError(t, err)
    91  		assert.EqualValues(t, expectedOutput, buf.String())
    92  		fmt.Println("all done")
    93  	}))
    94  	defer testServer.Close()
    95  	pushClient := PushClient{URL: testServer.URL, Client: &http.Client{}, JobName: "installer_metrics"}
    96  	err := pushClient.Push(promCollector...)
    97  	assert.NoError(t, err)
    98  }
    99  
   100  func TestPushMetricsBadServer(t *testing.T) {
   101  	counter := prometheus.NewCounter(
   102  		prometheus.CounterOpts{
   103  			Name:        "test_counter",
   104  			Help:        "test metrics",
   105  			ConstLabels: map[string]string{"test1": "test1"},
   106  		},
   107  	)
   108  	counter.Add(10)
   109  
   110  	promCollector := []prometheus.Collector{counter}
   111  
   112  	testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
   113  		http.Error(w, "test error message", http.StatusForbidden)
   114  	}))
   115  	defer testServer.Close()
   116  	pushClient := PushClient{URL: testServer.URL, Client: &http.Client{}, JobName: "installer_metrics"}
   117  	err := pushClient.Push(promCollector...)
   118  
   119  	errorMessage := fmt.Sprintf("failed to push metrics: unexpected status code 403 while pushing to %s/metrics/job/installer_metrics: test error message\n", testServer.URL)
   120  
   121  	if assert.Error(t, err) {
   122  		assert.EqualError(t, err, errorMessage)
   123  	}
   124  }