github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/mailgun/resource_mailgun_domain_test.go (about)

     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 resp mailgun.DomainResponse
    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", &resp),
    24  					testAccCheckMailgunDomainAttributes(&resp),
    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  					resource.TestCheckResourceAttr(
    34  						"mailgun_domain.foobar", "receiving_records.0.priority", "10"),
    35  					resource.TestCheckResourceAttr(
    36  						"mailgun_domain.foobar", "sending_records.0.name", "terraform.example.com"),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func testAccCheckMailgunDomainDestroy(s *terraform.State) error {
    44  	client := testAccProvider.Meta().(*mailgun.Client)
    45  
    46  	for _, rs := range s.RootModule().Resources {
    47  		if rs.Type != "mailgun_domain" {
    48  			continue
    49  		}
    50  
    51  		resp, err := client.RetrieveDomain(rs.Primary.ID)
    52  
    53  		if err == nil {
    54  			return fmt.Errorf("Domain still exists: %#v", resp)
    55  		}
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  func testAccCheckMailgunDomainAttributes(DomainResp *mailgun.DomainResponse) resource.TestCheckFunc {
    62  	return func(s *terraform.State) error {
    63  
    64  		if DomainResp.Domain.Name != "terraform.example.com" {
    65  			return fmt.Errorf("Bad name: %s", DomainResp.Domain.Name)
    66  		}
    67  
    68  		if DomainResp.Domain.SpamAction != "disabled" {
    69  			return fmt.Errorf("Bad spam_action: %s", DomainResp.Domain.SpamAction)
    70  		}
    71  
    72  		if DomainResp.Domain.Wildcard != true {
    73  			return fmt.Errorf("Bad wildcard: %t", DomainResp.Domain.Wildcard)
    74  		}
    75  
    76  		if DomainResp.Domain.SmtpPassword != "foobar" {
    77  			return fmt.Errorf("Bad smtp_password: %s", DomainResp.Domain.SmtpPassword)
    78  		}
    79  
    80  		if DomainResp.ReceivingRecords[0].Priority == "" {
    81  			return fmt.Errorf("Bad receiving_records: %s", DomainResp.ReceivingRecords)
    82  		}
    83  
    84  		if DomainResp.SendingRecords[0].Name == "" {
    85  			return fmt.Errorf("Bad sending_records: %s", DomainResp.SendingRecords)
    86  		}
    87  
    88  		return nil
    89  	}
    90  }
    91  
    92  func testAccCheckMailgunDomainExists(n string, DomainResp *mailgun.DomainResponse) resource.TestCheckFunc {
    93  	return func(s *terraform.State) error {
    94  		rs, ok := s.RootModule().Resources[n]
    95  
    96  		if !ok {
    97  			return fmt.Errorf("Not found: %s", n)
    98  		}
    99  
   100  		if rs.Primary.ID == "" {
   101  			return fmt.Errorf("No Domain ID is set")
   102  		}
   103  
   104  		client := testAccProvider.Meta().(*mailgun.Client)
   105  
   106  		resp, err := client.RetrieveDomain(rs.Primary.ID)
   107  
   108  		if err != nil {
   109  			return err
   110  		}
   111  
   112  		if resp.Domain.Name != rs.Primary.ID {
   113  			return fmt.Errorf("Domain not found")
   114  		}
   115  
   116  		*DomainResp = resp
   117  
   118  		return nil
   119  	}
   120  }
   121  
   122  const testAccCheckMailgunDomainConfig_basic = `
   123  resource "mailgun_domain" "foobar" {
   124      name = "terraform.example.com"
   125      spam_action = "disabled"
   126      smtp_password = "foobar"
   127      wildcard = true
   128  }`