Skip to content

Commit ea8f161

Browse files
committed
add acc test that fails on master
1 parent 0e908be commit ea8f161

File tree

2 files changed

+198
-33
lines changed

2 files changed

+198
-33
lines changed

builtin/providers/aws/resource_aws_elastic_beanstalk_environment.go

+44-33
Original file line numberDiff line numberDiff line change
@@ -328,39 +328,50 @@ func resourceAwsElasticBeanstalkEnvironmentUpdate(d *schema.ResourceData, meta i
328328
os := o.(*schema.Set)
329329
ns := n.(*schema.Set)
330330

331-
rm := extractOptionSettings(os.Difference(ns))
332-
add := extractOptionSettings(ns.Difference(os))
333-
334-
// Additions and removals of options are done in a single API call, so we
335-
// can't do our normal "remove these" and then later "add these", re-adding
336-
// any updated settings.
337-
// Because of this, we need to remove any settings in the "removable"
338-
// settings that are also found in the "add" settings, otherwise they
339-
// conflict. Here we loop through all the initial removables from the set
340-
// difference, and we build up a slice of settings not found in the "add"
341-
// set
342-
var remove []*elasticbeanstalk.ConfigurationOptionSetting
343-
if len(add) > 0 {
344-
for _, r := range rm {
345-
for _, a := range add {
346-
if *r.Namespace == *a.Namespace && *r.OptionName == *a.OptionName {
347-
continue
348-
}
349-
remove = append(remove, r)
350-
}
351-
}
352-
} else {
353-
remove = rm
354-
}
355-
356-
for _, elem := range remove {
357-
updateOpts.OptionsToRemove = append(updateOpts.OptionsToRemove, &elasticbeanstalk.OptionSpecification{
358-
Namespace: elem.Namespace,
359-
OptionName: elem.OptionName,
360-
})
361-
}
362-
363-
updateOpts.OptionSettings = add
331+
rm := extractOptionSettings(os.Difference(ns))
332+
add := extractOptionSettings(ns.Difference(os))
333+
334+
// Additions and removals of options are done in a single API call, so we
335+
// can't do our normal "remove these" and then later "add these", re-adding
336+
// any updated settings.
337+
// Because of this, we need to remove any settings in the "removable"
338+
// settings that are also found in the "add" settings, otherwise they
339+
// conflict. Here we loop through all the initial removables from the set
340+
// difference, and we build up a slice of settings not found in the "add"
341+
// set
342+
var remove []*elasticbeanstalk.ConfigurationOptionSetting
343+
if len(add) > 0 {
344+
for _, r := range rm {
345+
for _, a := range add {
346+
// ResourceNames are optional. Some defaults come with it, some do
347+
// not. We need to guard against nil/empty in state as well as
348+
// nil/empty from the API
349+
if a.ResourceName != nil {
350+
if r.ResourceName == nil {
351+
continue
352+
}
353+
if *r.ResourceName != *a.ResourceName {
354+
continue
355+
}
356+
}
357+
if *r.Namespace == *a.Namespace && *r.OptionName == *a.OptionName {
358+
continue
359+
}
360+
remove = append(remove, r)
361+
}
362+
}
363+
} else {
364+
remove = rm
365+
}
366+
367+
for _, elem := range remove {
368+
updateOpts.OptionsToRemove = append(updateOpts.OptionsToRemove, &elasticbeanstalk.OptionSpecification{
369+
Namespace: elem.Namespace,
370+
OptionName: elem.OptionName,
371+
})
372+
}
373+
374+
updateOpts.OptionSettings = add
364375
}
365376

366377
if d.HasChange("template_name") {

builtin/providers/aws/resource_aws_elastic_beanstalk_environment_test.go

+154
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package aws
33
import (
44
"fmt"
55
"log"
6+
"reflect"
67
"regexp"
8+
"sort"
79
"testing"
810

911
"github.com/aws/aws-sdk-go/aws"
@@ -212,6 +214,93 @@ func TestAccAWSBeanstalkEnv_template_change(t *testing.T) {
212214
})
213215
}
214216

