2018-12-20 03:25:32 +00:00
|
|
|
package e2eutil
|
|
|
|
|
|
|
|
import (
|
2019-01-08 22:37:08 +00:00
|
|
|
"fmt"
|
2019-01-04 13:53:50 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-12-20 03:25:32 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/hashicorp/nomad/helper"
|
|
|
|
"github.com/hashicorp/nomad/jobspec"
|
2019-01-18 04:32:45 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-12-20 03:25:32 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2019-01-26 00:51:20 +00:00
|
|
|
"github.com/kr/pretty"
|
2018-12-20 03:25:32 +00:00
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// retries is used to control how many times to retry checking if the cluster has a leader yet
|
|
|
|
const retries = 500
|
|
|
|
|
|
|
|
func WaitForLeader(t *testing.T, nomadClient *api.Client) {
|
|
|
|
statusAPI := nomadClient.Status()
|
|
|
|
|
|
|
|
testutil.WaitForResultRetries(retries, func() (bool, error) {
|
|
|
|
leader, err := statusAPI.Leader()
|
|
|
|
return leader != "", err
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("failed to find leader: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-08 22:37:08 +00:00
|
|
|
// WaitForNodesReady waits until at least `nodes` number of nodes are ready or
|
|
|
|
// fails the test.
|
2019-01-03 22:16:20 +00:00
|
|
|
func WaitForNodesReady(t *testing.T, nomadClient *api.Client, nodes int) {
|
|
|
|
nodesAPI := nomadClient.Nodes()
|
|
|
|
|
|
|
|
testutil.WaitForResultRetries(retries, func() (bool, error) {
|
2019-01-18 04:32:45 +00:00
|
|
|
defer time.Sleep(time.Millisecond * 100)
|
2019-01-03 22:16:20 +00:00
|
|
|
nodesList, _, err := nodesAPI.List(nil)
|
2019-01-08 22:37:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error listing nodes: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-01-03 22:16:20 +00:00
|
|
|
eligibleNodes := 0
|
|
|
|
for _, node := range nodesList {
|
|
|
|
if node.Status == "ready" {
|
|
|
|
eligibleNodes++
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 22:37:08 +00:00
|
|
|
|
|
|
|
return eligibleNodes >= nodes, fmt.Errorf("only %d nodes ready (wanted at least %d)", eligibleNodes, nodes)
|
2019-01-03 22:16:20 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("failed to get enough ready nodes: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-18 04:32:45 +00:00
|
|
|
func RegisterAndWaitForAllocs(t *testing.T, nomadClient *api.Client, jobFile string, jobID string) []*api.AllocationListStub {
|
2018-12-20 03:25:32 +00:00
|
|
|
// Parse job
|
|
|
|
job, err := jobspec.ParseFile(jobFile)
|
2019-01-18 04:32:45 +00:00
|
|
|
require := require.New(t)
|
2018-12-20 03:25:32 +00:00
|
|
|
require.Nil(err)
|
2019-01-03 22:16:20 +00:00
|
|
|
job.ID = helper.StringToPtr(jobID)
|
2018-12-20 03:25:32 +00:00
|
|
|
|
2019-01-26 00:51:20 +00:00
|
|
|
g := NewGomegaWithT(t)
|
|
|
|
|
2018-12-20 03:25:32 +00:00
|
|
|
// Register job
|
|
|
|
jobs := nomadClient.Jobs()
|
2019-01-26 00:51:20 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
resp, _, err := jobs.Register(job, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return resp.EvalID != "", fmt.Errorf("expected EvalID:%s", pretty.Sprint(resp))
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
2018-12-20 03:25:32 +00:00
|
|
|
|
|
|
|
// Wrap in retry to wait until placement
|
|
|
|
g.Eventually(func() []*api.AllocationListStub {
|
|
|
|
// Look for allocations
|
|
|
|
allocs, _, _ := jobs.Allocations(*job.ID, false, nil)
|
|
|
|
return allocs
|
2019-01-03 20:02:02 +00:00
|
|
|
}, 30*time.Second, time.Second).ShouldNot(BeEmpty())
|
2018-12-20 03:25:32 +00:00
|
|
|
|
|
|
|
allocs, _, err := jobs.Allocations(*job.ID, false, nil)
|
|
|
|
require.Nil(err)
|
2019-01-03 22:16:20 +00:00
|
|
|
return allocs
|
2018-12-20 03:25:32 +00:00
|
|
|
}
|
2019-01-18 04:32:45 +00:00
|
|
|
|
|
|
|
func WaitForAllocRunning(t *testing.T, nomadClient *api.Client, allocID string) {
|
|
|
|
testutil.WaitForResultRetries(retries, func() (bool, error) {
|
2019-01-23 19:09:49 +00:00
|
|
|
time.Sleep(time.Millisecond * 100)
|
2019-01-18 04:32:45 +00:00
|
|
|
alloc, _, err := nomadClient.Allocations().Info(allocID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return alloc.ClientStatus == structs.AllocClientStatusRunning, fmt.Errorf("expected status running, but was: %s", alloc.ClientStatus)
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("failed to wait on alloc: %v", err)
|
|
|
|
})
|
|
|
|
}
|