diff --git a/aws/internal/service/batch/equivalency/container_properties.go b/aws/internal/service/batch/equivalency/container_properties.go index a08d8f2b52c..a8b111e64e2 100644 --- a/aws/internal/service/batch/equivalency/container_properties.go +++ b/aws/internal/service/batch/equivalency/container_properties.go @@ -19,10 +19,26 @@ func (cp *containerProperties) Reduce() error { return aws.StringValue(cp.Environment[i].Name) < aws.StringValue(cp.Environment[j].Name) }) + if len(cp.Environment) == 0 { + cp.Environment = nil + } + + if len(cp.MountPoints) == 0 { + cp.MountPoints = nil + } + if len(cp.ResourceRequirements) == 0 { cp.ResourceRequirements = nil } + if len(cp.Ulimits) == 0 { + cp.Ulimits = nil + } + + if len(cp.Volumes) == 0 { + cp.Volumes = nil + } + return nil } diff --git a/aws/internal/service/batch/equivalency/container_properties_test.go b/aws/internal/service/batch/equivalency/container_properties_test.go index 16aa6fc9ef2..0255944352b 100644 --- a/aws/internal/service/batch/equivalency/container_properties_test.go +++ b/aws/internal/service/batch/equivalency/container_properties_test.go @@ -178,6 +178,33 @@ func TestEquivalentBatchContainerPropertiesJSON(t *testing.T) { } ] } +`, + ExpectEquivalent: true, + }, + { + Name: "empty environment, mountPoints, ulimits, and volumes", + ApiJson: ` +{ + "image": "example:image", + "vcpus": 8, + "memory": 2048, + "command": ["start.py", "Ref::S3bucket", "Ref::S3key"], + "jobRoleArn": "arn:aws:iam::123456789012:role/example", + "volumes": [], + "environment": [], + "mountPoints": [], + "ulimits": [], + "resourceRequirements": [] +} +`, + ConfigurationJson: ` +{ + "command": ["start.py", "Ref::S3bucket", "Ref::S3key"], + "image": "example:image", + "memory": 2048, + "vcpus": 8, + "jobRoleArn": "arn:aws:iam::123456789012:role/example" +} `, ExpectEquivalent: true, }, diff --git a/aws/resource_aws_batch_job_definition_test.go b/aws/resource_aws_batch_job_definition_test.go index 7b1609941ae..c63f1ee04e5 100644 --- a/aws/resource_aws_batch_job_definition_test.go +++ b/aws/resource_aws_batch_job_definition_test.go @@ -2,10 +2,8 @@ package aws import ( "fmt" - "strings" - "testing" - "reflect" + "testing" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/batch" @@ -15,6 +13,31 @@ import ( ) func TestAccAWSBatchJobDefinition_basic(t *testing.T) { + var jd batch.JobDefinition + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_batch_job_definition.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBatchJobDefinitionDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBatchJobDefinitionConfigName(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckBatchJobDefinitionExists(resourceName, &jd), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAWSBatchJobDefinition_ContainerProperties_Advanced(t *testing.T) { var jd batch.JobDefinition compare := batch.JobDefinition{ Parameters: map[string]*string{ @@ -50,16 +73,16 @@ func TestAccAWSBatchJobDefinition_basic(t *testing.T) { }, }, } - ri := acctest.RandInt() - config := fmt.Sprintf(testAccBatchJobDefinitionBaseConfig, ri) + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_batch_job_definition.test" + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckBatchJobDefinitionDestroy, Steps: []resource.TestStep{ { - Config: config, + Config: testAccBatchJobDefinitionConfigContainerPropertiesAdvanced(rName), Check: resource.ComposeTestCheckFunc( testAccCheckBatchJobDefinitionExists(resourceName, &jd), testAccCheckBatchJobDefinitionAttributes(&jd, &compare), @@ -75,26 +98,24 @@ func TestAccAWSBatchJobDefinition_basic(t *testing.T) { } func TestAccAWSBatchJobDefinition_updateForcesNewResource(t *testing.T) { - var before batch.JobDefinition - var after batch.JobDefinition - ri := acctest.RandInt() - config := fmt.Sprintf(testAccBatchJobDefinitionBaseConfig, ri) - updateConfig := fmt.Sprintf(testAccBatchJobDefinitionUpdateConfig, ri) + var before, after batch.JobDefinition + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_batch_job_definition.test" + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckBatchJobDefinitionDestroy, Steps: []resource.TestStep{ { - Config: config, + Config: testAccBatchJobDefinitionConfigContainerPropertiesAdvanced(rName), Check: resource.ComposeTestCheckFunc( testAccCheckBatchJobDefinitionExists(resourceName, &before), testAccCheckBatchJobDefinitionAttributes(&before, nil), ), }, { - Config: updateConfig, + Config: testAccBatchJobDefinitionConfigContainerPropertiesAdvancedUpdate(rName), Check: resource.ComposeTestCheckFunc( testAccCheckBatchJobDefinitionExists(resourceName, &after), testAccCheckJobDefinitionRecreated(t, &before, &after), @@ -137,9 +158,6 @@ func testAccCheckBatchJobDefinitionExists(n string, jd *batch.JobDefinition) res func testAccCheckBatchJobDefinitionAttributes(jd *batch.JobDefinition, compare *batch.JobDefinition) resource.TestCheckFunc { return func(s *terraform.State) error { - if !strings.HasPrefix(*jd.JobDefinitionName, "tf_acctest_batch_job_definition") { - return fmt.Errorf("Bad Job Definition name: %s", *jd.JobDefinitionName) - } for _, rs := range s.RootModule().Resources { if rs.Type != "aws_batch_job_definition" { continue @@ -190,9 +208,10 @@ func testAccCheckBatchJobDefinitionDestroy(s *terraform.State) error { return nil } -const testAccBatchJobDefinitionBaseConfig = ` +func testAccBatchJobDefinitionConfigContainerPropertiesAdvanced(rName string) string { + return fmt.Sprintf(` resource "aws_batch_job_definition" "test" { - name = "tf_acctest_batch_job_definition_%[1]d" + name = %[1]q type = "container" parameters = { param1 = "val1" @@ -238,11 +257,13 @@ resource "aws_batch_job_definition" "test" { } CONTAINER_PROPERTIES } -` +`, rName) +} -const testAccBatchJobDefinitionUpdateConfig = ` +func testAccBatchJobDefinitionConfigContainerPropertiesAdvancedUpdate(rName string) string { + return fmt.Sprintf(` resource "aws_batch_job_definition" "test" { - name = "tf_acctest_batch_job_definition_%[1]d" + name = %[1]q type = "container" container_properties = <