github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/grafana/resource_dashboard_test.go (about) 1 package grafana 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 gapi "github.com/apparentlymart/go-grafana-api" 9 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccDashboard_basic(t *testing.T) { 15 var dashboard gapi.Dashboard 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccDashboardCheckDestroy(&dashboard), 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccDashboardConfig_basic, 24 Check: resource.ComposeTestCheckFunc( 25 testAccDashboardCheckExists("grafana_dashboard.test", &dashboard), 26 resource.TestMatchResourceAttr( 27 "grafana_dashboard.test", "id", regexp.MustCompile(`terraform-acceptance-test.*`), 28 ), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func testAccDashboardCheckExists(rn string, dashboard *gapi.Dashboard) resource.TestCheckFunc { 36 return func(s *terraform.State) error { 37 rs, ok := s.RootModule().Resources[rn] 38 if !ok { 39 return fmt.Errorf("resource not found: %s", rn) 40 } 41 42 if rs.Primary.ID == "" { 43 return fmt.Errorf("resource id not set") 44 } 45 46 client := testAccProvider.Meta().(*gapi.Client) 47 gotDashboard, err := client.Dashboard(rs.Primary.ID) 48 if err != nil { 49 return fmt.Errorf("error getting dashboard: %s", err) 50 } 51 52 *dashboard = *gotDashboard 53 54 return nil 55 } 56 } 57 58 func testAccDashboardCheckDestroy(dashboard *gapi.Dashboard) resource.TestCheckFunc { 59 return func(s *terraform.State) error { 60 client := testAccProvider.Meta().(*gapi.Client) 61 _, err := client.Dashboard(dashboard.Meta.Slug) 62 if err == nil { 63 return fmt.Errorf("dashboard still exists") 64 } 65 return nil 66 } 67 } 68 69 // The "id" and "version" properties in the config below are there to test 70 // that we correctly normalize them away. They are not actually used by this 71 // resource, since it uses slugs for identification and never modifies an 72 // existing dashboard. 73 const testAccDashboardConfig_basic = ` 74 resource "grafana_dashboard" "test" { 75 config_json = <<EOT 76 { 77 "title": "Terraform Acceptance Test", 78 "id": 12, 79 "version": "43" 80 } 81 EOT 82 } 83 `