mirror of
https://github.com/kemko/nomad.git
synced 2026-01-08 11:25:41 +03:00
fixing up code review feedback
This commit is contained in:
@@ -8,10 +8,67 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/hashicorp/nomad/testutil"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const sleepJobOne = `job "sleep" {
|
||||
type = "batch"
|
||||
datacenters = ["dc1"]
|
||||
constraint {
|
||||
attribute = "${meta.secondary}"
|
||||
value = 1
|
||||
}
|
||||
group "group1" {
|
||||
restart {
|
||||
mode = "fail"
|
||||
}
|
||||
count = 1
|
||||
ephemeral_disk {
|
||||
migrate = true
|
||||
sticky = true
|
||||
}
|
||||
task "sleep" {
|
||||
template {
|
||||
data = "hello world"
|
||||
destination = "/local/hello-world"
|
||||
}
|
||||
driver = "exec"
|
||||
config {
|
||||
command = "/bin/sleep"
|
||||
args = [ "infinity" ]
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
const sleepJobTwo = `job "sleep" {
|
||||
type = "batch"
|
||||
datacenters = ["dc1"]
|
||||
constraint {
|
||||
attribute = "${meta.secondary}"
|
||||
value = 0
|
||||
}
|
||||
group "group1" {
|
||||
restart {
|
||||
mode = "fail"
|
||||
}
|
||||
count = 1
|
||||
ephemeral_disk {
|
||||
migrate = true
|
||||
sticky = true
|
||||
}
|
||||
task "sleep" {
|
||||
driver = "exec"
|
||||
|
||||
config {
|
||||
command = "test"
|
||||
args = [ "-f", "/local/hello-world" ]
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
func isSuccess(execCmd *exec.Cmd, retries int, keyword string) (string, error) {
|
||||
var successOut string
|
||||
var err error
|
||||
@@ -41,17 +98,23 @@ func isSuccess(execCmd *exec.Cmd, retries int, keyword string) (string, error) {
|
||||
return successOut, err
|
||||
}
|
||||
|
||||
func allNomadNodesAreReady(retries int) (string, error) {
|
||||
// allNodesAreReady attempts to query the status of a cluster a specific number
|
||||
// of times
|
||||
func allNodesAreReady(retries int) (string, error) {
|
||||
cmd := exec.Command("nomad", "node-status")
|
||||
return isSuccess(cmd, retries, "initializing")
|
||||
}
|
||||
|
||||
// jobIsReady attempts sto query the status of a specific job a fixed number of
|
||||
// times
|
||||
func jobIsReady(retries int, jobName string) (string, error) {
|
||||
cmd := exec.Command("nomad", "job", "status", jobName)
|
||||
return isSuccess(cmd, retries, "pending")
|
||||
}
|
||||
|
||||
// requires nomad executable on the path
|
||||
// startCluster will create a running cluster, given a list of agent config
|
||||
// files. In order to have a complete cluster, at least one server and one
|
||||
// client config file should be included.
|
||||
func startCluster(clusterConfig []string) (func(), error) {
|
||||
cmds := make([]*exec.Cmd, 0)
|
||||
|
||||
@@ -84,43 +147,14 @@ func TestJobMigrations(t *testing.T) {
|
||||
assert.Nil(err)
|
||||
defer stopCluster()
|
||||
|
||||
_, err = allNomadNodesAreReady(10)
|
||||
_, err = allNodesAreReady(10)
|
||||
assert.Nil(err)
|
||||
|
||||
fh, err := ioutil.TempFile("", "nomad-sleep-1")
|
||||
assert.Nil(err)
|
||||
|
||||
defer os.Remove(fh.Name())
|
||||
_, err = fh.WriteString(`
|
||||
job "sleep" {
|
||||
type = "batch"
|
||||
datacenters = ["dc1"]
|
||||
constraint {
|
||||
attribute = "${meta.secondary}"
|
||||
value = 1
|
||||
}
|
||||
group "group1" {
|
||||
restart {
|
||||
mode = "fail"
|
||||
}
|
||||
count = 1
|
||||
ephemeral_disk {
|
||||
migrate = true
|
||||
sticky = true
|
||||
}
|
||||
task "sleep" {
|
||||
template {
|
||||
data = "hello world"
|
||||
destination = "/local/hello-world"
|
||||
}
|
||||
driver = "exec"
|
||||
config {
|
||||
command = "/bin/sleep"
|
||||
args = [ "infinity" ]
|
||||
}
|
||||
}
|
||||
}
|
||||
}`)
|
||||
_, err = fh.WriteString(sleepJobOne)
|
||||
|
||||
assert.Nil(err)
|
||||
|
||||
@@ -136,33 +170,7 @@ func TestJobMigrations(t *testing.T) {
|
||||
assert.Nil(err)
|
||||
|
||||
defer os.Remove(fh2.Name())
|
||||
_, err = fh2.WriteString(`
|
||||
job "sleep" {
|
||||
type = "batch"
|
||||
datacenters = ["dc1"]
|
||||
constraint {
|
||||
attribute = "${meta.secondary}"
|
||||
value = 1
|
||||
}
|
||||
group "group1" {
|
||||
restart {
|
||||
mode = "fail"
|
||||
}
|
||||
count = 1
|
||||
ephemeral_disk {
|
||||
migrate = true
|
||||
sticky = true
|
||||
}
|
||||
task "sleep" {
|
||||
driver = "exec"
|
||||
|
||||
config {
|
||||
command = "test"
|
||||
args = [ "-f", "/local/hello-world" ]
|
||||
}
|
||||
}
|
||||
}
|
||||
}`)
|
||||
_, err = fh2.WriteString(sleepJobTwo)
|
||||
|
||||
assert.Nil(err)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user