github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  					resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "23456"),
    53  				),
    54  			},
    55  		},
    56  	})
    57  }
    58  
    59  func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc {
    60  	return func(s *terraform.State) error {
    61  		rs, ok := s.RootModule().Resources[rn]
    62  		if !ok {
    63  			return fmt.Errorf("resource not found: %s", rn)
    64  		}
    65  
    66  		if rs.Primary.ID == "" {
    67  			return fmt.Errorf("TestID not set")
    68  		}
    69  
    70  		client := testAccProvider.Meta().(*statuscake.Client)
    71  		testId, parseErr := strconv.Atoi(rs.Primary.ID)
    72  		if parseErr != nil {
    73  			return fmt.Errorf("error in statuscake test CheckExists: %s", parseErr)
    74  		}
    75  
    76  		gotTest, err := client.Tests().Detail(testId)
    77  		if err != nil {
    78  			return fmt.Errorf("error getting project: %s", err)
    79  		}
    80  
    81  		*test = *gotTest
    82  
    83  		return nil
    84  	}
    85  }
    86  
    87  func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc {
    88  	return func(s *terraform.State) error {
    89  		client := testAccProvider.Meta().(*statuscake.Client)
    90  		err := client.Tests().Delete(test.TestID)
    91  		if err == nil {
    92  			return fmt.Errorf("test still exists")
    93  		}
    94  
    95  		return nil
    96  	}
    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  	contact_id = 12345
   106  }
   107  `
   108  
   109  const testAccTestConfig_update = `
   110  resource "statuscake_test" "google" {
   111  	website_name = "google.com"
   112  	website_url = "www.google.com"
   113  	test_type = "HTTP"
   114  	check_rate = 500
   115  	paused = true
   116  	contact_id = 23456
   117  }
   118  `