Skip to content

Commit

Permalink
Merge pull request hashicorp#37871 from takaishi/feat/batch_ecs_prope…
Browse files Browse the repository at this point in the history
…rties

Add ecs_properties to job_definition of batch
  • Loading branch information
ewbankkit authored Aug 16, 2024
2 parents 6202a03 + cc5762c commit c428d2c
Show file tree
Hide file tree
Showing 10 changed files with 1,024 additions and 111 deletions.
3 changes: 3 additions & 0 deletions .changelog/37871.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`release-note:enhancement
resource/aws_batch_job_definition: Add `ecs_properties` argument
```
6 changes: 5 additions & 1 deletion internal/service/batch/container_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
)

const (
fargatePlatformVersionLatest = "LATEST"
)

type containerProperties awstypes.ContainerProperties

func (cp *containerProperties) reduce() {
Expand All @@ -40,7 +44,7 @@ func (cp *containerProperties) reduce() {

// Prevent difference of API response that contains the default Fargate platform configuration.
if cp.FargatePlatformConfiguration != nil {
if aws.ToString(cp.FargatePlatformConfiguration.PlatformVersion) == "LATEST" {
if aws.ToString(cp.FargatePlatformConfiguration.PlatformVersion) == fargatePlatformVersionLatest {
cp.FargatePlatformConfiguration = nil
}
}
Expand Down
92 changes: 48 additions & 44 deletions internal/service/batch/container_properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ package batch_test
import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-provider-aws/internal/acctest/jsoncmp"
tfbatch "github.com/hashicorp/terraform-provider-aws/internal/service/batch"
)

func TestEquivalentContainerPropertiesJSON(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
ApiJson string
ConfigurationJson string
ExpectEquivalent bool
ExpectError bool
apiJSON string
configurationJSON string
wantEquivalent bool
wantErr bool
}{
"empty": {
ApiJson: ``,
ConfigurationJson: ``,
ExpectEquivalent: true,
apiJSON: ``,
configurationJSON: ``,
wantEquivalent: true,
},
"empty ResourceRequirements": {
ApiJson: `
apiJSON: `
{
"command": ["ls", "-la"],
"environment": [
Expand Down Expand Up @@ -61,7 +63,7 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
]
}
`,
ConfigurationJson: `
configurationJSON: `
{
"command": ["ls", "-la"],
"environment": [
Expand Down Expand Up @@ -97,10 +99,10 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
]
}
`,
ExpectEquivalent: true,
wantEquivalent: true,
},
"reordered Environment": {
ApiJson: `
apiJSON: `
{
"command": ["ls", "-la"],
"environment": [
Expand Down Expand Up @@ -141,7 +143,7 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
]
}
`,
ConfigurationJson: `
configurationJSON: `
{
"command": ["ls", "-la"],
"environment": [
Expand Down Expand Up @@ -182,11 +184,11 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
]
}
`,
ExpectEquivalent: true,
wantEquivalent: true,
},
"empty environment, mountPoints, ulimits, and volumes": {
//lintignore:AWSAT005
ApiJson: `
apiJSON: `
{
"image": "example:image",
"vcpus": 8,
Expand All @@ -201,7 +203,7 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
}
`,
//lintignore:AWSAT005
ConfigurationJson: `
configurationJSON: `
{
"command": ["start.py", "Ref::S3bucket", "Ref::S3key"],
"image": "example:image",
Expand All @@ -210,11 +212,11 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
"jobRoleArn": "arn:aws:iam::123456789012:role/example"
}
`,
ExpectEquivalent: true,
wantEquivalent: true,
},
"empty command, logConfiguration.secretOptions, mountPoints, resourceRequirements, secrets, ulimits, volumes": {
//lintignore:AWSAT003,AWSAT005
ApiJson: `
apiJSON: `
{
"image": "123.dkr.ecr.us-east-1.amazonaws.com/my-app",
"vcpus": 1,
Expand All @@ -234,7 +236,7 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
}
`,
//lintignore:AWSAT003,AWSAT005
ConfigurationJson: `
configurationJSON: `
{
"image": "123.dkr.ecr.us-east-1.amazonaws.com/my-app",
"memory": 4096,
Expand All @@ -251,11 +253,11 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
}
}
`,
ExpectEquivalent: true,
wantEquivalent: true,
},
"no fargatePlatformConfiguration": {
//lintignore:AWSAT003,AWSAT005
ApiJson: `
apiJSON: `
{
"image": "123.dkr.ecr.us-east-1.amazonaws.com/my-app",
"resourceRequirements": [
Expand All @@ -274,7 +276,7 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
}
`,
//lintignore:AWSAT003,AWSAT005
ConfigurationJson: `
configurationJSON: `
{
"image": "123.dkr.ecr.us-east-1.amazonaws.com/my-app",
"resourceRequirements": [
Expand All @@ -289,11 +291,11 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
]
}
`,
ExpectEquivalent: true,
wantEquivalent: true,
},
"empty linuxParameters.devices, linuxParameters.tmpfs, logConfiguration.options": {
//lintignore:AWSAT003,AWSAT005
ApiJson: `
apiJSON: `
{
"image": "123.dkr.ecr.us-east-1.amazonaws.com/my-app",
"vcpus": 1,
Expand All @@ -312,7 +314,7 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
}
`,
//lintignore:AWSAT003,AWSAT005
ConfigurationJson: `
configurationJSON: `
{
"image": "123.dkr.ecr.us-east-1.amazonaws.com/my-app",
"vcpus": 1,
Expand All @@ -327,11 +329,11 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
}
}
`,
ExpectEquivalent: true,
wantEquivalent: true,
},
"empty linuxParameters.devices.permissions, linuxParameters.tmpfs.mountOptions": {
//lintignore:AWSAT003,AWSAT005
ApiJson: `
apiJSON: `
{
"image": "123.dkr.ecr.us-east-1.amazonaws.com/my-app",
"vcpus": 1,
Expand All @@ -354,7 +356,7 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
}
`,
//lintignore:AWSAT003,AWSAT005
ConfigurationJson: `
configurationJSON: `
{
"image": "123.dkr.ecr.us-east-1.amazonaws.com/my-app",
"vcpus": 1,
Expand All @@ -374,11 +376,11 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
}
}
`,
ExpectEquivalent: true,
wantEquivalent: true,
},
"empty environment variables": {
//lintignore:AWSAT005
ApiJson: `
apiJSON: `
{
"image": "example:image",
"vcpus": 8,
Expand All @@ -397,7 +399,7 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
"resourceRequirements": []
}`,
//lintignore:AWSAT005
ConfigurationJson: `
configurationJSON: `
{
"command": ["start.py", "Ref::S3bucket", "Ref::S3key"],
"image": "example:image",
Expand All @@ -415,11 +417,11 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
],
"jobRoleArn": "arn:aws:iam::123456789012:role/example"
}`,
ExpectEquivalent: true,
wantEquivalent: true,
},
"empty environment variable": {
//lintignore:AWSAT005
ApiJson: `
apiJSON: `
{
"image": "example:image",
"vcpus": 8,
Expand All @@ -433,7 +435,7 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
"resourceRequirements": []
}`,
//lintignore:AWSAT005
ConfigurationJson: `
configurationJSON: `
{
"command": ["start.py", "Ref::S3bucket", "Ref::S3key"],
"image": "example:image",
Expand All @@ -447,7 +449,7 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
],
"jobRoleArn": "arn:aws:iam::123456789012:role/example"
}`,
ExpectEquivalent: true,
wantEquivalent: true,
},
}

Expand All @@ -456,18 +458,20 @@ func TestEquivalentContainerPropertiesJSON(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

got, err := tfbatch.EquivalentContainerPropertiesJSON(testCase.ConfigurationJson, testCase.ApiJson)

if err != nil && !testCase.ExpectError {
t.Errorf("got unexpected error: %s", err)
}

if err == nil && testCase.ExpectError {
t.Errorf("expected error, but received none")
output, err := tfbatch.EquivalentContainerPropertiesJSON(testCase.configurationJSON, testCase.apiJSON)
if got, want := err != nil, testCase.wantErr; !cmp.Equal(got, want) {
t.Errorf("EquivalentContainerPropertiesJSON err %t, want %t", got, want)
}

if got != testCase.ExpectEquivalent {
t.Errorf("got %t, expected %t", got, testCase.ExpectEquivalent)
if err == nil {
if got, want := output, testCase.wantEquivalent; !cmp.Equal(got, want) {
t.Errorf("EquivalentContainerPropertiesJSON equivalent %t, want %t", got, want)
if want {
if diff := jsoncmp.Diff(testCase.configurationJSON, testCase.apiJSON); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
}
}
}
})
}
Expand Down
Loading

0 comments on commit c428d2c

Please sign in to comment.