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"
|
2015-06-04 11:26:16 +00:00
|
|
|
"time"
|
2015-09-07 01:07:05 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2019-01-15 19:46:12 +00:00
|
|
|
testing "github.com/mitchellh/go-testing-interface"
|
2019-01-08 21:21:49 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
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.
|
2017-07-25 21:21:59 +00:00
|
|
|
func WaitForLeader(t testing.T, rpc rpcFn) {
|
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
|
|
|
|
2019-04-28 21:32:07 +00:00
|
|
|
func RegisterJobWithToken(t testing.T, 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
|
|
|
|
args.WriteRequest.Region = "global"
|
2019-04-28 21:32:07 +00:00
|
|
|
args.AuthToken = token
|
|
|
|
args.Namespace = structs.DefaultNamespace
|
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
|
|
|
}
|
|
|
|
|
2019-04-28 21:32:07 +00:00
|
|
|
func RegisterJob(t testing.T, rpc rpcFn, job *structs.Job) {
|
|
|
|
RegisterJobWithToken(t, rpc, job, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func WaitForRunningWithToken(t testing.T, rpc rpcFn, job *structs.Job, token string) []*structs.AllocListStub {
|
|
|
|
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
|
|
|
|
|
|
|
|
WaitForResult(func() (bool, error) {
|
|
|
|
args := &structs.JobSpecificRequest{}
|
2018-09-06 00:06:30 +00:00
|
|
|
args.JobID = job.ID
|
|
|
|
args.QueryOptions.Region = "global"
|
2019-04-28 21:32:07 +00:00
|
|
|
args.AuthToken = token
|
|
|
|
args.Namespace = structs.DefaultNamespace
|
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 {
|
|
|
|
return false, fmt.Errorf("0 allocations")
|
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) {
|
2019-01-08 21:21:49 +00:00
|
|
|
require.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.
|
|
|
|
func WaitForRunning(t testing.T, rpc rpcFn, job *structs.Job) []*structs.AllocListStub {
|
|
|
|
return WaitForRunningWithToken(t, rpc, job, "")
|
|
|
|
}
|