2017-02-13 20:11:30 +01:00
|
|
|
package arukas
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
API "github.com/arukasio/cli"
|
2017-03-17 06:47:24 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2017-02-13 20:11:30 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccArukasContainer_Basic(t *testing.T) {
|
|
|
|
var container API.Container
|
2017-03-17 06:47:24 +01:00
|
|
|
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
|
|
|
|
name := fmt.Sprintf("terraform_acc_test_%s", randString)
|
|
|
|
endpoint := fmt.Sprintf("terraform-acc-test-endpoint-%s", randString)
|
|
|
|
|
2017-02-13 20:11:30 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckArukasContainerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-03-17 06:47:24 +01:00
|
|
|
Config: testAccCheckArukasContainerConfig_basic(randString),
|
2017-02-13 20:11:30 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckArukasContainerExists("arukas_container.foobar", &container),
|
|
|
|
resource.TestCheckResourceAttr(
|
2017-03-17 06:47:24 +01:00
|
|
|
"arukas_container.foobar", "name", name),
|
2017-02-13 20:11:30 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "image", "nginx:latest"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "instances", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "memory", "256"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2017-03-17 06:47:24 +01:00
|
|
|
"arukas_container.foobar", "endpoint", endpoint),
|
2017-02-13 20:11:30 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.0.protocol", "tcp"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.0.number", "80"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "environments.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "environments.0.key", "key"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "environments.0.value", "value"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "port_mappings.#", "1"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccArukasContainer_Update(t *testing.T) {
|
|
|
|
var container API.Container
|
2017-03-17 06:47:24 +01:00
|
|
|
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
|
|
|
|
name := fmt.Sprintf("terraform_acc_test_%s", randString)
|
|
|
|
updatedName := fmt.Sprintf("terraform_acc_test_update_%s", randString)
|
|
|
|
endpoint := fmt.Sprintf("terraform-acc-test-endpoint-%s", randString)
|
|
|
|
updatedEndpoint := fmt.Sprintf("terraform-acc-test-endpoint-update-%s", randString)
|
|
|
|
|
2017-02-13 20:11:30 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckArukasContainerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-03-17 06:47:24 +01:00
|
|
|
Config: testAccCheckArukasContainerConfig_basic(randString),
|
2017-02-13 20:11:30 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckArukasContainerExists("arukas_container.foobar", &container),
|
|
|
|
resource.TestCheckResourceAttr(
|
2017-03-17 06:47:24 +01:00
|
|
|
"arukas_container.foobar", "name", name),
|
2017-02-13 20:11:30 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "image", "nginx:latest"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "instances", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "memory", "256"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2017-03-17 06:47:24 +01:00
|
|
|
"arukas_container.foobar", "endpoint", endpoint),
|
2017-02-13 20:11:30 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.0.protocol", "tcp"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.0.number", "80"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "environments.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "environments.0.key", "key"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "environments.0.value", "value"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "port_mappings.#", "1"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2017-03-17 06:47:24 +01:00
|
|
|
Config: testAccCheckArukasContainerConfig_update(randString),
|
2017-02-13 20:11:30 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckArukasContainerExists("arukas_container.foobar", &container),
|
|
|
|
resource.TestCheckResourceAttr(
|
2017-03-17 06:47:24 +01:00
|
|
|
"arukas_container.foobar", "name", updatedName),
|
2017-02-13 20:11:30 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "image", "nginx:latest"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "instances", "2"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "memory", "512"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2017-03-17 06:47:24 +01:00
|
|
|
"arukas_container.foobar", "endpoint", updatedEndpoint),
|
2017-02-13 20:11:30 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.#", "2"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.0.protocol", "tcp"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.0.number", "80"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.1.protocol", "tcp"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.1.number", "443"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "environments.#", "2"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "environments.0.key", "key"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "environments.0.value", "value"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "environments.1.key", "key_upd"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "environments.1.value", "value_upd"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "port_mappings.#", "4"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccArukasContainer_Minimum(t *testing.T) {
|
|
|
|
var container API.Container
|
2017-03-17 06:47:24 +01:00
|
|
|
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
|
|
|
|
name := fmt.Sprintf("terraform_acc_test_minimum_%s", randString)
|
|
|
|
|
2017-02-13 20:11:30 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckArukasContainerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-03-17 06:47:24 +01:00
|
|
|
Config: testAccCheckArukasContainerConfig_minimum(randString),
|
2017-02-13 20:11:30 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckArukasContainerExists("arukas_container.foobar", &container),
|
|
|
|
resource.TestCheckResourceAttr(
|
2017-03-17 06:47:24 +01:00
|
|
|
"arukas_container.foobar", "name", name),
|
2017-02-13 20:11:30 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "image", "nginx:latest"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "instances", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "memory", "256"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.0.protocol", "tcp"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "ports.0.number", "80"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"arukas_container.foobar", "port_mappings.#", "1"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccArukasContainer_Import(t *testing.T) {
|
|
|
|
resourceName := "arukas_container.foobar"
|
2017-03-17 06:47:24 +01:00
|
|
|
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
|
|
|
|
|
2017-02-13 20:11:30 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckArukasContainerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-03-17 06:47:24 +01:00
|
|
|
Config: testAccCheckArukasContainerConfig_basic(randString),
|
2017-02-13 20:11:30 +01:00
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: resourceName,
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckArukasContainerExists(n string, container *API.Container) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No Container ID is set")
|
|
|
|
}
|
|
|
|
client := testAccProvider.Meta().(*ArukasClient)
|
|
|
|
var foundContainer API.Container
|
|
|
|
err := client.Get(&foundContainer, fmt.Sprintf("/containers/%s", rs.Primary.ID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundContainer.ID != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Container not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*container = foundContainer
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckArukasContainerDestroy(s *terraform.State) error {
|
|
|
|
client := testAccProvider.Meta().(*ArukasClient)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "arukas_container" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.Get(nil, fmt.Sprintf("/containers/%s", rs.Primary.ID))
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Note still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:47:24 +01:00
|
|
|
func testAccCheckArukasContainerConfig_basic(randString string) string {
|
|
|
|
return fmt.Sprintf(`
|
2017-02-13 20:11:30 +01:00
|
|
|
resource "arukas_container" "foobar" {
|
2017-03-17 06:47:24 +01:00
|
|
|
name = "terraform_acc_test_%s"
|
2017-02-13 20:11:30 +01:00
|
|
|
image = "nginx:latest"
|
|
|
|
instances = 1
|
|
|
|
memory = 256
|
2017-03-17 06:47:24 +01:00
|
|
|
endpoint = "terraform-acc-test-endpoint-%s"
|
2017-02-13 20:11:30 +01:00
|
|
|
ports = {
|
|
|
|
protocol = "tcp"
|
|
|
|
number = "80"
|
|
|
|
}
|
|
|
|
environments {
|
|
|
|
key = "key"
|
|
|
|
value = "value"
|
|
|
|
}
|
2017-03-17 06:47:24 +01:00
|
|
|
}`, randString, randString)
|
|
|
|
}
|
2017-02-13 20:11:30 +01:00
|
|
|
|
2017-03-17 06:47:24 +01:00
|
|
|
func testAccCheckArukasContainerConfig_update(randString string) string {
|
|
|
|
return fmt.Sprintf(`
|
2017-02-13 20:11:30 +01:00
|
|
|
resource "arukas_container" "foobar" {
|
2017-03-17 06:47:24 +01:00
|
|
|
name = "terraform_acc_test_update_%s"
|
2017-02-13 20:11:30 +01:00
|
|
|
image = "nginx:latest"
|
|
|
|
instances = 2
|
|
|
|
memory = 512
|
2017-03-17 06:47:24 +01:00
|
|
|
endpoint = "terraform-acc-test-endpoint-update-%s"
|
2017-02-13 20:11:30 +01:00
|
|
|
ports = {
|
|
|
|
protocol = "tcp"
|
|
|
|
number = "80"
|
|
|
|
}
|
|
|
|
ports = {
|
|
|
|
protocol = "tcp"
|
|
|
|
number = "443"
|
|
|
|
}
|
|
|
|
environments {
|
|
|
|
key = "key"
|
|
|
|
value = "value"
|
|
|
|
}
|
|
|
|
environments {
|
|
|
|
key = "key_upd"
|
|
|
|
value = "value_upd"
|
|
|
|
}
|
2017-03-17 06:47:24 +01:00
|
|
|
}`, randString, randString)
|
|
|
|
}
|
2017-02-13 20:11:30 +01:00
|
|
|
|
2017-03-17 06:47:24 +01:00
|
|
|
func testAccCheckArukasContainerConfig_minimum(randString string) string {
|
|
|
|
return fmt.Sprintf(`
|
2017-02-13 20:11:30 +01:00
|
|
|
resource "arukas_container" "foobar" {
|
2017-03-17 06:47:24 +01:00
|
|
|
name = "terraform_acc_test_minimum_%s"
|
2017-02-13 20:11:30 +01:00
|
|
|
image = "nginx:latest"
|
|
|
|
ports = {
|
|
|
|
number = "80"
|
|
|
|
}
|
2017-03-17 06:47:24 +01:00
|
|
|
}`, randString)
|
|
|
|
}
|