github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/openstack/resource_openstack_dns_zone_v2_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"regexp"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  
    13  	"github.com/gophercloud/gophercloud/openstack/dns/v2/zones"
    14  )
    15  
    16  func TestAccDNSV2Zone_basic(t *testing.T) {
    17  	var zone zones.Zone
    18  	var zoneName = fmt.Sprintf("ACPTTEST%s.com.", acctest.RandString(5))
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheckDNSZoneV2(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckDNSV2ZoneDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: testAccDNSV2Zone_basic(zoneName),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckDNSV2ZoneExists("openstack_dns_zone_v2.zone_1", &zone),
    29  					resource.TestCheckResourceAttr(
    30  						"openstack_dns_zone_v2.zone_1", "description", "a zone"),
    31  				),
    32  			},
    33  			resource.TestStep{
    34  				Config: testAccDNSV2Zone_update(zoneName),
    35  				Check: resource.ComposeTestCheckFunc(
    36  					resource.TestCheckResourceAttr("openstack_dns_zone_v2.zone_1", "name", zoneName),
    37  					resource.TestCheckResourceAttr("openstack_dns_zone_v2.zone_1", "email", "email2@example.com"),
    38  					resource.TestCheckResourceAttr("openstack_dns_zone_v2.zone_1", "ttl", "6000"),
    39  					resource.TestCheckResourceAttr("openstack_dns_zone_v2.zone_1", "type", "PRIMARY"),
    40  					resource.TestCheckResourceAttr(
    41  						"openstack_dns_zone_v2.zone_1", "description", "an updated zone"),
    42  				),
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func TestAccDNSV2Zone_readTTL(t *testing.T) {
    49  	var zone zones.Zone
    50  	var zoneName = fmt.Sprintf("ACPTTEST%s.com.", acctest.RandString(5))
    51  
    52  	resource.Test(t, resource.TestCase{
    53  		PreCheck:     func() { testAccPreCheckDNSZoneV2(t) },
    54  		Providers:    testAccProviders,
    55  		CheckDestroy: testAccCheckDNSV2ZoneDestroy,
    56  		Steps: []resource.TestStep{
    57  			resource.TestStep{
    58  				Config: testAccDNSV2Zone_readTTL(zoneName),
    59  				Check: resource.ComposeTestCheckFunc(
    60  					testAccCheckDNSV2ZoneExists("openstack_dns_zone_v2.zone_1", &zone),
    61  					resource.TestCheckResourceAttr("openstack_dns_zone_v2.zone_1", "type", "PRIMARY"),
    62  					resource.TestMatchResourceAttr(
    63  						"openstack_dns_zone_v2.zone_1", "ttl", regexp.MustCompile("^[0-9]+$")),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func TestAccDNSV2Zone_timeout(t *testing.T) {
    71  	var zone zones.Zone
    72  	var zoneName = fmt.Sprintf("ACPTTEST%s.com.", acctest.RandString(5))
    73  
    74  	resource.Test(t, resource.TestCase{
    75  		PreCheck:     func() { testAccPreCheckDNSZoneV2(t) },
    76  		Providers:    testAccProviders,
    77  		CheckDestroy: testAccCheckDNSV2ZoneDestroy,
    78  		Steps: []resource.TestStep{
    79  			resource.TestStep{
    80  				Config: testAccDNSV2Zone_timeout(zoneName),
    81  				Check: resource.ComposeTestCheckFunc(
    82  					testAccCheckDNSV2ZoneExists("openstack_dns_zone_v2.zone_1", &zone),
    83  				),
    84  			},
    85  		},
    86  	})
    87  }
    88  
    89  func testAccCheckDNSV2ZoneDestroy(s *terraform.State) error {
    90  	config := testAccProvider.Meta().(*Config)
    91  	dnsClient, err := config.dnsV2Client(OS_REGION_NAME)
    92  	if err != nil {
    93  		return fmt.Errorf("Error creating OpenStack DNS client: %s", err)
    94  	}
    95  
    96  	for _, rs := range s.RootModule().Resources {
    97  		if rs.Type != "openstack_dns_zone_v2" {
    98  			continue
    99  		}
   100  
   101  		_, err := zones.Get(dnsClient, rs.Primary.ID).Extract()
   102  		if err == nil {
   103  			return fmt.Errorf("Zone still exists")
   104  		}
   105  	}
   106  
   107  	return nil
   108  }
   109  
   110  func testAccCheckDNSV2ZoneExists(n string, zone *zones.Zone) resource.TestCheckFunc {
   111  	return func(s *terraform.State) error {
   112  		rs, ok := s.RootModule().Resources[n]
   113  		if !ok {
   114  			return fmt.Errorf("Not found: %s", n)
   115  		}
   116  
   117  		if rs.Primary.ID == "" {
   118  			return fmt.Errorf("No ID is set")
   119  		}
   120  
   121  		config := testAccProvider.Meta().(*Config)
   122  		dnsClient, err := config.dnsV2Client(OS_REGION_NAME)
   123  		if err != nil {
   124  			return fmt.Errorf("Error creating OpenStack DNS client: %s", err)
   125  		}
   126  
   127  		found, err := zones.Get(dnsClient, rs.Primary.ID).Extract()
   128  		if err != nil {
   129  			return err
   130  		}
   131  
   132  		if found.ID != rs.Primary.ID {
   133  			return fmt.Errorf("Zone not found")
   134  		}
   135  
   136  		*zone = *found
   137  
   138  		return nil
   139  	}
   140  }
   141  
   142  func testAccPreCheckDNSZoneV2(t *testing.T) {
   143  	v := os.Getenv("OS_AUTH_URL")
   144  	if v == "" {
   145  		t.Fatal("OS_AUTH_URL must be set for acceptance tests")
   146  	}
   147  }
   148  
   149  func testAccDNSV2Zone_basic(zoneName string) string {
   150  	return fmt.Sprintf(`
   151  		resource "openstack_dns_zone_v2" "zone_1" {
   152  			name = "%s"
   153  			email = "email1@example.com"
   154  			description = "a zone"
   155  			ttl = 3000
   156  			type = "PRIMARY"
   157  		}
   158  	`, zoneName)
   159  }
   160  
   161  func testAccDNSV2Zone_update(zoneName string) string {
   162  	return fmt.Sprintf(`
   163  		resource "openstack_dns_zone_v2" "zone_1" {
   164  			name = "%s"
   165  			email = "email2@example.com"
   166  			description = "an updated zone"
   167  			ttl = 6000
   168  			type = "PRIMARY"
   169  		}
   170  	`, zoneName)
   171  }
   172  
   173  func testAccDNSV2Zone_readTTL(zoneName string) string {
   174  	return fmt.Sprintf(`
   175  		resource "openstack_dns_zone_v2" "zone_1" {
   176  			name = "%s"
   177  			email = "email1@example.com"
   178  		}
   179  	`, zoneName)
   180  }
   181  
   182  func testAccDNSV2Zone_timeout(zoneName string) string {
   183  	return fmt.Sprintf(`
   184  		resource "openstack_dns_zone_v2" "zone_1" {
   185  			name = "%s"
   186  			email = "email@example.com"
   187  			ttl = 3000
   188  
   189  			timeouts {
   190  				create = "5m"
   191  				update = "5m"
   192  				delete = "5m"
   193  			}
   194  		}
   195  	`, zoneName)
   196  }