File: checkmgr_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 (148 lines) | stat: -rw-r--r-- 4,101 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// 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"
	"os"
	"testing"
)

func TestNewCheckManager1(t *testing.T) {
	t.Log("Testing correct error return when no Check Manager config supplied")

	expectedError := errors.New("Invalid Check Manager configuration (nil).")

	_, err := NewCheckManager(nil)

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

func TestNewCheckManager2(t *testing.T) {
	t.Log("Testing correct error return when no API Token and no Submission URL supplied")

	expectedError := errors.New("Invalid check manager configuration (no API token AND no submission url).")

	cfg := &Config{}
	_, err := NewCheckManager(cfg)

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

}

func TestNewCheckManager3(t *testing.T) {
	t.Log("Testing correct return with Submission URL (http) and no API Token supplied")

	cfg := &Config{}
	cfg.Check.SubmissionURL = "http://127.0.0.1:56104"

	cm, err := NewCheckManager(cfg)
	if err != nil {
		t.Errorf("Expected no error, got '%v'", err)
	}

	trap, err := cm.GetTrap()
	if err != nil {
		t.Errorf("Expected no error, got '%v'", err)
	}

	if trap.URL.String() != cfg.Check.SubmissionURL {
		t.Errorf("Expected '%s' == '%s'", trap.URL.String(), cfg.Check.SubmissionURL)
	}

	if trap.TLS != nil {
		t.Errorf("Expected nil found %#v", trap.TLS)
	}
}

func TestNewCheckManager4(t *testing.T) {
	t.Log("Testing correct return with Submission URL (https) and no API Token supplied")

	cfg := &Config{}
	cfg.Check.SubmissionURL = "https://127.0.0.1/v2"

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

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

	if trap.URL.String() != cfg.Check.SubmissionURL {
		t.Fatalf("Expected '%s' == '%s'", trap.URL.String(), cfg.Check.SubmissionURL)
	}

	if trap.TLS == nil {
		t.Fatalf("Expected a x509 cert pool, found nil")
	}
}

func TestNewCheckManager5(t *testing.T) {
	// flag to indicate whether to do this test
	if os.Getenv("CIRCONUS_CGM_CMTEST5") == "" {
		t.Skip("skipping test; $CIRCONUS_CGM_CMTEST5 not set")
	}

	// !!IMPORTANT!! this test is DESTRUCTIVE it will DELETE the check bundle
	//
	// this test expects to CREATE a check then, search (and find) the check.
	//
	// ensure there is no existing check which would match the default search criteria
	// it *will* be deleted at the end of this test...
	//
	// the default InstanceId is "os.hostname():program name" e.g. testhost1:checkmgr.test
	// the default SearchTag is "service:program name" e.g. service:checkmgr.test

	if os.Getenv("CIRCONUS_API_TOKEN") == "" {
		t.Skip("skipping test; $CIRCONUS_API_TOKEN not set")
	}

	t.Log("Testing correct check creation and search with API Token only")

	cfg := &Config{}
	cfg.API.TokenKey = os.Getenv("CIRCONUS_API_TOKEN")

	t.Log("Testing correct check creation - should create a check, if it doesn't exist")
	cm, err := NewCheckManager(cfg)
	if err != nil {
		t.Fatalf("Expected no error, got '%v'", err)
	}

	t.Log("Getting Trap from cm instance")
	trap, err := cm.GetTrap()
	if err != nil {
		t.Fatalf("Expected no error, got '%v'", err)
	}

	t.Log("Testing correct check search - should find the check created")
	cm2, err := NewCheckManager(cfg)
	if err != nil {
		t.Fatalf("Expected no error, got '%v'", err)
	}

	t.Log("Getting Trap from cm2 instance")
	trap2, err := cm2.GetTrap()
	if err != nil {
		t.Fatalf("Expected no error, got '%v'", err)
	}

	t.Log("Comparing Trap URLs")
	if trap.URL.String() != trap2.URL.String() {
		t.Fatalf("Expected '%s' == '%s'", trap.URL.String(), trap2.URL.String())
	}

	t.Logf("Deleting %s %s", cm2.checkBundle.Cid, cm2.checkBundle.DisplayName)
	_, err = cm2.apih.Delete(cm2.checkBundle.Cid)
	if err != nil {
		t.Fatalf("Expected no error, got '%v'", err)
	}
}