github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/statuscake/resource_statuscaketest_test.go (about)

     1  package statuscake
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/DreamItGetIT/statuscake"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccStatusCake_basic(t *testing.T) {
    14  	var test statuscake.Test
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccTestCheckDestroy(&test),
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccTestConfig_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccTestCheckExists("statuscake_test.google", &test),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func TestAccStatusCake_withUpdate(t *testing.T) {
    32  	var test statuscake.Test
    33  
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccTestCheckDestroy(&test),
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccTestConfig_basic,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccTestCheckExists("statuscake_test.google", &test),
    43  				),
    44  			},
    45  
    46  			resource.TestStep{
    47  				Config: testAccTestConfig_update,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccTestCheckExists("statuscake_test.google", &test),
    50  					resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"),
    51  					resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"),
    52  				),
    53  			},
    54  		},
    55  	})
    56  }
    57  
    58  func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc {
    59  	return func(s *terraform.State) error {
    60  		rs, ok := s.RootModule().Resources[rn]
    61  		if !ok {
    62  			return fmt.Errorf("resource not found: %s", rn)
    63  		}
    64  
    65  		if rs.Primary.ID == "" {
    66  			return fmt.Errorf("TestID not set")
    67  		}
    68  
    69  		client := testAccProvider.Meta().(*statuscake.Client)
    70  		testId, parseErr := strconv.Atoi(rs.Primary.ID)
    71  		if parseErr != nil {
    72  			return fmt.Errorf("error in statuscake test CheckExists: %s", parseErr)
    73  		}
    74  
    75  		gotTest, err := client.Tests().Detail(testId)
    76  		if err != nil {
    77  			return fmt.Errorf("error getting project: %s", err)
    78  		}
    79  
    80  		*test = *gotTest
    81  
    82  		return nil
    83  	}
    84  }
    85  
    86  func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc {
    87  	return func(s *terraform.State) error {
    88  		client := testAccProvider.Meta().(*statuscake.Client)
    89  		err := client.Tests().Delete(test.TestID)
    90  		if err == nil {
    91  			return fmt.Errorf("test still exists")
    92  		}
    93  
    94  		return nil
    95  	}
    96  	return nil
    97  }
    98  
    99  const testAccTestConfig_basic = `
   100  resource "statuscake_test" "google" {
   101    website_name = "google.com"
   102    website_url = "www.google.com"
   103    test_type = "HTTP"
   104    check_rate = 300
   105  }
   106  `
   107  
   108  const testAccTestConfig_update = `
   109  resource "statuscake_test" "google" {
   110    website_name = "google.com"
   111    website_url = "www.google.com"
   112    test_type = "HTTP"
   113    check_rate = 500
   114    paused = true
   115  }
   116  `