|
| 1 | +package mailgun |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform/helper/resource" |
| 8 | + "github.com/hashicorp/terraform/terraform" |
| 9 | + "github.com/pearkes/mailgun" |
| 10 | +) |
| 11 | + |
| 12 | +func TestAccMailgunDomain_Basic(t *testing.T) { |
| 13 | + var domain mailgun.Domain |
| 14 | + |
| 15 | + resource.Test(t, resource.TestCase{ |
| 16 | + PreCheck: func() { testAccPreCheck(t) }, |
| 17 | + Providers: testAccProviders, |
| 18 | + CheckDestroy: testAccCheckMailgunDomainDestroy, |
| 19 | + Steps: []resource.TestStep{ |
| 20 | + resource.TestStep{ |
| 21 | + Config: testAccCheckMailgunDomainConfig_basic, |
| 22 | + Check: resource.ComposeTestCheckFunc( |
| 23 | + testAccCheckMailgunDomainExists("mailgun_domain.foobar", &domain), |
| 24 | + testAccCheckMailgunDomainAttributes(&domain), |
| 25 | + resource.TestCheckResourceAttr( |
| 26 | + "mailgun_domain.foobar", "name", "terraform.example.com"), |
| 27 | + resource.TestCheckResourceAttr( |
| 28 | + "mailgun_domain.foobar", "spam_action", "disabled"), |
| 29 | + resource.TestCheckResourceAttr( |
| 30 | + "mailgun_domain.foobar", "smtp_password", "foobar"), |
| 31 | + resource.TestCheckResourceAttr( |
| 32 | + "mailgun_domain.foobar", "wildcard", "true"), |
| 33 | + ), |
| 34 | + }, |
| 35 | + }, |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +func testAccCheckMailgunDomainDestroy(s *terraform.State) error { |
| 40 | + client := testAccProvider.Meta().(*mailgun.Client) |
| 41 | + |
| 42 | + for _, rs := range s.Resources { |
| 43 | + if rs.Type != "mailgun_domain" { |
| 44 | + continue |
| 45 | + } |
| 46 | + |
| 47 | + _, err := client.RetrieveDomain(rs.ID) |
| 48 | + |
| 49 | + if err == nil { |
| 50 | + return fmt.Errorf("Domain still exists") |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +func testAccCheckMailgunDomainAttributes(Domain *mailgun.Domain) resource.TestCheckFunc { |
| 58 | + return func(s *terraform.State) error { |
| 59 | + |
| 60 | + if Domain.Name != "terraform.example.com" { |
| 61 | + return fmt.Errorf("Bad name: %s", Domain.Name) |
| 62 | + } |
| 63 | + |
| 64 | + if Domain.SpamAction != "disabled" { |
| 65 | + return fmt.Errorf("Bad spam_action: %s", Domain.SpamAction) |
| 66 | + } |
| 67 | + |
| 68 | + if Domain.Wildcard != true { |
| 69 | + return fmt.Errorf("Bad wildcard: %s", Domain.SpamAction) |
| 70 | + } |
| 71 | + |
| 72 | + if Domain.SmtpPassword != "foobar" { |
| 73 | + return fmt.Errorf("Bad smtp_password: %s", Domain.SmtpPassword) |
| 74 | + } |
| 75 | + |
| 76 | + return nil |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func testAccCheckMailgunDomainExists(n string, Domain *mailgun.Domain) resource.TestCheckFunc { |
| 81 | + return func(s *terraform.State) error { |
| 82 | + rs, ok := s.Resources[n] |
| 83 | + |
| 84 | + if !ok { |
| 85 | + return fmt.Errorf("Not found: %s", n) |
| 86 | + } |
| 87 | + |
| 88 | + if rs.ID == "" { |
| 89 | + return fmt.Errorf("No Domain ID is set") |
| 90 | + } |
| 91 | + |
| 92 | + client := testAccProvider.Meta().(*mailgun.Client) |
| 93 | + |
| 94 | + foundDomain, err := client.RetrieveDomain(rs.ID) |
| 95 | + |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + if foundDomain.Name != rs.ID { |
| 101 | + return fmt.Errorf("Domain not found") |
| 102 | + } |
| 103 | + |
| 104 | + *Domain = foundDomain |
| 105 | + |
| 106 | + return nil |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +const testAccCheckMailgunDomainConfig_basic = ` |
| 111 | +resource "mailgun_domain" "foobar" { |
| 112 | + name = "terraform.example.com" |
| 113 | + spam_action = "disabled" |
| 114 | + smtp_password = "foobar" |
| 115 | + wildcard = true |
| 116 | +}` |
0 commit comments