217+
func TestAccAWSBeanstalkEnv_basic_settings_update(t *testing.T) {
218+
var app elasticbeanstalk.EnvironmentDescription
219+
220+
rInt := acctest.RandInt()
221+
222+
resource.Test(t, resource.TestCase{
223+
PreCheck: func() { testAccPreCheck(t) },
224+
Providers: testAccProviders,
225+
CheckDestroy: testAccCheckBeanstalkEnvDestroy,
226+
Steps: []resource.TestStep{
227+
resource.TestStep{
228+
Config: testAccBeanstalkEnvConfig_empty_settings(rInt),
229+
Check: resource.ComposeTestCheckFunc(
230+
testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app),
231+
testAccVerifyBeanstalkConfig(&app, []string{}),
232+
),
233+
},
234+
resource.TestStep{
235+
Config: testAccBeanstalkEnvConfig_settings(rInt),
236+
Check: resource.ComposeTestCheckFunc(
237+
testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app),
238+
testAccVerifyBeanstalkConfig(&app, []string{"TF_LOG", "TF_SOME_VAR"}),
239+
),
240+
},
241+
resource.TestStep{
242+
Config: testAccBeanstalkEnvConfig_empty_settings(rInt),
243+
Check: resource.ComposeTestCheckFunc(
244+
testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app),
245+
testAccVerifyBeanstalkConfig(&app, []string{}),
246+
),
247+
},
248+
},
249+
})
250+
}
251+
252+
func testAccVerifyBeanstalkConfig(env *elasticbeanstalk.EnvironmentDescription, expected []string) resource.TestCheckFunc {
253+
return func(s *terraform.State) error {
254+
if env == nil {
255+
return fmt.Errorf("Nil environment in testAccVerifyBeanstalkConfig")
256+
}
257+
conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
258+
259+
resp, err := conn.DescribeConfigurationSettings(&elasticbeanstalk.DescribeConfigurationSettingsInput{
260+
ApplicationName: env.ApplicationName,
261+
EnvironmentName: env.EnvironmentName,
262+
})
263+
264+
if err != nil {
265+
return fmt.Errorf("Error describing config settings in testAccVerifyBeanstalkConfig: %s", err)
266+
}
267+
268+
// should only be 1 environment
269+
if len(resp.ConfigurationSettings) != 1 {
270+
return fmt.Errorf("Expected only 1 set of Configuration Settings in testAccVerifyBeanstalkConfig, got (%d)", len(resp.ConfigurationSettings))
271+
}
272+
273+
cs := resp.ConfigurationSettings[0]
274+
275+
var foundEnvs []string
276+
testStrings := []string{"TF_LOG", "TF_SOME_VAR"}
277+
for _, os := range cs.OptionSettings {
278+
for _, k := range testStrings {
279+
if *os.OptionName == k {
280+
foundEnvs = append(foundEnvs, k)
281+
}
282+
}
283+
}
284+
285+
// if expected is zero, then we should not have found any of the predefined
286+
// env vars
287+
if len(expected) == 0 {
288+
if len(foundEnvs) > 0 {
289+
return fmt.Errorf("Found configs we should not have: %#v", foundEnvs)
290+
}
291+
return nil
292+
}
293+
294+
sort.Strings(testStrings)
295+
sort.Strings(expected)
296+
if !reflect.DeepEqual(testStrings, expected) {
297+
return fmt.Errorf("Error matching strings, expected:\n\n%#v\n\ngot:\n\n%#v\n", testStrings, foundEnvs)
298+
}
299+
300+
return nil
301+
}
302+
}
303+
215304
func testAccCheckBeanstalkEnvDestroy(s *terraform.State) error {
216305
conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
217306

@@ -376,6 +465,71 @@ resource "aws_elastic_beanstalk_environment" "tfenvtest" {
376465
}
377466
`
378467

468+
func testAccBeanstalkEnvConfig_empty_settings(r int) string {
469+
return fmt.Sprintf(`
470+
resource "aws_elastic_beanstalk_application" "tftest" {
471+
name = "tf-test-name-%d"
472+
description = "tf-test-desc"
473+
}
474+
475+
resource "aws_elastic_beanstalk_environment" "tfenvtest" {
476+
name = "tf-test-name-%d"
477+
application = "${aws_elastic_beanstalk_application.tftest.name}"
478+
solution_stack_name = "64bit Amazon Linux running Python"
479+
480+
wait_for_ready_timeout = "15m"
481+
}`, r, r)
482+
}
483+
484+
func testAccBeanstalkEnvConfig_settings(r int) string {
485+
return fmt.Sprintf(`
486+
resource "aws_elastic_beanstalk_application" "tftest" {
487+
name = "tf-test-name-%d"
488+
description = "tf-test-desc"
489+
}
490+
491+
resource "aws_elastic_beanstalk_environment" "tfenvtest" {
492+
name = "tf-test-name-%d"
493+
application = "${aws_elastic_beanstalk_application.tftest.name}"
494+
solution_stack_name = "64bit Amazon Linux running Python"
495+
496+
wait_for_ready_timeout = "15m"
497+
498+
setting {
499+
namespace = "aws:elasticbeanstalk:application:environment"
500+
name = "TF_LOG"
501+
value = "true"
502+
}
503+
504+
setting {
505+
namespace = "aws:elasticbeanstalk:application:environment"
506+
name = "TF_SOME_VAR"
507+
value = "true"
508+
}
509+
510+
setting {
511+
namespace = "aws:autoscaling:scheduledaction"
512+
resource = "ScheduledAction01"
513+
name = "MinSize"
514+
value = 2
515+
}
516+
517+
setting {
518+
namespace = "aws:autoscaling:scheduledaction"
519+
resource = "ScheduledAction01"
520+
name = "MaxSize"
521+
value = 3
522+
}
523+
524+
setting {
525+
namespace = "aws:autoscaling:scheduledaction"
526+
resource = "ScheduledAction01"
527+
name = "StartTime"
528+
value = "2016-07-28T04:07:02Z"
529+
}
530+
}`, r, r)
531+
}
532+
379533
const testAccBeanstalkWorkerEnvConfig = `
380534
resource "aws_iam_instance_profile" "tftest" {
381535
name = "tftest_profile"

0 commit comments

Comments
 (0)