github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/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  			{
    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_tcp(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  			{
    41  				Config: testAccTestConfig_tcp,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccTestCheckExists("statuscake_test.google", &test),
    44  					testAccTestCheckAttributes("statuscake_test.google", &test),
    45  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func TestAccStatusCake_withUpdate(t *testing.T) {
    52  	var test statuscake.Test
    53  
    54  	resource.Test(t, resource.TestCase{
    55  		PreCheck:     func() { testAccPreCheck(t) },
    56  		Providers:    testAccProviders,
    57  		CheckDestroy: testAccTestCheckDestroy(&test),
    58  		Steps: []resource.TestStep{
    59  			{
    60  				Config: testAccTestConfig_basic,
    61  				Check: resource.ComposeTestCheckFunc(
    62  					testAccTestCheckExists("statuscake_test.google", &test),
    63  				),
    64  			},
    65  
    66  			{
    67  				Config: testAccTestConfig_update,
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccTestCheckExists("statuscake_test.google", &test),
    70  					testAccTestCheckAttributes("statuscake_test.google", &test),
    71  					resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"),
    72  					resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"),
    73  					resource.TestCheckResourceAttr("statuscake_test.google", "timeout", "40"),
    74  					resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "0"),
    75  					resource.TestCheckResourceAttr("statuscake_test.google", "confirmations", "0"),
    76  					resource.TestCheckResourceAttr("statuscake_test.google", "trigger_rate", "20"),
    77  				),
    78  			},
    79  		},
    80  	})
    81  }
    82  
    83  func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc {
    84  	return func(s *terraform.State) error {
    85  		rs, ok := s.RootModule().Resources[rn]
    86  		if !ok {
    87  			return fmt.Errorf("resource not found: %s", rn)
    88  		}
    89  
    90  		if rs.Primary.ID == "" {
    91  			return fmt.Errorf("TestID not set")
    92  		}
    93  
    94  		client := testAccProvider.Meta().(*statuscake.Client)
    95  		testId, parseErr := strconv.Atoi(rs.Primary.ID)
    96  		if parseErr != nil {
    97  			return fmt.Errorf("error in statuscake test CheckExists: %s", parseErr)
    98  		}
    99  
   100  		gotTest, err := client.Tests().Detail(testId)
   101  		if err != nil {
   102  			return fmt.Errorf("error getting test: %s", err)
   103  		}
   104  
   105  		*test = *gotTest
   106  
   107  		return nil
   108  	}
   109  }
   110  
   111  func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestCheckFunc {
   112  	return func(s *terraform.State) error {
   113  		attrs := s.RootModule().Resources[rn].Primary.Attributes
   114  
   115  		check := func(key, stateValue, testValue string) error {
   116  			if testValue != stateValue {
   117  				return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)",
   118  					key, stateValue, testValue)
   119  			}
   120  			return nil
   121  		}
   122  
   123  		for key, value := range attrs {
   124  			var err error
   125  
   126  			switch key {
   127  			case "website_name":
   128  				err = check(key, value, test.WebsiteName)
   129  			case "website_url":
   130  				err = check(key, value, test.WebsiteURL)
   131  			case "check_rate":
   132  				err = check(key, value, strconv.Itoa(test.CheckRate))
   133  			case "test_type":
   134  				err = check(key, value, test.TestType)
   135  			case "paused":
   136  				err = check(key, value, strconv.FormatBool(test.Paused))
   137  			case "timeout":
   138  				err = check(key, value, strconv.Itoa(test.Timeout))
   139  			case "contact_id":
   140  				err = check(key, value, strconv.Itoa(test.ContactID))
   141  			case "confirmations":
   142  				err = check(key, value, strconv.Itoa(test.Confirmation))
   143  			case "trigger_rate":
   144  				err = check(key, value, strconv.Itoa(test.TriggerRate))
   145  			}
   146  
   147  			if err != nil {
   148  				return err
   149  			}
   150  		}
   151  		return nil
   152  	}
   153  }
   154  
   155  func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc {
   156  	return func(s *terraform.State) error {
   157  		client := testAccProvider.Meta().(*statuscake.Client)
   158  		err := client.Tests().Delete(test.TestID)
   159  		if err == nil {
   160  			return fmt.Errorf("test still exists")
   161  		}
   162  
   163  		return nil
   164  	}
   165  }
   166  
   167  const testAccTestConfig_basic = `
   168  resource "statuscake_test" "google" {
   169  	website_name = "google.com"
   170  	website_url = "www.google.com"
   171  	test_type = "HTTP"
   172  	check_rate = 300
   173  	timeout = 10
   174  	contact_id = 43402
   175  	confirmations = 1
   176  	trigger_rate = 10
   177  }
   178  `
   179  
   180  const testAccTestConfig_update = `
   181  resource "statuscake_test" "google" {
   182  	website_name = "google.com"
   183  	website_url = "www.google.com"
   184  	test_type = "HTTP"
   185  	check_rate = 500
   186  	paused = true
   187  	trigger_rate = 20
   188  }
   189  `
   190  
   191  const testAccTestConfig_tcp = `
   192  resource "statuscake_test" "google" {
   193  	website_name = "google.com"
   194  	website_url = "www.google.com"
   195  	test_type = "TCP"
   196  	check_rate = 300
   197  	timeout = 10
   198  	contact_id = 43402
   199  	confirmations = 1
   200  	port = 80
   201  }
   202  `