Skip to content

Commit

Permalink
resource/aws_elastictranscoder_present: Prevent empty configuration b…
Browse files Browse the repository at this point in the history
…lock panics (#14092)

Reference: #14087

Previously:

```
=== CONT  TestAccAWSElasticTranscoderPreset_AudioCodecOptions_empty
panic: interface conversion: interface {} is nil, not map[string]interface {}

goroutine 378 [running]:
github.com/terraform-providers/terraform-provider-aws/aws.expandETAudioCodecOptions(0xc000fb9880, 0xc0028af590)
  /Users/bflad/src/github.com/terraform-providers/terraform-provider-aws/aws/resource_aws_elastic_transcoder_preset.go:434 +0x458
github.com/terraform-providers/terraform-provider-aws/aws.expandETAudioParams(0xc000fb9880, 0xc0027d5708)
  /Users/bflad/src/github.com/terraform-providers/terraform-provider-aws/aws/resource_aws_elastic_transcoder_preset.go:423 +0x2a0
github.com/terraform-providers/terraform-provider-aws/aws.resourceAwsElasticTranscoderPresetCreate(0xc000fb9880, 0x66543e0, 0xc002660000, 0x2, 0xbbdb620)
  /Users/bflad/src/github.com/terraform-providers/terraform-provider-aws/aws/resource_aws_elastic_transcoder_preset.go:326 +0x6c
```

Output from acceptance testing:

```
--- PASS: TestAccAWSElasticTranscoderPreset_disappears (13.09s)
--- PASS: TestAccAWSElasticTranscoderPreset_AudioCodecOptions_empty (18.51s)
--- PASS: TestAccAWSElasticTranscoderPreset_Description (18.51s)
--- PASS: TestAccAWSElasticTranscoderPreset_basic (18.98s)
--- PASS: TestAccAWSElasticTranscoderPreset_Full (34.69s)
```
  • Loading branch information
bflad authored Jul 31, 2020
1 parent 1e32674 commit c6b2e16
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
12 changes: 8 additions & 4 deletions aws/resource_aws_elastic_transcoder_preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func expandETThumbnails(d *schema.ResourceData) *elastictranscoder.Thumbnails {
}

l := list.([]interface{})
if len(l) == 0 {
if len(l) == 0 || l[0] == nil {
return nil
}
t := l[0].(map[string]interface{})
Expand Down Expand Up @@ -410,7 +410,7 @@ func expandETAudioParams(d *schema.ResourceData) *elastictranscoder.AudioParamet
}

l := list.([]interface{})
if len(l) == 0 {
if len(l) == 0 || l[0] == nil {
return nil
}
audio := l[0].(map[string]interface{})
Expand All @@ -427,7 +427,7 @@ func expandETAudioParams(d *schema.ResourceData) *elastictranscoder.AudioParamet

func expandETAudioCodecOptions(d *schema.ResourceData) *elastictranscoder.AudioCodecOptions {
l := d.Get("audio_codec_options").([]interface{})
if len(l) == 0 {
if len(l) == 0 || l[0] == nil {
return nil
}

Expand Down Expand Up @@ -456,7 +456,7 @@ func expandETAudioCodecOptions(d *schema.ResourceData) *elastictranscoder.AudioC

func expandETVideoParams(d *schema.ResourceData) *elastictranscoder.VideoParameters {
l := d.Get("video").([]interface{})
if len(l) == 0 {
if len(l) == 0 || l[0] == nil {
return nil
}
p := l[0].(map[string]interface{})
Expand Down Expand Up @@ -541,6 +541,10 @@ func expandETVideoWatermarks(d *schema.ResourceData) []*elastictranscoder.Preset
var watermarks []*elastictranscoder.PresetWatermark

for _, w := range s.List() {
if w == nil {
continue
}

p := w.(map[string]interface{})
watermark := &elastictranscoder.PresetWatermark{
HorizontalAlign: aws.String(p["horizontal_align"].(string)),
Expand Down
46 changes: 46 additions & 0 deletions aws/resource_aws_elastic_transcoder_preset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ func TestAccAWSElasticTranscoderPreset_disappears(t *testing.T) {
})
}

// Reference: https:/terraform-providers/terraform-provider-aws/issues/14087
func TestAccAWSElasticTranscoderPreset_AudioCodecOptions_empty(t *testing.T) {
var preset elastictranscoder.Preset
resourceName := "aws_elastictranscoder_preset.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSElasticTranscoder(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckElasticTranscoderPresetDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsElasticTranscoderPresetConfigAudioCodecOptionsEmpty(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckElasticTranscoderPresetExists(resourceName, &preset),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"audio_codec_options.#"}, // Due to incorrect schema (should be nested under audio)
},
},
})
}

func TestAccAWSElasticTranscoderPreset_Description(t *testing.T) {
var preset elastictranscoder.Preset
resourceName := "aws_elastictranscoder_preset.test"
Expand Down Expand Up @@ -240,6 +267,25 @@ resource "aws_elastictranscoder_preset" "test" {
`, rName)
}

func testAccAwsElasticTranscoderPresetConfigAudioCodecOptionsEmpty(rName string) string {
return fmt.Sprintf(`
resource "aws_elastictranscoder_preset" "test" {
container = "mp4"
name = %[1]q
audio {
audio_packing_mode = "SingleTrack"
bit_rate = 320
channels = 2
codec = "mp3"
sample_rate = 44100
}
audio_codec_options {}
}
`, rName)
}

func testAccAwsElasticTranscoderPresetConfigDescription(rName string, description string) string {
return fmt.Sprintf(`
resource "aws_elastictranscoder_preset" "test" {
Expand Down

0 comments on commit c6b2e16

Please sign in to comment.