2016-10-24 08:18:04 +02:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2017-02-04 00:19:23 +01:00
|
|
|
"errors"
|
2016-10-24 08:18:04 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
r "github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestResourceJob_basic(t *testing.T) {
|
|
|
|
r.Test(t, r.TestCase{
|
|
|
|
Providers: testProviders,
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Steps: []r.TestStep{
|
2017-02-04 00:19:23 +01:00
|
|
|
{
|
2016-10-24 08:18:04 +02:00
|
|
|
Config: testResourceJob_initialConfig,
|
|
|
|
Check: testResourceJob_initialCheck,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
CheckDestroy: testResourceJob_checkDestroy("foo"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceJob_refresh(t *testing.T) {
|
|
|
|
r.Test(t, r.TestCase{
|
|
|
|
Providers: testProviders,
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Steps: []r.TestStep{
|
2017-02-04 00:19:23 +01:00
|
|
|
{
|
2016-10-24 08:18:04 +02:00
|
|
|
Config: testResourceJob_initialConfig,
|
|
|
|
Check: testResourceJob_initialCheck,
|
|
|
|
},
|
|
|
|
|
|
|
|
// This should successfully cause the job to be recreated,
|
|
|
|
// testing the Exists function.
|
2017-02-04 00:19:23 +01:00
|
|
|
{
|
2016-10-24 08:18:04 +02:00
|
|
|
PreConfig: testResourceJob_deregister(t, "foo"),
|
|
|
|
Config: testResourceJob_initialConfig,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceJob_disableDestroyDeregister(t *testing.T) {
|
|
|
|
r.Test(t, r.TestCase{
|
|
|
|
Providers: testProviders,
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Steps: []r.TestStep{
|
2017-02-04 00:19:23 +01:00
|
|
|
{
|
2016-10-24 08:18:04 +02:00
|
|
|
Config: testResourceJob_noDestroy,
|
|
|
|
Check: testResourceJob_initialCheck,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Destroy with our setting set
|
2017-02-04 00:19:23 +01:00
|
|
|
{
|
2016-10-24 08:18:04 +02:00
|
|
|
Destroy: true,
|
|
|
|
Config: testResourceJob_noDestroy,
|
|
|
|
Check: testResourceJob_checkExists,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Re-apply without the setting set
|
2017-02-04 00:19:23 +01:00
|
|
|
{
|
2016-10-24 08:18:04 +02:00
|
|
|
Config: testResourceJob_initialConfig,
|
|
|
|
Check: testResourceJob_checkExists,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceJob_idChange(t *testing.T) {
|
|
|
|
r.Test(t, r.TestCase{
|
|
|
|
Providers: testProviders,
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Steps: []r.TestStep{
|
2017-02-04 00:19:23 +01:00
|
|
|
{
|
2016-10-24 08:18:04 +02:00
|
|
|
Config: testResourceJob_initialConfig,
|
|
|
|
Check: testResourceJob_initialCheck,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Change our ID
|
2017-02-04 00:19:23 +01:00
|
|
|
{
|
2016-10-24 08:18:04 +02:00
|
|
|
Config: testResourceJob_updateConfig,
|
|
|
|
Check: testResourceJob_updateCheck,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-04 00:19:23 +01:00
|
|
|
func TestResourceJob_parameterizedJob(t *testing.T) {
|
|
|
|
r.Test(t, r.TestCase{
|
|
|
|
Providers: testProviders,
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Steps: []r.TestStep{
|
|
|
|
{
|
|
|
|
Config: testResourceJob_parameterizedJob,
|
|
|
|
Check: testResourceJob_initialCheck,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-24 08:18:04 +02:00
|
|
|
var testResourceJob_initialConfig = `
|
|
|
|
resource "nomad_job" "test" {
|
|
|
|
jobspec = <<EOT
|
|
|
|
job "foo" {
|
|
|
|
datacenters = ["dc1"]
|
|
|
|
type = "service"
|
|
|
|
group "foo" {
|
|
|
|
task "foo" {
|
|
|
|
driver = "raw_exec"
|
|
|
|
config {
|
|
|
|
command = "/bin/sleep"
|
|
|
|
args = ["1"]
|
|
|
|
}
|
|
|
|
|
|
|
|
resources {
|
|
|
|
cpu = 20
|
|
|
|
memory = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
logs {
|
|
|
|
max_files = 3
|
|
|
|
max_file_size = 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOT
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testResourceJob_noDestroy = `
|
|
|
|
resource "nomad_job" "test" {
|
|
|
|
deregister_on_destroy = false
|
|
|
|
jobspec = <<EOT
|
|
|
|
job "foo" {
|
|
|
|
datacenters = ["dc1"]
|
|
|
|
type = "service"
|
|
|
|
group "foo" {
|
|
|
|
task "foo" {
|
|
|
|
driver = "raw_exec"
|
|
|
|
config {
|
|
|
|
command = "/bin/sleep"
|
|
|
|
args = ["1"]
|
|
|
|
}
|
|
|
|
|
|
|
|
resources {
|
|
|
|
cpu = 20
|
|
|
|
memory = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
logs {
|
|
|
|
max_files = 3
|
|
|
|
max_file_size = 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOT
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func testResourceJob_initialCheck(s *terraform.State) error {
|
|
|
|
resourceState := s.Modules[0].Resources["nomad_job.test"]
|
|
|
|
if resourceState == nil {
|
2017-02-04 00:19:23 +01:00
|
|
|
return errors.New("resource not found in state")
|
2016-10-24 08:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
instanceState := resourceState.Primary
|
|
|
|
if instanceState == nil {
|
2017-02-04 00:19:23 +01:00
|
|
|
return errors.New("resource has no primary instance")
|
2016-10-24 08:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jobID := instanceState.ID
|
|
|
|
|
|
|
|
client := testProvider.Meta().(*api.Client)
|
|
|
|
job, _, err := client.Jobs().Info(jobID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error reading back job: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := job.ID, jobID; got != want {
|
|
|
|
return fmt.Errorf("jobID is %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testResourceJob_checkExists(s *terraform.State) error {
|
|
|
|
jobID := "foo"
|
|
|
|
|
|
|
|
client := testProvider.Meta().(*api.Client)
|
|
|
|
_, _, err := client.Jobs().Info(jobID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error reading back job: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testResourceJob_checkDestroy(jobID string) r.TestCheckFunc {
|
|
|
|
return func(*terraform.State) error {
|
|
|
|
client := testProvider.Meta().(*api.Client)
|
2017-05-03 22:33:42 +02:00
|
|
|
job, _, err := client.Jobs().Info(jobID, nil)
|
|
|
|
// This should likely never happen, due to how nomad caches jobs
|
|
|
|
if err != nil && strings.Contains(err.Error(), "404") || job == nil {
|
2016-10-24 08:18:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-05-03 22:33:42 +02:00
|
|
|
|
|
|
|
if job.Status != "dead" {
|
|
|
|
return fmt.Errorf("Job %q has not been stopped. Status: %s", jobID, job.Status)
|
2016-10-24 08:18:04 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 22:33:42 +02:00
|
|
|
return nil
|
2016-10-24 08:18:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testResourceJob_deregister(t *testing.T, jobID string) func() {
|
|
|
|
return func() {
|
|
|
|
client := testProvider.Meta().(*api.Client)
|
|
|
|
_, _, err := client.Jobs().Deregister(jobID, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error deregistering job: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var testResourceJob_updateConfig = `
|
|
|
|
resource "nomad_job" "test" {
|
|
|
|
jobspec = <<EOT
|
|
|
|
job "bar" {
|
|
|
|
datacenters = ["dc1"]
|
|
|
|
type = "service"
|
|
|
|
group "foo" {
|
|
|
|
task "foo" {
|
|
|
|
driver = "raw_exec"
|
|
|
|
config {
|
|
|
|
command = "/bin/sleep"
|
|
|
|
args = ["1"]
|
|
|
|
}
|
|
|
|
|
|
|
|
resources {
|
|
|
|
cpu = 20
|
|
|
|
memory = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
logs {
|
|
|
|
max_files = 3
|
|
|
|
max_file_size = 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOT
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func testResourceJob_updateCheck(s *terraform.State) error {
|
|
|
|
resourceState := s.Modules[0].Resources["nomad_job.test"]
|
|
|
|
if resourceState == nil {
|
2017-02-04 00:19:23 +01:00
|
|
|
return errors.New("resource not found in state")
|
2016-10-24 08:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
instanceState := resourceState.Primary
|
|
|
|
if instanceState == nil {
|
2017-02-04 00:19:23 +01:00
|
|
|
return errors.New("resource has no primary instance")
|
2016-10-24 08:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jobID := instanceState.ID
|
|
|
|
|
|
|
|
client := testProvider.Meta().(*api.Client)
|
|
|
|
job, _, err := client.Jobs().Info(jobID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error reading back job: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := job.ID, jobID; got != want {
|
|
|
|
return fmt.Errorf("jobID is %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Verify foo doesn't exist
|
2017-05-03 22:33:42 +02:00
|
|
|
job, _, err := client.Jobs().Info("foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error reading %q job: %s", "foo", err)
|
|
|
|
}
|
|
|
|
if job.Status != "dead" {
|
|
|
|
return fmt.Errorf("%q job is not dead. Status: %q", "foo", job.Status)
|
2016-10-24 08:18:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-04 00:19:23 +01:00
|
|
|
|
|
|
|
var testResourceJob_parameterizedJob = `
|
|
|
|
resource "nomad_job" "test" {
|
|
|
|
jobspec = <<EOT
|
|
|
|
job "bar" {
|
|
|
|
datacenters = ["dc1"]
|
|
|
|
type = "batch"
|
|
|
|
parameterized {
|
|
|
|
payload = "required"
|
|
|
|
}
|
|
|
|
group "foo" {
|
|
|
|
task "foo" {
|
|
|
|
driver = "raw_exec"
|
|
|
|
config {
|
|
|
|
command = "/bin/sleep"
|
|
|
|
args = ["1"]
|
|
|
|
}
|
|
|
|
resources {
|
|
|
|
cpu = 20
|
|
|
|
memory = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
logs {
|
|
|
|
max_files = 3
|
|
|
|
max_file_size = 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOT
|
|
|
|
}
|
|
|
|
`
|