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

     1  package arukas
     2  
     3  import (
     4  	"fmt"
     5  	API "github.com/arukasio/cli"
     6  	"github.com/hashicorp/terraform/helper/acctest"
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"testing"
    10  )
    11  
    12  func TestAccArukasContainer_Basic(t *testing.T) {
    13  	var container API.Container
    14  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
    15  	name := fmt.Sprintf("terraform_acc_test_%s", randString)
    16  	endpoint := fmt.Sprintf("terraform-acc-test-endpoint-%s", randString)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckArukasContainerDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccCheckArukasContainerConfig_basic(randString),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckArukasContainerExists("arukas_container.foobar", &container),
    27  					resource.TestCheckResourceAttr(
    28  						"arukas_container.foobar", "name", name),
    29  					resource.TestCheckResourceAttr(
    30  						"arukas_container.foobar", "image", "nginx:latest"),
    31  					resource.TestCheckResourceAttr(
    32  						"arukas_container.foobar", "instances", "1"),
    33  					resource.TestCheckResourceAttr(
    34  						"arukas_container.foobar", "memory", "256"),
    35  					resource.TestCheckResourceAttr(
    36  						"arukas_container.foobar", "endpoint", endpoint),
    37  					resource.TestCheckResourceAttr(
    38  						"arukas_container.foobar", "ports.#", "1"),
    39  					resource.TestCheckResourceAttr(
    40  						"arukas_container.foobar", "ports.0.protocol", "tcp"),
    41  					resource.TestCheckResourceAttr(
    42  						"arukas_container.foobar", "ports.0.number", "80"),
    43  					resource.TestCheckResourceAttr(
    44  						"arukas_container.foobar", "environments.#", "1"),
    45  					resource.TestCheckResourceAttr(
    46  						"arukas_container.foobar", "environments.0.key", "key"),
    47  					resource.TestCheckResourceAttr(
    48  						"arukas_container.foobar", "environments.0.value", "value"),
    49  					resource.TestCheckResourceAttr(
    50  						"arukas_container.foobar", "port_mappings.#", "1"),
    51  				),
    52  			},
    53  		},
    54  	})
    55  }
    56  
    57  func TestAccArukasContainer_Update(t *testing.T) {
    58  	var container API.Container
    59  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
    60  	name := fmt.Sprintf("terraform_acc_test_%s", randString)
    61  	updatedName := fmt.Sprintf("terraform_acc_test_update_%s", randString)
    62  	endpoint := fmt.Sprintf("terraform-acc-test-endpoint-%s", randString)
    63  	updatedEndpoint := fmt.Sprintf("terraform-acc-test-endpoint-update-%s", randString)
    64  
    65  	resource.Test(t, resource.TestCase{
    66  		PreCheck:     func() { testAccPreCheck(t) },
    67  		Providers:    testAccProviders,
    68  		CheckDestroy: testAccCheckArukasContainerDestroy,
    69  		Steps: []resource.TestStep{
    70  			resource.TestStep{
    71  				Config: testAccCheckArukasContainerConfig_basic(randString),
    72  				Check: resource.ComposeTestCheckFunc(
    73  					testAccCheckArukasContainerExists("arukas_container.foobar", &container),
    74  					resource.TestCheckResourceAttr(
    75  						"arukas_container.foobar", "name", name),
    76  					resource.TestCheckResourceAttr(
    77  						"arukas_container.foobar", "image", "nginx:latest"),
    78  					resource.TestCheckResourceAttr(
    79  						"arukas_container.foobar", "instances", "1"),
    80  					resource.TestCheckResourceAttr(
    81  						"arukas_container.foobar", "memory", "256"),
    82  					resource.TestCheckResourceAttr(
    83  						"arukas_container.foobar", "endpoint", endpoint),
    84  					resource.TestCheckResourceAttr(
    85  						"arukas_container.foobar", "ports.#", "1"),
    86  					resource.TestCheckResourceAttr(
    87  						"arukas_container.foobar", "ports.0.protocol", "tcp"),
    88  					resource.TestCheckResourceAttr(
    89  						"arukas_container.foobar", "ports.0.number", "80"),
    90  					resource.TestCheckResourceAttr(
    91  						"arukas_container.foobar", "environments.#", "1"),
    92  					resource.TestCheckResourceAttr(
    93  						"arukas_container.foobar", "environments.0.key", "key"),
    94  					resource.TestCheckResourceAttr(
    95  						"arukas_container.foobar", "environments.0.value", "value"),
    96  					resource.TestCheckResourceAttr(
    97  						"arukas_container.foobar", "port_mappings.#", "1"),
    98  				),
    99  			},
   100  			resource.TestStep{
   101  				Config: testAccCheckArukasContainerConfig_update(randString),
   102  				Check: resource.ComposeTestCheckFunc(
   103  					testAccCheckArukasContainerExists("arukas_container.foobar", &container),
   104  					resource.TestCheckResourceAttr(
   105  						"arukas_container.foobar", "name", updatedName),
   106  					resource.TestCheckResourceAttr(
   107  						"arukas_container.foobar", "image", "nginx:latest"),
   108  					resource.TestCheckResourceAttr(
   109  						"arukas_container.foobar", "instances", "2"),
   110  					resource.TestCheckResourceAttr(
   111  						"arukas_container.foobar", "memory", "512"),
   112  					resource.TestCheckResourceAttr(
   113  						"arukas_container.foobar", "endpoint", updatedEndpoint),
   114  					resource.TestCheckResourceAttr(
   115  						"arukas_container.foobar", "ports.#", "2"),
   116  					resource.TestCheckResourceAttr(
   117  						"arukas_container.foobar", "ports.0.protocol", "tcp"),
   118  					resource.TestCheckResourceAttr(
   119  						"arukas_container.foobar", "ports.0.number", "80"),
   120  					resource.TestCheckResourceAttr(
   121  						"arukas_container.foobar", "ports.1.protocol", "tcp"),
   122  					resource.TestCheckResourceAttr(
   123  						"arukas_container.foobar", "ports.1.number", "443"),
   124  					resource.TestCheckResourceAttr(
   125  						"arukas_container.foobar", "environments.#", "2"),
   126  					resource.TestCheckResourceAttr(
   127  						"arukas_container.foobar", "environments.0.key", "key"),
   128  					resource.TestCheckResourceAttr(
   129  						"arukas_container.foobar", "environments.0.value", "value"),
   130  					resource.TestCheckResourceAttr(
   131  						"arukas_container.foobar", "environments.1.key", "key_upd"),
   132  					resource.TestCheckResourceAttr(
   133  						"arukas_container.foobar", "environments.1.value", "value_upd"),
   134  					resource.TestCheckResourceAttr(
   135  						"arukas_container.foobar", "port_mappings.#", "4"),
   136  				),
   137  			},
   138  		},
   139  	})
   140  }
   141  
   142  func TestAccArukasContainer_Minimum(t *testing.T) {
   143  	var container API.Container
   144  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
   145  	name := fmt.Sprintf("terraform_acc_test_minimum_%s", randString)
   146  
   147  	resource.Test(t, resource.TestCase{
   148  		PreCheck:     func() { testAccPreCheck(t) },
   149  		Providers:    testAccProviders,
   150  		CheckDestroy: testAccCheckArukasContainerDestroy,
   151  		Steps: []resource.TestStep{
   152  			resource.TestStep{
   153  				Config: testAccCheckArukasContainerConfig_minimum(randString),
   154  				Check: resource.ComposeTestCheckFunc(
   155  					testAccCheckArukasContainerExists("arukas_container.foobar", &container),
   156  					resource.TestCheckResourceAttr(
   157  						"arukas_container.foobar", "name", name),
   158  					resource.TestCheckResourceAttr(
   159  						"arukas_container.foobar", "image", "nginx:latest"),
   160  					resource.TestCheckResourceAttr(
   161  						"arukas_container.foobar", "instances", "1"),
   162  					resource.TestCheckResourceAttr(
   163  						"arukas_container.foobar", "memory", "256"),
   164  					resource.TestCheckResourceAttr(
   165  						"arukas_container.foobar", "ports.#", "1"),
   166  					resource.TestCheckResourceAttr(
   167  						"arukas_container.foobar", "ports.0.protocol", "tcp"),
   168  					resource.TestCheckResourceAttr(
   169  						"arukas_container.foobar", "ports.0.number", "80"),
   170  					resource.TestCheckResourceAttr(
   171  						"arukas_container.foobar", "port_mappings.#", "1"),
   172  				),
   173  			},
   174  		},
   175  	})
   176  }
   177  
   178  func TestAccArukasContainer_Import(t *testing.T) {
   179  	resourceName := "arukas_container.foobar"
   180  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
   181  
   182  	resource.Test(t, resource.TestCase{
   183  		PreCheck:     func() { testAccPreCheck(t) },
   184  		Providers:    testAccProviders,
   185  		CheckDestroy: testAccCheckArukasContainerDestroy,
   186  		Steps: []resource.TestStep{
   187  			resource.TestStep{
   188  				Config: testAccCheckArukasContainerConfig_basic(randString),
   189  			},
   190  			resource.TestStep{
   191  				ResourceName:      resourceName,
   192  				ImportState:       true,
   193  				ImportStateVerify: true,
   194  			},
   195  		},
   196  	})
   197  }
   198  
   199  func testAccCheckArukasContainerExists(n string, container *API.Container) resource.TestCheckFunc {
   200  	return func(s *terraform.State) error {
   201  		rs, ok := s.RootModule().Resources[n]
   202  
   203  		if !ok {
   204  			return fmt.Errorf("Not found: %s", n)
   205  		}
   206  
   207  		if rs.Primary.ID == "" {
   208  			return fmt.Errorf("No Container ID is set")
   209  		}
   210  		client := testAccProvider.Meta().(*ArukasClient)
   211  		var foundContainer API.Container
   212  		err := client.Get(&foundContainer, fmt.Sprintf("/containers/%s", rs.Primary.ID))
   213  
   214  		if err != nil {
   215  			return err
   216  		}
   217  
   218  		if foundContainer.ID != rs.Primary.ID {
   219  			return fmt.Errorf("Container not found")
   220  		}
   221  
   222  		*container = foundContainer
   223  
   224  		return nil
   225  	}
   226  }
   227  
   228  func testAccCheckArukasContainerDestroy(s *terraform.State) error {
   229  	client := testAccProvider.Meta().(*ArukasClient)
   230  
   231  	for _, rs := range s.RootModule().Resources {
   232  		if rs.Type != "arukas_container" {
   233  			continue
   234  		}
   235  
   236  		err := client.Get(nil, fmt.Sprintf("/containers/%s", rs.Primary.ID))
   237  
   238  		if err == nil {
   239  			return fmt.Errorf("Note still exists")
   240  		}
   241  	}
   242  
   243  	return nil
   244  }
   245  
   246  func testAccCheckArukasContainerConfig_basic(randString string) string {
   247  	return fmt.Sprintf(`
   248  resource "arukas_container" "foobar" {
   249      name = "terraform_acc_test_%s"
   250      image = "nginx:latest"
   251      instances = 1
   252      memory = 256
   253      endpoint = "terraform-acc-test-endpoint-%s"
   254      ports = {
   255          protocol = "tcp"
   256          number = "80"
   257      }
   258      environments {
   259          key = "key"
   260          value = "value"
   261      }
   262  }`, randString, randString)
   263  }
   264  
   265  func testAccCheckArukasContainerConfig_update(randString string) string {
   266  	return fmt.Sprintf(`
   267  resource "arukas_container" "foobar" {
   268      name = "terraform_acc_test_update_%s"
   269      image = "nginx:latest"
   270      instances = 2
   271      memory = 512
   272      endpoint = "terraform-acc-test-endpoint-update-%s"
   273      ports = {
   274          protocol = "tcp"
   275          number = "80"
   276      }
   277      ports = {
   278          protocol = "tcp"
   279          number = "443"
   280      }
   281      environments {
   282          key = "key"
   283          value = "value"
   284      }
   285      environments {
   286          key = "key_upd"
   287          value = "value_upd"
   288      }
   289  }`, randString, randString)
   290  }
   291  
   292  func testAccCheckArukasContainerConfig_minimum(randString string) string {
   293  	return fmt.Sprintf(`
   294  resource "arukas_container" "foobar" {
   295      name = "terraform_acc_test_minimum_%s"
   296      image = "nginx:latest"
   297      ports = {
   298          number = "80"
   299      }
   300  }`, randString)
   301  }