|
5 | 5 | "testing" |
6 | 6 |
|
7 | 7 | consul "github.com/hashicorp/consul/api" |
8 | | - "github.com/hashicorp/consul/testutil" |
9 | 8 | "github.com/stretchr/testify/assert" |
10 | 9 | ) |
11 | 10 |
|
@@ -71,113 +70,117 @@ func TestCheckForChanges(t *testing.T) { |
71 | 70 | assert.True(t, didChange, "value for 'didChange' after t3") |
72 | 71 | } |
73 | 72 |
|
74 | | -/* |
75 | | -The TestWithConsul suite of tests uses Hashicorp's own testutil for managing |
76 | | -a Consul server for testing. The 'consul' binary must be in the $PATH |
77 | | -ref https://github.com/hashicorp/consul/tree/master/testutil |
78 | | -*/ |
79 | | - |
80 | | -var testServer *testutil.TestServer |
81 | | - |
82 | 73 | func TestWithConsul(t *testing.T) { |
83 | | - testServer, _ = testutil.NewTestServerConfigT(t, func(c *testutil.TestServerConfig) { |
84 | | - c.LogLevel = "err" |
85 | | - }) |
| 74 | + testServer, err := NewTestServer(8500) |
| 75 | + if err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
86 | 78 | defer testServer.Stop() |
87 | | - t.Run("TestConsulTTLPass", testConsulTTLPass) |
88 | | - t.Run("TestConsulReregister", testConsulReregister) |
89 | | - t.Run("TestConsulCheckForChanges", testConsulCheckForChanges) |
90 | | - t.Run("TestConsulEnableTagOverride", testConsulEnableTagOverride) |
| 79 | + |
| 80 | + testServer.WaitForAPI() |
| 81 | + |
| 82 | + t.Run("TestConsulTTLPass", testConsulTTLPass(testServer)) |
| 83 | + t.Run("TestConsulReregister", testConsulReregister(testServer)) |
| 84 | + t.Run("TestConsulCheckForChanges", testConsulCheckForChanges(testServer)) |
| 85 | + t.Run("TestConsulEnableTagOverride", testConsulEnableTagOverride(testServer)) |
91 | 86 | } |
92 | 87 |
|
93 | | -func testConsulTTLPass(t *testing.T) { |
94 | | - consul, _ := NewConsul(testServer.HTTPAddr) |
95 | | - name := fmt.Sprintf("TestConsulTTLPass") |
96 | | - service := generateServiceDefinition(name, consul) |
97 | | - checkID := fmt.Sprintf("service:%s", service.ID) |
98 | | - |
99 | | - service.SendHeartbeat() // force registration and 1st heartbeat |
100 | | - checks, _ := consul.Agent().Checks() |
101 | | - check := checks[checkID] |
102 | | - if check.Status != "passing" { |
103 | | - t.Fatalf("status of check %s should be 'passing' but is %s", checkID, check.Status) |
| 88 | +func testConsulTTLPass(testServer *TestServer) func(*testing.T) { |
| 89 | + return func(t *testing.T) { |
| 90 | + consul, _ := NewConsul(testServer.HTTPAddr) |
| 91 | + name := fmt.Sprintf("TestConsulTTLPass") |
| 92 | + service := generateServiceDefinition(name, consul) |
| 93 | + checkID := fmt.Sprintf("service:%s", service.ID) |
| 94 | + |
| 95 | + service.SendHeartbeat() // force registration and 1st heartbeat |
| 96 | + checks, _ := consul.Agent().Checks() |
| 97 | + check := checks[checkID] |
| 98 | + if check.Status != "passing" { |
| 99 | + t.Fatalf("status of check %s should be 'passing' but is %s", checkID, check.Status) |
| 100 | + } |
104 | 101 | } |
105 | 102 | } |
106 | 103 |
|
107 | | -func testConsulReregister(t *testing.T) { |
108 | | - consul, _ := NewConsul(testServer.HTTPAddr) |
109 | | - name := fmt.Sprintf("TestConsulReregister") |
110 | | - service := generateServiceDefinition(name, consul) |
111 | | - id := service.ID |
112 | | - |
113 | | - service.SendHeartbeat() // force registration and 1st heartbeat |
114 | | - services, _ := consul.Agent().Services() |
115 | | - svc := services[id] |
116 | | - if svc.Address != "192.168.1.1" { |
117 | | - t.Fatalf("service address should be '192.168.1.1' but is %s", svc.Address) |
118 | | - } |
| 104 | +func testConsulReregister(testServer *TestServer) func(*testing.T) { |
| 105 | + return func(t *testing.T) { |
| 106 | + consul, _ := NewConsul(testServer.HTTPAddr) |
| 107 | + name := fmt.Sprintf("TestConsulReregister") |
| 108 | + service := generateServiceDefinition(name, consul) |
| 109 | + id := service.ID |
| 110 | + |
| 111 | + service.SendHeartbeat() // force registration and 1st heartbeat |
| 112 | + services, _ := consul.Agent().Services() |
| 113 | + svc := services[id] |
| 114 | + if svc.Address != "192.168.1.1" { |
| 115 | + t.Fatalf("service address should be '192.168.1.1' but is %s", svc.Address) |
| 116 | + } |
119 | 117 |
|
120 | | - // new Consul client (as though we've restarted) |
121 | | - consul, _ = NewConsul(testServer.HTTPAddr) |
122 | | - service = generateServiceDefinition(name, consul) |
123 | | - service.IPAddress = "192.168.1.2" |
124 | | - service.SendHeartbeat() // force re-registration and 1st heartbeat |
| 118 | + // new Consul client (as though we've restarted) |
| 119 | + consul, _ = NewConsul(testServer.HTTPAddr) |
| 120 | + service = generateServiceDefinition(name, consul) |
| 121 | + service.IPAddress = "192.168.1.2" |
| 122 | + service.SendHeartbeat() // force re-registration and 1st heartbeat |
125 | 123 |
|
126 | | - services, _ = consul.Agent().Services() |
127 | | - svc = services[id] |
128 | | - if svc.Address != "192.168.1.2" { |
129 | | - t.Fatalf("service address should be '192.168.1.2' but is %s", svc.Address) |
| 124 | + services, _ = consul.Agent().Services() |
| 125 | + svc = services[id] |
| 126 | + if svc.Address != "192.168.1.2" { |
| 127 | + t.Fatalf("service address should be '192.168.1.2' but is %s", svc.Address) |
| 128 | + } |
130 | 129 | } |
131 | 130 | } |
132 | 131 |
|
133 | | -func testConsulCheckForChanges(t *testing.T) { |
134 | | - backend := fmt.Sprintf("TestConsulCheckForChanges") |
135 | | - consul, _ := NewConsul(testServer.HTTPAddr) |
136 | | - service := generateServiceDefinition(backend, consul) |
137 | | - id := service.ID |
138 | | - if changed, _ := consul.CheckForUpstreamChanges(backend, "", ""); changed { |
139 | | - t.Fatalf("First read of %s should show `false` for change", id) |
140 | | - } |
141 | | - service.SendHeartbeat() // force registration and 1st heartbeat |
| 132 | +func testConsulCheckForChanges(testServer *TestServer) func(*testing.T) { |
| 133 | + return func(t *testing.T) { |
| 134 | + backend := fmt.Sprintf("TestConsulCheckForChanges") |
| 135 | + consul, _ := NewConsul(testServer.HTTPAddr) |
| 136 | + service := generateServiceDefinition(backend, consul) |
| 137 | + id := service.ID |
| 138 | + if changed, _ := consul.CheckForUpstreamChanges(backend, "", ""); changed { |
| 139 | + t.Fatalf("First read of %s should show `false` for change", id) |
| 140 | + } |
| 141 | + service.SendHeartbeat() // force registration and 1st heartbeat |
142 | 142 |
|
143 | | - if changed, _ := consul.CheckForUpstreamChanges(backend, "", ""); !changed { |
144 | | - t.Errorf("%v should have changed after first health check TTL", id) |
145 | | - } |
146 | | - if changed, _ := consul.CheckForUpstreamChanges(backend, "", ""); changed { |
147 | | - t.Errorf("%v should not have changed without TTL expiring", id) |
148 | | - } |
149 | | - check := fmt.Sprintf("service:TestConsulCheckForChanges") |
150 | | - consul.Agent().UpdateTTL(check, "expired", "critical") |
151 | | - if changed, _ := consul.CheckForUpstreamChanges(backend, "", ""); !changed { |
152 | | - t.Errorf("%v should have changed after TTL expired.", id) |
| 143 | + if changed, _ := consul.CheckForUpstreamChanges(backend, "", ""); !changed { |
| 144 | + t.Errorf("%v should have changed after first health check TTL", id) |
| 145 | + } |
| 146 | + if changed, _ := consul.CheckForUpstreamChanges(backend, "", ""); changed { |
| 147 | + t.Errorf("%v should not have changed without TTL expiring", id) |
| 148 | + } |
| 149 | + check := fmt.Sprintf("service:TestConsulCheckForChanges") |
| 150 | + consul.Agent().UpdateTTL(check, "expired", "critical") |
| 151 | + if changed, _ := consul.CheckForUpstreamChanges(backend, "", ""); !changed { |
| 152 | + t.Errorf("%v should have changed after TTL expired.", id) |
| 153 | + } |
153 | 154 | } |
154 | 155 | } |
155 | 156 |
|
156 | | -func testConsulEnableTagOverride(t *testing.T) { |
157 | | - backend := fmt.Sprintf("TestConsulEnableTagOverride") |
158 | | - consul, _ := NewConsul(testServer.HTTPAddr) |
159 | | - service := &ServiceDefinition{ |
160 | | - ID: backend, |
161 | | - Name: backend, |
162 | | - IPAddress: "192.168.1.1", |
163 | | - TTL: 1, |
164 | | - Port: 9000, |
165 | | - EnableTagOverride: true, |
166 | | - Consul: consul, |
167 | | - } |
168 | | - id := service.ID |
169 | | - if changed, _ := consul.CheckForUpstreamChanges(backend, "", ""); changed { |
170 | | - t.Fatalf("First read of %s should show `false` for change", id) |
171 | | - } |
172 | | - service.SendHeartbeat() // force registration |
173 | | - catalogService, _, err := consul.Catalog().Service(id, "", nil) |
174 | | - if err != nil { |
175 | | - t.Fatalf("error finding service: %v", err) |
176 | | - } |
| 157 | +func testConsulEnableTagOverride(testServer *TestServer) func(*testing.T) { |
| 158 | + return func(t *testing.T) { |
| 159 | + backend := fmt.Sprintf("TestConsulEnableTagOverride") |
| 160 | + consul, _ := NewConsul(testServer.HTTPAddr) |
| 161 | + service := &ServiceDefinition{ |
| 162 | + ID: backend, |
| 163 | + Name: backend, |
| 164 | + IPAddress: "192.168.1.1", |
| 165 | + TTL: 1, |
| 166 | + Port: 9000, |
| 167 | + EnableTagOverride: true, |
| 168 | + Consul: consul, |
| 169 | + } |
| 170 | + id := service.ID |
| 171 | + if changed, _ := consul.CheckForUpstreamChanges(backend, "", ""); changed { |
| 172 | + t.Fatalf("First read of %s should show `false` for change", id) |
| 173 | + } |
| 174 | + service.SendHeartbeat() // force registration |
| 175 | + catalogService, _, err := consul.Catalog().Service(id, "", nil) |
| 176 | + if err != nil { |
| 177 | + t.Fatalf("error finding service: %v", err) |
| 178 | + } |
177 | 179 |
|
178 | | - for _, service := range catalogService { |
179 | | - if service.ServiceEnableTagOverride != true { |
180 | | - t.Errorf("%v should have had EnableTagOverride set to true", id) |
| 180 | + for _, service := range catalogService { |
| 181 | + if service.ServiceEnableTagOverride != true { |
| 182 | + t.Errorf("%v should have had EnableTagOverride set to true", id) |
| 183 | + } |
181 | 184 | } |
182 | 185 | } |
183 | 186 | } |
|
0 commit comments