-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconsul_test.go
122 lines (107 loc) · 3.45 KB
/
consul_test.go
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
package test
import (
"net"
"os"
"strconv"
"testing"
consul "github.com/hashicorp/consul/api"
"github.com/stretchr/testify/suite"
)
// ConsulSuite is the test suite for consul.
type ConsulSuite struct {
suite.Suite
// service is the consul service to test.
service *consulService
}
// SetupSuite runs before all the tests.
func (s *ConsulSuite) SetupSuite() {
s.service = &consulService{}
}
// TestConsulSuite runs all the tests in consul suite.
func TestConsulSuite(t *testing.T) {
if testing.Short() {
t.Skip("Skip consul long test")
return
}
if os.Getenv(envProject) != projDeepLearning {
t.Skip("Skip consul test for non-deep learning project.")
return
}
suite.Run(t, new(ConsulSuite))
}
// TestStartAndStop tests Start() and Stop()
func (s *ConsulSuite) TestStartAndStop() {
ipport, err := s.service.Start()
s.NoError(err, "No error is expected")
_, strPort, err := net.SplitHostPort(ipport)
s.NoError(err, "No error is expected")
port, _ := strconv.Atoi(strPort)
isListening := CheckListening(port)
s.True(isListening, "Should be listening")
err = s.service.Stop()
s.NoError(err, "No error is expected")
isListening = CheckListening(port)
s.False(isListening, "Should not be listening")
}
// TestRegisterDeregister tests Register() and Deregister() for consul.
func (s *ConsulSuite) TestRegisterDeregister() {
ipport, err := s.service.Start()
s.NoError(err, "No error is expected")
_, strPort, err := net.SplitHostPort(ipport)
s.NoError(err, "No error is expected")
port, _ := strconv.Atoi(strPort)
isListening := CheckListening(port)
s.True(isListening, "Should be listening")
config := consul.DefaultConfig()
config.Address = ipport
client, err := consul.NewClient(config)
s.NoError(err, "No error is expected")
expect := map[string]*consul.AgentService{
"consul": &consul.AgentService{},
}
svcs, err := client.Agent().Services()
s.assertServices(svcs, expect)
// Test the case that a service is registered.
testID := "test_id"
testName := "test_name"
expect[testID] = &consul.AgentService{
ID: testID,
Service: testName,
}
reg := &consul.AgentServiceRegistration{
ID: testID,
Name: testName,
}
err = client.Agent().ServiceRegister(reg)
s.NoError(err, "No error is expected")
svcs, err = client.Agent().Services()
s.NoError(err, "No error is expected")
s.assertServices(svcs, expect)
// Test the case that a service is deregistered.
delete(expect, testID)
err = client.Agent().ServiceDeregister(testID)
s.NoError(err, "No error is expected")
svcs, err = client.Agent().Services()
s.NoError(err, "No error is expected")
s.assertServices(svcs, expect)
err = s.service.Stop()
s.NoError(err, "No error is expected")
isListening = CheckListening(port)
s.False(isListening, "Should not be listening")
}
// assertServices checks whether the services input are equal to each other or not.
func (s *ConsulSuite) assertServices(actual map[string]*consul.AgentService, expect map[string]*consul.AgentService) {
s.Equal(len(actual), len(expect), "Should be of same length")
for k, v := range expect {
result, ok := actual[k]
s.True(ok, "Should have service consul")
if k == "consul" {
// Note that the service consul in AgentService contains Server port only,
// which is different from the HTTP port returned and used by client.
// Thus, we do not check the whole structure but the id only.
s.Equal(result.ID, "consul", "ID should be consul")
continue
}
s.Equal(result, v, "Should be as expected")
}
}