github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_db_option_group_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/rds"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSDBOptionGroup_basic(t *testing.T) {
    16  	var v rds.OptionGroup
    17  	rName := fmt.Sprintf("option-group-test-terraform-%s", acctest.RandString(5))
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckAWSDBOptionGroupDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccAWSDBOptionGroupBasicConfig(rName),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckAWSDBOptionGroupExists("aws_db_option_group.bar", &v),
    28  					testAccCheckAWSDBOptionGroupAttributes(&v),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_db_option_group.bar", "name", rName),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccAWSDBOptionGroup_basicDestroyWithInstance(t *testing.T) {
    38  	rName := fmt.Sprintf("option-group-test-terraform-%s", acctest.RandString(5))
    39  
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccPreCheck(t) },
    42  		Providers:    testAccProviders,
    43  		CheckDestroy: testAccCheckAWSDBOptionGroupDestroy,
    44  		Steps: []resource.TestStep{
    45  			resource.TestStep{
    46  				Config: testAccAWSDBOptionGroupBasicDestroyConfig(rName),
    47  			},
    48  		},
    49  	})
    50  }
    51  
    52  func TestAccAWSDBOptionGroup_OptionSettings(t *testing.T) {
    53  	var v rds.OptionGroup
    54  	rName := fmt.Sprintf("option-group-test-terraform-%s", acctest.RandString(5))
    55  
    56  	resource.Test(t, resource.TestCase{
    57  		PreCheck:     func() { testAccPreCheck(t) },
    58  		Providers:    testAccProviders,
    59  		CheckDestroy: testAccCheckAWSDBOptionGroupDestroy,
    60  		Steps: []resource.TestStep{
    61  			resource.TestStep{
    62  				Config: testAccAWSDBOptionGroupOptionSettings(rName),
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckAWSDBOptionGroupExists("aws_db_option_group.bar", &v),
    65  					resource.TestCheckResourceAttr(
    66  						"aws_db_option_group.bar", "name", rName),
    67  					resource.TestCheckResourceAttr(
    68  						"aws_db_option_group.bar", "option.#", "1"),
    69  					resource.TestCheckResourceAttr(
    70  						"aws_db_option_group.bar", "option.961211605.option_settings.129825347.value", "UTC"),
    71  				),
    72  			},
    73  			resource.TestStep{
    74  				Config: testAccAWSDBOptionGroupOptionSettings_update(rName),
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckAWSDBOptionGroupExists("aws_db_option_group.bar", &v),
    77  					resource.TestCheckResourceAttr(
    78  						"aws_db_option_group.bar", "name", rName),
    79  					resource.TestCheckResourceAttr(
    80  						"aws_db_option_group.bar", "option.#", "1"),
    81  					resource.TestCheckResourceAttr(
    82  						"aws_db_option_group.bar", "option.2422743510.option_settings.1350509764.value", "US/Pacific"),
    83  				),
    84  			},
    85  		},
    86  	})
    87  }
    88  
    89  func TestAccAWSDBOptionGroup_sqlServerOptionsUpdate(t *testing.T) {
    90  	var v rds.OptionGroup
    91  	rName := fmt.Sprintf("option-group-test-terraform-%s", acctest.RandString(5))
    92  
    93  	resource.Test(t, resource.TestCase{
    94  		PreCheck:     func() { testAccPreCheck(t) },
    95  		Providers:    testAccProviders,
    96  		CheckDestroy: testAccCheckAWSDBOptionGroupDestroy,
    97  		Steps: []resource.TestStep{
    98  			resource.TestStep{
    99  				Config: testAccAWSDBOptionGroupSqlServerEEOptions(rName),
   100  				Check: resource.ComposeTestCheckFunc(
   101  					testAccCheckAWSDBOptionGroupExists("aws_db_option_group.bar", &v),
   102  					resource.TestCheckResourceAttr(
   103  						"aws_db_option_group.bar", "name", rName),
   104  				),
   105  			},
   106  
   107  			resource.TestStep{
   108  				Config: testAccAWSDBOptionGroupSqlServerEEOptions_update(rName),
   109  				Check: resource.ComposeTestCheckFunc(
   110  					testAccCheckAWSDBOptionGroupExists("aws_db_option_group.bar", &v),
   111  					resource.TestCheckResourceAttr(
   112  						"aws_db_option_group.bar", "name", rName),
   113  					resource.TestCheckResourceAttr(
   114  						"aws_db_option_group.bar", "option.#", "1"),
   115  				),
   116  			},
   117  		},
   118  	})
   119  }
   120  
   121  func TestAccAWSDBOptionGroup_multipleOptions(t *testing.T) {
   122  	var v rds.OptionGroup
   123  	rName := fmt.Sprintf("option-group-test-terraform-%s", acctest.RandString(5))
   124  
   125  	resource.Test(t, resource.TestCase{
   126  		PreCheck:     func() { testAccPreCheck(t) },
   127  		Providers:    testAccProviders,
   128  		CheckDestroy: testAccCheckAWSDBOptionGroupDestroy,
   129  		Steps: []resource.TestStep{
   130  			resource.TestStep{
   131  				Config: testAccAWSDBOptionGroupMultipleOptions(rName),
   132  				Check: resource.ComposeTestCheckFunc(
   133  					testAccCheckAWSDBOptionGroupExists("aws_db_option_group.bar", &v),
   134  					resource.TestCheckResourceAttr(
   135  						"aws_db_option_group.bar", "name", rName),
   136  					resource.TestCheckResourceAttr(
   137  						"aws_db_option_group.bar", "option.#", "2"),
   138  				),
   139  			},
   140  		},
   141  	})
   142  }
   143  
   144  func testAccCheckAWSDBOptionGroupAttributes(v *rds.OptionGroup) resource.TestCheckFunc {
   145  	return func(s *terraform.State) error {
   146  
   147  		if *v.EngineName != "mysql" {
   148  			return fmt.Errorf("bad engine_name: %#v", *v.EngineName)
   149  		}
   150  
   151  		if *v.MajorEngineVersion != "5.6" {
   152  			return fmt.Errorf("bad major_engine_version: %#v", *v.MajorEngineVersion)
   153  		}
   154  
   155  		if *v.OptionGroupDescription != "Test option group for terraform" {
   156  			return fmt.Errorf("bad option_group_description: %#v", *v.OptionGroupDescription)
   157  		}
   158  
   159  		return nil
   160  	}
   161  }
   162  
   163  func TestResourceAWSDBOptionGroupName_validation(t *testing.T) {
   164  	cases := []struct {
   165  		Value    string
   166  		ErrCount int
   167  	}{
   168  		{
   169  			Value:    "testing123!",
   170  			ErrCount: 1,
   171  		},
   172  		{
   173  			Value:    "1testing123",
   174  			ErrCount: 1,
   175  		},
   176  		{
   177  			Value:    "testing--123",
   178  			ErrCount: 1,
   179  		},
   180  		{
   181  			Value:    "testing123-",
   182  			ErrCount: 1,
   183  		},
   184  		{
   185  			Value:    randomString(256),
   186  			ErrCount: 1,
   187  		},
   188  	}
   189  
   190  	for _, tc := range cases {
   191  		_, errors := validateDbOptionGroupName(tc.Value, "aws_db_option_group_name")
   192  
   193  		if len(errors) != tc.ErrCount {
   194  			t.Fatalf("Expected the DB Option Group Name to trigger a validation error")
   195  		}
   196  	}
   197  }
   198  
   199  func testAccCheckAWSDBOptionGroupExists(n string, v *rds.OptionGroup) resource.TestCheckFunc {
   200  	return func(s *terraform.State) error {
   201  		rs, ok := s.RootModule().Resources[n]
   202  		if !ok {
   203  			return fmt.Errorf("Not found: %s", n)
   204  		}
   205  
   206  		if rs.Primary.ID == "" {
   207  			return fmt.Errorf("No DB Option Group Name is set")
   208  		}
   209  
   210  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   211  
   212  		opts := rds.DescribeOptionGroupsInput{
   213  			OptionGroupName: aws.String(rs.Primary.ID),
   214  		}
   215  
   216  		resp, err := conn.DescribeOptionGroups(&opts)
   217  
   218  		if err != nil {
   219  			return err
   220  		}
   221  
   222  		if len(resp.OptionGroupsList) != 1 ||
   223  			*resp.OptionGroupsList[0].OptionGroupName != rs.Primary.ID {
   224  			return fmt.Errorf("DB Option Group not found")
   225  		}
   226  
   227  		*v = *resp.OptionGroupsList[0]
   228  
   229  		return nil
   230  	}
   231  }
   232  
   233  func testAccCheckAWSDBOptionGroupDestroy(s *terraform.State) error {
   234  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
   235  
   236  	for _, rs := range s.RootModule().Resources {
   237  		if rs.Type != "aws_db_option_group" {
   238  			continue
   239  		}
   240  
   241  		resp, err := conn.DescribeOptionGroups(
   242  			&rds.DescribeOptionGroupsInput{
   243  				OptionGroupName: aws.String(rs.Primary.ID),
   244  			})
   245  
   246  		if err == nil {
   247  			if len(resp.OptionGroupsList) != 0 &&
   248  				*resp.OptionGroupsList[0].OptionGroupName == rs.Primary.ID {
   249  				return fmt.Errorf("DB Option Group still exists")
   250  			}
   251  		}
   252  
   253  		// Verify the error
   254  		newerr, ok := err.(awserr.Error)
   255  		if !ok {
   256  			return err
   257  		}
   258  		if newerr.Code() != "OptionGroupNotFoundFault" {
   259  			return err
   260  		}
   261  	}
   262  
   263  	return nil
   264  }
   265  
   266  func testAccAWSDBOptionGroupBasicConfig(r string) string {
   267  	return fmt.Sprintf(`
   268  resource "aws_db_option_group" "bar" {
   269    name                     = "%s"
   270    option_group_description = "Test option group for terraform"
   271    engine_name              = "mysql"
   272    major_engine_version     = "5.6"
   273  }
   274  `, r)
   275  }
   276  
   277  func testAccAWSDBOptionGroupBasicDestroyConfig(r string) string {
   278  	return fmt.Sprintf(`
   279  resource "aws_db_instance" "bar" {
   280  	allocated_storage = 10
   281  	engine = "MySQL"
   282  	engine_version = "5.6.21"
   283  	instance_class = "db.t2.micro"
   284  	name = "baz"
   285  	password = "barbarbarbar"
   286  	username = "foo"
   287  
   288  
   289  	# Maintenance Window is stored in lower case in the API, though not strictly
   290  	# documented. Terraform will downcase this to match (as opposed to throw a
   291  	# validation error).
   292  	maintenance_window = "Fri:09:00-Fri:09:30"
   293  
   294  	backup_retention_period = 0
   295  
   296  	option_group_name = "${aws_db_option_group.bar.name}"
   297  }
   298  
   299  resource "aws_db_option_group" "bar" {
   300    name                     = "%s"
   301    option_group_description = "Test option group for terraform"
   302    engine_name              = "mysql"
   303    major_engine_version     = "5.6"
   304  }
   305  `, r)
   306  }
   307  
   308  func testAccAWSDBOptionGroupOptionSettings(r string) string {
   309  	return fmt.Sprintf(`
   310  resource "aws_db_option_group" "bar" {
   311    name                     = "%s"
   312    option_group_description = "Test option group for terraform"
   313    engine_name              = "oracle-ee"
   314    major_engine_version     = "11.2"
   315  
   316    option {
   317      option_name = "Timezone"
   318      option_settings {
   319        name = "TIME_ZONE"
   320        value = "UTC"
   321      }
   322    }
   323  }
   324  `, r)
   325  }
   326  
   327  func testAccAWSDBOptionGroupOptionSettings_update(r string) string {
   328  	return fmt.Sprintf(`
   329  resource "aws_db_option_group" "bar" {
   330    name                     = "%s"
   331    option_group_description = "Test option group for terraform"
   332    engine_name              = "oracle-ee"
   333    major_engine_version     = "11.2"
   334  
   335    option {
   336      option_name = "Timezone"
   337      option_settings {
   338        name = "TIME_ZONE"
   339        value = "US/Pacific"
   340      }
   341    }
   342  }
   343  `, r)
   344  }
   345  
   346  func testAccAWSDBOptionGroupSqlServerEEOptions(r string) string {
   347  	return fmt.Sprintf(`
   348  resource "aws_db_option_group" "bar" {
   349    name                     = "%s"
   350    option_group_description = "Test option group for terraform"
   351    engine_name              = "sqlserver-ee"
   352    major_engine_version     = "11.00"
   353  }
   354  `, r)
   355  }
   356  
   357  func testAccAWSDBOptionGroupSqlServerEEOptions_update(r string) string {
   358  	return fmt.Sprintf(`
   359  resource "aws_db_option_group" "bar" {
   360    name                     = "%s"
   361    option_group_description = "Test option group for terraform"
   362    engine_name              = "sqlserver-ee"
   363    major_engine_version     = "11.00"
   364  
   365    option {
   366      option_name = "Mirroring"
   367    }
   368  }
   369  `, r)
   370  }
   371  
   372  func testAccAWSDBOptionGroupMultipleOptions(r string) string {
   373  	return fmt.Sprintf(`
   374  resource "aws_db_option_group" "bar" {
   375    name                     = "%s"
   376    option_group_description = "Test option group for terraform"
   377    engine_name              = "oracle-se"
   378    major_engine_version     = "11.2"
   379  
   380    option {
   381      option_name = "STATSPACK"
   382    }
   383  
   384    option {
   385      option_name = "XMLDB"
   386    }
   387  }
   388  `, r)
   389  }