github.com/simonswine/terraform@v0.9.0-beta2/builtin/providers/ns1/resource_monitoringjob_test.go (about)

     1  package ns1
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  
    11  	ns1 "gopkg.in/ns1/ns1-go.v2/rest"
    12  	"gopkg.in/ns1/ns1-go.v2/rest/model/monitor"
    13  )
    14  
    15  func TestAccMonitoringJob_basic(t *testing.T) {
    16  	var mj monitor.Job
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckMonitoringJobDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccMonitoringJobBasic,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckMonitoringJobExists("ns1_monitoringjob.it", &mj),
    26  					testAccCheckMonitoringJobName(&mj, "terraform test"),
    27  					testAccCheckMonitoringJobActive(&mj, true),
    28  					testAccCheckMonitoringJobRegions(&mj, []string{"lga"}),
    29  					testAccCheckMonitoringJobType(&mj, "tcp"),
    30  					testAccCheckMonitoringJobFrequency(&mj, 60),
    31  					testAccCheckMonitoringJobRapidRecheck(&mj, false),
    32  					testAccCheckMonitoringJobPolicy(&mj, "quorum"),
    33  					testAccCheckMonitoringJobConfigSend(&mj, "HEAD / HTTP/1.0\r\n\r\n"),
    34  					testAccCheckMonitoringJobConfigPort(&mj, 80),
    35  					testAccCheckMonitoringJobConfigHost(&mj, "1.1.1.1"),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func TestAccMonitoringJob_updated(t *testing.T) {
    43  	var mj monitor.Job
    44  	resource.Test(t, resource.TestCase{
    45  		PreCheck:     func() { testAccPreCheck(t) },
    46  		Providers:    testAccProviders,
    47  		CheckDestroy: testAccCheckMonitoringJobDestroy,
    48  		Steps: []resource.TestStep{
    49  			resource.TestStep{
    50  				Config: testAccMonitoringJobBasic,
    51  				Check: resource.ComposeTestCheckFunc(
    52  					testAccCheckMonitoringJobExists("ns1_monitoringjob.it", &mj),
    53  					testAccCheckMonitoringJobName(&mj, "terraform test"),
    54  					testAccCheckMonitoringJobActive(&mj, true),
    55  					testAccCheckMonitoringJobRegions(&mj, []string{"lga"}),
    56  					testAccCheckMonitoringJobType(&mj, "tcp"),
    57  					testAccCheckMonitoringJobFrequency(&mj, 60),
    58  					testAccCheckMonitoringJobRapidRecheck(&mj, false),
    59  					testAccCheckMonitoringJobPolicy(&mj, "quorum"),
    60  					testAccCheckMonitoringJobConfigSend(&mj, "HEAD / HTTP/1.0\r\n\r\n"),
    61  					testAccCheckMonitoringJobConfigPort(&mj, 80),
    62  					testAccCheckMonitoringJobConfigHost(&mj, "1.1.1.1"),
    63  				),
    64  			},
    65  			resource.TestStep{
    66  				Config: testAccMonitoringJobUpdated,
    67  				Check: resource.ComposeTestCheckFunc(
    68  					testAccCheckMonitoringJobExists("ns1_monitoringjob.it", &mj),
    69  					testAccCheckMonitoringJobName(&mj, "terraform test"),
    70  					testAccCheckMonitoringJobActive(&mj, true),
    71  					testAccCheckMonitoringJobRegions(&mj, []string{"lga"}),
    72  					testAccCheckMonitoringJobType(&mj, "tcp"),
    73  					testAccCheckMonitoringJobFrequency(&mj, 120),
    74  					testAccCheckMonitoringJobRapidRecheck(&mj, true),
    75  					testAccCheckMonitoringJobPolicy(&mj, "all"),
    76  					testAccCheckMonitoringJobConfigSend(&mj, "HEAD / HTTP/1.0\r\n\r\n"),
    77  					testAccCheckMonitoringJobConfigPort(&mj, 443),
    78  					testAccCheckMonitoringJobConfigHost(&mj, "1.1.1.1"),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  func testAccCheckMonitoringJobState(key, value string) resource.TestCheckFunc {
    86  	return func(s *terraform.State) error {
    87  		rs, ok := s.RootModule().Resources["ns1_monitoringjob.it"]
    88  		if !ok {
    89  			return fmt.Errorf("Not found: %s", "ns1_monitoringjob.it")
    90  		}
    91  
    92  		if rs.Primary.ID == "" {
    93  			return fmt.Errorf("No ID is set")
    94  		}
    95  
    96  		p := rs.Primary
    97  		if p.Attributes[key] != value {
    98  			return fmt.Errorf(
    99  				"%s != %s (actual: %s)", key, value, p.Attributes[key])
   100  		}
   101  
   102  		return nil
   103  	}
   104  }
   105  
   106  func testAccCheckMonitoringJobExists(n string, monitoringJob *monitor.Job) resource.TestCheckFunc {
   107  	return func(s *terraform.State) error {
   108  		rs, ok := s.RootModule().Resources[n]
   109  
   110  		if !ok {
   111  			return fmt.Errorf("Resource not found: %v", n)
   112  		}
   113  
   114  		id := rs.Primary.ID
   115  		if id == "" {
   116  			return fmt.Errorf("ID is not set")
   117  		}
   118  
   119  		client := testAccProvider.Meta().(*ns1.Client)
   120  
   121  		foundMj, _, err := client.Jobs.Get(id)
   122  
   123  		if err != nil {
   124  			return err
   125  		}
   126  
   127  		if foundMj.ID != id {
   128  			return fmt.Errorf("Monitoring Job not found want: %#v, got %#v", id, foundMj)
   129  		}
   130  
   131  		*monitoringJob = *foundMj
   132  
   133  		return nil
   134  	}
   135  }
   136  
   137  func testAccCheckMonitoringJobDestroy(s *terraform.State) error {
   138  	client := testAccProvider.Meta().(*ns1.Client)
   139  
   140  	for _, rs := range s.RootModule().Resources {
   141  		if rs.Type != "ns1_monitoringjob" {
   142  			continue
   143  		}
   144  
   145  		mj, _, err := client.Jobs.Get(rs.Primary.Attributes["id"])
   146  
   147  		if err == nil {
   148  			return fmt.Errorf("Monitoring Job still exists %#v: %#v", err, mj)
   149  		}
   150  	}
   151  
   152  	return nil
   153  }
   154  
   155  func testAccCheckMonitoringJobName(mj *monitor.Job, expected string) resource.TestCheckFunc {
   156  	return func(s *terraform.State) error {
   157  		if mj.Name != expected {
   158  			return fmt.Errorf("Name: got: %#v want: %#v", mj.Name, expected)
   159  		}
   160  		return nil
   161  	}
   162  }
   163  
   164  func testAccCheckMonitoringJobActive(mj *monitor.Job, expected bool) resource.TestCheckFunc {
   165  	return func(s *terraform.State) error {
   166  		if mj.Active != expected {
   167  			return fmt.Errorf("Active: got: %#v want: %#v", mj.Active, expected)
   168  		}
   169  		return nil
   170  	}
   171  }
   172  
   173  func testAccCheckMonitoringJobRegions(mj *monitor.Job, expected []string) resource.TestCheckFunc {
   174  	return func(s *terraform.State) error {
   175  		if !reflect.DeepEqual(mj.Regions, expected) {
   176  			return fmt.Errorf("Regions: got: %#v want: %#v", mj.Regions, expected)
   177  		}
   178  		return nil
   179  	}
   180  }
   181  
   182  func testAccCheckMonitoringJobType(mj *monitor.Job, expected string) resource.TestCheckFunc {
   183  	return func(s *terraform.State) error {
   184  		if mj.Type != expected {
   185  			return fmt.Errorf("Type: got: %#v want: %#v", mj.Type, expected)
   186  		}
   187  		return nil
   188  	}
   189  }
   190  
   191  func testAccCheckMonitoringJobFrequency(mj *monitor.Job, expected int) resource.TestCheckFunc {
   192  	return func(s *terraform.State) error {
   193  		if mj.Frequency != expected {
   194  			return fmt.Errorf("Frequency: got: %#v want: %#v", mj.Frequency, expected)
   195  		}
   196  		return nil
   197  	}
   198  }
   199  
   200  func testAccCheckMonitoringJobRapidRecheck(mj *monitor.Job, expected bool) resource.TestCheckFunc {
   201  	return func(s *terraform.State) error {
   202  		if mj.RapidRecheck != expected {
   203  			return fmt.Errorf("RapidRecheck: got: %#v want: %#v", mj.RapidRecheck, expected)
   204  		}
   205  		return nil
   206  	}
   207  }
   208  
   209  func testAccCheckMonitoringJobPolicy(mj *monitor.Job, expected string) resource.TestCheckFunc {
   210  	return func(s *terraform.State) error {
   211  		if mj.Policy != expected {
   212  			return fmt.Errorf("Policy: got: %#v want: %#v", mj.Policy, expected)
   213  		}
   214  		return nil
   215  	}
   216  }
   217  
   218  func testAccCheckMonitoringJobConfigSend(mj *monitor.Job, expected string) resource.TestCheckFunc {
   219  	return func(s *terraform.State) error {
   220  		if mj.Config["send"].(string) != expected {
   221  			return fmt.Errorf("Config.send: got: %#v want: %#v", mj.Config["send"].(string), expected)
   222  		}
   223  		return nil
   224  	}
   225  }
   226  
   227  func testAccCheckMonitoringJobConfigPort(mj *monitor.Job, expected float64) resource.TestCheckFunc {
   228  	return func(s *terraform.State) error {
   229  		if mj.Config["port"].(float64) != expected {
   230  			return fmt.Errorf("Config.port: got: %#v want: %#v", mj.Config["port"].(float64), expected)
   231  		}
   232  		return nil
   233  	}
   234  }
   235  
   236  func testAccCheckMonitoringJobConfigHost(mj *monitor.Job, expected string) resource.TestCheckFunc {
   237  	return func(s *terraform.State) error {
   238  		if mj.Config["host"].(string) != expected {
   239  			return fmt.Errorf("Config.host: got: %#v want: %#v", mj.Config["host"].(string), expected)
   240  		}
   241  		return nil
   242  	}
   243  }
   244  
   245  const testAccMonitoringJobBasic = `
   246  resource "ns1_monitoringjob" "it" {
   247    job_type = "tcp"
   248    name     = "terraform test"
   249  
   250    regions   = ["lga"]
   251    frequency = 60
   252  
   253    config {
   254      send = "HEAD / HTTP/1.0\r\n\r\n"
   255      port = 80
   256      host = "1.1.1.1"
   257    }
   258  }
   259  `
   260  
   261  const testAccMonitoringJobUpdated = `
   262  resource "ns1_monitoringjob" "it" {
   263    job_type = "tcp"
   264    name     = "terraform test"
   265  
   266    active        = true
   267    regions       = ["lga"]
   268    frequency     = 120
   269    rapid_recheck = true
   270    policy        = "all"
   271  
   272    config {
   273      send = "HEAD / HTTP/1.0\r\n\r\n"
   274      port = 443
   275      host = "1.1.1.1"
   276    }
   277  }
   278  `