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