2015-06-04 11:02:39 +00:00
|
|
|
package testutil
|
|
|
|
|
2015-06-04 11:26:16 +00:00
|
|
|
import (
|
2018-09-06 00:06:30 +00:00
|
|
|
"fmt"
|
2016-01-20 20:00:20 +00:00
|
|
|
"os"
|
2022-10-03 21:35:02 +00:00
|
|
|
"runtime"
|
2021-06-07 19:01:01 +00:00
|
|
|
"testing"
|
2015-06-04 11:26:16 +00:00
|
|
|
"time"
|
2015-09-07 01:07:05 +00:00
|
|
|
|
2023-01-25 20:53:59 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2015-09-07 01:07:05 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2019-10-01 20:06:24 +00:00
|
|
|
"github.com/kr/pretty"
|
2023-01-25 20:53:59 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2015-06-04 11:26:16 +00:00
|
|
|
)
|
2015-06-04 11:02:39 +00:00
|
|
|
|
|
|
|
type testFn func() (bool, error)
|
|
|
|
type errorFn func(error)
|
|
|
|
|
2022-09-13 19:52:47 +00:00
|
|
|
func Wait(t *testing.T, test testFn) {
|
|
|
|
t.Helper()
|
|
|
|
retries := 500 * TestMultiplier()
|
2022-10-03 21:35:02 +00:00
|
|
|
warn := int64(float64(retries) * 0.75)
|
|
|
|
for tries := retries; tries > 0; {
|
2022-09-13 19:52:47 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2022-10-03 21:35:02 +00:00
|
|
|
tries--
|
2022-09-13 19:52:47 +00:00
|
|
|
|
|
|
|
success, err := test()
|
|
|
|
if success {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:35:02 +00:00
|
|
|
switch tries {
|
|
|
|
case 0:
|
2022-09-13 19:52:47 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("timeout waiting for test function to succeed (you should probably return a helpful error instead of nil!)")
|
|
|
|
} else {
|
|
|
|
t.Fatalf("timeout: %v", err)
|
|
|
|
}
|
2022-10-03 21:35:02 +00:00
|
|
|
case warn:
|
|
|
|
pc, _, _, _ := runtime.Caller(1)
|
|
|
|
f := runtime.FuncForPC(pc)
|
|
|
|
t.Logf("%d/%d retries reached for %s (err=%v)", warn, retries, f.Name(), err)
|
2022-09-13 19:52:47 +00:00
|
|
|
}
|
2022-10-03 21:35:02 +00:00
|
|
|
|
2022-09-13 19:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-04 11:02:39 +00:00
|
|
|
func WaitForResult(test testFn, error errorFn) {
|
2018-03-28 18:57:47 +00:00
|
|
|
WaitForResultRetries(500*TestMultiplier(), test, error)
|
2016-01-20 20:00:20 +00:00
|
|
|
}
|
2015-06-04 11:02:39 +00:00
|
|
|
|
2016-01-21 20:29:13 +00:00
|
|
|
func WaitForResultRetries(retries int64, test testFn, error errorFn) {
|
2015-06-04 11:02:39 +00:00
|
|
|
for retries > 0 {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
retries--
|
|
|
|
|
|
|
|
success, err := test()
|
|
|
|
if success {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if retries == 0 {
|
|
|
|
error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-04 11:26:16 +00:00
|
|
|
|
2021-06-21 18:22:49 +00:00
|
|
|
// WaitForResultUntil waits the duration for the test to pass.
|
|
|
|
// Otherwise error is called after the deadline expires.
|
|
|
|
func WaitForResultUntil(until time.Duration, test testFn, errorFunc errorFn) {
|
|
|
|
var success bool
|
|
|
|
var err error
|
|
|
|
deadline := time.Now().Add(until)
|
|
|
|
for time.Now().Before(deadline) {
|
|
|
|
success, err = test()
|
|
|
|
if success {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Sleep some arbitrary fraction of the deadline
|
|
|
|
time.Sleep(until / 30)
|
|
|
|
}
|
|
|
|
errorFunc(err)
|
|
|
|
}
|
|
|
|
|
2017-04-07 00:05:09 +00:00
|
|
|
// AssertUntil asserts the test function passes throughout the given duration.
|
|
|
|
// Otherwise error is called on failure.
|
|
|
|
func AssertUntil(until time.Duration, test testFn, error errorFn) {
|
|
|
|
deadline := time.Now().Add(until)
|
|
|
|
for time.Now().Before(deadline) {
|
|
|
|
success, err := test()
|
|
|
|
if !success {
|
|
|
|
error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Sleep some arbitrary fraction of the deadline
|
|
|
|
time.Sleep(until / 30)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 20:00:20 +00:00
|
|
|
// TestMultiplier returns a multiplier for retries and waits given environment
|
|
|
|
// the tests are being run under.
|
2016-01-21 20:29:13 +00:00
|
|
|
func TestMultiplier() int64 {
|
2019-02-20 12:52:27 +00:00
|
|
|
if IsCI() {
|
2018-03-28 18:57:47 +00:00
|
|
|
return 4
|
2016-01-20 20:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-04-03 23:51:57 +00:00
|
|
|
// Timeout takes the desired timeout and increases it if running in Travis
|
|
|
|
func Timeout(original time.Duration) time.Duration {
|
|
|
|
return original * time.Duration(TestMultiplier())
|
|
|
|
}
|
|
|
|
|
2019-02-20 12:41:47 +00:00
|
|
|
func IsCI() bool {
|
|
|
|
_, ok := os.LookupEnv("CI")
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:28:48 +00:00
|
|
|
func IsTravis() bool {
|
2019-02-20 12:41:47 +00:00
|
|
|
_, ok := os.LookupEnv("TRAVIS")
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsAppVeyor() bool {
|
|
|
|
_, ok := os.LookupEnv("APPVEYOR")
|
2016-01-21 21:28:48 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2015-06-04 11:26:16 +00:00
|
|
|
type rpcFn func(string, interface{}, interface{}) error
|
|
|
|
|
2018-09-06 00:06:30 +00:00
|
|
|
// WaitForLeader blocks until a leader is elected.
|
2021-06-07 19:01:01 +00:00
|
|
|
func WaitForLeader(t testing.TB, rpc rpcFn) {
|
2020-03-06 19:39:39 +00:00
|
|
|
t.Helper()
|
2015-06-04 11:26:16 +00:00
|
|
|
WaitForResult(func() (bool, error) {
|
2015-09-07 01:07:05 +00:00
|
|
|
args := &structs.GenericRequest{}
|
2015-06-04 11:26:16 +00:00
|
|
|
var leader string
|
|
|
|
err := rpc("Status.Leader", args, &leader)
|
|
|
|
return leader != "", err
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("failed to find leader: %v", err)
|
|
|
|
})
|
|
|
|
}
|
2018-09-06 00:06:30 +00:00
|
|
|
|
2020-11-12 16:25:28 +00:00
|
|
|
// WaitForClient blocks until the client can be found
|
2021-10-12 20:58:41 +00:00
|
|
|
func WaitForClient(t testing.TB, rpc rpcFn, nodeID string, region string) {
|
2023-01-25 20:53:59 +00:00
|
|
|
t.Helper()
|
|
|
|
WaitForClientStatus(t, rpc, nodeID, region, structs.NodeStatusReady)
|
|
|
|
}
|
2021-10-12 20:58:41 +00:00
|
|
|
|
2023-01-25 20:53:59 +00:00
|
|
|
// WaitForClientStatus blocks until the client is in the expected status.
|
|
|
|
func WaitForClientStatus(t testing.TB, rpc rpcFn, nodeID string, region string, status string) {
|
2020-11-12 16:25:28 +00:00
|
|
|
t.Helper()
|
2021-10-12 20:58:41 +00:00
|
|
|
|
|
|
|
if region == "" {
|
|
|
|
region = "global"
|
|
|
|
}
|
2020-11-12 16:25:28 +00:00
|
|
|
WaitForResult(func() (bool, error) {
|
|
|
|
req := structs.NodeSpecificRequest{
|
|
|
|
NodeID: nodeID,
|
2021-10-12 20:58:41 +00:00
|
|
|
QueryOptions: structs.QueryOptions{Region: region},
|
2020-11-12 16:25:28 +00:00
|
|
|
}
|
|
|
|
var out structs.SingleNodeResponse
|
|
|
|
|
|
|
|
err := rpc("Node.GetNode", &req, &out)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if out.Node == nil {
|
|
|
|
return false, fmt.Errorf("node not found")
|
|
|
|
}
|
2023-01-25 20:53:59 +00:00
|
|
|
if out.Node.Status != status {
|
|
|
|
return false, fmt.Errorf("node is %s, not %s", out.Node.Status, status)
|
|
|
|
}
|
|
|
|
return true, nil
|
2020-11-12 16:25:28 +00:00
|
|
|
}, func(err error) {
|
2023-01-25 20:53:59 +00:00
|
|
|
t.Fatalf("failed to wait for node staus: %v", err)
|
2020-11-12 16:25:28 +00:00
|
|
|
})
|
2021-10-12 20:58:41 +00:00
|
|
|
|
2023-01-25 20:53:59 +00:00
|
|
|
t.Logf("[TEST] Client for test %s %s, id: %s, region: %s", t.Name(), status, nodeID, region)
|
2020-11-12 16:25:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 12:49:33 +00:00
|
|
|
// WaitForVotingMembers blocks until autopilot promotes all server peers
|
|
|
|
// to be voting members.
|
|
|
|
//
|
|
|
|
// Useful for tests that change cluster topology (e.g. kill a node)
|
|
|
|
// that should wait until cluster is stable.
|
2021-06-07 19:01:01 +00:00
|
|
|
func WaitForVotingMembers(t testing.TB, rpc rpcFn, nPeers int) {
|
2019-09-04 12:49:33 +00:00
|
|
|
WaitForResult(func() (bool, error) {
|
|
|
|
args := &structs.GenericRequest{}
|
|
|
|
args.AllowStale = true
|
|
|
|
args.Region = "global"
|
|
|
|
args.Namespace = structs.DefaultNamespace
|
|
|
|
resp := structs.RaftConfigurationResponse{}
|
|
|
|
err := rpc("Operator.RaftGetConfiguration", args, &resp)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to query raft: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Servers) != nPeers {
|
|
|
|
return false, fmt.Errorf("expected %d peers found %d", nPeers, len(resp.Servers))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range resp.Servers {
|
|
|
|
if !s.Voter {
|
|
|
|
return false, fmt.Errorf("found nonvoting server: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("failed to wait until voting members: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// RegisterJobWithToken registers a job and uses the job's Region and Namespace.
|
2021-06-07 19:01:01 +00:00
|
|
|
func RegisterJobWithToken(t testing.TB, rpc rpcFn, job *structs.Job, token string) {
|
2018-09-06 00:06:30 +00:00
|
|
|
WaitForResult(func() (bool, error) {
|
2018-09-27 00:33:32 +00:00
|
|
|
args := &structs.JobRegisterRequest{}
|
|
|
|
args.Job = job
|
2019-10-01 20:06:24 +00:00
|
|
|
args.WriteRequest.Region = job.Region
|
2019-04-28 21:32:07 +00:00
|
|
|
args.AuthToken = token
|
2019-10-01 20:06:24 +00:00
|
|
|
args.Namespace = job.Namespace
|
2018-09-27 00:33:32 +00:00
|
|
|
var jobResp structs.JobRegisterResponse
|
|
|
|
err := rpc("Job.Register", args, &jobResp)
|
|
|
|
return err == nil, fmt.Errorf("Job.Register error: %v", err)
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("error registering job: %v", err)
|
|
|
|
})
|
2018-09-06 00:06:30 +00:00
|
|
|
|
2018-09-27 00:33:32 +00:00
|
|
|
t.Logf("Job %q registered", job.ID)
|
2019-01-08 21:21:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 19:01:01 +00:00
|
|
|
func RegisterJob(t testing.TB, rpc rpcFn, job *structs.Job) {
|
2019-04-28 21:32:07 +00:00
|
|
|
RegisterJobWithToken(t, rpc, job, "")
|
|
|
|
}
|
|
|
|
|
2021-06-07 19:01:01 +00:00
|
|
|
func WaitForRunningWithToken(t testing.TB, rpc rpcFn, job *structs.Job, token string) []*structs.AllocListStub {
|
2019-04-28 21:32:07 +00:00
|
|
|
RegisterJobWithToken(t, rpc, job, token)
|
2018-09-06 00:06:30 +00:00
|
|
|
|
2018-09-27 00:33:32 +00:00
|
|
|
var resp structs.JobAllocationsResponse
|
|
|
|
|
2021-10-18 17:32:41 +00:00
|
|
|
// This can be quite slow if the job has expensive setup such as
|
|
|
|
// downloading large artifacts or creating a chroot.
|
|
|
|
WaitForResultRetries(2000*TestMultiplier(), func() (bool, error) {
|
2018-09-27 00:33:32 +00:00
|
|
|
args := &structs.JobSpecificRequest{}
|
2018-09-06 00:06:30 +00:00
|
|
|
args.JobID = job.ID
|
2019-10-01 20:06:24 +00:00
|
|
|
args.QueryOptions.Region = job.Region
|
2019-04-28 21:32:07 +00:00
|
|
|
args.AuthToken = token
|
2019-10-01 20:06:24 +00:00
|
|
|
args.Namespace = job.Namespace
|
2018-09-27 00:33:32 +00:00
|
|
|
err := rpc("Job.Allocations", args, &resp)
|
2018-09-06 00:06:30 +00:00
|
|
|
if err != nil {
|
2018-09-27 00:33:32 +00:00
|
|
|
return false, fmt.Errorf("Job.Allocations error: %v", err)
|
2018-09-06 00:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 00:33:32 +00:00
|
|
|
if len(resp.Allocations) == 0 {
|
2019-10-01 20:06:24 +00:00
|
|
|
evals := structs.JobEvaluationsResponse{}
|
2023-02-27 18:17:47 +00:00
|
|
|
must.NoError(t, rpc("Job.Evaluations", args, &evals), must.Sprintf("error looking up evals"))
|
2019-10-01 20:06:24 +00:00
|
|
|
return false, fmt.Errorf("0 allocations; evals: %s", pretty.Sprint(evals.Evaluations))
|
2018-09-06 00:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 00:33:32 +00:00
|
|
|
for _, alloc := range resp.Allocations {
|
2019-01-10 20:36:57 +00:00
|
|
|
if alloc.ClientStatus == structs.AllocClientStatusPending {
|
2018-09-27 00:33:32 +00:00
|
|
|
return false, fmt.Errorf("alloc not running: id=%v tg=%v status=%v",
|
|
|
|
alloc.ID, alloc.TaskGroup, alloc.ClientStatus)
|
2018-09-06 00:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
2023-02-27 18:17:47 +00:00
|
|
|
must.NoError(t, err)
|
2018-09-06 00:06:30 +00:00
|
|
|
})
|
2018-09-27 00:33:32 +00:00
|
|
|
|
|
|
|
return resp.Allocations
|
2018-09-06 00:06:30 +00:00
|
|
|
}
|
2019-04-28 21:32:07 +00:00
|
|
|
|
|
|
|
// WaitForRunning runs a job and blocks until all allocs are out of pending.
|
2021-06-07 19:01:01 +00:00
|
|
|
func WaitForRunning(t testing.TB, rpc rpcFn, job *structs.Job) []*structs.AllocListStub {
|
2019-04-28 21:32:07 +00:00
|
|
|
return WaitForRunningWithToken(t, rpc, job, "")
|
|
|
|
}
|
2020-10-14 19:16:10 +00:00
|
|
|
|
2023-01-25 20:53:59 +00:00
|
|
|
// WaitforJobAllocStatus blocks until the ClientStatus of allocations for a job
|
|
|
|
// match the expected map of <ClientStatus>: <count>.
|
|
|
|
func WaitForJobAllocStatus(t testing.TB, rpc rpcFn, job *structs.Job, allocStatus map[string]int) {
|
|
|
|
t.Helper()
|
|
|
|
WaitForJobAllocStatusWithToken(t, rpc, job, allocStatus, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForJobAllocStatusWithToken behaves the same way as WaitForJobAllocStatus
|
|
|
|
// but is used for clusters with ACL enabled.
|
2023-02-27 18:17:47 +00:00
|
|
|
func WaitForJobAllocStatusWithToken(t testing.TB, rpc rpcFn, job *structs.Job, allocStatus map[string]int, token string) []*structs.AllocListStub {
|
2023-01-25 20:53:59 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2023-02-27 18:17:47 +00:00
|
|
|
var allocs []*structs.AllocListStub
|
2023-01-25 20:53:59 +00:00
|
|
|
WaitForResultRetries(2000*TestMultiplier(), func() (bool, error) {
|
|
|
|
args := &structs.JobSpecificRequest{
|
|
|
|
JobID: job.ID,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
AuthToken: token,
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
Region: job.Region,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp structs.JobAllocationsResponse
|
|
|
|
err := rpc("Job.Allocations", args, &resp)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Job.Allocations error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Allocations) == 0 {
|
|
|
|
evals := structs.JobEvaluationsResponse{}
|
2023-02-27 18:17:47 +00:00
|
|
|
must.NoError(t, rpc("Job.Evaluations", args, &evals), must.Sprintf("error looking up evals"))
|
2023-01-25 20:53:59 +00:00
|
|
|
return false, fmt.Errorf("0 allocations; evals: %s", pretty.Sprint(evals.Evaluations))
|
|
|
|
}
|
|
|
|
|
2023-02-27 18:17:47 +00:00
|
|
|
allocs = resp.Allocations
|
|
|
|
|
2023-01-25 20:53:59 +00:00
|
|
|
got := map[string]int{}
|
|
|
|
for _, alloc := range resp.Allocations {
|
|
|
|
got[alloc.ClientStatus]++
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(allocStatus, got); diff != "" {
|
|
|
|
return false, fmt.Errorf("alloc status mismatch (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
must.NoError(t, err)
|
|
|
|
})
|
2023-02-27 18:17:47 +00:00
|
|
|
|
|
|
|
return allocs
|
2023-01-25 20:53:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 19:16:10 +00:00
|
|
|
// WaitForFiles blocks until all the files in the slice are present
|
2021-06-07 19:01:01 +00:00
|
|
|
func WaitForFiles(t testing.TB, files []string) {
|
2020-10-14 19:16:10 +00:00
|
|
|
WaitForResult(func() (bool, error) {
|
2021-10-13 22:00:55 +00:00
|
|
|
return FilesExist(files)
|
2020-10-14 19:16:10 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("missing expected files: %v", err)
|
|
|
|
})
|
|
|
|
}
|
2021-06-21 18:22:49 +00:00
|
|
|
|
|
|
|
// WaitForFilesUntil blocks until duration or all the files in the slice are present
|
|
|
|
func WaitForFilesUntil(t testing.TB, files []string, until time.Duration) {
|
|
|
|
WaitForResultUntil(until, func() (bool, error) {
|
2021-10-13 22:00:55 +00:00
|
|
|
return FilesExist(files)
|
2021-06-21 18:22:49 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("missing expected files: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilesExist verifies all files in the slice are present
|
2021-10-13 22:00:55 +00:00
|
|
|
func FilesExist(files []string) (bool, error) {
|
2021-06-21 18:22:49 +00:00
|
|
|
for _, f := range files {
|
|
|
|
if _, err := os.Stat(f); os.IsNotExist(err) {
|
2021-10-13 22:00:55 +00:00
|
|
|
return false, fmt.Errorf("expected file not found: %v", f)
|
2021-06-21 18:22:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-13 22:00:55 +00:00
|
|
|
return true, nil
|
2021-06-21 18:22:49 +00:00
|
|
|
}
|