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

     1  package clc
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	clc "github.com/CenturyLinkCloud/clc-sdk"
     9  	"github.com/CenturyLinkCloud/clc-sdk/server"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  // things to test:
    15  //   basic crud
    16  //   modify specs
    17  //   power operations
    18  //   add'l disks
    19  //   custom fields? (skip)
    20  
    21  func TestAccServerBasic(t *testing.T) {
    22  	var resp server.Response
    23  	resource.Test(t, resource.TestCase{
    24  		PreCheck:     func() { testAccPreCheck(t) },
    25  		Providers:    testAccProviders,
    26  		CheckDestroy: testAccCheckServerDestroy,
    27  		Steps: []resource.TestStep{
    28  			resource.TestStep{
    29  				Config: testAccCheckServerConfigBasic,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckServerExists("clc_server.acc_test_server", &resp),
    32  					resource.TestCheckResourceAttr(
    33  						"clc_server.acc_test_server", "name_template", "test"),
    34  					resource.TestCheckResourceAttr(
    35  						"clc_server.acc_test_server", "cpu", "1"),
    36  					resource.TestCheckResourceAttr(
    37  						"clc_server.acc_test_server", "memory_mb", "1024"),
    38  				),
    39  			},
    40  			// update simple attrs
    41  			resource.TestStep{
    42  				Config: testAccCheckServerConfigCPUMEM,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckServerExists("clc_server.acc_test_server", &resp),
    45  					resource.TestCheckResourceAttr(
    46  						"clc_server.acc_test_server", "cpu", "2"),
    47  					resource.TestCheckResourceAttr(
    48  						"clc_server.acc_test_server", "memory_mb", "2048"),
    49  					resource.TestCheckResourceAttr(
    50  						"clc_server.acc_test_server", "password", "Green123$"),
    51  					testAccCheckServerUpdatedSpec("clc_server.acc_test_server", &resp),
    52  				),
    53  			},
    54  			// toggle power
    55  			resource.TestStep{
    56  				Config: testAccCheckServerConfigPower,
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testAccCheckServerExists("clc_server.acc_test_server", &resp),
    59  					resource.TestCheckResourceAttr(
    60  						"clc_server.acc_test_server", "power_state", "stopped"),
    61  				),
    62  			},
    63  			/* // currently broken since disk updates require diskId
    64  			// add disks
    65  			resource.TestStep{
    66  				Config: testAccCheckServerConfig_disks,
    67  				Check: resource.ComposeTestCheckFunc(
    68  					testAccCheckServerExists("clc_server.acc_test_server", &resp),
    69  					// power still off
    70  					resource.TestCheckResourceAttr(
    71  						"clc_server.acc_test_server", "power_state", "stopped"),
    72  					testAccCheckServerUpdatedDisks("clc_server.acc_test_server", &resp),
    73  				),
    74  			},
    75  			*/
    76  			/* // broken since network id is a (account-specific) guid
    77  			// set network id
    78  			resource.TestStep{
    79  				Config: testAccCheckServerConfigNetwork,
    80  				Check: resource.ComposeTestCheckFunc(
    81  					testAccCheckServerExists("clc_server.acc_test_server", &resp),
    82  					resource.TestCheckResourceAttr(
    83  						"clc_server.acc_test_server", "network_id", "15a0f669c332435ebf375e010ac79fbb"),
    84  					testAccCheckServerUpdatedSpec("clc_server.acc_test_server", &resp),
    85  				),
    86  			},
    87  			*/
    88  		},
    89  	})
    90  }
    91  
    92  func testAccCheckServerDestroy(s *terraform.State) error {
    93  	client := testAccProvider.Meta().(*clc.Client)
    94  
    95  	for _, rs := range s.RootModule().Resources {
    96  		if rs.Type != "clc_server" {
    97  			continue
    98  		}
    99  
   100  		_, err := client.Server.Get(rs.Primary.ID)
   101  
   102  		if err == nil {
   103  			return fmt.Errorf("Server still exists")
   104  		}
   105  	}
   106  
   107  	return nil
   108  }
   109  
   110  func testAccCheckServerExists(n string, resp *server.Response) resource.TestCheckFunc {
   111  	return func(s *terraform.State) error {
   112  		rs, ok := s.RootModule().Resources[n]
   113  		if !ok {
   114  			return fmt.Errorf("Not found: %s", n)
   115  		}
   116  		if rs.Primary.ID == "" {
   117  			return fmt.Errorf("No server ID is set")
   118  		}
   119  
   120  		client := testAccProvider.Meta().(*clc.Client)
   121  		srv, err := client.Server.Get(rs.Primary.ID)
   122  		if err != nil {
   123  			return err
   124  		}
   125  
   126  		if strings.ToUpper(srv.ID) != rs.Primary.ID {
   127  			return fmt.Errorf("Server not found")
   128  		}
   129  		*resp = *srv
   130  		return nil
   131  	}
   132  }
   133  
   134  func testAccCheckServerUpdatedSpec(n string, resp *server.Response) resource.TestCheckFunc {
   135  	return func(s *terraform.State) error {
   136  		rs, ok := s.RootModule().Resources[n]
   137  		if !ok {
   138  			return fmt.Errorf("Not found: %s", n)
   139  		}
   140  		client := testAccProvider.Meta().(*clc.Client)
   141  		srv, err := client.Server.Get(rs.Primary.ID)
   142  		if err != nil {
   143  			return err
   144  		}
   145  		cpu := srv.Details.CPU
   146  		mem := srv.Details.MemoryMB
   147  		scpu := fmt.Sprintf("%v", cpu)
   148  		smem := fmt.Sprintf("%v", mem)
   149  		excpu := rs.Primary.Attributes["cpu"]
   150  		exmem := rs.Primary.Attributes["memory_mb"]
   151  		if scpu != excpu {
   152  			return fmt.Errorf("Expected CPU to be %v but found %v", excpu, scpu)
   153  		}
   154  		if smem != exmem {
   155  			return fmt.Errorf("Expected MEM to be %v but found %v", exmem, smem)
   156  		}
   157  		return nil
   158  	}
   159  }
   160  
   161  func testAccCheckServerUpdatedDisks(n string, resp *server.Response) resource.TestCheckFunc {
   162  	return func(s *terraform.State) error {
   163  		rs, ok := s.RootModule().Resources[n]
   164  		if !ok {
   165  			return fmt.Errorf("Not found: %s", n)
   166  		}
   167  		client := testAccProvider.Meta().(*clc.Client)
   168  		srv, err := client.Server.Get(rs.Primary.ID)
   169  		if err != nil {
   170  			return err
   171  		}
   172  
   173  		if len(srv.Details.Disks) <= 3 {
   174  			return fmt.Errorf("Expected total of > 3 drives. found: %v", len(srv.Details.Disks))
   175  		}
   176  
   177  		return nil
   178  	}
   179  }
   180  
   181  const testAccCheckServerConfigBasic = `
   182  variable "dc" { default = "IL1" }
   183  
   184  resource "clc_group" "acc_test_group_server" {
   185    location_id	= "${var.dc}"
   186    name		= "acc_test_group_server"
   187    parent	= "Default Group"
   188  }
   189  
   190  resource "clc_server" "acc_test_server" {
   191    name_template		= "test"
   192    source_server_id	= "UBUNTU-14-64-TEMPLATE"
   193    group_id		= "${clc_group.acc_test_group_server.id}"
   194    cpu			= 1
   195    memory_mb		= 1024
   196    password		= "Green123$"
   197  }
   198  `
   199  
   200  const testAccCheckServerConfigCPUMEM = `
   201  variable "dc" { default = "IL1" }
   202  
   203  resource "clc_group" "acc_test_group_server" {
   204    location_id	= "${var.dc}"
   205    name		= "acc_test_group_server"
   206    parent	= "Default Group"
   207  }
   208  
   209  resource "clc_server" "acc_test_server" {
   210    name_template		= "test"
   211    source_server_id	= "UBUNTU-14-64-TEMPLATE"
   212    group_id		= "${clc_group.acc_test_group_server.id}"
   213    cpu			= 2
   214    memory_mb		= 2048
   215    password		= "Green123$"
   216    power_state		= "started"
   217  }
   218  `
   219  
   220  const testAccCheckServerConfigPower = `
   221  variable "dc" { default = "IL1" }
   222  
   223  resource "clc_group" "acc_test_group_server" {
   224    location_id	= "${var.dc}"
   225    name		= "acc_test_group_server"
   226    parent	= "Default Group"
   227  }
   228  
   229  resource "clc_server" "acc_test_server" {
   230    name_template		= "test"
   231    source_server_id	= "UBUNTU-14-64-TEMPLATE"
   232    group_id		= "${clc_group.acc_test_group_server.id}"
   233    cpu			= 2
   234    memory_mb		= 2048
   235    password		= "Green123$"
   236    power_state		= "stopped"
   237  }
   238  `
   239  
   240  const testAccCheckServerConfigDisks = `
   241  variable "dc" { default = "IL1" }
   242  
   243  resource "clc_group" "acc_test_group_server" {
   244    location_id	= "${var.dc}"
   245    name		= "acc_test_group_server"
   246    parent	= "Default Group"
   247  }
   248  
   249  resource "clc_server" "acc_test_server" {
   250    name_template		= "test"
   251    source_server_id	= "UBUNTU-14-64-TEMPLATE"
   252    group_id		= "${clc_group.acc_test_group_server.id}"
   253    cpu			= 2
   254    memory_mb		= 2048
   255    password		= "Green123$"
   256    power_state		= "stopped"
   257    # network_id = "15a0f669c332435ebf375e010ac79fbb"
   258    additional_disks
   259      {
   260        path = "/data1"
   261        size_gb = 100
   262        type = "partitioned"
   263      }
   264  
   265  }
   266  `