github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/consul/resource_consul_service_test.go (about)

     1  package consul
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	consulapi "github.com/hashicorp/consul/api"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccConsulService_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() {},
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckConsulServiceDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccConsulServiceConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckConsulServiceExists(),
    22  					testAccCheckConsulServiceValue("consul_service.app", "address", "www.google.com"),
    23  					testAccCheckConsulServiceValue("consul_service.app", "id", "google"),
    24  					testAccCheckConsulServiceValue("consul_service.app", "service_id", "google"),
    25  					testAccCheckConsulServiceValue("consul_service.app", "name", "google"),
    26  					testAccCheckConsulServiceValue("consul_service.app", "port", "80"),
    27  					testAccCheckConsulServiceValue("consul_service.app", "tags.#", "2"),
    28  					testAccCheckConsulServiceValue("consul_service.app", "tags.0", "tag0"),
    29  					testAccCheckConsulServiceValue("consul_service.app", "tags.1", "tag1"),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func testAccCheckConsulServiceDestroy(s *terraform.State) error {
    37  	agent := testAccProvider.Meta().(*consulapi.Client).Agent()
    38  	services, err := agent.Services()
    39  	if err != nil {
    40  		return fmt.Errorf("Could not retrieve services: %#v", err)
    41  	}
    42  	_, ok := services["google"]
    43  	if ok {
    44  		return fmt.Errorf("Service still exists: %#v", "google")
    45  	}
    46  	return nil
    47  }
    48  
    49  func testAccCheckConsulServiceExists() resource.TestCheckFunc {
    50  	return func(s *terraform.State) error {
    51  		agent := testAccProvider.Meta().(*consulapi.Client).Agent()
    52  		services, err := agent.Services()
    53  		if err != nil {
    54  			return err
    55  		}
    56  		_, ok := services["google"]
    57  		if !ok {
    58  			return fmt.Errorf("Service does not exist: %#v", "google")
    59  		}
    60  		return nil
    61  	}
    62  }
    63  
    64  func testAccCheckConsulServiceValue(n, attr, val string) resource.TestCheckFunc {
    65  	return func(s *terraform.State) error {
    66  		rn, ok := s.RootModule().Resources[n]
    67  		if !ok {
    68  			return fmt.Errorf("Resource not found")
    69  		}
    70  		out, ok := rn.Primary.Attributes[attr]
    71  		if !ok {
    72  			return fmt.Errorf("Attribute '%s' not found: %#v", attr, rn.Primary.Attributes)
    73  		}
    74  		if val != "<any>" && out != val {
    75  			return fmt.Errorf("Attribute '%s' value '%s' != '%s'", attr, out, val)
    76  		}
    77  		if val == "<any>" && out == "" {
    78  			return fmt.Errorf("Attribute '%s' value '%s'", attr, out)
    79  		}
    80  		return nil
    81  	}
    82  }
    83  
    84  const testAccConsulServiceConfig = `
    85  resource "consul_service" "app" {
    86  	address = "www.google.com"
    87  	service_id = "google"
    88  	name = "google"
    89  	port = 80
    90  	tags = ["tag0", "tag1"]
    91  }
    92  `