|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "sort" |
| 7 | + "strconv" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform/helper/resource" |
| 11 | + "github.com/hashicorp/terraform/terraform" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAccAWSDbEventCategories_basic(t *testing.T) { |
| 15 | + resource.Test(t, resource.TestCase{ |
| 16 | + PreCheck: func() { testAccPreCheck(t) }, |
| 17 | + Providers: testAccProviders, |
| 18 | + Steps: []resource.TestStep{ |
| 19 | + { |
| 20 | + Config: testAccCheckAwsDbEventCategoriesConfig, |
| 21 | + Check: resource.ComposeTestCheckFunc( |
| 22 | + testAccCheckAwsDbEventCategoriesAttr("data.aws_db_event_categories.example"), |
| 23 | + ), |
| 24 | + }, |
| 25 | + }, |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +func testAccCheckAwsDbEventCategoriesAttr(n string) resource.TestCheckFunc { |
| 30 | + return func(s *terraform.State) error { |
| 31 | + rs, ok := s.RootModule().Resources[n] |
| 32 | + if !ok { |
| 33 | + return fmt.Errorf("Can't find DB Event Categories: %s", n) |
| 34 | + } |
| 35 | + |
| 36 | + if rs.Primary.ID == "" { |
| 37 | + return fmt.Errorf("DB Event Categories resource ID not set.") |
| 38 | + } |
| 39 | + |
| 40 | + actual, err := testAccCheckAwsDbEventCategoriesBuild(rs.Primary.Attributes) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + expected := []string{ |
| 46 | + "notification", |
| 47 | + "deletion", |
| 48 | + "failover", |
| 49 | + "maintenance", |
| 50 | + "availability", |
| 51 | + "read replica", |
| 52 | + "failure", |
| 53 | + "configuration change", |
| 54 | + "recovery", |
| 55 | + "low storage", |
| 56 | + "backup", |
| 57 | + "creation", |
| 58 | + "backtrack", |
| 59 | + "restoration", |
| 60 | + } |
| 61 | + |
| 62 | + sort.Strings(actual) |
| 63 | + sort.Strings(expected) |
| 64 | + if reflect.DeepEqual(expected, actual) != true { |
| 65 | + return fmt.Errorf("DB Event Categories not matched: expected %v, got %v", expected, actual) |
| 66 | + } |
| 67 | + |
| 68 | + return nil |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func testAccCheckAwsDbEventCategoriesBuild(attrs map[string]string) ([]string, error) { |
| 73 | + v, ok := attrs["event_categories.#"] |
| 74 | + if !ok { |
| 75 | + return nil, fmt.Errorf("DB Event Categories list is missing.") |
| 76 | + } |
| 77 | + |
| 78 | + qty, err := strconv.Atoi(v) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + if qty < 1 { |
| 83 | + return nil, fmt.Errorf("No DB Event Categories found.") |
| 84 | + } |
| 85 | + eventCategories := make([]string, qty) |
| 86 | + for n := range eventCategories { |
| 87 | + eventCategory, ok := attrs["event_categories."+strconv.Itoa(n)] |
| 88 | + if !ok { |
| 89 | + return nil, fmt.Errorf("DB Event Categories list is corrupted.") |
| 90 | + } |
| 91 | + eventCategories[n] = eventCategory |
| 92 | + } |
| 93 | + return eventCategories, nil |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +var testAccCheckAwsDbEventCategoriesConfig = ` |
| 98 | +data "aws_db_event_categories" "example" {} |
| 99 | +` |
0 commit comments