github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/digitalocean/resource_digitalocean_loadbalancer_test.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/digitalocean/godo"
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccDigitalOceanLoadbalancer_Basic(t *testing.T) {
    15  	var loadbalancer godo.LoadBalancer
    16  	rInt := acctest.RandInt()
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckDigitalOceanLoadbalancerDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: testAccCheckDigitalOceanLoadbalancerConfig_basic(rInt),
    25  				Check: resource.ComposeAggregateTestCheckFunc(
    26  					testAccCheckDigitalOceanLoadbalancerExists("digitalocean_loadbalancer.foobar", &loadbalancer),
    27  					resource.TestCheckResourceAttr(
    28  						"digitalocean_loadbalancer.foobar", "name", fmt.Sprintf("loadbalancer-%d", rInt)),
    29  					resource.TestCheckResourceAttr(
    30  						"digitalocean_loadbalancer.foobar", "region", "nyc3"),
    31  					resource.TestCheckResourceAttr(
    32  						"digitalocean_loadbalancer.foobar", "forwarding_rule.#", "1"),
    33  					resource.TestCheckResourceAttr(
    34  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.entry_port", "80"),
    35  					resource.TestCheckResourceAttr(
    36  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.entry_protocol", "http"),
    37  					resource.TestCheckResourceAttr(
    38  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.target_port", "80"),
    39  					resource.TestCheckResourceAttr(
    40  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.target_protocol", "http"),
    41  					resource.TestCheckResourceAttr(
    42  						"digitalocean_loadbalancer.foobar", "healthcheck.#", "1"),
    43  					resource.TestCheckResourceAttr(
    44  						"digitalocean_loadbalancer.foobar", "healthcheck.0.port", "22"),
    45  					resource.TestCheckResourceAttr(
    46  						"digitalocean_loadbalancer.foobar", "healthcheck.0.protocol", "tcp"),
    47  					resource.TestCheckResourceAttr(
    48  						"digitalocean_loadbalancer.foobar", "droplet_ids.#", "1"),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func TestAccDigitalOceanLoadbalancer_Updated(t *testing.T) {
    56  	var loadbalancer godo.LoadBalancer
    57  	rInt := acctest.RandInt()
    58  
    59  	resource.Test(t, resource.TestCase{
    60  		PreCheck:     func() { testAccPreCheck(t) },
    61  		Providers:    testAccProviders,
    62  		CheckDestroy: testAccCheckDigitalOceanLoadbalancerDestroy,
    63  		Steps: []resource.TestStep{
    64  			{
    65  				Config: testAccCheckDigitalOceanLoadbalancerConfig_basic(rInt),
    66  				Check: resource.ComposeAggregateTestCheckFunc(
    67  					testAccCheckDigitalOceanLoadbalancerExists("digitalocean_loadbalancer.foobar", &loadbalancer),
    68  					resource.TestCheckResourceAttr(
    69  						"digitalocean_loadbalancer.foobar", "name", fmt.Sprintf("loadbalancer-%d", rInt)),
    70  					resource.TestCheckResourceAttr(
    71  						"digitalocean_loadbalancer.foobar", "region", "nyc3"),
    72  					resource.TestCheckResourceAttr(
    73  						"digitalocean_loadbalancer.foobar", "forwarding_rule.#", "1"),
    74  					resource.TestCheckResourceAttr(
    75  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.entry_port", "80"),
    76  					resource.TestCheckResourceAttr(
    77  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.entry_protocol", "http"),
    78  					resource.TestCheckResourceAttr(
    79  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.target_port", "80"),
    80  					resource.TestCheckResourceAttr(
    81  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.target_protocol", "http"),
    82  					resource.TestCheckResourceAttr(
    83  						"digitalocean_loadbalancer.foobar", "healthcheck.#", "1"),
    84  					resource.TestCheckResourceAttr(
    85  						"digitalocean_loadbalancer.foobar", "healthcheck.0.port", "22"),
    86  					resource.TestCheckResourceAttr(
    87  						"digitalocean_loadbalancer.foobar", "healthcheck.0.protocol", "tcp"),
    88  					resource.TestCheckResourceAttr(
    89  						"digitalocean_loadbalancer.foobar", "droplet_ids.#", "1"),
    90  				),
    91  			},
    92  			{
    93  				Config: testAccCheckDigitalOceanLoadbalancerConfig_updated(rInt),
    94  				Check: resource.ComposeAggregateTestCheckFunc(
    95  					testAccCheckDigitalOceanLoadbalancerExists("digitalocean_loadbalancer.foobar", &loadbalancer),
    96  					resource.TestCheckResourceAttr(
    97  						"digitalocean_loadbalancer.foobar", "name", fmt.Sprintf("loadbalancer-%d", rInt)),
    98  					resource.TestCheckResourceAttr(
    99  						"digitalocean_loadbalancer.foobar", "region", "nyc3"),
   100  					resource.TestCheckResourceAttr(
   101  						"digitalocean_loadbalancer.foobar", "forwarding_rule.#", "1"),
   102  					resource.TestCheckResourceAttr(
   103  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.entry_port", "81"),
   104  					resource.TestCheckResourceAttr(
   105  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.entry_protocol", "http"),
   106  					resource.TestCheckResourceAttr(
   107  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.target_port", "81"),
   108  					resource.TestCheckResourceAttr(
   109  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.target_protocol", "http"),
   110  					resource.TestCheckResourceAttr(
   111  						"digitalocean_loadbalancer.foobar", "healthcheck.#", "1"),
   112  					resource.TestCheckResourceAttr(
   113  						"digitalocean_loadbalancer.foobar", "healthcheck.0.port", "22"),
   114  					resource.TestCheckResourceAttr(
   115  						"digitalocean_loadbalancer.foobar", "healthcheck.0.protocol", "tcp"),
   116  					resource.TestCheckResourceAttr(
   117  						"digitalocean_loadbalancer.foobar", "droplet_ids.#", "2"),
   118  				),
   119  			},
   120  		},
   121  	})
   122  }
   123  
   124  func TestAccDigitalOceanLoadbalancer_dropletTag(t *testing.T) {
   125  	var loadbalancer godo.LoadBalancer
   126  	rInt := acctest.RandInt()
   127  
   128  	resource.Test(t, resource.TestCase{
   129  		PreCheck:     func() { testAccPreCheck(t) },
   130  		Providers:    testAccProviders,
   131  		CheckDestroy: testAccCheckDigitalOceanLoadbalancerDestroy,
   132  		Steps: []resource.TestStep{
   133  			{
   134  				Config: testAccCheckDigitalOceanLoadbalancerConfig_dropletTag(rInt),
   135  				Check: resource.ComposeAggregateTestCheckFunc(
   136  					testAccCheckDigitalOceanLoadbalancerExists("digitalocean_loadbalancer.foobar", &loadbalancer),
   137  					resource.TestCheckResourceAttr(
   138  						"digitalocean_loadbalancer.foobar", "name", fmt.Sprintf("loadbalancer-%d", rInt)),
   139  					resource.TestCheckResourceAttr(
   140  						"digitalocean_loadbalancer.foobar", "region", "nyc3"),
   141  					resource.TestCheckResourceAttr(
   142  						"digitalocean_loadbalancer.foobar", "forwarding_rule.#", "1"),
   143  					resource.TestCheckResourceAttr(
   144  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.entry_port", "80"),
   145  					resource.TestCheckResourceAttr(
   146  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.entry_protocol", "http"),
   147  					resource.TestCheckResourceAttr(
   148  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.target_port", "80"),
   149  					resource.TestCheckResourceAttr(
   150  						"digitalocean_loadbalancer.foobar", "forwarding_rule.0.target_protocol", "http"),
   151  					resource.TestCheckResourceAttr(
   152  						"digitalocean_loadbalancer.foobar", "healthcheck.#", "1"),
   153  					resource.TestCheckResourceAttr(
   154  						"digitalocean_loadbalancer.foobar", "healthcheck.0.port", "22"),
   155  					resource.TestCheckResourceAttr(
   156  						"digitalocean_loadbalancer.foobar", "healthcheck.0.protocol", "tcp"),
   157  					resource.TestCheckResourceAttr(
   158  						"digitalocean_loadbalancer.foobar", "droplet_tag", "sample"),
   159  				),
   160  			},
   161  		},
   162  	})
   163  }
   164  
   165  func testAccCheckDigitalOceanLoadbalancerDestroy(s *terraform.State) error {
   166  	client := testAccProvider.Meta().(*godo.Client)
   167  
   168  	for _, rs := range s.RootModule().Resources {
   169  		if rs.Type != "digitalocean_loadbalancer" {
   170  			continue
   171  		}
   172  
   173  		_, _, err := client.LoadBalancers.Get(rs.Primary.ID)
   174  
   175  		if err != nil && !strings.Contains(err.Error(), "404") {
   176  			return fmt.Errorf(
   177  				"Error waiting for loadbalancer (%s) to be destroyed: %s",
   178  				rs.Primary.ID, err)
   179  		}
   180  	}
   181  
   182  	return nil
   183  }
   184  
   185  func testAccCheckDigitalOceanLoadbalancerExists(n string, loadbalancer *godo.LoadBalancer) resource.TestCheckFunc {
   186  	return func(s *terraform.State) error {
   187  		rs, ok := s.RootModule().Resources[n]
   188  		if !ok {
   189  			return fmt.Errorf("Not found: %s", n)
   190  		}
   191  
   192  		if rs.Primary.ID == "" {
   193  			return fmt.Errorf("No Loadbalancer ID is set")
   194  		}
   195  
   196  		client := testAccProvider.Meta().(*godo.Client)
   197  
   198  		lb, _, err := client.LoadBalancers.Get(rs.Primary.ID)
   199  
   200  		if err != nil {
   201  			return err
   202  		}
   203  
   204  		if lb.ID != rs.Primary.ID {
   205  			return fmt.Errorf("Loabalancer not found")
   206  		}
   207  
   208  		*loadbalancer = *lb
   209  
   210  		return nil
   211  	}
   212  }
   213  
   214  func testAccCheckDigitalOceanLoadbalancerConfig_basic(rInt int) string {
   215  	return fmt.Sprintf(`
   216  resource "digitalocean_droplet" "foobar" {
   217    name      = "foo-%d"
   218    size      = "512mb"
   219    image     = "centos-7-x64"
   220    region    = "nyc3"
   221  }
   222  
   223  resource "digitalocean_loadbalancer" "foobar" {
   224    name = "loadbalancer-%d"
   225    region = "nyc3"
   226  
   227    forwarding_rule {
   228      entry_port = 80
   229      entry_protocol = "http"
   230  
   231      target_port = 80
   232      target_protocol = "http"
   233    }
   234  
   235    healthcheck {
   236      port = 22
   237      protocol = "tcp"
   238    }
   239  
   240    droplet_ids = ["${digitalocean_droplet.foobar.id}"]
   241  }`, rInt, rInt)
   242  }
   243  
   244  func testAccCheckDigitalOceanLoadbalancerConfig_updated(rInt int) string {
   245  	return fmt.Sprintf(`
   246  resource "digitalocean_droplet" "foobar" {
   247    name      = "foo-%d"
   248    size      = "512mb"
   249    image     = "centos-7-x64"
   250    region    = "nyc3"
   251  }
   252  
   253  resource "digitalocean_droplet" "foo" {
   254    name      = "foo-%d"
   255    size      = "512mb"
   256    image     = "centos-7-x64"
   257    region    = "nyc3"
   258  }
   259  
   260  resource "digitalocean_loadbalancer" "foobar" {
   261    name = "loadbalancer-%d"
   262    region = "nyc3"
   263  
   264    forwarding_rule {
   265      entry_port = 81
   266      entry_protocol = "http"
   267  
   268      target_port = 81
   269      target_protocol = "http"
   270    }
   271  
   272    healthcheck {
   273      port = 22
   274      protocol = "tcp"
   275    }
   276  
   277    droplet_ids = ["${digitalocean_droplet.foobar.id}","${digitalocean_droplet.foo.id}"]
   278  }`, rInt, rInt, rInt)
   279  }
   280  
   281  func testAccCheckDigitalOceanLoadbalancerConfig_dropletTag(rInt int) string {
   282  	return fmt.Sprintf(`
   283  resource "digitalocean_tag" "barbaz" {
   284    name = "sample"
   285  }
   286  
   287  resource "digitalocean_droplet" "foobar" {
   288    name      = "foo-%d"
   289    size      = "512mb"
   290    image     = "centos-7-x64"
   291    region    = "nyc3"
   292    tags = ["${digitalocean_tag.barbaz.id}"]
   293  }
   294  
   295  resource "digitalocean_loadbalancer" "foobar" {
   296    name = "loadbalancer-%d"
   297    region = "nyc3"
   298  
   299    forwarding_rule {
   300      entry_port = 80
   301      entry_protocol = "http"
   302  
   303      target_port = 80
   304      target_protocol = "http"
   305    }
   306  
   307    healthcheck {
   308      port = 22
   309      protocol = "tcp"
   310    }
   311  
   312    droplet_tag = "${digitalocean_tag.barbaz.name}"
   313  
   314    depends_on = ["digitalocean_droplet.foobar"]
   315  }`, rInt, rInt)
   316  }