api: add new test case for force-leave (#16260)

Signed-off-by: dttung2905 <ttdao.2015@accountancy.smu.edu.sg>
This commit is contained in:
Dao Thanh Tung 2023-03-03 23:38:40 +08:00 committed by GitHub
parent e81fecdd1f
commit 62a69552c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 3 deletions

View File

@ -113,7 +113,7 @@ 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. Returns the
// We attempt to join all 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) (int, error) {

View File

@ -1,6 +1,7 @@
package api
import (
"fmt"
"sort"
"strings"
"testing"
@ -8,6 +9,7 @@ import (
"github.com/hashicorp/nomad/api/internal/testutil"
"github.com/shoenig/test/must"
"github.com/shoenig/test/wait"
)
func TestAgent_Self(t *testing.T) {
@ -110,7 +112,43 @@ func TestAgent_ForceLeave(t *testing.T) {
err := a.ForceLeave("nope")
must.NoError(t, err)
// TODO: test force-leave on an existing node
// Force-leave on an existing node
_, s2 := makeClient(t, nil, func(c *testutil.TestServerConfig) {
c.Server.BootstrapExpect = 0
})
defer s2.Stop()
// Create a new node to join
n, err := a.Join(s2.SerfAddr)
must.NoError(t, err)
must.One(t, n)
membersBefore, err := a.MembersOpts(&QueryOptions{})
must.Eq(t, membersBefore.Members[1].Status, "alive")
err = a.ForceLeave(membersBefore.Members[1].Name)
must.NoError(t, err)
time.Sleep(3 * time.Second)
f := func() error {
membersAfter, err := a.MembersOpts(&QueryOptions{})
if err != nil {
return err
}
for _, node := range membersAfter.Members {
if node.Name == membersBefore.Members[1].Name {
if node.Status != "leaving" {
return fmt.Errorf("node did not leave")
}
}
}
return nil
}
must.Wait(t, wait.InitialSuccess(
wait.ErrorFunc(f),
wait.Timeout(3*time.Second),
wait.Gap(100*time.Millisecond),
))
}
func (a *AgentMember) String() string {

View File

@ -35,7 +35,7 @@ func (c *Client) Nodes() *Nodes {
return &Nodes{client: c}
}
// List is used to list out all of the nodes
// List is used to list out all the nodes
func (n *Nodes) List(q *QueryOptions) ([]*NodeListStub, *QueryMeta, error) {
var resp NodeIndexSort
qm, err := n.client.query("/v1/nodes", &resp, q)