<commit_before>package aws

import (
	"fmt"
	"testing"

	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
	"github.com/mitchellh/goamz/rds"
)

func TestAccAWSDBInstance(t *testing.T) {
	var v rds.DBInstance

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSDBInstanceDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: testAccAWSDBInstanceConfig,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v),
					testAccCheckAWSDBInstanceAttributes(&v),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "identifier", "foobarbaz-test-terraform"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "allocated_storage", "10"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "engine", "mysql"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "engine_version", "5.6.17"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "instance_class", "db.t1.micro"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "name", "baz"),
					resource.TestCheckResourceAttr(
						// Shouldn't save password to state
						"aws_db_instance.bar", "password", ""),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "username", "foo"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "skip_final_snapshot", "true"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "security_group_names.0", "secfoobarbaz-test-terraform"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "parameter_group_name", "default.mysql5.6"),
				),
			},
		},
	})
}

func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error {
	conn := testAccProvider.Meta().(*AWSClient).rdsconn

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_db_instance" {
			continue
		}

		// Try to find the Group
		resp, err := conn.DescribeDBInstances(
			&rds.DescribeDBInstances{
				DBInstanceIdentifier: rs.Primary.ID,
			})

		if err == nil {
			if len(resp.DBInstances) != 0 &&
				resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
				return fmt.Errorf("DB Instance still exists")
			}
		}

		// Verify the error
		newerr, ok := err.(*rds.Error)
		if !ok {
			return err
		}
		if newerr.Code != "InvalidDBInstance.NotFound" {
			return err
		}
	}

	return nil
}

func testAccCheckAWSDBInstanceAttributes(v *rds.DBInstance) resource.TestCheckFunc {
	return func(s *terraform.State) error {

		if len(v.DBSecurityGroupNames) == 0 {
			return fmt.Errorf("no sec names: %#v", v.DBSecurityGroupNames)
		}

		if v.Engine != "mysql" {
			return fmt.Errorf("bad engine: %#v", v.Engine)
		}

		if v.EngineVersion != "5.6.17" {
			return fmt.Errorf("bad engine_version: %#v", v.EngineVersion)
		}

		return nil
	}
}

func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No DB Instance ID is set")
		}

		conn := testAccProvider.Meta().(*AWSClient).rdsconn

		opts := rds.DescribeDBInstances{
			DBInstanceIdentifier: rs.Primary.ID,
		}

		resp, err := conn.DescribeDBInstances(&opts)

		if err != nil {
			return err
		}

		if len(resp.DBInstances) != 1 ||
			resp.DBInstances[0].DBInstanceIdentifier != rs.Primary.ID {
			return fmt.Errorf("DB Instance not found")
		}

		*v = resp.DBInstances[0]

		return nil
	}
}

const testAccAWSDBInstanceConfig = `
resource "aws_db_security_group" "bar" {
	name = "secfoobarbaz-test-terraform"
	description = "just cuz"

	ingress {
		cidr = "10.0.0.1/24"
	}
}

resource "aws_db_instance" "bar" {
	identifier = "foobarbaz-test-terraform"

	allocated_storage = 10
	engine = "mysql"
	engine_version = "5.6.17"
	instance_class = "db.t1.micro"
	name = "baz"
	password = "<PASSWORD>"
	username = "foo"

	skip_final_snapshot = true

	security_group_names = ["${aws_db_security_group.bar.name}"]
	parameter_group_name = "default.mysql5.6"
}
`
<commit_msg>provider/aws: fix aws_db_instance acceptance tests<commit_after>package aws

import (
	"fmt"
	"testing"

	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
	"github.com/mitchellh/goamz/rds"
)

func TestAccAWSDBInstance(t *testing.T) {
	var v rds.DBInstance

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSDBInstanceDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: testAccAWSDBInstanceConfig,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v),
					testAccCheckAWSDBInstanceAttributes(&v),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "identifier", "foobarbaz-test-terraform"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "allocated_storage", "10"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "engine", "mysql"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "engine_version", "5.6.21"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "instance_class", "db.t1.micro"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "name", "baz"),
					resource.TestCheckResourceAttr(
						// Shouldn't save password to state
						"aws_db_instance.bar", "password", ""),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "username", "foo"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "skip_final_snapshot", "true"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "security_group_names.3322503515", "secfoobarbaz-test-terraform"),
					resource.TestCheckResourceAttr(
						"aws_db_instance.bar", "parameter_group_name", "default.mysql5.6"),
				),
			},
		},
	})
}

func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error {
	conn := testAccProvider.Meta().(*AWSClient).rdsconn

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_db_instance" {
			continue
		}

		// Try to find the Group
		resp, err := conn.DescribeDBInstances(
			&rds.DescribeDBInstances{
				DBInstanceIdentifier: rs.Primary.ID,
			})

		if err == nil {
			if len(resp.DBInstances) != 0 &&
				resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
				return fmt.Errorf("DB Instance still exists")
			}
		}

		// Verify the error
		newerr, ok := err.(*rds.Error)
		if !ok {
			return err
		}
		if newerr.Code != "InvalidDBInstance.NotFound" {
			return err
		}
	}

	return nil
}

func testAccCheckAWSDBInstanceAttributes(v *rds.DBInstance) resource.TestCheckFunc {
	return func(s *terraform.State) error {

		if len(v.DBSecurityGroupNames) == 0 {
			return fmt.Errorf("no sec names: %#v", v.DBSecurityGroupNames)
		}

		if v.Engine != "mysql" {
			return fmt.Errorf("bad engine: %#v", v.Engine)
		}

		if v.EngineVersion != "5.6.21" {
			return fmt.Errorf("bad engine_version: %#v", v.EngineVersion)
		}

		return nil
	}
}

func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No DB Instance ID is set")
		}

		conn := testAccProvider.Meta().(*AWSClient).rdsconn

		opts := rds.DescribeDBInstances{
			DBInstanceIdentifier: rs.Primary.ID,
		}

		resp, err := conn.DescribeDBInstances(&opts)

		if err != nil {
			return err
		}

		if len(resp.DBInstances) != 1 ||
			resp.DBInstances[0].DBInstanceIdentifier != rs.Primary.ID {
			return fmt.Errorf("DB Instance not found")
		}

		*v = resp.DBInstances[0]

		return nil
	}
}

const testAccAWSDBInstanceConfig = `
resource "aws_db_security_group" "bar" {
	name = "secfoobarbaz-test-terraform"
	description = "just cuz"

	ingress {
		cidr = "10.0.0.1/24"
	}
}

resource "aws_db_instance" "bar" {
	identifier = "foobarbaz-test-terraform"

	allocated_storage = 10
	engine = "mysql"
	engine_version = "5.6.21"
	instance_class = "db.t1.micro"
	name = "baz"
	password = "<PASSWORD>"
	username = "foo"

	skip_final_snapshot = true

	security_group_names = ["${aws_db_security_group.bar.name}"]
	parameter_group_name = "default.mysql5.6"
}
`

{'fraction_non_alphanumeric': 0.13677811550151975, 'fraction_numerical': 0.009469254150105215, 'mean_word_length': 3.939376443418014, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1457661', 'n_tokens_mistral': 3477, 'n_tokens_neox': 3025, 'n_words': 536}