MCPcopy
hub / github.com/fabiolb/fabio / TestConsulSource

Function TestConsulSource

cert/source_test.go:223–301  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

221}
222
223func TestConsulSource(t *testing.T) {
224 const certURL = "http://127.0.0.1:8500/v1/kv/fabio/test/consul-server"
225
226 // run a consul server if it isn't already running
227 _, err := http.Get("http://127.0.0.1:8500/v1/status/leader")
228 if err != nil {
229 consul := os.Getenv("CONSUL_EXE")
230 if consul == "" {
231 consul = "consul"
232 }
233
234 version, err := exec.Command(consul, "--version").Output()
235 if err != nil {
236 t.Fatalf("Failed to run %s --version", consul)
237 }
238 cr := bytes.IndexRune(version, '\n')
239 t.Logf("Starting %s: %s", consul, string(version[:cr]))
240
241 start := time.Now()
242 cmd := exec.Command(consul, "agent", "-bind", "127.0.0.1", "-server", "-dev")
243 if err := cmd.Start(); err != nil {
244 t.Fatalf("Failed to start consul server. %s", err)
245 }
246 defer cmd.Process.Kill()
247
248 isUp := func() bool {
249 resp, err := http.Get("http://127.0.0.1:8500/v1/status/leader")
250 // /v1/status/leader returns '\n""' while consul is in leader election mode
251 // and '"127.0.0.1:8300"' when not. So we punt by checking the
252 // body length instead of the actual body content :)
253 if err != nil {
254 return false
255 }
256 defer resp.Body.Close()
257
258 if resp.StatusCode != 200 {
259 return false
260 }
261
262 n, err := io.Copy(io.Discard, resp.Body)
263 return err == nil && n > 10
264 }
265
266 // We need give consul ~8-10 seconds to become ready until I've
267 // figured out whether we can speed this up. Make sure that this is
268 // less than the global test timeout in Makefile.
269 if !waitFor(12*time.Second, isUp) {
270 t.Fatalf("Timeout waiting for consul server after %2.1f seconds", time.Since(start).Seconds())
271 }
272 t.Logf("Consul is ready after %2.1f seconds", time.Since(start).Seconds())
273 } else {
274 t.Log("Using existing consul server")
275 }
276
277 config, key, err := parseConsulURL(certURL)
278 if err != nil {
279 t.Fatalf("Failed to parse consul url: %s", err)
280 }

Callers

nothing calls this directly

Calls 9

waitForFunction · 0.85
parseConsulURLFunction · 0.85
makePEMFunction · 0.85
testSourceFunction · 0.85
makeCertPoolFunction · 0.85
CloseMethod · 0.65
LogMethod · 0.65
GetMethod · 0.45
StartMethod · 0.45

Tested by

no test coverage detected