github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/clc/resource_clc_load_balancer_pool_test.go (about)

     1  package clc
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	clc "github.com/CenturyLinkCloud/clc-sdk"
     8  	lb "github.com/CenturyLinkCloud/clc-sdk/lb"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  // things to test:
    14  //   basic create/delete
    15  //   update nodes
    16  //   works for 80 and 443 together
    17  
    18  func TestAccLBPoolBasic(t *testing.T) {
    19  	var pool lb.Pool
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckLBPDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: testAccCheckLBPConfigBasic,
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckLBPExists("clc_load_balancer_pool.acc_test_pool", &pool),
    29  					resource.TestCheckResourceAttr("clc_load_balancer_pool.acc_test_pool", "port", "80"),
    30  				),
    31  			},
    32  			resource.TestStep{
    33  				Config: testAccCheckLBPConfigUpdates,
    34  				Check: resource.ComposeTestCheckFunc(
    35  					testAccCheckLBPExists("clc_load_balancer_pool.acc_test_pool", &pool),
    36  					resource.TestCheckResourceAttr("clc_load_balancer.acc_test_lbp", "description", "description modified"),
    37  					resource.TestCheckResourceAttr("clc_load_balancer.acc_test_lbp", "status", "disabled"),
    38  					resource.TestCheckResourceAttr("clc_load_balancer_pool.acc_test_pool", "nodes.0.privatePort", "8080"),
    39  				),
    40  			},
    41  		},
    42  	})
    43  }
    44  
    45  func testAccCheckLBPDestroy(s *terraform.State) error {
    46  	client := testAccProvider.Meta().(*clc.Client)
    47  	for _, rs := range s.RootModule().Resources {
    48  		if rs.Type != "clc_load_balancer_pool" {
    49  			continue
    50  		}
    51  		lbid := rs.Primary.Attributes["load_balancer"]
    52  		if _, err := client.LB.Get(testAccDC, rs.Primary.ID); err != nil {
    53  			return nil // parent LB already gone
    54  		}
    55  		if _, err := client.LB.GetPool(testAccDC, lbid, rs.Primary.ID); err == nil {
    56  			return fmt.Errorf("LB still exists")
    57  		}
    58  	}
    59  	return nil
    60  }
    61  
    62  func testAccCheckLBPExists(n string, resp *lb.Pool) resource.TestCheckFunc {
    63  	return func(s *terraform.State) error {
    64  		rs, ok := s.RootModule().Resources[n]
    65  		if !ok {
    66  			return fmt.Errorf("Not found: %s", n)
    67  		}
    68  		if rs.Primary.ID == "" {
    69  			return fmt.Errorf("No ID is set")
    70  		}
    71  		lbid := rs.Primary.Attributes["load_balancer"]
    72  		client := testAccProvider.Meta().(*clc.Client)
    73  		p, err := client.LB.GetPool(testAccDC, lbid, rs.Primary.ID)
    74  		if err != nil {
    75  			return err
    76  		}
    77  		if p.ID != rs.Primary.ID {
    78  			return fmt.Errorf("Pool not found")
    79  		}
    80  		*resp = *p
    81  		return nil
    82  	}
    83  }
    84  
    85  const testAccCheckLBPConfigBasic = `
    86  variable "dc" { default = "IL1" }
    87  
    88  resource "clc_group" "acc_test_lbp_group" {
    89    location_id		= "${var.dc}"
    90    name			= "acc_test_lbp_group"
    91    parent		= "Default Group"
    92  }
    93  
    94  # need a server here because we need to reference an ip owned by this account
    95  resource "clc_server" "acc_test_lbp_server" {
    96    name_template		= "node"
    97    description		= "load balanced"
    98    source_server_id	= "UBUNTU-14-64-TEMPLATE"
    99    type			= "standard"
   100    group_id		= "${clc_group.acc_test_lbp_group.id}"
   101    cpu			= 1
   102    memory_mb		= 1024
   103    password		= "Green123$"
   104    power_state		= "started"
   105  
   106  }
   107  
   108  resource "clc_load_balancer" "acc_test_lbp" {
   109    data_center		= "${var.dc}"
   110    name			= "acc_test_lb"
   111    description		= "load balancer test"
   112    status		= "enabled"
   113    depends_on            = ["clc_server.acc_test_lbp_server"]
   114  }
   115  
   116  resource "clc_load_balancer_pool" "acc_test_pool" {
   117    port			= 80
   118    data_center		= "${var.dc}"
   119    load_balancer		= "${clc_load_balancer.acc_test_lbp.id}"
   120    nodes
   121      {
   122        status		= "enabled"
   123        ipAddress		= "${clc_server.acc_test_lbp_server.private_ip_address}"
   124        privatePort	= 80
   125      }
   126    depends_on            = ["clc_server.acc_test_lbp_server"]
   127  }
   128  `
   129  
   130  const testAccCheckLBPConfigUpdates = `
   131  variable "dc" { default = "IL1" }
   132  
   133  resource "clc_group" "acc_test_lbp_group" {
   134    location_id		= "${var.dc}"
   135    name			= "acc_test_lbp_group"
   136    parent		= "Default Group"
   137  }
   138  
   139  # need a server here because we need to reference an ip owned by this account
   140  resource "clc_server" "acc_test_lbp_server" {
   141    name_template		= "node"
   142    description		= "load balanced"
   143    source_server_id	= "UBUNTU-14-64-TEMPLATE"
   144    type			= "standard"
   145    group_id		= "${clc_group.acc_test_lbp_group.id}"
   146    cpu			= 1
   147    memory_mb		= 1024
   148    password		= "Green123$"
   149    power_state		= "started"
   150  
   151  }
   152  
   153  resource "clc_load_balancer" "acc_test_lbp" {
   154    data_center		= "${var.dc}"
   155    name			= "acc_test_lb"
   156    description		= "description modified"
   157    status		= "disabled"
   158    depends_on            = ["clc_server.acc_test_lbp_server"]
   159  }
   160  
   161  resource "clc_load_balancer_pool" "acc_test_pool" {
   162    port			= 80
   163    data_center		= "${var.dc}"
   164    load_balancer		= "${clc_load_balancer.acc_test_lbp.id}"
   165    nodes
   166      {
   167        status		= "enabled"
   168        ipAddress		= "${clc_server.acc_test_lbp_server.private_ip_address}"
   169        privatePort	= 8080
   170      }
   171    depends_on            = ["clc_server.acc_test_lbp_server"]
   172  }
   173  `