github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/pagerduty/resource_pagerduty_addon_test.go (about) 1 package pagerduty 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/PagerDuty/go-pagerduty" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccPagerDutyAddon_Basic(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 CheckDestroy: testAccCheckPagerDutyAddonDestroy, 17 Steps: []resource.TestStep{ 18 resource.TestStep{ 19 Config: testAccCheckPagerDutyAddonConfig, 20 Check: resource.ComposeTestCheckFunc( 21 testAccCheckPagerDutyAddonExists("pagerduty_addon.foo"), 22 resource.TestCheckResourceAttr( 23 "pagerduty_addon.foo", "name", "Foo status page"), 24 resource.TestCheckResourceAttr( 25 "pagerduty_addon.foo", "src", "https://intranet.foo.com/status"), 26 ), 27 }, 28 resource.TestStep{ 29 Config: testAccCheckPagerDutyAddonConfigUpdated, 30 Check: resource.ComposeTestCheckFunc( 31 testAccCheckPagerDutyAddonExists("pagerduty_addon.foo"), 32 resource.TestCheckResourceAttr( 33 "pagerduty_addon.foo", "name", "Bar status page"), 34 resource.TestCheckResourceAttr( 35 "pagerduty_addon.foo", "src", "https://intranet.bar.com/status"), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func testAccCheckPagerDutyAddonDestroy(s *terraform.State) error { 43 client := testAccProvider.Meta().(*pagerduty.Client) 44 for _, r := range s.RootModule().Resources { 45 if r.Type != "pagerduty_addon" { 46 continue 47 } 48 49 if _, err := client.GetAddon(r.Primary.ID); err == nil { 50 return fmt.Errorf("Add-on still exists") 51 } 52 53 } 54 return nil 55 } 56 57 func testAccCheckPagerDutyAddonExists(n string) resource.TestCheckFunc { 58 return func(s *terraform.State) error { 59 rs, ok := s.RootModule().Resources[n] 60 if !ok { 61 return fmt.Errorf("Not found: %s", n) 62 } 63 64 if rs.Primary.ID == "" { 65 return fmt.Errorf("No add-on ID is set") 66 } 67 68 client := testAccProvider.Meta().(*pagerduty.Client) 69 70 found, err := client.GetAddon(rs.Primary.ID) 71 if err != nil { 72 return err 73 } 74 75 if found.ID != rs.Primary.ID { 76 return fmt.Errorf("Add-on not found: %v - %v", rs.Primary.ID, found) 77 } 78 79 return nil 80 } 81 } 82 83 const testAccCheckPagerDutyAddonConfig = ` 84 resource "pagerduty_addon" "foo" { 85 name = "Foo status page" 86 src = "https://intranet.foo.com/status" 87 } 88 ` 89 90 const testAccCheckPagerDutyAddonConfigUpdated = ` 91 resource "pagerduty_addon" "foo" { 92 name = "Bar status page" 93 src = "https://intranet.bar.com/status" 94 } 95 `