Skip to content

Commit

Permalink
periodic e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
drewbailey committed Jan 19, 2021
1 parent 3ed335f commit 09d5cbb
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 12 deletions.
1 change: 1 addition & 0 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
_ "github.com/hashicorp/nomad/e2e/nodedrain"
_ "github.com/hashicorp/nomad/e2e/nomad09upgrade"
_ "github.com/hashicorp/nomad/e2e/nomadexec"
_ "github.com/hashicorp/nomad/e2e/periodic"
_ "github.com/hashicorp/nomad/e2e/podman"
_ "github.com/hashicorp/nomad/e2e/quotas"
_ "github.com/hashicorp/nomad/e2e/rescheduling"
Expand Down
29 changes: 28 additions & 1 deletion e2e/e2eutil/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"io/ioutil"
"os/exec"
"regexp"
"strings"
)

// 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 {

cmd := exec.Command("nomad", "job", "run", "-")
stdin, err := cmd.StdinPipe()
if err != nil {
Expand Down Expand Up @@ -40,6 +40,33 @@ func Register(jobID, jobFilePath string) error {
return nil
}

// PeriodicForce forces a periodic job to dispatch, returning the child job ID
// or an error
func PeriodicForce(jobID string) error {
// nomad job periodic force
cmd := exec.Command("nomad", "job", "periodic", "force", jobID)

out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("could not register job: %w\n%v", err, string(out))
}

return nil
}

// JobInspectTemplate runs nomad job inspect and formats the output
// using the specified go template
func JobInspectTemplate(jobID, template string) (string, error) {
cmd := exec.Command("nomad", "job", "inspect", "-t", template, jobID)
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
}

// Register registers a jobspec from a string, also with a unique ID.
// The caller is responsible for recording that ID for later cleanup.
func RegisterFromJobspec(jobID, jobspec string) error {
Expand Down
13 changes: 10 additions & 3 deletions e2e/periodic/input/simple.nomad
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
job "test" {
job "periodic" {
datacenters = ["dc1"]
type = "batch"

constraint {
attribute = "${attr.kernel.name}"
value = "linux"
}


periodic {
cron = "* * * * *"
prohibit_overlap = true
Expand All @@ -12,8 +18,9 @@ job "test" {
driver = "docker"

config {
image = "alpine:latest"
command = "ls"
image = "busybox:1"
command = "/bin/sh"
args = ["-c", "sleep 5"]
}
}
}
Expand Down
38 changes: 34 additions & 4 deletions e2e/periodic/periodic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/hashicorp/nomad/e2e/e2eutil"
"github.com/hashicorp/nomad/e2e/framework"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/require"
)

type PeriodicTest struct {
Expand Down Expand Up @@ -41,12 +43,40 @@ func (tc *PeriodicTest) AfterEach(f *framework.F) {
func (tc *PeriodicTest) TestPeriodicDispatch_Basic(f *framework.F) {
t := f.T()

nomadClient := tc.Nomad()

uuid := uuid.Generate()
jobID := fmt.Sprintf("deployment-%s", uuid[0:8])
jobID := fmt.Sprintf("periodicjob-%s", uuid[0:8])
tc.jobIDs = append(tc.jobIDs, jobID)

// register job
e2eutil.RegisterAndWaitForAllocs(t, nomadClient, "periodic/input/simple.nomad", jobID, "")
e2eutil.Register(jobID, "periodic/input/simple.nomad")

// force dispatch
require.NoError(t, e2eutil.PeriodicForce(jobID))

// Get the child job ID
childID, err := e2eutil.JobInspectTemplate(jobID, `{{with index . 1}}{{printf "%s" .ID}}{{end}}`)
require.NoError(t, err)
require.NotEmpty(t, childID)

testutil.WaitForResult(func() (bool, error) {
status, err := e2eutil.JobInspectTemplate(jobID, `{{with index . 1}}{{printf "%s" .Status}}{{end}}`)
require.NoError(t, err)
require.NotEmpty(t, status)
if status == "dead" {
return true, nil
}
return false, fmt.Errorf("expected periodic job to be dead, got %s", status)
}, func(err error) {
require.NoError(t, err)
})

// Assert there are no pending children
pending, err := e2eutil.JobInspectTemplate(jobID, `{{with index . 0}}{{printf "%d" .JobSummary.Children.Pending}}{{end}}`)
require.NoError(t, err)
require.Equal(t, "0", pending)

// Assert there are no pending children
dead, err := e2eutil.JobInspectTemplate(jobID, `{{with index . 0}}{{printf "%d" .JobSummary.Children.Dead}}{{end}}`)
require.NoError(t, err)
require.Equal(t, "1", dead)
}
3 changes: 3 additions & 0 deletions e2e/terraform/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
NOMAD_SHA ?= $(shell git rev-parse HEAD)
PKG_PATH = $(shell pwd)/../../pkg/linux_amd64/nomad

# The version of nomad that gets deployed depends on an order of precedence
# linked below
# https://github.com/hashicorp/nomad/blob/master/e2e/terraform/README.md#nomad-version
dev-cluster:
terraform apply -auto-approve \
-var="nomad_sha=$(NOMAD_SHA)"
Expand Down
6 changes: 2 additions & 4 deletions nomad/fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3439,9 +3439,7 @@ func TestFSM_EventBroker_JobRegisterFSMEvents(t *testing.T) {
Eval: eval,
}
buf, err := structs.Encode(structs.JobRegisterRequestType, req)
if err != nil {
t.Fatalf("err: %v", err)
}
require.NoError(t, err)

resp := fsm.Apply(makeLog(buf))
require.Nil(t, resp)
Expand Down Expand Up @@ -3479,5 +3477,5 @@ func TestFSM_EventBroker_JobRegisterFSMEvents(t *testing.T) {
}

require.Len(t, events, 1)
require.Equal(t, events[0].Type, structs.TypeJobRegistered)
require.Equal(t, structs.TypeJobRegistered, events[0].Type)
}

0 comments on commit 09d5cbb

Please sign in to comment.