github.com/anfernee/terraform@v0.6.16-0.20160430000239-06e5085a92f2/builtin/providers/cloudflare/resource_cloudflare_record_test.go (about)

     1  package cloudflare
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  	"time"
     8  
     9  	"golang.org/x/net/context"
    10  
    11  	"github.com/crackcomm/cloudflare"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestAccCloudFlareRecord_Basic(t *testing.T) {
    17  	var record cloudflare.Record
    18  	domain := os.Getenv("CLOUDFLARE_DOMAIN")
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckCloudFlareRecordDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigBasic, domain),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record),
    29  					testAccCheckCloudFlareRecordAttributes(&record),
    30  					resource.TestCheckResourceAttr(
    31  						"cloudflare_record.foobar", "name", "terraform"),
    32  					resource.TestCheckResourceAttr(
    33  						"cloudflare_record.foobar", "domain", domain),
    34  					resource.TestCheckResourceAttr(
    35  						"cloudflare_record.foobar", "value", "192.168.0.10"),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func TestAccCloudFlareRecord_Proxied(t *testing.T) {
    43  	var record cloudflare.Record
    44  	domain := os.Getenv("CLOUDFLARE_DOMAIN")
    45  
    46  	resource.Test(t, resource.TestCase{
    47  		PreCheck:     func() { testAccPreCheck(t) },
    48  		Providers:    testAccProviders,
    49  		CheckDestroy: testAccCheckCloudFlareRecordDestroy,
    50  		Steps: []resource.TestStep{
    51  			resource.TestStep{
    52  				Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigProxied, domain, domain),
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record),
    55  					resource.TestCheckResourceAttr(
    56  						"cloudflare_record.foobar", "domain", domain),
    57  					resource.TestCheckResourceAttr(
    58  						"cloudflare_record.foobar", "name", "terraform"),
    59  					resource.TestCheckResourceAttr(
    60  						"cloudflare_record.foobar", "proxied", "true"),
    61  					resource.TestCheckResourceAttr(
    62  						"cloudflare_record.foobar", "type", "CNAME"),
    63  					resource.TestCheckResourceAttr(
    64  						"cloudflare_record.foobar", "value", domain),
    65  				),
    66  			},
    67  		},
    68  	})
    69  }
    70  
    71  func TestAccCloudFlareRecord_Updated(t *testing.T) {
    72  	var record cloudflare.Record
    73  	domain := os.Getenv("CLOUDFLARE_DOMAIN")
    74  
    75  	resource.Test(t, resource.TestCase{
    76  		PreCheck:     func() { testAccPreCheck(t) },
    77  		Providers:    testAccProviders,
    78  		CheckDestroy: testAccCheckCloudFlareRecordDestroy,
    79  		Steps: []resource.TestStep{
    80  			resource.TestStep{
    81  				Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigBasic, domain),
    82  				Check: resource.ComposeTestCheckFunc(
    83  					testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record),
    84  					testAccCheckCloudFlareRecordAttributes(&record),
    85  					resource.TestCheckResourceAttr(
    86  						"cloudflare_record.foobar", "name", "terraform"),
    87  					resource.TestCheckResourceAttr(
    88  						"cloudflare_record.foobar", "domain", domain),
    89  					resource.TestCheckResourceAttr(
    90  						"cloudflare_record.foobar", "value", "192.168.0.10"),
    91  				),
    92  			},
    93  			resource.TestStep{
    94  				Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigNewValue, domain),
    95  				Check: resource.ComposeTestCheckFunc(
    96  					testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record),
    97  					testAccCheckCloudFlareRecordAttributesUpdated(&record),
    98  					resource.TestCheckResourceAttr(
    99  						"cloudflare_record.foobar", "name", "terraform"),
   100  					resource.TestCheckResourceAttr(
   101  						"cloudflare_record.foobar", "domain", domain),
   102  					resource.TestCheckResourceAttr(
   103  						"cloudflare_record.foobar", "value", "192.168.0.11"),
   104  				),
   105  			},
   106  		},
   107  	})
   108  }
   109  
   110  func TestAccCloudFlareRecord_forceNewRecord(t *testing.T) {
   111  	var afterCreate, afterUpdate cloudflare.Record
   112  	domain := os.Getenv("CLOUDFLARE_DOMAIN")
   113  
   114  	resource.Test(t, resource.TestCase{
   115  		PreCheck:     func() { testAccPreCheck(t) },
   116  		Providers:    testAccProviders,
   117  		CheckDestroy: testAccCheckCloudFlareRecordDestroy,
   118  		Steps: []resource.TestStep{
   119  			resource.TestStep{
   120  				Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigBasic, domain),
   121  				Check: resource.ComposeTestCheckFunc(
   122  					testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &afterCreate),
   123  				),
   124  			},
   125  			resource.TestStep{
   126  				Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigForceNew, domain, domain),
   127  				Check: resource.ComposeTestCheckFunc(
   128  					testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &afterUpdate),
   129  					testAccCheckCloudFlareRecordRecreated(t, &afterCreate, &afterUpdate),
   130  				),
   131  			},
   132  		},
   133  	})
   134  }
   135  
   136  func testAccCheckCloudFlareRecordRecreated(t *testing.T,
   137  	before, after *cloudflare.Record) resource.TestCheckFunc {
   138  	return func(s *terraform.State) error {
   139  		if before.ID == after.ID {
   140  			t.Fatalf("Expected change of Record Ids, but both were %v", before.ID)
   141  		}
   142  		return nil
   143  	}
   144  }
   145  
   146  func testAccCheckCloudFlareRecordDestroy(s *terraform.State) error {
   147  	var (
   148  		client = testAccProvider.Meta().(*cloudflare.Client)
   149  		ctx, _ = context.WithDeadline(context.Background(), time.Now().Add(time.Second*30))
   150  	)
   151  
   152  	for _, rs := range s.RootModule().Resources {
   153  		if rs.Type != "cloudflare_record" {
   154  			continue
   155  		}
   156  
   157  		_, err := client.Records.Details(ctx, rs.Primary.Attributes["zone_id"], rs.Primary.ID)
   158  		if err == nil {
   159  			return fmt.Errorf("Record still exists")
   160  		}
   161  	}
   162  
   163  	return nil
   164  }
   165  
   166  func testAccCheckCloudFlareRecordAttributes(record *cloudflare.Record) resource.TestCheckFunc {
   167  	return func(s *terraform.State) error {
   168  
   169  		if record.Content != "192.168.0.10" {
   170  			return fmt.Errorf("Bad content: %s", record.Content)
   171  		}
   172  
   173  		return nil
   174  	}
   175  }
   176  
   177  func testAccCheckCloudFlareRecordAttributesUpdated(record *cloudflare.Record) resource.TestCheckFunc {
   178  	return func(s *terraform.State) error {
   179  
   180  		if record.Content != "192.168.0.11" {
   181  			return fmt.Errorf("Bad content: %s", record.Content)
   182  		}
   183  
   184  		return nil
   185  	}
   186  }
   187  
   188  func testAccCheckCloudFlareRecordExists(n string, record *cloudflare.Record) resource.TestCheckFunc {
   189  	return func(s *terraform.State) error {
   190  		rs, ok := s.RootModule().Resources[n]
   191  		if !ok {
   192  			return fmt.Errorf("Not found: %s", n)
   193  		}
   194  
   195  		if rs.Primary.ID == "" {
   196  			return fmt.Errorf("No Record ID is set")
   197  		}
   198  
   199  		var (
   200  			client = testAccProvider.Meta().(*cloudflare.Client)
   201  			ctx, _ = context.WithDeadline(context.Background(), time.Now().Add(time.Second*30))
   202  		)
   203  
   204  		foundRecord, err := client.Records.Details(ctx, rs.Primary.Attributes["zone_id"], rs.Primary.ID)
   205  		if err != nil {
   206  			return err
   207  		}
   208  
   209  		if foundRecord.ID != rs.Primary.ID {
   210  			return fmt.Errorf("Record not found")
   211  		}
   212  
   213  		*record = *foundRecord
   214  
   215  		return nil
   216  	}
   217  }
   218  
   219  const testAccCheckCloudFlareRecordConfigBasic = `
   220  resource "cloudflare_record" "foobar" {
   221  	domain = "%s"
   222  
   223  	name = "terraform"
   224  	value = "192.168.0.10"
   225  	type = "A"
   226  	ttl = 3600
   227  }`
   228  
   229  const testAccCheckCloudFlareRecordConfigProxied = `
   230  resource "cloudflare_record" "foobar" {
   231  	domain = "%s"
   232  
   233  	name = "terraform"
   234  	value = "%s"
   235  	type = "CNAME"
   236  	proxied = true
   237  }`
   238  
   239  const testAccCheckCloudFlareRecordConfigNewValue = `
   240  resource "cloudflare_record" "foobar" {
   241  	domain = "%s"
   242  
   243  	name = "terraform"
   244  	value = "192.168.0.11"
   245  	type = "A"
   246  	ttl = 3600
   247  }`
   248  
   249  const testAccCheckCloudFlareRecordConfigForceNew = `
   250  resource "cloudflare_record" "foobar" {
   251  	domain = "%s"
   252  
   253  	name = "terraform"
   254  	value = "%s"
   255  	type = "CNAME"
   256  	ttl = 3600
   257  }`