File: broker_test.go

package info (click to toggle)
golang-github-circonus-labs-circonus-gometrics 0.0~git20160822.0.2f5be48-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 236 kB
  • ctags: 202
  • sloc: makefile: 2
file content (107 lines) | stat: -rw-r--r-- 2,726 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2016 Circonus, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package checkmgr

import (
	"errors"
	"fmt"
	"os"
	"reflect"
	"testing"
	"time"

	"github.com/circonus-labs/circonus-gometrics/api"
)

func TestGetBrokerInvalid(t *testing.T) {
	if os.Getenv("CIRCONUS_API_TOKEN") == "" {
		t.Skip("skipping test; $CIRCONUS_API_TOKEN not set")
	}

	t.Log("Testing invalid custom broker id (275/chicago-no httptrap)")

	cm := &CheckManager{}
	ac := &api.Config{}
	ac.TokenKey = os.Getenv("CIRCONUS_API_TOKEN")
	apih, err := api.NewAPI(ac)
	if err != nil {
		t.Errorf("Expected no error, got '%v'", err)
	}
	cm.apih = apih
	cm.brokerID = 275

	expectedError := errors.New("[ERROR] designated broker 275 [Chicago, IL, US] is invalid (not active, does not support required check type, or connectivity issue)")

	_, err = cm.getBroker()
	if err == nil || err.Error() != expectedError.Error() {
		t.Errorf("Expected an '%#v' error, got '%#v'", expectedError, err)
	}
}

func TestGetBrokerValid(t *testing.T) {
	if os.Getenv("CIRCONUS_API_TOKEN") == "" {
		t.Skip("skipping test; $CIRCONUS_API_TOKEN not set")
	}

	t.Log("Testing valid custom broker id (35/httptrap)")

	cm := &CheckManager{}
	ac := &api.Config{}
	ac.TokenKey = os.Getenv("CIRCONUS_API_TOKEN")
	apih, err := api.NewAPI(ac)
	if err != nil {
		t.Errorf("Expected no error, got '%v'", err)
	}
	cm.apih = apih
	cm.brokerID = 35
	cm.brokerMaxResponseTime, _ = time.ParseDuration("5s")
	cm.checkType = "httptrap"

	broker, err := cm.getBroker()
	if err != nil {
		t.Fatalf("Expected no error, got '%v'", err)
	}

	expectedCid := fmt.Sprintf("/broker/%d", int(cm.brokerID))
	if broker.Cid != expectedCid {
		t.Fatalf("%s != %s", broker.Cid, expectedCid)
	}
}

func TestGetBrokerSelection(t *testing.T) {
	if os.Getenv("CIRCONUS_API_TOKEN") == "" {
		t.Skip("skipping test; $CIRCONUS_API_TOKEN not set")
	}

	t.Log("Testing broker selection")

	cm := &CheckManager{}
	ac := &api.Config{}
	ac.TokenKey = os.Getenv("CIRCONUS_API_TOKEN")
	apih, err := api.NewAPI(ac)
	if err != nil {
		t.Errorf("Expected no error, got '%v'", err)
	}
	cm.apih = apih
	cm.brokerMaxResponseTime, _ = time.ParseDuration("5s")
	cm.checkType = "httptrap"

	broker, err := cm.getBroker()
	if err != nil {
		t.Fatalf("Expected no error, got '%v'", err)
	}

	actualType := reflect.TypeOf(broker)
	expectedType := "*api.Broker"
	if actualType.String() != expectedType {
		t.Errorf("Expected *api.Broker, got %s", actualType.String())
	}

	if broker.Cid[:8] != "/broker/" {
		t.Errorf("Expected cid to start with '/broker/', found: %s", broker.Cid)
	}

	t.Logf("Selected broker %s %s", broker.Name, broker.Cid)
}