func TestServer_JoinLAN(t *testing.T) { dir1, s1 := testServer(t) defer os.RemoveAll(dir1) defer s1.Shutdown() dir2, s2 := testServer(t) defer os.RemoveAll(dir2) defer s2.Shutdown() // Try to join addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfLANConfig.MemberlistConfig.BindPort) if _, err := s2.JoinLAN([]string{addr}); err != nil { t.Fatalf("err: %v", err) } // Check the members testutil.WaitForResult(func() (bool, error) { return len(s1.LANMembers()) == 2, nil }, func(err error) { t.Fatalf("bad len") }) testutil.WaitForResult(func() (bool, error) { return len(s2.LANMembers()) == 2, nil }, func(err error) { t.Fatalf("bad len") }) }
func TestClient_JoinLAN(t *testing.T) { dir1, s1 := testServer(t) defer os.RemoveAll(dir1) defer s1.Shutdown() dir2, c1 := testClient(t) defer os.RemoveAll(dir2) defer c1.Shutdown() // Try to join addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfLANConfig.MemberlistConfig.BindPort) if _, err := c1.JoinLAN([]string{addr}); err != nil { t.Fatalf("err: %v", err) } // Check the members testutil.WaitForResult(func() (bool, error) { server_check := len(s1.LANMembers()) == 2 client_check := len(c1.LANMembers()) == 2 return server_check && client_check, nil }, func(err error) { t.Fatalf("bad len") }) // Check we have a new consul testutil.WaitForResult(func() (bool, error) { return len(c1.consuls) == 1, nil }, func(err error) { t.Fatalf("expected consul server") }) }
func TestServer_JoinLAN_TLS(t *testing.T) { dir1, conf1 := testServerConfig(t, "a.testco.internal") conf1.VerifyIncoming = true conf1.VerifyOutgoing = true configureTLS(conf1) s1, err := NewServer(conf1) if err != nil { t.Fatalf("err: %v", err) } defer os.RemoveAll(dir1) defer s1.Shutdown() dir2, conf2 := testServerConfig(t, "b.testco.internal") conf2.Bootstrap = false conf2.VerifyIncoming = true conf2.VerifyOutgoing = true configureTLS(conf2) s2, err := NewServer(conf2) if err != nil { t.Fatalf("err: %v", err) } defer os.RemoveAll(dir2) defer s2.Shutdown() // Try to join addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfLANConfig.MemberlistConfig.BindPort) if _, err := s2.JoinLAN([]string{addr}); err != nil { t.Fatalf("err: %v", err) } // Check the members testutil.WaitForResult(func() (bool, error) { return len(s1.LANMembers()) == 2, nil }, func(err error) { t.Fatalf("bad len") }) testutil.WaitForResult(func() (bool, error) { return len(s2.LANMembers()) == 2, nil }, func(err error) { t.Fatalf("bad len") }) // Verify Raft has established a peer testutil.WaitForResult(func() (bool, error) { peers, _ := s1.numPeers() return peers == 2, nil }, func(err error) { t.Fatalf("no peer established") }) testutil.WaitForResult(func() (bool, error) { peers, _ := s2.numPeers() return peers == 2, nil }, func(err error) { t.Fatalf("no peer established") }) }
func TestLeader_FailedMember(t *testing.T) { dir1, s1 := testServer(t) defer os.RemoveAll(dir1) defer s1.Shutdown() dir2, c1 := testClient(t) defer os.RemoveAll(dir2) defer c1.Shutdown() testutil.WaitForLeader(t, s1.RPC, "dc1") // Try to join addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfLANConfig.MemberlistConfig.BindPort) if _, err := c1.JoinLAN([]string{addr}); err != nil { t.Fatalf("err: %v", err) } // Fail the member c1.Shutdown() // Should be registered state := s1.fsm.State() testutil.WaitForResult(func() (bool, error) { _, node, err := state.GetNode(c1.config.NodeName) if err != nil { t.Fatalf("err: %v", err) } return node != nil, nil }, func(err error) { t.Fatalf("client not registered") }) // Should have a check _, checks, err := state.NodeChecks(c1.config.NodeName) if err != nil { t.Fatalf("err: %v", err) } if len(checks) != 1 { t.Fatalf("client missing check") } if checks[0].CheckID != SerfCheckID { t.Fatalf("bad check: %v", checks[0]) } if checks[0].Name != SerfCheckName { t.Fatalf("bad check: %v", checks[0]) } testutil.WaitForResult(func() (bool, error) { _, checks, err = state.NodeChecks(c1.config.NodeName) if err != nil { t.Fatalf("err: %v", err) } return checks[0].Status == structs.HealthCritical, errors.New(checks[0].Status) }, func(err error) { t.Fatalf("check status is %v, should be critical", err) }) }
func TestCatalog_Nodes_MetaFilter(t *testing.T) { meta := map[string]string{"somekey": "somevalue"} c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) { conf.NodeMeta = meta }) defer s.Stop() catalog := c.Catalog() // Make sure we get the node back when filtering by its metadata testutil.WaitForResult(func() (bool, error) { nodes, meta, err := catalog.Nodes(&QueryOptions{NodeMeta: meta}) if err != nil { return false, err } if meta.LastIndex == 0 { return false, fmt.Errorf("Bad: %v", meta) } if len(nodes) == 0 { return false, fmt.Errorf("Bad: %v", nodes) } if _, ok := nodes[0].TaggedAddresses["wan"]; !ok { return false, fmt.Errorf("Bad: %v", nodes[0]) } if v, ok := nodes[0].Meta["somekey"]; !ok || v != "somevalue" { return false, fmt.Errorf("Bad: %v", nodes[0].Meta) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) // Get nothing back when we use an invalid filter testutil.WaitForResult(func() (bool, error) { nodes, meta, err := catalog.Nodes(&QueryOptions{NodeMeta: map[string]string{"nope": "nope"}}) if err != nil { return false, err } if meta.LastIndex == 0 { return false, fmt.Errorf("Bad: %v", meta) } if len(nodes) != 0 { return false, fmt.Errorf("Bad: %v", nodes) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) }
func TestServer_Leave(t *testing.T) { dir1, s1 := testServer(t) defer os.RemoveAll(dir1) defer s1.Shutdown() // Second server not in bootstrap mode dir2, s2 := testServerDCBootstrap(t, "dc1", false) defer os.RemoveAll(dir2) defer s2.Shutdown() // Try to join addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfLANConfig.MemberlistConfig.BindPort) if _, err := s2.JoinLAN([]string{addr}); err != nil { t.Fatalf("err: %v", err) } var p1 int var p2 int testutil.WaitForResult(func() (bool, error) { p1, _ = s1.numPeers() return p1 == 2, errors.New(fmt.Sprintf("%d", p1)) }, func(err error) { t.Fatalf("should have 2 peers: %v", err) }) testutil.WaitForResult(func() (bool, error) { p2, _ = s2.numPeers() return p2 == 2, errors.New(fmt.Sprintf("%d", p1)) }, func(err error) { t.Fatalf("should have 2 peers: %v", err) }) // Issue a leave to the non-leader for _, s := range []*Server{s1, s2} { if s.IsLeader() { continue } if err := s.Leave(); err != nil { t.Fatalf("err: %v", err) } } // Should lose a peer for _, s := range []*Server{s1, s2} { testutil.WaitForResult(func() (bool, error) { p1, _ = s.numPeers() return p1 == 1, nil }, func(err error) { t.Fatalf("should have 1 peer: %v", p1) }) } }
func TestLeader_LeftServer(t *testing.T) { dir1, s1 := testServer(t) defer os.RemoveAll(dir1) defer s1.Shutdown() dir2, s2 := testServerDCBootstrap(t, "dc1", false) defer os.RemoveAll(dir2) defer s2.Shutdown() dir3, s3 := testServerDCBootstrap(t, "dc1", false) defer os.RemoveAll(dir3) defer s3.Shutdown() servers := []*Server{s1, s2, s3} // Try to join addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfLANConfig.MemberlistConfig.BindPort) if _, err := s2.JoinLAN([]string{addr}); err != nil { t.Fatalf("err: %v", err) } if _, err := s3.JoinLAN([]string{addr}); err != nil { t.Fatalf("err: %v", err) } for _, s := range servers { testutil.WaitForResult(func() (bool, error) { peers, _ := s.raftPeers.Peers() return len(peers) == 3, nil }, func(err error) { t.Fatalf("should have 3 peers") }) } testutil.WaitForResult(func() (bool, error) { // Kill any server servers[0].Shutdown() // Force remove the non-leader (transition to left state) if err := servers[1].RemoveFailedNode(servers[0].config.NodeName); err != nil { t.Fatalf("err: %v", err) } for _, s := range servers[1:] { peers, _ := s.raftPeers.Peers() return len(peers) == 2, errors.New(fmt.Sprintf("%v", peers)) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) }
func TestLeader_ReapMember(t *testing.T) { dir1, s1 := testServer(t) defer os.RemoveAll(dir1) defer s1.Shutdown() dir2, c1 := testClient(t) defer os.RemoveAll(dir2) defer c1.Shutdown() // Try to join addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfLANConfig.MemberlistConfig.BindPort) if _, err := c1.JoinLAN([]string{addr}); err != nil { t.Fatalf("err: %v", err) } state := s1.fsm.State() // Should be registered testutil.WaitForResult(func() (bool, error) { _, node, err := state.GetNode(c1.config.NodeName) if err != nil { t.Fatalf("err: %v", err) } return node != nil, nil }, func(err error) { t.Fatalf("client should be registered") }) // Simulate a node reaping mems := s1.LANMembers() var c1mem serf.Member for _, m := range mems { if m.Name == c1.config.NodeName { c1mem = m c1mem.Status = StatusReap break } } s1.reconcileCh <- c1mem // Should be deregistered testutil.WaitForResult(func() (bool, error) { _, node, err := state.GetNode(c1.config.NodeName) if err != nil { t.Fatalf("err: %v", err) } return node == nil, nil }, func(err error) { t.Fatalf("client should not be registered") }) }
func TestHealthChecksInState(t *testing.T) { httpTest(t, func(srv *HTTPServer) { req, err := http.NewRequest("GET", "/v1/health/state/warning?dc=dc1", nil) if err != nil { t.Fatalf("err: %v", err) } testutil.WaitForResult(func() (bool, error) { resp := httptest.NewRecorder() obj, err := srv.HealthChecksInState(resp, req) if err != nil { return false, err } if err := checkIndex(resp); err != nil { return false, err } // Should be a non-nil empty list nodes := obj.(structs.HealthChecks) if nodes == nil || len(nodes) != 0 { return false, fmt.Errorf("bad: %v", obj) } return true, nil }, func(err error) { t.Fatalf("err: %v", err) }) }) httpTest(t, func(srv *HTTPServer) { req, err := http.NewRequest("GET", "/v1/health/state/passing?dc=dc1", nil) if err != nil { t.Fatalf("err: %v", err) } testutil.WaitForResult(func() (bool, error) { resp := httptest.NewRecorder() obj, err := srv.HealthChecksInState(resp, req) if err != nil { return false, err } if err := checkIndex(resp); err != nil { return false, err } // Should be 1 health check for the server nodes := obj.(structs.HealthChecks) if len(nodes) != 1 { return false, fmt.Errorf("bad: %v", obj) } return true, nil }, func(err error) { t.Fatalf("err: %v", err) }) }) }
func TestCatalog_Services_NodeMetaFilter(t *testing.T) { meta := map[string]string{"somekey": "somevalue"} c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) { conf.NodeMeta = meta }) defer s.Stop() catalog := c.Catalog() // Make sure we get the service back when filtering by the node's metadata testutil.WaitForResult(func() (bool, error) { services, meta, err := catalog.Services(&QueryOptions{NodeMeta: meta}) if err != nil { return false, err } if meta.LastIndex == 0 { return false, fmt.Errorf("Bad: %v", meta) } if len(services) == 0 { return false, fmt.Errorf("Bad: %v", services) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) // Get nothing back when using an invalid filter testutil.WaitForResult(func() (bool, error) { services, meta, err := catalog.Services(&QueryOptions{NodeMeta: map[string]string{"nope": "nope"}}) if err != nil { return false, err } if meta.LastIndex == 0 { return false, fmt.Errorf("Bad: %v", meta) } if len(services) != 0 { return false, fmt.Errorf("Bad: %v", services) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) }
func TestRPCClientForceLeave(t *testing.T) { p1 := testRPCClient(t) p2 := testRPCClient(t) defer p1.Close() defer p2.Close() s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.Ports.SerfLan) if _, err := p1.agent.JoinLAN([]string{s2Addr}); err != nil { t.Fatalf("err: %s", err) } if err := p2.agent.Shutdown(); err != nil { t.Fatalf("err: %s", err) } if err := p1.client.ForceLeave(p2.agent.config.NodeName); err != nil { t.Fatalf("err: %s", err) } m := p1.agent.LANMembers() if len(m) != 2 { t.Fatalf("should have 2 members: %#v", m) } testutil.WaitForResult(func() (bool, error) { m := p1.agent.LANMembers() success := m[1].Status == serf.StatusLeft return success, errors.New(m[1].Status.String()) }, func(err error) { t.Fatalf("member status is %v, should be left", err) }) }
func TestSnapshot_Forward_Datacenter(t *testing.T) { dir1, s1 := testServerDC(t, "dc1") defer os.RemoveAll(dir1) defer s1.Shutdown() dir2, s2 := testServerDC(t, "dc2") defer os.RemoveAll(dir2) defer s2.Shutdown() testutil.WaitForLeader(t, s1.RPC, "dc1") testutil.WaitForLeader(t, s2.RPC, "dc2") // Try to WAN join. addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfWANConfig.MemberlistConfig.BindPort) if _, err := s2.JoinWAN([]string{addr}); err != nil { t.Fatalf("err: %v", err) } testutil.WaitForResult( func() (bool, error) { return len(s1.WANMembers()) > 1, nil }, func(err error) { t.Fatalf("Failed waiting for WAN join: %v", err) }) // Run a snapshot from each server locally and remotely to ensure we // forward. for _, s := range []*Server{s1, s2} { verifySnapshot(t, s, "dc1", "") verifySnapshot(t, s, "dc2", "") } }
func TestCatalog_Nodes(t *testing.T) { c, s := makeClient(t) defer s.Stop() catalog := c.Catalog() testutil.WaitForResult(func() (bool, error) { nodes, meta, err := catalog.Nodes(nil) if err != nil { return false, err } if meta.LastIndex == 0 { return false, fmt.Errorf("Bad: %v", meta) } if len(nodes) == 0 { return false, fmt.Errorf("Bad: %v", nodes) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) }
func TestAgentAddDNS(t *testing.T) { c, w, s := makeTestAgentWorker(t) defer s.Stop() dns := makeDNSInfo() if fail := w.AddDNS(dns, makeDefaultTestService()); fail != nil { t.Fatal("AddDNS failed. ", fail) } testutil.WaitForResult(func() (bool, error) { catalog := c.Catalog() x, _, err := catalog.Services(nil) if err != nil { return false, err } for x := range x { print("Service:", x, "\n") } return true, nil }, func(err error) { t.Fatal("Error: ", err) }) }
func TestCatalog_Register(t *testing.T) { dir1, s1 := testServer(t) defer os.RemoveAll(dir1) defer s1.Shutdown() codec := rpcClient(t, s1) defer codec.Close() arg := structs.RegisterRequest{ Datacenter: "dc1", Node: "foo", Address: "127.0.0.1", Service: &structs.NodeService{ Service: "db", Tags: []string{"master"}, Port: 8000, }, Check: &structs.HealthCheck{ ServiceID: "db", }, } var out struct{} err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out) if err != nil { t.Fatalf("err: %v", err) } testutil.WaitForResult(func() (bool, error) { err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out) return err == nil, err }, func(err error) { t.Fatalf("err: %v", err) }) }
func TestHealth_Node(t *testing.T) { t.Parallel() c, s := makeClient(t) defer s.Stop() agent := c.Agent() health := c.Health() info, err := agent.Self() if err != nil { t.Fatalf("err: %v", err) } name := info["Config"]["NodeName"].(string) testutil.WaitForResult(func() (bool, error) { checks, meta, err := health.Node(name, nil) if err != nil { return false, err } if meta.LastIndex == 0 { return false, fmt.Errorf("bad: %v", meta) } if len(checks) == 0 { return false, fmt.Errorf("bad: %v", checks) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) }
func TestHTTPAgentJoin_WAN(t *testing.T) { dir, srv := makeHTTPServer(t) defer os.RemoveAll(dir) defer srv.Shutdown() defer srv.agent.Shutdown() dir2, a2 := makeAgent(t, nextConfig()) defer os.RemoveAll(dir2) defer a2.Shutdown() addr := fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfWan) req, err := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s?wan=true", addr), nil) if err != nil { t.Fatalf("err: %v", err) } obj, err := srv.AgentJoin(nil, req) if err != nil { t.Fatalf("Err: %v", err) } if obj != nil { t.Fatalf("Err: %v", obj) } testutil.WaitForResult(func() (bool, error) { return len(a2.WANMembers()) == 2, nil }, func(err error) { t.Fatalf("should have 2 members") }) }
func TestCatalog_Node(t *testing.T) { t.Parallel() c, s := makeClient(t) defer s.Stop() catalog := c.Catalog() name, _ := c.Agent().NodeName() testutil.WaitForResult(func() (bool, error) { info, meta, err := catalog.Node(name, nil) if err != nil { return false, err } if meta.LastIndex == 0 { return false, fmt.Errorf("Bad: %v", meta) } if len(info.Services) == 0 { return false, fmt.Errorf("Bad: %v", info) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) }
func makeClientWithConfig(t *testing.T, clientConfig func(c *Config), serverConfig func(c *testServerConfig)) (*Client, *testServer) { server := newTestServerWithConfig(t, serverConfig) conf := DefaultConfig() clientConfig(conf) client, err := NewClient(conf) if err != nil { t.Fatalf("err: %v", err) } // Allow the server some time to start, and verify we have a leader. testutil.WaitForResult(func() (bool, error) { req := client.newRequest("GET", "/v1/catalog/nodes") _, resp, err := client.doRequest(req) if err != nil { return false, err } resp.Body.Close() // Ensure we have a leader and a node registeration if leader := resp.Header.Get("X-Consul-KnownLeader"); leader != "true" { return false, fmt.Errorf("Consul leader status: %#v", leader) } if resp.Header.Get("X-Consul-Index") == "0" { return false, fmt.Errorf("Consul index is 0") } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) return client, server }
func TestCatalog_Service_NodeMetaFilter(t *testing.T) { t.Parallel() meta := map[string]string{"somekey": "somevalue"} c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) { conf.NodeMeta = meta }) defer s.Stop() catalog := c.Catalog() testutil.WaitForResult(func() (bool, error) { services, meta, err := catalog.Service("consul", "", &QueryOptions{NodeMeta: meta}) if err != nil { return false, err } if meta.LastIndex == 0 { return false, fmt.Errorf("Bad: %v", meta) } if len(services) == 0 { return false, fmt.Errorf("Bad: %v", services) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) }
func TestCatalog_Service(t *testing.T) { t.Parallel() c, s := makeClient(t) defer s.Stop() catalog := c.Catalog() testutil.WaitForResult(func() (bool, error) { services, meta, err := catalog.Service("consul", "", nil) if err != nil { return false, err } if meta.LastIndex == 0 { return false, fmt.Errorf("Bad: %v", meta) } if len(services) == 0 { return false, fmt.Errorf("Bad: %v", services) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) }
func TestHealth_Service(t *testing.T) { c, s := makeClient(t) defer s.Stop() health := c.Health() testutil.WaitForResult(func() (bool, error) { // consul service should always exist... checks, meta, err := health.Service("consul", "", true, nil) if err != nil { return false, err } if meta.LastIndex == 0 { return false, fmt.Errorf("bad: %v", meta) } if len(checks) == 0 { return false, fmt.Errorf("Bad: %v", checks) } if _, ok := checks[0].Node.TaggedAddresses["wan"]; !ok { return false, fmt.Errorf("Bad: %v", checks[0].Node) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) }
func TestLeader_Reconcile(t *testing.T) { dir1, s1 := testServer(t) defer os.RemoveAll(dir1) defer s1.Shutdown() dir2, c1 := testClient(t) defer os.RemoveAll(dir2) defer c1.Shutdown() // Join before we have a leader, this should cause a reconcile! addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfLANConfig.MemberlistConfig.BindPort) if _, err := c1.JoinLAN([]string{addr}); err != nil { t.Fatalf("err: %v", err) } // Should not be registered state := s1.fsm.State() _, found, _ := state.GetNode(c1.config.NodeName) if found { t.Fatalf("client registered") } // Should be registered testutil.WaitForResult(func() (bool, error) { _, found, _ = state.GetNode(c1.config.NodeName) return found == true, nil }, func(err error) { t.Fatalf("client should be registered") }) }
func TestCatalogRegister(t *testing.T) { dir1, s1 := testServer(t) defer os.RemoveAll(dir1) defer s1.Shutdown() client := rpcClient(t, s1) defer client.Close() arg := structs.RegisterRequest{ Datacenter: "dc1", Node: "foo", Address: "127.0.0.1", Service: &structs.NodeService{ Service: "db", Tags: []string{"master"}, Port: 8000, }, } var out struct{} err := client.Call("Catalog.Register", &arg, &out) if err == nil || err.Error() != "No cluster leader" { t.Fatalf("err: %v", err) } testutil.WaitForResult(func() (bool, error) { err := client.Call("Catalog.Register", &arg, &out) return err == nil, err }, func(err error) { t.Fatalf("err: %v", err) }) }
func TestLeader_MultiBootstrap(t *testing.T) { dir1, s1 := testServer(t) defer os.RemoveAll(dir1) defer s1.Shutdown() dir2, s2 := testServer(t) defer os.RemoveAll(dir2) defer s2.Shutdown() servers := []*Server{s1, s2} // Try to join addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfLANConfig.MemberlistConfig.BindPort) if _, err := s2.JoinLAN([]string{addr}); err != nil { t.Fatalf("err: %v", err) } for _, s := range servers { testutil.WaitForResult(func() (bool, error) { peers := s.serfLAN.Members() return len(peers) == 2, nil }, func(err error) { t.Fatalf("should have 2 peers") }) } // Ensure we don't have multiple raft peers for _, s := range servers { peers, _ := s.raftPeers.Peers() if len(peers) != 1 { t.Fatalf("should only have 1 raft peer!") } } }
func TestCheckHTTP_TLSSkipVerify_true_fail(t *testing.T) { server := mockTLSHTTPServer(500) defer server.Close() mock := &MockNotify{ state: make(map[types.CheckID]string), updates: make(map[types.CheckID]int), output: make(map[types.CheckID]string), } check := &CheckHTTP{ Notify: mock, CheckID: types.CheckID("skipverify_true"), HTTP: server.URL, Interval: 5 * time.Millisecond, Logger: log.New(os.Stderr, "", log.LstdFlags), TLSSkipVerify: true, } check.Start() defer check.Stop() if !check.httpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify { t.Fatalf("should be true") } testutil.WaitForResult(func() (bool, error) { if mock.state["skipverify_true"] != structs.HealthCritical { return false, fmt.Errorf("should be critical %v", mock.state) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) }
func expectStatus(t *testing.T, script, status string) { mock := &MockNotify{ state: make(map[string]string), updates: make(map[string]int), output: make(map[string]string), } check := &CheckMonitor{ Notify: mock, CheckID: "foo", Script: script, Interval: 10 * time.Millisecond, Logger: log.New(os.Stderr, "", log.LstdFlags), } check.Start() defer check.Stop() testutil.WaitForResult(func() (bool, error) { // Should have at least 2 updates if mock.updates["foo"] < 2 { return false, fmt.Errorf("should have 2 updates %v", mock.updates) } if mock.state["foo"] != status { return false, fmt.Errorf("should be %v %v", status, mock.state) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) }
func TestHealth_Checks(t *testing.T) { c, s := makeClient(t) defer s.Stop() 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") testutil.WaitForResult(func() (bool, error) { checks, meta, err := health.Checks("foo", nil) if err != nil { return false, err } if meta.LastIndex == 0 { return false, fmt.Errorf("bad: %v", meta) } if len(checks) == 0 { return false, fmt.Errorf("Bad: %v", checks) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) }
func TestEventList(t *testing.T) { httpTest(t, func(srv *HTTPServer) { p := &UserEvent{Name: "test"} if err := srv.agent.UserEvent("", p); err != nil { t.Fatalf("err: %v", err) } testutil.WaitForResult(func() (bool, error) { req, err := http.NewRequest("GET", "/v1/event/list", nil) if err != nil { return false, err } resp := httptest.NewRecorder() obj, err := srv.EventList(resp, req) if err != nil { return false, err } list, ok := obj.([]*UserEvent) if !ok { return false, fmt.Errorf("bad: %#v", obj) } if len(list) != 1 || list[0].Name != "test" { return false, fmt.Errorf("bad: %#v", list) } header := resp.Header().Get("X-Consul-Index") if header == "" || header == "0" { return false, fmt.Errorf("bad: %#v", header) } return true, nil }, func(err error) { t.Fatalf("err: %v", err) }) }) }
func expectHTTPStatus(t *testing.T, url string, status string) { mock := &MockNotify{ state: make(map[types.CheckID]string), updates: make(map[types.CheckID]int), output: make(map[types.CheckID]string), } check := &CheckHTTP{ Notify: mock, CheckID: types.CheckID("foo"), HTTP: url, Interval: 10 * time.Millisecond, Logger: log.New(os.Stderr, "", log.LstdFlags), } check.Start() defer check.Stop() testutil.WaitForResult(func() (bool, error) { // Should have at least 2 updates if mock.updates["foo"] < 2 { return false, fmt.Errorf("should have 2 updates %v", mock.updates) } if mock.state["foo"] != status { return false, fmt.Errorf("should be %v %v", status, mock.state) } // Allow slightly more data than CheckBufSize, for the header if n := len(mock.output["foo"]); n > (CheckBufSize + 256) { return false, fmt.Errorf("output too long: %d (%d-byte limit)", n, CheckBufSize) } return true, nil }, func(err error) { t.Fatalf("err: %s", err) }) }