github.com/hashicorp/go-metrics@v0.5.3/circonus/circonus_test.go (about)

     1  package circonus
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/hashicorp/go-metrics"
    13  )
    14  
    15  func TestNewCirconusSink(t *testing.T) {
    16  
    17  	// test with invalid config (nil)
    18  	expectedError := errors.New("invalid check manager configuration (no API token AND no submission url)")
    19  	_, err := NewCirconusSink(nil)
    20  	if err == nil || !strings.Contains(err.Error(), expectedError.Error()) {
    21  		t.Errorf("Expected an '%#v' error, got '%#v'", expectedError, err)
    22  	}
    23  
    24  	// test w/submission url and w/o token
    25  	cfg := &Config{}
    26  	cfg.CheckManager.Check.SubmissionURL = "http://127.0.0.1:43191/"
    27  	_, err = NewCirconusSink(cfg)
    28  	if err != nil {
    29  		t.Errorf("Expected no error, got '%v'", err)
    30  	}
    31  
    32  	// note: a test with a valid token is *not* done as it *will* create a
    33  	//       check resulting in testing the api more than the circonus sink
    34  	// see circonus-gometrics/checkmgr/checkmgr_test.go for testing of api token
    35  }
    36  
    37  func TestFlattenKey(t *testing.T) {
    38  	var testKeys = []struct {
    39  		input    []string
    40  		expected string
    41  	}{
    42  		{[]string{"a", "b", "c"}, "a`b`c"},
    43  		{[]string{"a-a", "b_b", "c/c"}, "a-a`b_b`c/c"},
    44  		{[]string{"spaces must", "flatten", "to", "underscores"}, "spaces_must`flatten`to`underscores"},
    45  	}
    46  
    47  	c := &CirconusSink{}
    48  
    49  	for _, test := range testKeys {
    50  		if actual := c.flattenKey(test.input); actual != test.expected {
    51  			t.Fatalf("Flattening %v failed, expected '%s' got '%s'", test.input, test.expected, actual)
    52  		}
    53  	}
    54  }
    55  
    56  func fakeBroker(q chan string) *httptest.Server {
    57  	handler := func(w http.ResponseWriter, r *http.Request) {
    58  		w.WriteHeader(200)
    59  		w.Header().Set("Content-Type", "application/json")
    60  		defer r.Body.Close()
    61  		body, err := ioutil.ReadAll(r.Body)
    62  		if err != nil {
    63  			q <- err.Error()
    64  			fmt.Fprintln(w, err.Error())
    65  		} else {
    66  			q <- string(body)
    67  			fmt.Fprintln(w, `{"stats":1}`)
    68  		}
    69  	}
    70  
    71  	return httptest.NewServer(http.HandlerFunc(handler))
    72  }
    73  
    74  func TestSetGauge(t *testing.T) {
    75  	q := make(chan string)
    76  
    77  	server := fakeBroker(q)
    78  	defer server.Close()
    79  
    80  	cfg := &Config{}
    81  	cfg.CheckManager.Check.SubmissionURL = server.URL
    82  
    83  	cs, err := NewCirconusSink(cfg)
    84  	if err != nil {
    85  		t.Errorf("Expected no error, got '%v'", err)
    86  	}
    87  
    88  	go func() {
    89  		cs.SetGauge([]string{"foo", "bar"}, 1)
    90  		cs.Flush()
    91  	}()
    92  
    93  	expect := "{\"foo`bar\":{\"_type\":\"l\",\"_value\":1}}"
    94  	actual := <-q
    95  
    96  	if actual != expect {
    97  		t.Errorf("Expected '%s', got '%s'", expect, actual)
    98  
    99  	}
   100  }
   101  
   102  func TestSetPrecisionGauge(t *testing.T) {
   103  	q := make(chan string)
   104  
   105  	server := fakeBroker(q)
   106  	defer server.Close()
   107  
   108  	cfg := &Config{}
   109  	cfg.CheckManager.Check.SubmissionURL = server.URL
   110  
   111  	cs, err := NewCirconusSink(cfg)
   112  	if err != nil {
   113  		t.Errorf("Expected no error, got '%v'", err)
   114  	}
   115  
   116  	go func() {
   117  		cs.SetPrecisionGauge([]string{"foo", "bar"}, 1)
   118  		cs.Flush()
   119  	}()
   120  
   121  	expect := "{\"foo`bar\":{\"_type\":\"n\",\"_value\":1}}"
   122  	actual := <-q
   123  
   124  	if actual != expect {
   125  		t.Errorf("Expected '%s', got '%s'", expect, actual)
   126  
   127  	}
   128  }
   129  
   130  func TestIncrCounter(t *testing.T) {
   131  	q := make(chan string)
   132  
   133  	server := fakeBroker(q)
   134  	defer server.Close()
   135  
   136  	cfg := &Config{}
   137  	cfg.CheckManager.Check.SubmissionURL = server.URL
   138  
   139  	cs, err := NewCirconusSink(cfg)
   140  	if err != nil {
   141  		t.Errorf("Expected no error, got '%v'", err)
   142  	}
   143  
   144  	go func() {
   145  		cs.IncrCounter([]string{"foo", "bar"}, 1)
   146  		cs.Flush()
   147  	}()
   148  
   149  	expect := "{\"foo`bar\":{\"_type\":\"L\",\"_value\":1}}"
   150  	actual := <-q
   151  
   152  	if actual != expect {
   153  		t.Errorf("Expected '%s', got '%s'", expect, actual)
   154  
   155  	}
   156  }
   157  
   158  func TestAddSample(t *testing.T) {
   159  	q := make(chan string)
   160  
   161  	server := fakeBroker(q)
   162  	defer server.Close()
   163  
   164  	cfg := &Config{}
   165  	cfg.CheckManager.Check.SubmissionURL = server.URL
   166  
   167  	cs, err := NewCirconusSink(cfg)
   168  	if err != nil {
   169  		t.Errorf("Expected no error, got '%v'", err)
   170  	}
   171  
   172  	go func() {
   173  		cs.AddSample([]string{"foo", "bar"}, 1)
   174  		cs.Flush()
   175  	}()
   176  
   177  	expect := "{\"foo`bar\":{\"_type\":\"n\",\"_value\":[\"H[1.0e+00]=1\"]}}"
   178  	actual := <-q
   179  
   180  	if actual != expect {
   181  		t.Errorf("Expected '%s', got '%s'", expect, actual)
   182  
   183  	}
   184  }
   185  
   186  func TestMetricSinkInterface(t *testing.T) {
   187  	var cs *CirconusSink
   188  	_ = metrics.MetricSink(cs)
   189  }