github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_db_instance_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  
    12  	"github.com/aws/aws-sdk-go/aws"
    13  	"github.com/aws/aws-sdk-go/aws/awserr"
    14  	"github.com/aws/aws-sdk-go/service/rds"
    15  )
    16  
    17  func TestAccAWSDBInstance_basic(t *testing.T) {
    18  	var v rds.DBInstance
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckAWSDBInstanceDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: testAccAWSDBInstanceConfig,
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v),
    29  					testAccCheckAWSDBInstanceAttributes(&v),
    30  					resource.TestCheckResourceAttr(
    31  						"aws_db_instance.bar", "allocated_storage", "10"),
    32  					resource.TestCheckResourceAttr(
    33  						"aws_db_instance.bar", "engine", "mysql"),
    34  					resource.TestCheckResourceAttr(
    35  						"aws_db_instance.bar", "license_model", "general-public-license"),
    36  					resource.TestCheckResourceAttr(
    37  						"aws_db_instance.bar", "instance_class", "db.t1.micro"),
    38  					resource.TestCheckResourceAttr(
    39  						"aws_db_instance.bar", "name", "baz"),
    40  					resource.TestCheckResourceAttr(
    41  						"aws_db_instance.bar", "username", "foo"),
    42  					resource.TestCheckResourceAttr(
    43  						"aws_db_instance.bar", "parameter_group_name", "default.mysql5.6"),
    44  				),
    45  			},
    46  		},
    47  	})
    48  }
    49  
    50  func TestAccAWSDBInstanceReplica(t *testing.T) {
    51  	var s, r rds.DBInstance
    52  
    53  	resource.Test(t, resource.TestCase{
    54  		PreCheck:     func() { testAccPreCheck(t) },
    55  		Providers:    testAccProviders,
    56  		CheckDestroy: testAccCheckAWSDBInstanceDestroy,
    57  		Steps: []resource.TestStep{
    58  			resource.TestStep{
    59  				Config: testAccReplicaInstanceConfig(rand.New(rand.NewSource(time.Now().UnixNano())).Int()),
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &s),
    62  					testAccCheckAWSDBInstanceExists("aws_db_instance.replica", &r),
    63  					testAccCheckAWSDBInstanceReplicaAttributes(&s, &r),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error {
    71  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
    72  
    73  	for _, rs := range s.RootModule().Resources {
    74  		if rs.Type != "aws_db_instance" {
    75  			continue
    76  		}
    77  
    78  		// Try to find the Group
    79  		var err error
    80  		resp, err := conn.DescribeDBInstances(
    81  			&rds.DescribeDBInstancesInput{
    82  				DBInstanceIdentifier: aws.String(rs.Primary.ID),
    83  			})
    84  
    85  		if err == nil {
    86  			if len(resp.DBInstances) != 0 &&
    87  				*resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
    88  				return fmt.Errorf("DB Instance still exists")
    89  			}
    90  		}
    91  
    92  		// Verify the error
    93  		newerr, ok := err.(awserr.Error)
    94  		if !ok {
    95  			return err
    96  		}
    97  		if newerr.Code() != "InvalidDBInstance.NotFound" {
    98  			return err
    99  		}
   100  	}
   101  
   102  	return nil
   103  }
   104  
   105  func testAccCheckAWSDBInstanceAttributes(v *rds.DBInstance) resource.TestCheckFunc {
   106  	return func(s *terraform.State) error {
   107  
   108  		if *v.Engine != "mysql" {
   109  			return fmt.Errorf("bad engine: %#v", *v.Engine)
   110  		}
   111  
   112  		if *v.EngineVersion == "" {
   113  			return fmt.Errorf("bad engine_version: %#v", *v.EngineVersion)
   114  		}
   115  
   116  		if *v.BackupRetentionPeriod != 0 {
   117  			return fmt.Errorf("bad backup_retention_period: %#v", *v.BackupRetentionPeriod)
   118  		}
   119  
   120  		return nil
   121  	}
   122  }
   123  
   124  func testAccCheckAWSDBInstanceReplicaAttributes(source, replica *rds.DBInstance) resource.TestCheckFunc {
   125  	return func(s *terraform.State) error {
   126  
   127  		if replica.ReadReplicaSourceDBInstanceIdentifier != nil && *replica.ReadReplicaSourceDBInstanceIdentifier != *source.DBInstanceIdentifier {
   128  			return fmt.Errorf("bad source identifier for replica, expected: '%s', got: '%s'", *source.DBInstanceIdentifier, *replica.ReadReplicaSourceDBInstanceIdentifier)
   129  		}
   130  
   131  		return nil
   132  	}
   133  }
   134  
   135  func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc {
   136  	return func(s *terraform.State) error {
   137  		rs, ok := s.RootModule().Resources[n]
   138  		if !ok {
   139  			return fmt.Errorf("Not found: %s", n)
   140  		}
   141  
   142  		if rs.Primary.ID == "" {
   143  			return fmt.Errorf("No DB Instance ID is set")
   144  		}
   145  
   146  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   147  
   148  		opts := rds.DescribeDBInstancesInput{
   149  			DBInstanceIdentifier: aws.String(rs.Primary.ID),
   150  		}
   151  
   152  		resp, err := conn.DescribeDBInstances(&opts)
   153  
   154  		if err != nil {
   155  			return err
   156  		}
   157  
   158  		if len(resp.DBInstances) != 1 ||
   159  			*resp.DBInstances[0].DBInstanceIdentifier != rs.Primary.ID {
   160  			return fmt.Errorf("DB Instance not found")
   161  		}
   162  
   163  		*v = *resp.DBInstances[0]
   164  
   165  		return nil
   166  	}
   167  }
   168  
   169  // Database names cannot collide, and deletion takes so long, that making the
   170  // name a bit random helps so able we can kill a test that's just waiting for a
   171  // delete and not be blocked on kicking off another one.
   172  var testAccAWSDBInstanceConfig = fmt.Sprintf(`
   173  resource "aws_db_instance" "bar" {
   174  	identifier = "foobarbaz-test-terraform-%d"
   175  
   176  	allocated_storage = 10
   177  	engine = "MySQL"
   178  	engine_version = "5.6.21"
   179  	instance_class = "db.t1.micro"
   180  	name = "baz"
   181  	password = "barbarbarbar"
   182  	username = "foo"
   183  
   184  
   185  	# Maintenance Window is stored in lower case in the API, though not strictly 
   186  	# documented. Terraform will downcase this to match (as opposed to throw a 
   187  	# validation error).
   188  	maintenance_window = "Fri:09:00-Fri:09:30"
   189  
   190  	backup_retention_period = 0
   191  
   192  	parameter_group_name = "default.mysql5.6"
   193  }`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
   194  
   195  func testAccReplicaInstanceConfig(val int) string {
   196  	return fmt.Sprintf(`
   197  	resource "aws_db_instance" "bar" {
   198  		identifier = "foobarbaz-test-terraform-%d"
   199  
   200  		allocated_storage = 5
   201  		engine = "mysql"
   202  		engine_version = "5.6.21"
   203  		instance_class = "db.t1.micro"
   204  		name = "baz"
   205  		password = "barbarbarbar"
   206  		username = "foo"
   207  
   208  		backup_retention_period = 1
   209  
   210  		parameter_group_name = "default.mysql5.6"
   211  	}
   212  	
   213  	resource "aws_db_instance" "replica" {
   214  	  identifier = "tf-replica-db-%d"
   215  		backup_retention_period = 0
   216  		replicate_source_db = "${aws_db_instance.bar.identifier}"
   217  		allocated_storage = "${aws_db_instance.bar.allocated_storage}"
   218  		engine = "${aws_db_instance.bar.engine}"
   219  		engine_version = "${aws_db_instance.bar.engine_version}"
   220  		instance_class = "${aws_db_instance.bar.instance_class}"
   221  		password = "${aws_db_instance.bar.password}"
   222  		username = "${aws_db_instance.bar.username}"
   223  		tags {
   224  			Name = "tf-replica-db"
   225  		}
   226  	}
   227  	`, val, val)
   228  }