github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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, 443),
    35  					testAccCheckMonitoringJobConfigHost(&mj, "1.2.3.4"),
    36  					testAccCheckMonitoringJobRuleValue(&mj, "200 OK"),
    37  					testAccCheckMonitoringJobRuleComparison(&mj, "contains"),
    38  					testAccCheckMonitoringJobRuleKey(&mj, "output"),
    39  				),
    40  			},
    41  		},
    42  	})
    43  }
    44  
    45  func TestAccMonitoringJob_updated(t *testing.T) {
    46  	var mj monitor.Job
    47  	resource.Test(t, resource.TestCase{
    48  		PreCheck:     func() { testAccPreCheck(t) },
    49  		Providers:    testAccProviders,
    50  		CheckDestroy: testAccCheckMonitoringJobDestroy,
    51  		Steps: []resource.TestStep{
    52  			resource.TestStep{
    53  				Config: testAccMonitoringJobBasic,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccCheckMonitoringJobExists("ns1_monitoringjob.it", &mj),
    56  					testAccCheckMonitoringJobName(&mj, "terraform test"),
    57  					testAccCheckMonitoringJobActive(&mj, true),
    58  					testAccCheckMonitoringJobRegions(&mj, []string{"lga"}),
    59  					testAccCheckMonitoringJobType(&mj, "tcp"),
    60  					testAccCheckMonitoringJobFrequency(&mj, 60),
    61  					testAccCheckMonitoringJobRapidRecheck(&mj, false),
    62  					testAccCheckMonitoringJobPolicy(&mj, "quorum"),
    63  					testAccCheckMonitoringJobConfigSend(&mj, "HEAD / HTTP/1.0\r\n\r\n"),
    64  					testAccCheckMonitoringJobConfigPort(&mj, 443),
    65  					testAccCheckMonitoringJobConfigHost(&mj, "1.2.3.4"),
    66  					testAccCheckMonitoringJobRuleValue(&mj, "200 OK"),
    67  					testAccCheckMonitoringJobRuleComparison(&mj, "contains"),
    68  					testAccCheckMonitoringJobRuleKey(&mj, "output"),
    69  				),
    70  			},
    71  			resource.TestStep{
    72  				Config: testAccMonitoringJobUpdated,
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testAccCheckMonitoringJobExists("ns1_monitoringjob.it", &mj),
    75  					testAccCheckMonitoringJobName(&mj, "terraform test"),
    76  					testAccCheckMonitoringJobActive(&mj, true),
    77  					testAccCheckMonitoringJobRegions(&mj, []string{"lga"}),
    78  					testAccCheckMonitoringJobType(&mj, "tcp"),
    79  					testAccCheckMonitoringJobFrequency(&mj, 120),
    80  					testAccCheckMonitoringJobRapidRecheck(&mj, true),
    81  					testAccCheckMonitoringJobPolicy(&mj, "all"),
    82  					testAccCheckMonitoringJobConfigSend(&mj, "HEAD / HTTP/1.0\r\n\r\n"),
    83  					testAccCheckMonitoringJobConfigPort(&mj, 443),
    84  					testAccCheckMonitoringJobConfigHost(&mj, "1.1.1.1"),
    85  					testAccCheckMonitoringJobRuleValue(&mj, "200"),
    86  					testAccCheckMonitoringJobRuleComparison(&mj, "<="),
    87  					testAccCheckMonitoringJobRuleKey(&mj, "connect"),
    88  				),
    89  			},
    90  		},
    91  	})
    92  }
    93  
    94  func testAccCheckMonitoringJobState(key, value string) resource.TestCheckFunc {
    95  	return func(s *terraform.State) error {
    96  		rs, ok := s.RootModule().Resources["ns1_monitoringjob.it"]
    97  		if !ok {
    98  			return fmt.Errorf("Not found: %s", "ns1_monitoringjob.it")
    99  		}
   100  
   101  		if rs.Primary.ID == "" {
   102  			return fmt.Errorf("No ID is set")
   103  		}
   104  
   105  		p := rs.Primary
   106  		if p.Attributes[key] != value {
   107  			return fmt.Errorf(
   108  				"%s != %s (actual: %s)", key, value, p.Attributes[key])
   109  		}
   110  
   111  		return nil
   112  	}
   113  }
   114  
   115  func testAccCheckMonitoringJobExists(n string, monitoringJob *monitor.Job) resource.TestCheckFunc {
   116  	return func(s *terraform.State) error {
   117  		rs, ok := s.RootModule().Resources[n]
   118  
   119  		if !ok {
   120  			return fmt.Errorf("Resource not found: %v", n)
   121  		}
   122  
   123  		id := rs.Primary.ID
   124  		if id == "" {
   125  			return fmt.Errorf("ID is not set")
   126  		}
   127  
   128  		client := testAccProvider.Meta().(*ns1.Client)
   129  
   130  		foundMj, _, err := client.Jobs.Get(id)
   131  
   132  		if err != nil {
   133  			return err
   134  		}
   135  
   136  		if foundMj.ID != id {
   137  			return fmt.Errorf("Monitoring Job not found want: %#v, got %#v", id, foundMj)
   138  		}
   139  
   140  		*monitoringJob = *foundMj
   141  
   142  		return nil
   143  	}
   144  }
   145  
   146  func testAccCheckMonitoringJobDestroy(s *terraform.State) error {
   147  	client := testAccProvider.Meta().(*ns1.Client)
   148  
   149  	for _, rs := range s.RootModule().Resources {
   150  		if rs.Type != "ns1_monitoringjob" {
   151  			continue
   152  		}
   153  
   154  		mj, _, err := client.Jobs.Get(rs.Primary.Attributes["id"])
   155  
   156  		if err == nil {
   157  			return fmt.Errorf("Monitoring Job still exists %#v: %#v", err, mj)
   158  		}
   159  	}
   160  
   161  	return nil
   162  }
   163  
   164  func testAccCheckMonitoringJobName(mj *monitor.Job, expected string) resource.TestCheckFunc {
   165  	return func(s *terraform.State) error {
   166  		if mj.Name != expected {
   167  			return fmt.Errorf("Name: got: %#v want: %#v", mj.Name, expected)
   168  		}
   169  		return nil
   170  	}
   171  }
   172  
   173  func testAccCheckMonitoringJobActive(mj *monitor.Job, expected bool) resource.TestCheckFunc {
   174  	return func(s *terraform.State) error {
   175  		if mj.Active != expected {
   176  			return fmt.Errorf("Active: got: %#v want: %#v", mj.Active, expected)
   177  		}
   178  		return nil
   179  	}
   180  }
   181  
   182  func testAccCheckMonitoringJobRegions(mj *monitor.Job, expected []string) resource.TestCheckFunc {
   183  	return func(s *terraform.State) error {
   184  		if !reflect.DeepEqual(mj.Regions, expected) {
   185  			return fmt.Errorf("Regions: got: %#v want: %#v", mj.Regions, expected)
   186  		}
   187  		return nil
   188  	}
   189  }
   190  
   191  func testAccCheckMonitoringJobType(mj *monitor.Job, expected string) resource.TestCheckFunc {
   192  	return func(s *terraform.State) error {
   193  		if mj.Type != expected {
   194  			return fmt.Errorf("Type: got: %#v want: %#v", mj.Type, expected)
   195  		}
   196  		return nil
   197  	}
   198  }
   199  
   200  func testAccCheckMonitoringJobFrequency(mj *monitor.Job, expected int) resource.TestCheckFunc {
   201  	return func(s *terraform.State) error {
   202  		if mj.Frequency != expected {
   203  			return fmt.Errorf("Frequency: got: %#v want: %#v", mj.Frequency, expected)
   204  		}
   205  		return nil
   206  	}
   207  }
   208  
   209  func testAccCheckMonitoringJobRapidRecheck(mj *monitor.Job, expected bool) resource.TestCheckFunc {
   210  	return func(s *terraform.State) error {
   211  		if mj.RapidRecheck != expected {
   212  			return fmt.Errorf("RapidRecheck: got: %#v want: %#v", mj.RapidRecheck, expected)
   213  		}
   214  		return nil
   215  	}
   216  }
   217  
   218  func testAccCheckMonitoringJobPolicy(mj *monitor.Job, expected string) resource.TestCheckFunc {
   219  	return func(s *terraform.State) error {
   220  		if mj.Policy != expected {
   221  			return fmt.Errorf("Policy: got: %#v want: %#v", mj.Policy, expected)
   222  		}
   223  		return nil
   224  	}
   225  }
   226  
   227  func testAccCheckMonitoringJobConfigSend(mj *monitor.Job, expected string) resource.TestCheckFunc {
   228  	return func(s *terraform.State) error {
   229  		if mj.Config["send"].(string) != expected {
   230  			return fmt.Errorf("Config.send: got: %#v want: %#v", mj.Config["send"].(string), expected)
   231  		}
   232  		return nil
   233  	}
   234  }
   235  
   236  func testAccCheckMonitoringJobConfigPort(mj *monitor.Job, expected float64) resource.TestCheckFunc {
   237  	return func(s *terraform.State) error {
   238  		if mj.Config["port"].(float64) != expected {
   239  			return fmt.Errorf("Config.port: got: %#v want: %#v", mj.Config["port"].(float64), expected)
   240  		}
   241  		return nil
   242  	}
   243  }
   244  
   245  func testAccCheckMonitoringJobConfigHost(mj *monitor.Job, expected string) resource.TestCheckFunc {
   246  	return func(s *terraform.State) error {
   247  		if mj.Config["host"].(string) != expected {
   248  			return fmt.Errorf("Config.host: got: %#v want: %#v", mj.Config["host"].(string), expected)
   249  		}
   250  		return nil
   251  	}
   252  }
   253  
   254  func testAccCheckMonitoringJobRuleValue(mj *monitor.Job, expected string) resource.TestCheckFunc {
   255  	return func(s *terraform.State) error {
   256  		if mj.Rules[0].Value.(string) != expected {
   257  			return fmt.Errorf("Rules[0].Value: got: %#v want: %#v", mj.Rules[0].Value.(string), expected)
   258  		}
   259  		return nil
   260  	}
   261  }
   262  
   263  func testAccCheckMonitoringJobRuleComparison(mj *monitor.Job, expected string) resource.TestCheckFunc {
   264  	return func(s *terraform.State) error {
   265  		if mj.Rules[0].Comparison != expected {
   266  			return fmt.Errorf("Rules[0].Comparison: got: %#v want: %#v", mj.Rules[0].Comparison, expected)
   267  		}
   268  		return nil
   269  	}
   270  }
   271  
   272  func testAccCheckMonitoringJobRuleKey(mj *monitor.Job, expected string) resource.TestCheckFunc {
   273  	return func(s *terraform.State) error {
   274  		if mj.Rules[0].Key != expected {
   275  			return fmt.Errorf("Rules[0].Key: got: %#v want: %#v", mj.Rules[0].Key, expected)
   276  		}
   277  		return nil
   278  	}
   279  }
   280  
   281  const testAccMonitoringJobBasic = `
   282  resource "ns1_monitoringjob" "it" {
   283    job_type = "tcp"
   284    name     = "terraform test"
   285  
   286    regions   = ["lga"]
   287    frequency = 60
   288  
   289    config = {
   290      ssl = "1",
   291      send = "HEAD / HTTP/1.0\r\n\r\n"
   292      port = 443
   293      host = "1.2.3.4"
   294    }
   295    rules = {
   296      value = "200 OK"
   297      comparison = "contains"
   298      key = "output"
   299    }
   300  }
   301  `
   302  
   303  const testAccMonitoringJobUpdated = `
   304  resource "ns1_monitoringjob" "it" {
   305    job_type = "tcp"
   306    name     = "terraform test"
   307  
   308    active        = true
   309    regions       = ["lga"]
   310    frequency     = 120
   311    rapid_recheck = true
   312    policy        = "all"
   313  
   314    config = {
   315      ssl = "1",
   316      send = "HEAD / HTTP/1.0\r\n\r\n"
   317      port = 443
   318      host = "1.1.1.1"
   319    }
   320    rules = {
   321      value = 200
   322      comparison = "<="
   323      key = "connect"
   324    }
   325  }
   326  `