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