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

     1  package icinga2
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/lrsmith/go-icinga2-api/iapi"
    11  )
    12  
    13  func TestAccCreateService(t *testing.T) {
    14  
    15  	var testAccCreateService = fmt.Sprintf(`
    16  		resource "icinga2_service" "tf-service-1" {
    17  		hostname      = "docker-icinga2"
    18  		name          = "ssh3"
    19  		check_command = "ssh"
    20  	}`)
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:  func() { testAccPreCheck(t) },
    24  		Providers: testAccProviders,
    25  		Steps: []resource.TestStep{
    26  			resource.TestStep{
    27  				Config: testAccCreateService,
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckServiceExists("icinga2_service.tf-service-1"),
    30  					testAccCheckResourceState("icinga2_service.tf-service-1", "hostname", "docker-icinga2"),
    31  					testAccCheckResourceState("icinga2_service.tf-service-1", "name", "ssh3"),
    32  					testAccCheckResourceState("icinga2_service.tf-service-1", "check_command", "ssh"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccCheckServiceExists(rn string) resource.TestCheckFunc {
    40  	return func(s *terraform.State) error {
    41  		resource, ok := s.RootModule().Resources[rn]
    42  		if !ok {
    43  			return fmt.Errorf("Service resource not found: %s", rn)
    44  		}
    45  
    46  		if resource.Primary.ID == "" {
    47  			return fmt.Errorf("resource id not set")
    48  		}
    49  
    50  		client := testAccProvider.Meta().(*iapi.Server)
    51  		tokens := strings.Split(resource.Primary.ID, "!")
    52  
    53  		_, err := client.GetService(tokens[1], tokens[0])
    54  		if err != nil {
    55  			return fmt.Errorf("error getting getting Service: %s", err)
    56  		}
    57  
    58  		return nil
    59  	}
    60  
    61  }