File: health_test.go

package info (click to toggle)
golang-github-armon-consul-api 0.0~git20150107.0.dcfedd5-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 168 kB
  • ctags: 177
  • sloc: makefile: 3
file content (98 lines) | stat: -rw-r--r-- 1,755 bytes parent folder | download | duplicates (4)
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
package consulapi

import (
	"testing"
	"time"
)

func TestHealth_Node(t *testing.T) {
	c := makeClient(t)
	agent := c.Agent()
	health := c.Health()

	info, err := agent.Self()
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	name := info["Config"]["NodeName"].(string)

	checks, meta, err := health.Node(name, nil)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	if meta.LastIndex == 0 {
		t.Fatalf("bad: %v", meta)
	}
	if len(checks) == 0 {
		t.Fatalf("Bad: %v", checks)
	}
}

func TestHealth_Checks(t *testing.T) {
	c := makeClient(t)
	agent := c.Agent()
	health := c.Health()

	// Make a service with a check
	reg := &AgentServiceRegistration{
		Name: "foo",
		Check: &AgentServiceCheck{
			TTL: "15s",
		},
	}
	if err := agent.ServiceRegister(reg); err != nil {
		t.Fatalf("err: %v", err)
	}
	defer agent.ServiceDeregister("foo")

	// Wait for the register...
	time.Sleep(20 * time.Millisecond)

	checks, meta, err := health.Checks("foo", nil)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	if meta.LastIndex == 0 {
		t.Fatalf("bad: %v", meta)
	}
	if len(checks) == 0 {
		t.Fatalf("Bad: %v", checks)
	}
}

func TestHealth_Service(t *testing.T) {
	c := makeClient(t)
	health := c.Health()

	// consul service should always exist...
	checks, meta, err := health.Service("consul", "", true, nil)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	if meta.LastIndex == 0 {
		t.Fatalf("bad: %v", meta)
	}
	if len(checks) == 0 {
		t.Fatalf("Bad: %v", checks)
	}
}

func TestHealth_State(t *testing.T) {
	c := makeClient(t)
	health := c.Health()

	checks, meta, err := health.State("any", nil)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	if meta.LastIndex == 0 {
		t.Fatalf("bad: %v", meta)
	}
	if len(checks) == 0 {
		t.Fatalf("Bad: %v", checks)
	}
}