diff --git a/api/agent.go b/api/agent.go index 3cf5b90cc..c7fc96205 100644 --- a/api/agent.go +++ b/api/agent.go @@ -95,9 +95,10 @@ func (a *Agent) Region() (string, error) { // Join is used to instruct a server node to join another server // via the gossip protocol. Multiple addresses may be specified. -// We attempt to join all of the hosts in the list. If one or +// We attempt to join all of the hosts in the list. Returns the +// number of nodes successfully joined and any error. If one or // more nodes have a successful result, no error is returned. -func (a *Agent) Join(addrs ...string) error { +func (a *Agent) Join(addrs ...string) (int, error) { // Accumulate the addresses v := url.Values{} for _, addr := range addrs { @@ -108,12 +109,12 @@ func (a *Agent) Join(addrs ...string) error { var resp joinResponse _, err := a.client.write("/v1/agent/join?"+v.Encode(), nil, &resp, nil) if err != nil { - return fmt.Errorf("failed joining: %s", err) + return 0, fmt.Errorf("failed joining: %s", err) } if resp.Error != "" { - return fmt.Errorf("failed joining: %s", resp.Error) + return 0, fmt.Errorf("failed joining: %s", resp.Error) } - return nil + return resp.NumNodes, nil } // Members is used to query all of the known server members diff --git a/api/agent_test.go b/api/agent_test.go index 7eb891ea1..0d7f427cf 100644 --- a/api/agent_test.go +++ b/api/agent_test.go @@ -59,22 +59,32 @@ func TestAgent_Datacenter(t *testing.T) { } func TestAgent_Join(t *testing.T) { - c, s := makeClient(t, nil, nil) - defer s.Stop() - a := c.Agent() + c1, s1 := makeClient(t, nil, nil) + defer s1.Stop() + a1 := c.Agent() + + c2, s2 := makeClient(t, nil, func(c *testutil.TestServerConfig) { + c.Server.Bootstrap = false + }) + defer s2.Stop() // Attempting to join a non-existent host returns error - if err := a.Join("nope"); err == nil { + n, err := a1.Join("nope") + if err == nil { t.Fatalf("expected error, got nothing") } + if n != 0 { + t.Fatalf("expected 0 nodes, got: %d", n) + } - // TODO(ryanuber): This is pretty much a worthless test, - // since we are just joining ourselves. Once the agent - // respects config options, change this to actually make - // two nodes and join them. - if err := a.Join("127.0.0.1"); err != nil { + // Returns correctly if join succeeds + n, err = a1.Join(s2.SerfAddr) + if err != nil { t.Fatalf("err: %s", err) } + if n != 1 { + t.Fatalf("expected 1 node, got: %d", n) + } } func TestAgent_Members(t *testing.T) {