github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_codedeploy_deployment_config_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/codedeploy"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSCodeDeployDeploymentConfig_fleetPercent(t *testing.T) {
    16  	var config codedeploy.DeploymentConfigInfo
    17  
    18  	rName := acctest.RandString(5)
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckAWSCodeDeployDeploymentConfigDestroy,
    24  		Steps: []resource.TestStep{
    25  			{
    26  				Config: testAccAWSCodeDeployDeploymentConfigFleet(rName),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckAWSCodeDeployDeploymentConfigExists("aws_codedeploy_deployment_config.foo", &config),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_codedeploy_deployment_config.foo", "minimum_healthy_hosts.0.type", "FLEET_PERCENT"),
    31  					resource.TestCheckResourceAttr(
    32  						"aws_codedeploy_deployment_config.foo", "minimum_healthy_hosts.0.value", "75"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccAWSCodeDeployDeploymentConfig_hostCount(t *testing.T) {
    40  	var config codedeploy.DeploymentConfigInfo
    41  
    42  	rName := acctest.RandString(5)
    43  
    44  	resource.Test(t, resource.TestCase{
    45  		PreCheck:     func() { testAccPreCheck(t) },
    46  		Providers:    testAccProviders,
    47  		CheckDestroy: testAccCheckAWSCodeDeployDeploymentConfigDestroy,
    48  		Steps: []resource.TestStep{
    49  			{
    50  				Config: testAccAWSCodeDeployDeploymentConfigHostCount(rName),
    51  				Check: resource.ComposeTestCheckFunc(
    52  					testAccCheckAWSCodeDeployDeploymentConfigExists("aws_codedeploy_deployment_config.foo", &config),
    53  					resource.TestCheckResourceAttr(
    54  						"aws_codedeploy_deployment_config.foo", "minimum_healthy_hosts.0.type", "HOST_COUNT"),
    55  					resource.TestCheckResourceAttr(
    56  						"aws_codedeploy_deployment_config.foo", "minimum_healthy_hosts.0.value", "1"),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func TestValidateAWSCodeDeployMinimumHealthyHostsType(t *testing.T) {
    64  	cases := []struct {
    65  		Value    string
    66  		ErrCount int
    67  	}{
    68  		{
    69  			Value:    "FLEET_PERCENT",
    70  			ErrCount: 0,
    71  		},
    72  		{
    73  			Value:    "HOST_COUNT",
    74  			ErrCount: 0,
    75  		},
    76  		{
    77  			Value:    "host_count",
    78  			ErrCount: 1,
    79  		},
    80  		{
    81  			Value:    "hostcount",
    82  			ErrCount: 1,
    83  		},
    84  		{
    85  			Value:    "FleetPercent",
    86  			ErrCount: 1,
    87  		},
    88  		{
    89  			Value:    "Foo",
    90  			ErrCount: 1,
    91  		},
    92  		{
    93  			Value:    "",
    94  			ErrCount: 1,
    95  		},
    96  	}
    97  
    98  	for _, tc := range cases {
    99  		_, errors := validateMinimumHealtyHostsType(tc.Value, "minimum_healthy_hosts_type")
   100  		if len(errors) != tc.ErrCount {
   101  			t.Fatalf("Minimum Healthy Hosts validation failed for type %q: %q", tc.Value, errors)
   102  		}
   103  	}
   104  }
   105  
   106  func testAccCheckAWSCodeDeployDeploymentConfigDestroy(s *terraform.State) error {
   107  	conn := testAccProvider.Meta().(*AWSClient).codedeployconn
   108  
   109  	for _, rs := range s.RootModule().Resources {
   110  		if rs.Type != "aws_codedeploy_deployment_config" {
   111  			continue
   112  		}
   113  
   114  		resp, err := conn.GetDeploymentConfig(&codedeploy.GetDeploymentConfigInput{
   115  			DeploymentConfigName: aws.String(rs.Primary.ID),
   116  		})
   117  
   118  		if ae, ok := err.(awserr.Error); ok && ae.Code() == "DeploymentConfigDoesNotExistException" {
   119  			continue
   120  		}
   121  
   122  		if err == nil {
   123  			if resp.DeploymentConfigInfo != nil {
   124  				return fmt.Errorf("CodeDeploy deployment config still exists:\n%#v", *resp.DeploymentConfigInfo.DeploymentConfigName)
   125  			}
   126  		}
   127  
   128  		return err
   129  	}
   130  
   131  	return nil
   132  }
   133  
   134  func testAccCheckAWSCodeDeployDeploymentConfigExists(name string, config *codedeploy.DeploymentConfigInfo) resource.TestCheckFunc {
   135  	return func(s *terraform.State) error {
   136  		rs, ok := s.RootModule().Resources[name]
   137  		if !ok {
   138  			return fmt.Errorf("Not found: %s", name)
   139  		}
   140  
   141  		conn := testAccProvider.Meta().(*AWSClient).codedeployconn
   142  
   143  		resp, err := conn.GetDeploymentConfig(&codedeploy.GetDeploymentConfigInput{
   144  			DeploymentConfigName: aws.String(rs.Primary.ID),
   145  		})
   146  
   147  		if err != nil {
   148  			return err
   149  		}
   150  
   151  		*config = *resp.DeploymentConfigInfo
   152  
   153  		return nil
   154  	}
   155  }
   156  
   157  func testAccAWSCodeDeployDeploymentConfigFleet(rName string) string {
   158  	return fmt.Sprintf(`
   159  resource "aws_codedeploy_deployment_config" "foo" {
   160  	deployment_config_name = "test-deployment-config-%s"
   161  	minimum_healthy_hosts {
   162  		type = "FLEET_PERCENT"
   163  		value = 75
   164  	}
   165  }`, rName)
   166  }
   167  
   168  func testAccAWSCodeDeployDeploymentConfigHostCount(rName string) string {
   169  	return fmt.Sprintf(`
   170  resource "aws_codedeploy_deployment_config" "foo" {
   171  	deployment_config_name = "test-deployment-config-%s"
   172  	minimum_healthy_hosts {
   173  		type = "HOST_COUNT"
   174  		value = 1
   175  	}
   176  }`, rName)
   177  }