github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/data_source_aws_autoscaling_groups_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"sort"
     7  	"strconv"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSAutoscalingGroups_basic(t *testing.T) {
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:  func() { testAccPreCheck(t) },
    18  		Providers: testAccProviders,
    19  		Steps: []resource.TestStep{
    20  			{
    21  				Config: testAccCheckAwsAutoscalingGroupsConfig(acctest.RandInt(), acctest.RandInt(), acctest.RandInt()),
    22  			},
    23  			{
    24  				Config: testAccCheckAwsAutoscalingGroupsConfigWithDataSource(acctest.RandInt(), acctest.RandInt(), acctest.RandInt()),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckAwsAutoscalingGroups("data.aws_autoscaling_groups.group_list"),
    27  					resource.TestCheckResourceAttr("data.aws_autoscaling_groups.group_list", "names.#", "3"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func testAccCheckAwsAutoscalingGroups(n string) resource.TestCheckFunc {
    35  	return func(s *terraform.State) error {
    36  		rs, ok := s.RootModule().Resources[n]
    37  		if !ok {
    38  			return fmt.Errorf("Can't find ASG resource: %s", n)
    39  		}
    40  
    41  		if rs.Primary.ID == "" {
    42  			return fmt.Errorf("AZ resource ID not set.")
    43  		}
    44  
    45  		actual, err := testAccCheckAwsAutoscalingGroupsAvailable(rs.Primary.Attributes)
    46  		if err != nil {
    47  			return err
    48  		}
    49  
    50  		expected := actual
    51  		sort.Strings(expected)
    52  		if reflect.DeepEqual(expected, actual) != true {
    53  			return fmt.Errorf("ASG not sorted - expected %v, got %v", expected, actual)
    54  		}
    55  		return nil
    56  	}
    57  }
    58  
    59  func testAccCheckAwsAutoscalingGroupsAvailable(attrs map[string]string) ([]string, error) {
    60  	v, ok := attrs["names.#"]
    61  	if !ok {
    62  		return nil, fmt.Errorf("Available ASG list is missing.")
    63  	}
    64  	qty, err := strconv.Atoi(v)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	if qty < 1 {
    69  		return nil, fmt.Errorf("No ASG found in region, this is probably a bug.")
    70  	}
    71  	zones := make([]string, qty)
    72  	for n := range zones {
    73  		zone, ok := attrs["names."+strconv.Itoa(n)]
    74  		if !ok {
    75  			return nil, fmt.Errorf("ASG list corrupt, this is definitely a bug.")
    76  		}
    77  		zones[n] = zone
    78  	}
    79  	return zones, nil
    80  }
    81  
    82  func testAccCheckAwsAutoscalingGroupsConfig(rInt1, rInt2, rInt3 int) string {
    83  	return fmt.Sprintf(`
    84  resource "aws_launch_configuration" "foobar" {
    85    image_id = "ami-21f78e11"
    86    instance_type = "t1.micro"
    87  }
    88  
    89  resource "aws_autoscaling_group" "bar" {
    90    availability_zones = ["us-west-2a"]
    91    name = "test-asg-%d"
    92    max_size = 1
    93    min_size = 0
    94    health_check_type = "EC2"
    95    desired_capacity = 0
    96    force_delete = true
    97  
    98    launch_configuration = "${aws_launch_configuration.foobar.name}"
    99  
   100    tag {
   101      key = "Foo"
   102      value = "foo-bar"
   103      propagate_at_launch = true
   104    }
   105  }
   106  
   107  resource "aws_autoscaling_group" "foo" {
   108    availability_zones = ["us-west-2b"]
   109    name = "test-asg-%d"
   110    max_size = 1
   111    min_size = 0
   112    health_check_type = "EC2"
   113    desired_capacity = 0
   114    force_delete = true
   115  
   116    launch_configuration = "${aws_launch_configuration.foobar.name}"
   117  
   118    tag {
   119      key = "Foo"
   120      value = "foo-bar"
   121      propagate_at_launch = true
   122    }
   123  }
   124  
   125  resource "aws_autoscaling_group" "barbaz" {
   126    availability_zones = ["us-west-2c"]
   127    name = "test-asg-%d"
   128    max_size = 1
   129    min_size = 0
   130    health_check_type = "EC2"
   131    desired_capacity = 0
   132    force_delete = true
   133  
   134    launch_configuration = "${aws_launch_configuration.foobar.name}"
   135  
   136    tag {
   137      key = "Foo"
   138      value = "foo-bar"
   139      propagate_at_launch = true
   140    }
   141  }`, rInt1, rInt2, rInt3)
   142  }
   143  
   144  func testAccCheckAwsAutoscalingGroupsConfigWithDataSource(rInt1, rInt2, rInt3 int) string {
   145  	return fmt.Sprintf(`
   146  resource "aws_launch_configuration" "foobar" {
   147    image_id = "ami-21f78e11"
   148    instance_type = "t1.micro"
   149  }
   150  
   151  resource "aws_autoscaling_group" "bar" {
   152    availability_zones = ["us-west-2a"]
   153    name = "test-asg-%d"
   154    max_size = 1
   155    min_size = 0
   156    health_check_type = "EC2"
   157    desired_capacity = 0
   158    force_delete = true
   159  
   160    launch_configuration = "${aws_launch_configuration.foobar.name}"
   161  
   162    tag {
   163      key = "Foo"
   164      value = "foo-bar"
   165      propagate_at_launch = true
   166    }
   167  }
   168  
   169  resource "aws_autoscaling_group" "foo" {
   170    availability_zones = ["us-west-2b"]
   171    name = "test-asg-%d"
   172    max_size = 1
   173    min_size = 0
   174    health_check_type = "EC2"
   175    desired_capacity = 0
   176    force_delete = true
   177  
   178    launch_configuration = "${aws_launch_configuration.foobar.name}"
   179  
   180    tag {
   181      key = "Foo"
   182      value = "foo-bar"
   183      propagate_at_launch = true
   184    }
   185  }
   186  
   187  resource "aws_autoscaling_group" "barbaz" {
   188    availability_zones = ["us-west-2c"]
   189    name = "test-asg-%d"
   190    max_size = 1
   191    min_size = 0
   192    health_check_type = "EC2"
   193    desired_capacity = 0
   194    force_delete = true
   195  
   196    launch_configuration = "${aws_launch_configuration.foobar.name}"
   197  
   198    tag {
   199      key = "Foo"
   200      value = "foo-bar"
   201      propagate_at_launch = true
   202    }
   203  }
   204  
   205  data "aws_autoscaling_groups" "group_list" {}
   206  `, rInt1, rInt2, rInt3)
   207  }