github.com/nevins-b/terraform@v0.3.8-0.20170215184714-bbae22007d5a/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  					testAccTestCheckAttributes("statuscake_test.google", &test),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccStatusCake_withUpdate(t *testing.T) {
    33  	var test statuscake.Test
    34  
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccTestCheckDestroy(&test),
    39  		Steps: []resource.TestStep{
    40  			resource.TestStep{
    41  				Config: testAccTestConfig_basic,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccTestCheckExists("statuscake_test.google", &test),
    44  				),
    45  			},
    46  
    47  			resource.TestStep{
    48  				Config: testAccTestConfig_update,
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccTestCheckExists("statuscake_test.google", &test),
    51  					testAccTestCheckAttributes("statuscake_test.google", &test),
    52  					resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"),
    53  					resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"),
    54  					resource.TestCheckResourceAttr("statuscake_test.google", "timeout", "40"),
    55  					resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "0"),
    56  					resource.TestCheckResourceAttr("statuscake_test.google", "confirmations", "0"),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc {
    64  	return func(s *terraform.State) error {
    65  		rs, ok := s.RootModule().Resources[rn]
    66  		if !ok {
    67  			return fmt.Errorf("resource not found: %s", rn)
    68  		}
    69  
    70  		if rs.Primary.ID == "" {
    71  			return fmt.Errorf("TestID not set")
    72  		}
    73  
    74  		client := testAccProvider.Meta().(*statuscake.Client)
    75  		testId, parseErr := strconv.Atoi(rs.Primary.ID)
    76  		if parseErr != nil {
    77  			return fmt.Errorf("error in statuscake test CheckExists: %s", parseErr)
    78  		}
    79  
    80  		gotTest, err := client.Tests().Detail(testId)
    81  		if err != nil {
    82  			return fmt.Errorf("error getting test: %s", err)
    83  		}
    84  
    85  		*test = *gotTest
    86  
    87  		return nil
    88  	}
    89  }
    90  
    91  func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestCheckFunc {
    92  	return func(s *terraform.State) error {
    93  		attrs := s.RootModule().Resources[rn].Primary.Attributes
    94  
    95  		check := func(key, stateValue, testValue string) error {
    96  			if testValue != stateValue {
    97  				return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)",
    98  					key, stateValue, testValue)
    99  			}
   100  			return nil
   101  		}
   102  
   103  		for key, value := range attrs {
   104  			var err error
   105  
   106  			switch key {
   107  			case "website_name":
   108  				err = check(key, value, test.WebsiteName)
   109  			case "website_url":
   110  				err = check(key, value, test.WebsiteURL)
   111  			case "check_rate":
   112  				err = check(key, value, strconv.Itoa(test.CheckRate))
   113  			case "test_type":
   114  				err = check(key, value, test.TestType)
   115  			case "paused":
   116  				err = check(key, value, strconv.FormatBool(test.Paused))
   117  			case "timeout":
   118  				err = check(key, value, strconv.Itoa(test.Timeout))
   119  			case "contact_id":
   120  				err = check(key, value, strconv.Itoa(test.ContactID))
   121  			case "confirmations":
   122  				err = check(key, value, strconv.Itoa(test.Confirmation))
   123  			}
   124  
   125  			if err != nil {
   126  				return err
   127  			}
   128  		}
   129  		return nil
   130  	}
   131  }
   132  
   133  func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc {
   134  	return func(s *terraform.State) error {
   135  		client := testAccProvider.Meta().(*statuscake.Client)
   136  		err := client.Tests().Delete(test.TestID)
   137  		if err == nil {
   138  			return fmt.Errorf("test still exists")
   139  		}
   140  
   141  		return nil
   142  	}
   143  }
   144  
   145  const testAccTestConfig_basic = `
   146  resource "statuscake_test" "google" {
   147  	website_name = "google.com"
   148  	website_url = "www.google.com"
   149  	test_type = "HTTP"
   150  	check_rate = 300
   151  	timeout = 10
   152  	contact_id = 12345
   153  	confirmations = 1
   154  }
   155  `
   156  
   157  const testAccTestConfig_update = `
   158  resource "statuscake_test" "google" {
   159  	website_name = "google.com"
   160  	website_url = "www.google.com"
   161  	test_type = "HTTP"
   162  	check_rate = 500
   163  	paused = true
   164  }
   165  `