Skip to content

Commit

Permalink
Merge pull request #63 from terraform-providers/f-rich-diff-merged
Browse files Browse the repository at this point in the history
F rich diff merged
  • Loading branch information
Chris Baker committed May 15, 2019
2 parents 1820ef1 + 039114d commit 923c6ab
Show file tree
Hide file tree
Showing 21 changed files with 4,385 additions and 240 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

IMPROVEMENTS:
* **Target Nomad 0.9.1**: updated the nomad client to support Nomad API verison 0.9.1 ([#61](https://github.com/terraform-providers/terraform-provider-nomad/issues/61))
* Added `allocation_ids` as an attribute to Nomad Job resources. ([#63](https://github.com/terraform-providers/terraform-provider-nomad/issues/63))
* Richer diffs for Nomad jobs ([#63](https://github.com/terraform-providers/terraform-provider-nomad/issues/63)][[#15](https://github.com/terraform-providers/terraform-provider-nomad/issues/15))

INTERNAL:

Expand Down
9 changes: 5 additions & 4 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ build: fmtcheck
test: fmtcheck
go test -i $(TEST) || exit 1
echo $(TEST) | \
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4 -count=1

testacc: fmtcheck
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m -count=1

localtestacc: fmtcheck
scripts/start-nomad.sh
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
-export NOMAD_TOKEN=$(shell scripts/start-nomad.sh); \
export TF_ACC=1; \
go test $(TEST) -v $(TESTARGS) -timeout 120m -count=1
scripts/stop-nomad.sh

vet:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ require (
github.com/hashicorp/vault v0.10.4
github.com/hashicorp/yamux v0.0.0-20180917205041-7221087c3d28 // indirect
github.com/mitchellh/mapstructure v1.1.2
github.com/stretchr/testify v1.3.0
google.golang.org/grpc v1.19.0 // indirect
)
56 changes: 49 additions & 7 deletions nomad/data_source_deployments_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package nomad

import (
"testing"

"fmt"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"regexp"
"strconv"
"testing"
)

// Test will
Expand All @@ -21,28 +24,67 @@ func TestAccDataSourceDeployments(t *testing.T) {
Config: testAccCheckDataSourceNomadDeploymentsJobCfg,
},
{
Config: testAccCheckDataSourceNomadDeploymentsCfgWithJob,
Check: resource.TestCheckResourceAttr("data.nomad_deployments.foobar", "deployments.#", "1"),
Config: testAccCheckDataSourceNomadDeploymentsCfgWithJob,
Check: func(s *terraform.State) error {
rs, _ := s.RootModule().Resources["data.nomad_deployments.foobar"]
is := rs.Primary
v, ok := is.Attributes["deployments.#"]
if !ok {
return fmt.Errorf("Attribute '%s' not found", "deployments.#")
}
numDeployments, err := strconv.Atoi(v)
if err != nil {
return fmt.Errorf("received error parsing 'deployments.#': %v", err)
} else if numDeployments < 1 {
return fmt.Errorf("Attribute 'deployments.#' should be >= 1, got %v", v)
}
return nil
},
Destroy: true,
},
{
Config: testAccCheckDataSourceNomadDeploymentsCfg,
Check: resource.TestCheckResourceAttr("data.nomad_deployments.foobar", "deployments.0.Status", "cancelled"),
Check: func(s *terraform.State) error {
re := regexp.MustCompile(`^deployments.(\d+).JobID$`)
rs, _ := s.RootModule().Resources["data.nomad_deployments.foobar"]
is := rs.Primary
index := -1
for k, v := range is.Attributes {
// any match of this job should be fine, all deployments should be "cancelled"
if submatch := re.FindStringSubmatch(k); submatch != nil && v == "foo_deploy" {
index, _ = strconv.Atoi(submatch[1])
break
}
}
if index < 0 {
return fmt.Errorf("did not find expected deployment for job 'foo_deploy'")
}
statusAttr := fmt.Sprintf("deployments.%d.Status", index)
if s, ok := is.Attributes[statusAttr]; !ok || s != "cancelled" {
if !ok {
return fmt.Errorf("did not find expected attributed '%v'", statusAttr)
}
return fmt.Errorf("'%v': expected 'cancelled', got '%v' to be 'cancelled'", statusAttr, s)
}
return nil
},
},
},

// Somewhat-abuse CheckDestroy to actually do our cleanup... :/
CheckDestroy: testResourceJob_forceDestroyWithPurge("foo_deploy"),
})
}

var testAccCheckDataSourceNomadDeploymentsJobCfg = `
resource "nomad_job" "foobar" {
jobspec = <<EOT
job "foo" {
job "foo_deploy" {
update {} ## creates deployment
datacenters = ["dc1"]
type = "service"
group "foo" {
task "foo" {
leader = true ## new in Nomad 0.5.6
driver = "raw_exec"
config {
command = "/bin/sleep"
Expand Down
43 changes: 9 additions & 34 deletions nomad/datasource_nomad_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ func TestAccDataSourceNomadJob_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testProviders,
CheckDestroy: testAccCheckNomadJobDestroy,
CheckDestroy: testResourceJob_forceDestroyWithPurge("foo"),
Steps: []resource.TestStep{
{
Config: testAccCheckDataSourceNomadJobConfig,
},
{
Config: testAccCheckDataSourceNomadJobConfigWithJob,
Check: testAccCheckDataSourceNomadJobExists("data.nomad_job.foobaz"),
},
{
Config: testAccCheckDataSourceNomadJobConfig,
Config: testAccCheckDataSourceNomadJobConfigWithJob,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.nomad_job.foobaz", "name", "foo"),
Expand Down Expand Up @@ -122,44 +125,16 @@ resource "nomad_job" "foobar" {
}
EOT
}
`

var testAccCheckDataSourceNomadJobConfigWithJob = testAccCheckDataSourceNomadJobConfig + `
data "nomad_job" "foobaz" {
job_id = "foo"
}
`

var testAccCheckDataSourceNomadJobConfigErr = `
resource "nomad_job" "foobar" {
jobspec = <<EOT
job "foo" {
datacenters = ["dc1"]
type = "service"
group "foo" {
task "foo" {
leader = true ## new in Nomad 0.5.6
driver = "raw_exec"
config {
command = "/bin/sleep"
args = ["1"]
}
resources {
cpu = 100
memory = 10
}
logs {
max_files = 3
max_file_size = 10
}
}
}
}
EOT
}
var testAccCheckDataSourceNomadJobConfigErr = testAccCheckDataSourceNomadJobConfig + `
data "nomad_job" "foobar" {
job_id = "foo-mia"
job_id = "foo-missing"
}
`
Loading

0 comments on commit 923c6ab

Please sign in to comment.