2020-09-16 20:10:06 +00:00
|
|
|
package e2eutil
|
|
|
|
|
|
|
|
import (
|
2022-03-04 13:55:36 +00:00
|
|
|
"context"
|
2020-09-16 20:10:06 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
2021-01-22 14:18:17 +00:00
|
|
|
"strings"
|
2022-03-25 18:44:16 +00:00
|
|
|
"testing"
|
2022-03-04 13:55:36 +00:00
|
|
|
"time"
|
2022-03-25 18:44:16 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-09-16 20:10:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Register registers a jobspec from a file but with a unique ID.
|
|
|
|
// The caller is responsible for recording that ID for later cleanup.
|
|
|
|
func Register(jobID, jobFilePath string) error {
|
2022-03-04 13:55:36 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cancel()
|
|
|
|
return register(jobID, jobFilePath, exec.CommandContext(ctx, "nomad", "job", "run", "-detach", "-"))
|
2021-11-23 08:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterWithArgs registers a jobspec from a file but with a unique ID. The
|
|
|
|
// optional args are added to the run command. The caller is responsible for
|
|
|
|
// recording that ID for later cleanup.
|
|
|
|
func RegisterWithArgs(jobID, jobFilePath string, args ...string) error {
|
|
|
|
|
|
|
|
baseArgs := []string{"job", "run", "-detach"}
|
|
|
|
for i := range args {
|
|
|
|
baseArgs = append(baseArgs, args[i])
|
|
|
|
}
|
|
|
|
baseArgs = append(baseArgs, "-")
|
2022-03-04 13:55:36 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cancel()
|
2021-11-23 08:23:31 +00:00
|
|
|
|
2022-03-04 13:55:36 +00:00
|
|
|
return register(jobID, jobFilePath, exec.CommandContext(ctx, "nomad", baseArgs...))
|
2021-11-23 08:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func register(jobID, jobFilePath string, cmd *exec.Cmd) error {
|
2020-09-16 20:10:06 +00:00
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not open stdin?: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(jobFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not open job file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// hack off the first line to replace with our unique ID
|
2020-11-13 14:31:21 +00:00
|
|
|
var re = regexp.MustCompile(`(?m)^job ".*" \{`)
|
2020-09-16 20:10:06 +00:00
|
|
|
jobspec := re.ReplaceAllString(string(content),
|
|
|
|
fmt.Sprintf("job \"%s\" {", jobID))
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer stdin.Close()
|
|
|
|
io.WriteString(stdin, jobspec)
|
|
|
|
}()
|
|
|
|
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not register job: %w\n%v", err, string(out))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-14 12:43:28 +00:00
|
|
|
|
2021-03-24 21:42:10 +00:00
|
|
|
// PeriodicForce forces a periodic job to dispatch
|
2021-01-22 14:18:17 +00:00
|
|
|
func PeriodicForce(jobID string) error {
|
|
|
|
// nomad job periodic force
|
2022-03-04 13:55:36 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cancel()
|
|
|
|
cmd := exec.CommandContext(ctx, "nomad", "job", "periodic", "force", jobID)
|
2021-01-22 14:18:17 +00:00
|
|
|
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not register job: %w\n%v", err, string(out))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-24 21:42:10 +00:00
|
|
|
// Dispatch dispatches a parameterized job
|
|
|
|
func Dispatch(jobID string, meta map[string]string, payload string) error {
|
|
|
|
// nomad job periodic force
|
|
|
|
args := []string{"job", "dispatch"}
|
|
|
|
for k, v := range meta {
|
|
|
|
args = append(args, "-meta", fmt.Sprintf("%v=%v", k, v))
|
|
|
|
}
|
|
|
|
args = append(args, jobID)
|
|
|
|
if payload != "" {
|
|
|
|
args = append(args, "-")
|
|
|
|
}
|
|
|
|
|
2022-03-04 13:55:36 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cancel()
|
|
|
|
cmd := exec.CommandContext(ctx, "nomad", args...)
|
2021-03-24 21:42:10 +00:00
|
|
|
cmd.Stdin = strings.NewReader(payload)
|
|
|
|
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not dispatch job: %w\n%v", err, string(out))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-22 14:18:17 +00:00
|
|
|
// JobInspectTemplate runs nomad job inspect and formats the output
|
|
|
|
// using the specified go template
|
|
|
|
func JobInspectTemplate(jobID, template string) (string, error) {
|
2022-03-04 13:55:36 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cancel()
|
|
|
|
cmd := exec.CommandContext(ctx, "nomad", "job", "inspect", "-t", template, jobID)
|
2021-01-22 14:18:17 +00:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not inspect job: %w\n%v", err, string(out))
|
|
|
|
}
|
|
|
|
outStr := string(out)
|
|
|
|
outStr = strings.TrimSuffix(outStr, "\n")
|
|
|
|
return outStr, nil
|
|
|
|
}
|
|
|
|
|
2021-08-30 09:08:12 +00:00
|
|
|
// RegisterFromJobspec registers a jobspec from a string, also with a unique
|
|
|
|
// ID. The caller is responsible for recording that ID for later cleanup.
|
2020-10-14 12:43:28 +00:00
|
|
|
func RegisterFromJobspec(jobID, jobspec string) error {
|
2022-03-04 13:55:36 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cancel()
|
|
|
|
cmd := exec.CommandContext(ctx, "nomad", "job", "run", "-detach", "-")
|
2020-10-14 12:43:28 +00:00
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not open stdin?: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// hack off the first line to replace with our unique ID
|
|
|
|
var re = regexp.MustCompile(`^job "\w+" \{`)
|
|
|
|
jobspec = re.ReplaceAllString(jobspec,
|
|
|
|
fmt.Sprintf("job \"%s\" {", jobID))
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer stdin.Close()
|
|
|
|
io.WriteString(stdin, jobspec)
|
|
|
|
}()
|
|
|
|
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not register job: %w\n%v", err, string(out))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-18 17:21:53 +00:00
|
|
|
|
|
|
|
func ChildrenJobSummary(jobID string) ([]map[string]string, error) {
|
|
|
|
out, err := Command("nomad", "job", "status", jobID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("nomad job status failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
section, err := GetSection(out, "Children Job Summary")
|
|
|
|
if err != nil {
|
2021-03-24 21:42:10 +00:00
|
|
|
section, err = GetSection(out, "Parameterized Job Summary")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not find children job summary section: %w", err)
|
|
|
|
}
|
2021-02-18 17:21:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
summary, err := ParseColumns(section)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse children job summary section: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return summary, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func PreviouslyLaunched(jobID string) ([]map[string]string, error) {
|
|
|
|
out, err := Command("nomad", "job", "status", jobID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("nomad job status failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
section, err := GetSection(out, "Previously Launched Jobs")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not find previously launched jobs section: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
summary, err := ParseColumns(section)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse previously launched jobs section: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return summary, nil
|
|
|
|
}
|
2021-03-24 21:42:10 +00:00
|
|
|
|
|
|
|
func DispatchedJobs(jobID string) ([]map[string]string, error) {
|
|
|
|
out, err := Command("nomad", "job", "status", jobID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("nomad job status failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
section, err := GetSection(out, "Dispatched Jobs")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not find previously launched jobs section: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
summary, err := ParseColumns(section)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse previously launched jobs section: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return summary, nil
|
|
|
|
}
|
2022-02-01 13:16:37 +00:00
|
|
|
|
|
|
|
func StopJob(jobID string, args ...string) error {
|
|
|
|
|
|
|
|
// Build our argument list in the correct order, ensuring the jobID is last
|
|
|
|
// and the Nomad subcommand are first.
|
|
|
|
baseArgs := []string{"job", "stop"}
|
|
|
|
for i := range args {
|
|
|
|
baseArgs = append(baseArgs, args[i])
|
|
|
|
}
|
|
|
|
baseArgs = append(baseArgs, jobID)
|
|
|
|
|
|
|
|
// Execute the command. We do not care about the stdout, only stderr.
|
|
|
|
_, err := Command("nomad", baseArgs...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// When stopping a job and monitoring the resulting deployment, we
|
|
|
|
// expect that the monitor fails and exits with status code one because
|
|
|
|
// technically the deployment has failed. Overwrite the error to be
|
|
|
|
// nil.
|
2022-03-04 13:55:22 +00:00
|
|
|
if strings.Contains(err.Error(), "Description = Cancelled because job is stopped") ||
|
|
|
|
strings.Contains(err.Error(), "Description = Failed due to progress deadline") {
|
2022-02-01 13:16:37 +00:00
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2022-03-25 18:44:16 +00:00
|
|
|
|
|
|
|
// CleanupJobsAndGC stops and purges the list of jobIDs and runs a
|
|
|
|
// system gc. Returns a func so that the return value can be used
|
|
|
|
// in t.Cleanup
|
|
|
|
func CleanupJobsAndGC(t *testing.T, jobIDs *[]string) func() {
|
|
|
|
return func() {
|
|
|
|
for _, jobID := range *jobIDs {
|
|
|
|
err := StopJob(jobID, "-purge", "-detach")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
_, err := Command("nomad", "system", "gc")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 09:12:24 +00:00
|
|
|
|
|
|
|
// CleanupJobsAndGCWithContext stops and purges the list of jobIDs and runs a
|
|
|
|
// system gc. The passed context allows callers to cancel the execution of the
|
|
|
|
// cleanup as they desire. This is useful for tests which attempt to remove the
|
|
|
|
// job as part of their run, but may fail before that point is reached.
|
|
|
|
func CleanupJobsAndGCWithContext(t *testing.T, ctx context.Context, jobIDs *[]string) {
|
|
|
|
|
|
|
|
// Check the context before continuing. If this has been closed return,
|
|
|
|
// otherwise fallthrough and complete the work.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
for _, jobID := range *jobIDs {
|
|
|
|
err := StopJob(jobID, "-purge", "-detach")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
_, err := Command("nomad", "system", "gc")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|