github.com/nevins-b/terraform@v0.3.8-0.20170215184714-bbae22007d5a/builtin/providers/librato/resource_librato_service_test.go (about)

     1  package librato
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/henrikhodne/go-librato/librato"
    11  )
    12  
    13  func TestAccLibratoService_Basic(t *testing.T) {
    14  	var service librato.Service
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckLibratoServiceDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccCheckLibratoServiceConfig_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckLibratoServiceExists("librato_service.foobar", &service),
    25  					testAccCheckLibratoServiceTitle(&service, "Foo Bar"),
    26  					resource.TestCheckResourceAttr(
    27  						"librato_service.foobar", "title", "Foo Bar"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccLibratoService_Updated(t *testing.T) {
    35  	var service librato.Service
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckLibratoServiceDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccCheckLibratoServiceConfig_basic,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckLibratoServiceExists("librato_service.foobar", &service),
    46  					testAccCheckLibratoServiceTitle(&service, "Foo Bar"),
    47  					resource.TestCheckResourceAttr(
    48  						"librato_service.foobar", "title", "Foo Bar"),
    49  				),
    50  			},
    51  			resource.TestStep{
    52  				Config: testAccCheckLibratoServiceConfig_new_value,
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testAccCheckLibratoServiceExists("librato_service.foobar", &service),
    55  					testAccCheckLibratoServiceTitle(&service, "Bar Baz"),
    56  					resource.TestCheckResourceAttr(
    57  						"librato_service.foobar", "title", "Bar Baz"),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func testAccCheckLibratoServiceDestroy(s *terraform.State) error {
    65  	client := testAccProvider.Meta().(*librato.Client)
    66  
    67  	for _, rs := range s.RootModule().Resources {
    68  		if rs.Type != "librato_service" {
    69  			continue
    70  		}
    71  
    72  		id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
    73  		if err != nil {
    74  			return fmt.Errorf("ID not a number")
    75  		}
    76  
    77  		_, _, err = client.Services.Get(uint(id))
    78  
    79  		if err == nil {
    80  			return fmt.Errorf("Service still exists")
    81  		}
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  func testAccCheckLibratoServiceTitle(service *librato.Service, title string) resource.TestCheckFunc {
    88  	return func(s *terraform.State) error {
    89  
    90  		if service.Title == nil || *service.Title != title {
    91  			return fmt.Errorf("Bad title: %s", *service.Title)
    92  		}
    93  
    94  		return nil
    95  	}
    96  }
    97  
    98  func testAccCheckLibratoServiceExists(n string, service *librato.Service) resource.TestCheckFunc {
    99  	return func(s *terraform.State) error {
   100  		rs, ok := s.RootModule().Resources[n]
   101  
   102  		if !ok {
   103  			return fmt.Errorf("Not found: %s", n)
   104  		}
   105  
   106  		if rs.Primary.ID == "" {
   107  			return fmt.Errorf("No Service ID is set")
   108  		}
   109  
   110  		client := testAccProvider.Meta().(*librato.Client)
   111  
   112  		id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
   113  		if err != nil {
   114  			return fmt.Errorf("ID not a number")
   115  		}
   116  
   117  		foundService, _, err := client.Services.Get(uint(id))
   118  
   119  		if err != nil {
   120  			return err
   121  		}
   122  
   123  		if foundService.ID == nil || *foundService.ID != uint(id) {
   124  			return fmt.Errorf("Service not found")
   125  		}
   126  
   127  		*service = *foundService
   128  
   129  		return nil
   130  	}
   131  }
   132  
   133  const testAccCheckLibratoServiceConfig_basic = `
   134  resource "librato_service" "foobar" {
   135      title = "Foo Bar"
   136      type = "mail"
   137      settings = <<EOF
   138  {
   139    "addresses": "admin@example.com"
   140  }
   141  EOF
   142  }`
   143  
   144  const testAccCheckLibratoServiceConfig_new_value = `
   145  resource "librato_service" "foobar" {
   146      title = "Bar Baz"
   147      type = "mail"
   148      settings = <<EOF
   149  {
   150    "addresses": "admin@example.com"
   151  }
   152  EOF
   153  }`