github.com/sarguru/terraform@v0.6.17-0.20160525232901-8fcdfd7e3dc9/builtin/providers/nsone/resource_zone_test.go (about) 1 package nsone 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/bobtfish/go-nsone-api" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccZone_basic(t *testing.T) { 13 var zone nsone.Zone 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 CheckDestroy: testAccCheckZoneDestroy, 18 Steps: []resource.TestStep{ 19 resource.TestStep{ 20 Config: testAccZoneBasic, 21 Check: resource.ComposeTestCheckFunc( 22 testAccCheckZoneState("zone", "terraform.io"), 23 testAccCheckZoneExists("nsone_zone.foobar", &zone), 24 testAccCheckZoneAttributes(&zone), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func TestAccZone_updated(t *testing.T) { 32 var zone nsone.Zone 33 resource.Test(t, resource.TestCase{ 34 PreCheck: func() { testAccPreCheck(t) }, 35 Providers: testAccProviders, 36 CheckDestroy: testAccCheckZoneDestroy, 37 Steps: []resource.TestStep{ 38 resource.TestStep{ 39 Config: testAccZoneBasic, 40 Check: resource.ComposeTestCheckFunc( 41 testAccCheckZoneState("zone", "terraform.io"), 42 testAccCheckZoneExists("nsone_zone.foobar", &zone), 43 testAccCheckZoneAttributes(&zone), 44 ), 45 }, 46 resource.TestStep{ 47 Config: testAccZoneUpdated, 48 Check: resource.ComposeTestCheckFunc( 49 testAccCheckZoneState("zone", "terraform.io"), 50 testAccCheckZoneExists("nsone_zone.foobar", &zone), 51 testAccCheckZoneAttributesUpdated(&zone), 52 ), 53 }, 54 }, 55 }) 56 } 57 58 func testAccCheckZoneState(key, value string) resource.TestCheckFunc { 59 return func(s *terraform.State) error { 60 rs, ok := s.RootModule().Resources["nsone_zone.foobar"] 61 if !ok { 62 return fmt.Errorf("Not found: %s", "nsone_zone.foobar") 63 } 64 65 if rs.Primary.ID == "" { 66 return fmt.Errorf("No ID is set") 67 } 68 69 p := rs.Primary 70 if p.Attributes[key] != value { 71 return fmt.Errorf( 72 "%s != %s (actual: %s)", key, value, p.Attributes[key]) 73 } 74 75 return nil 76 } 77 } 78 79 func testAccCheckZoneExists(n string, zone *nsone.Zone) resource.TestCheckFunc { 80 return func(s *terraform.State) error { 81 rs, ok := s.RootModule().Resources[n] 82 83 if !ok { 84 return fmt.Errorf("Not found: %s", n) 85 } 86 87 if rs.Primary.ID == "" { 88 return fmt.Errorf("NoID is set") 89 } 90 91 client := testAccProvider.Meta().(*nsone.APIClient) 92 93 foundZone, err := client.GetZone(rs.Primary.Attributes["zone"]) 94 95 p := rs.Primary 96 97 if err != nil { 98 return err 99 } 100 101 if foundZone.Id != p.Attributes["id"] { 102 return fmt.Errorf("Zone not found") 103 } 104 105 *zone = *foundZone 106 107 return nil 108 } 109 } 110 111 func testAccCheckZoneDestroy(s *terraform.State) error { 112 client := testAccProvider.Meta().(*nsone.APIClient) 113 114 for _, rs := range s.RootModule().Resources { 115 if rs.Type != "nsone_zone" { 116 continue 117 } 118 119 _, err := client.GetZone(rs.Primary.Attributes["zone"]) 120 121 if err == nil { 122 return fmt.Errorf("Record still exists") 123 } 124 } 125 126 return nil 127 } 128 129 func testAccCheckZoneAttributes(zone *nsone.Zone) resource.TestCheckFunc { 130 return func(s *terraform.State) error { 131 if zone.Ttl != 3600 { 132 return fmt.Errorf("Bad value : %d", zone.Ttl) 133 } 134 135 if zone.Nx_ttl != 3600 { 136 return fmt.Errorf("Bad value : %d", zone.Nx_ttl) 137 } 138 139 return nil 140 } 141 } 142 143 func testAccCheckZoneAttributesUpdated(zone *nsone.Zone) resource.TestCheckFunc { 144 return func(s *terraform.State) error { 145 if zone.Ttl != 3601 { 146 return fmt.Errorf("Bad value : %d", zone.Ttl) 147 } 148 149 if zone.Nx_ttl != 3601 { 150 return fmt.Errorf("Bad value : %d", zone.Nx_ttl) 151 } 152 153 return nil 154 } 155 } 156 157 const testAccZoneBasic = ` 158 resource "nsone_zone" "foobar" { 159 zone = "terraform.io" 160 hostmaster = "hostmaster@nsone.net" 161 ttl = "3600" 162 nx_ttl = "3600" 163 }` 164 165 const testAccZoneUpdated = ` 166 resource "nsone_zone" "foobar" { 167 zone = "terraform.io" 168 hostmaster = "hostmaster@nsone.net" 169 ttl = "3601" 170 nx_ttl = "3601" 171 }`