github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/integration/e2e/service_test.go (about)

     1  // +build requires_docker
     2  
     3  package e2e
     4  
     5  import (
     6  	"math"
     7  	"net"
     8  	"net/http"
     9  	"strconv"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/grafana/dskit/backoff"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestWaitSumMetric(t *testing.T) {
    19  	// Listen on a random port before starting the HTTP server, to
    20  	// make sure the port is already open when we'll call WaitSumMetric()
    21  	// the first time (this avoid flaky tests).
    22  	ln, err := net.Listen("tcp", "localhost:0")
    23  	require.NoError(t, err)
    24  	defer ln.Close()
    25  
    26  	// Get the port.
    27  	_, addrPort, err := net.SplitHostPort(ln.Addr().String())
    28  	require.NoError(t, err)
    29  
    30  	port, err := strconv.Atoi(addrPort)
    31  	require.NoError(t, err)
    32  
    33  	// Start an HTTP server exposing the metrics.
    34  	srv := &http.Server{
    35  		Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    36  			_, _ = w.Write([]byte(`
    37  # HELP metric_c cheescake
    38  # TYPE metric_c gauge
    39  metric_c 20
    40  # HELP metric_a cheescake
    41  # TYPE metric_a gauge
    42  metric_a 1
    43  metric_a{first="value1"} 10
    44  metric_a{first="value1", something="x"} 4
    45  metric_a{first="value1", something2="a"} 203
    46  metric_a{first="value2"} 2
    47  metric_a{second="value1"} 1
    48  # HELP metric_b cheescake
    49  # TYPE metric_b gauge
    50  metric_b 1000
    51  # HELP metric_b_counter cheescake
    52  # TYPE metric_b_counter counter
    53  metric_b_counter 1020
    54  # HELP metric_b_hist cheescake
    55  # TYPE metric_b_hist histogram
    56  metric_b_hist_count 5
    57  metric_b_hist_sum 124
    58  metric_b_hist_bucket{le="5.36870912e+08"} 1
    59  metric_b_hist_bucket{le="+Inf"} 5
    60  # HELP metric_b_summary cheescake
    61  # TYPE metric_b_summary summary
    62  metric_b_summary_sum 22
    63  metric_b_summary_count 1
    64  `))
    65  		}),
    66  	}
    67  	defer srv.Close()
    68  
    69  	go func() {
    70  		_ = srv.Serve(ln)
    71  	}()
    72  
    73  	s := &HTTPService{
    74  		httpPort: 0,
    75  		ConcreteService: &ConcreteService{
    76  			networkPortsContainerToLocal: map[int]int{
    77  				0: port,
    78  			},
    79  		},
    80  	}
    81  
    82  	s.SetBackoff(backoff.Config{
    83  		MinBackoff: 300 * time.Millisecond,
    84  		MaxBackoff: 600 * time.Millisecond,
    85  		MaxRetries: 50,
    86  	})
    87  	require.NoError(t, s.WaitSumMetrics(Equals(221), "metric_a"))
    88  
    89  	// No retry.
    90  	s.SetBackoff(backoff.Config{
    91  		MinBackoff: 0,
    92  		MaxBackoff: 0,
    93  		MaxRetries: 1,
    94  	})
    95  	require.Error(t, s.WaitSumMetrics(Equals(16), "metric_a"))
    96  
    97  	require.NoError(t, s.WaitSumMetrics(Equals(1000), "metric_b"))
    98  	require.NoError(t, s.WaitSumMetrics(Equals(1020), "metric_b_counter"))
    99  	require.NoError(t, s.WaitSumMetrics(Equals(124), "metric_b_hist"))
   100  	require.NoError(t, s.WaitSumMetrics(Equals(22), "metric_b_summary"))
   101  
   102  	require.NoError(t, s.WaitSumMetrics(EqualsAmongTwo, "metric_a", "metric_a"))
   103  	require.Error(t, s.WaitSumMetrics(EqualsAmongTwo, "metric_a", "metric_b"))
   104  
   105  	require.NoError(t, s.WaitSumMetrics(GreaterAmongTwo, "metric_b", "metric_a"))
   106  	require.Error(t, s.WaitSumMetrics(GreaterAmongTwo, "metric_a", "metric_b"))
   107  
   108  	require.NoError(t, s.WaitSumMetrics(LessAmongTwo, "metric_a", "metric_b"))
   109  	require.Error(t, s.WaitSumMetrics(LessAmongTwo, "metric_b", "metric_a"))
   110  
   111  	require.Error(t, s.WaitSumMetrics(Equals(0), "non_existing_metric"))
   112  }
   113  
   114  func TestWaitSumMetric_Nan(t *testing.T) {
   115  	// Listen on a random port before starting the HTTP server, to
   116  	// make sure the port is already open when we'll call WaitSumMetric()
   117  	// the first time (this avoid flaky tests).
   118  	ln, err := net.Listen("tcp", "localhost:0")
   119  	require.NoError(t, err)
   120  	defer ln.Close()
   121  
   122  	// Get the port.
   123  	_, addrPort, err := net.SplitHostPort(ln.Addr().String())
   124  	require.NoError(t, err)
   125  
   126  	port, err := strconv.Atoi(addrPort)
   127  	require.NoError(t, err)
   128  
   129  	// Start an HTTP server exposing the metrics.
   130  	srv := &http.Server{
   131  		Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   132  			_, _ = w.Write([]byte(`
   133  # HELP metric_c cheescake
   134  # TYPE metric_c GAUGE
   135  metric_c 20
   136  # HELP metric_a cheescake
   137  # TYPE metric_a GAUGE
   138  metric_a 1
   139  metric_a{first="value1"} 10
   140  metric_a{first="value1", something="x"} 4
   141  metric_a{first="value1", something2="a"} 203
   142  metric_a{first="value1", something3="b"} Nan
   143  metric_a{first="value2"} 2
   144  metric_a{second="value1"} 1
   145  # HELP metric_b cheescake
   146  # TYPE metric_b GAUGE
   147  metric_b 1000
   148  `))
   149  		}),
   150  	}
   151  	defer srv.Close()
   152  
   153  	go func() {
   154  		_ = srv.Serve(ln)
   155  	}()
   156  
   157  	s := &HTTPService{
   158  		httpPort: 0,
   159  		ConcreteService: &ConcreteService{
   160  			networkPortsContainerToLocal: map[int]int{
   161  				0: port,
   162  			},
   163  		},
   164  	}
   165  
   166  	s.SetBackoff(backoff.Config{
   167  		MinBackoff: 300 * time.Millisecond,
   168  		MaxBackoff: 600 * time.Millisecond,
   169  		MaxRetries: 50,
   170  	})
   171  	require.NoError(t, s.WaitSumMetrics(Equals(math.NaN()), "metric_a"))
   172  }
   173  
   174  func TestParseDockerPort(t *testing.T) {
   175  	_, err := parseDockerIPv4Port("")
   176  	assert.Error(t, err)
   177  
   178  	actual, err := parseDockerIPv4Port("0.0.0.0:36999")
   179  	assert.NoError(t, err)
   180  	assert.Equal(t, 36999, actual)
   181  
   182  	actual, err = parseDockerIPv4Port("0.0.0.0:49155\n:::49156")
   183  	assert.NoError(t, err)
   184  	assert.Equal(t, 49155, actual)
   185  
   186  	actual, err = parseDockerIPv4Port(":::49156\n0.0.0.0:49155")
   187  	assert.NoError(t, err)
   188  	assert.Equal(t, 49155, actual)
   189  }