open-nomad/e2e/migrations/migrations_test.go

194 lines
3.8 KiB
Go
Raw Normal View History

2017-10-20 18:10:30 +00:00
package e2e
import (
"bytes"
"flag"
2017-10-20 18:10:30 +00:00
"io/ioutil"
"os"
"os/exec"
2017-10-24 13:56:46 +00:00
"strings"
2017-10-20 18:10:30 +00:00
"testing"
2017-10-25 00:56:19 +00:00
"github.com/hashicorp/nomad/testutil"
2017-10-25 01:38:15 +00:00
"github.com/stretchr/testify/assert"
2017-10-20 18:10:30 +00:00
)
var integration = flag.Bool("integration", false, "run integration tests")
2017-10-25 01:38:15 +00:00
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" ]
}
}
}
}`
2017-10-25 00:56:19 +00:00
func isSuccess(execCmd *exec.Cmd, retries int, keyword string) (string, error) {
var successOut string
var err error
2017-10-25 00:56:19 +00:00
testutil.WaitForResultRetries(2000, func() (bool, error) {
var out bytes.Buffer
cmd := *execCmd
cmd.Stdout = &out
err := cmd.Run()
2017-10-25 00:56:19 +00:00
if err != nil {
2017-10-25 00:56:19 +00:00
return false, err
}
2017-10-25 00:56:19 +00:00
success := (out.String() != "" && !strings.Contains(out.String(), keyword))
if !success {
out.Reset()
return false, err
}
2017-10-25 00:56:19 +00:00
successOut = out.String()
return true, nil
}, func(cmd_err error) {
err = cmd_err
})
2017-10-25 00:56:19 +00:00
return successOut, err
}
2017-10-25 01:38:15 +00:00
// 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")
}
2017-10-25 01:38:15 +00:00
// jobIsReady attempts sto query the status of a specific job a fixed number of
// times
2017-10-25 00:56:19 +00:00
func jobIsReady(retries int, jobName string) (string, error) {
cmd := exec.Command("nomad", "job", "status", jobName)
return isSuccess(cmd, retries, "pending")
}
2017-10-25 01:38:15 +00:00
// 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.
2017-10-20 19:09:58 +00:00
func startCluster(clusterConfig []string) (func(), error) {
cmds := make([]*exec.Cmd, 0)
2017-10-20 18:10:30 +00:00
2017-10-20 19:09:58 +00:00
for _, agentConfig := range clusterConfig {
cmd := exec.Command("nomad", "agent", "-config", agentConfig)
err := cmd.Start()
2017-10-20 18:10:30 +00:00
2017-10-20 19:09:58 +00:00
if err != nil {
return func() {}, err
}
2017-10-20 18:10:30 +00:00
2017-10-20 19:09:58 +00:00
cmds = append(cmds, cmd)
2017-10-20 18:10:30 +00:00
}
f := func() {
2017-10-20 19:09:58 +00:00
for _, cmd := range cmds {
cmd.Process.Kill()
}
2017-10-20 18:10:30 +00:00
}
return f, nil
}
func TestJobMigrations(t *testing.T) {
flag.Parse()
if !*integration {
t.Skip("skipping test in non-integration mode.")
}
2017-10-20 18:10:30 +00:00
t.Parallel()
assert := assert.New(t)
2017-10-20 19:09:58 +00:00
clusterConfig := []string{"server.hcl", "client1.hcl", "client2.hcl"}
stopCluster, err := startCluster(clusterConfig)
2017-10-20 18:10:30 +00:00
assert.Nil(err)
defer stopCluster()
2017-10-25 01:38:15 +00:00
_, err = allNodesAreReady(10)
2017-10-25 00:56:19 +00:00
assert.Nil(err)
2017-10-20 18:10:30 +00:00
fh, err := ioutil.TempFile("", "nomad-sleep-1")
assert.Nil(err)
defer os.Remove(fh.Name())
2017-10-25 01:38:15 +00:00
_, err = fh.WriteString(sleepJobOne)
2017-10-20 18:10:30 +00:00
assert.Nil(err)
2017-10-20 18:10:30 +00:00
jobCmd := exec.Command("nomad", "run", fh.Name())
err = jobCmd.Run()
assert.Nil(err)
2017-10-25 00:56:19 +00:00
firstJoboutput, err := jobIsReady(20, "sleep")
assert.Nil(err)
assert.NotContains(firstJoboutput, "failed")
2017-10-20 18:10:30 +00:00
fh2, err := ioutil.TempFile("", "nomad-sleep-2")
assert.Nil(err)
defer os.Remove(fh2.Name())
2017-10-25 01:38:15 +00:00
_, err = fh2.WriteString(sleepJobTwo)
2017-10-20 18:10:30 +00:00
assert.Nil(err)
secondJobCmd := exec.Command("nomad", "run", fh2.Name())
err = secondJobCmd.Run()
2017-10-20 18:10:30 +00:00
assert.Nil(err)
2017-10-25 00:56:19 +00:00
jobOutput, err := jobIsReady(20, "sleep")
assert.Nil(err)
2017-10-20 18:10:30 +00:00
assert.NotContains(jobOutput, "failed")
assert.Contains(jobOutput, "complete")
}