github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/icinga2/resource_icinga2_host_test.go (about)

     1  package icinga2
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/lrsmith/go-icinga2-api/iapi"
    10  )
    11  
    12  func TestAccCreateBasicHost(t *testing.T) {
    13  
    14  	var testAccCreateBasicHost = fmt.Sprintf(`
    15  		resource "icinga2_host" "tf-1" {
    16  		hostname      = "terraform-host-1"
    17  		address       = "10.10.10.1"
    18  		check_command = "hostalive"
    19  	}`)
    20  
    21  	resource.Test(t, resource.TestCase{
    22  		PreCheck:  func() { testAccPreCheck(t) },
    23  		Providers: testAccProviders,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: testAccCreateBasicHost,
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckHostExists("icinga2_host.tf-1"),
    29  					testAccCheckResourceState("icinga2_host.tf-1", "hostname", "terraform-host-1"),
    30  					testAccCheckResourceState("icinga2_host.tf-1", "address", "10.10.10.1"),
    31  					testAccCheckResourceState("icinga2_host.tf-1", "check_command", "hostalive"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func TestAccCreateVariableHost(t *testing.T) {
    39  
    40  	var testAccCreateVariableHost = fmt.Sprintf(`
    41  		resource "icinga2_host" "tf-3" {
    42  		hostname = "terraform-host-3"
    43  		address = "10.10.10.3"
    44  		check_command = "hostalive"
    45  		vars {
    46  		  os = "linux"
    47  		  osver = "1"
    48  		  allowance = "none" }
    49  		}`)
    50  
    51  	resource.Test(t, resource.TestCase{
    52  		PreCheck:  func() { testAccPreCheck(t) },
    53  		Providers: testAccProviders,
    54  		Steps: []resource.TestStep{
    55  			resource.TestStep{
    56  				Config: testAccCreateVariableHost,
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testAccCheckHostExists("icinga2_host.tf-3"),
    59  					testAccCheckResourceState("icinga2_host.tf-3", "hostname", "terraform-host-3"),
    60  					testAccCheckResourceState("icinga2_host.tf-3", "address", "10.10.10.3"),
    61  					testAccCheckResourceState("icinga2_host.tf-3", "check_command", "hostalive"),
    62  					testAccCheckResourceState("icinga2_host.tf-3", "vars.%", "3"),
    63  					testAccCheckResourceState("icinga2_host.tf-3", "vars.allowance", "none"),
    64  					testAccCheckResourceState("icinga2_host.tf-3", "vars.os", "linux"),
    65  					testAccCheckResourceState("icinga2_host.tf-3", "vars.osver", "1"),
    66  				),
    67  			},
    68  		},
    69  	})
    70  }
    71  
    72  func testAccCheckHostExists(rn string) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  		resource, ok := s.RootModule().Resources[rn]
    75  		if !ok {
    76  			return fmt.Errorf("Host resource not found: %s", rn)
    77  		}
    78  
    79  		if resource.Primary.ID == "" {
    80  			return fmt.Errorf("resource id not set")
    81  		}
    82  
    83  		client := testAccProvider.Meta().(*iapi.Server)
    84  		_, err := client.GetHost(resource.Primary.ID)
    85  		if err != nil {
    86  			return fmt.Errorf("error getting getting host: %s", err)
    87  		}
    88  
    89  		return nil
    90  	}
    91  
    92  }