2015-07-05 18:39:01 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-05 22:47:10 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-07-05 18:39:01 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2017-01-13 17:03:28 +01:00
|
|
|
"strconv"
|
2015-07-05 18:39:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccContainerCluster_basic(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckContainerClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccContainerCluster_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckContainerClusterExists(
|
|
|
|
"google_container_cluster.primary"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-04 07:14:39 +01:00
|
|
|
func TestAccContainerCluster_withAdditionalZones(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckContainerClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccContainerCluster_withAdditionalZones,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckContainerClusterExists(
|
|
|
|
"google_container_cluster.with_additional_zones"),
|
2017-01-13 17:03:28 +01:00
|
|
|
testAccCheckContainerClusterAdditionalZonesExist(
|
2017-02-07 02:21:34 +01:00
|
|
|
"google_container_cluster.with_additional_zones", 2),
|
2017-01-04 07:14:39 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-18 14:50:46 +01:00
|
|
|
func TestAccContainerCluster_withVersion(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckContainerClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccContainerCluster_withVersion,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckContainerClusterExists(
|
|
|
|
"google_container_cluster.with_version"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-20 22:40:38 +02:00
|
|
|
func TestAccContainerCluster_withNodeConfig(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckContainerClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccContainerCluster_withNodeConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckContainerClusterExists(
|
|
|
|
"google_container_cluster.with_node_config"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-15 00:50:24 +01:00
|
|
|
func TestAccContainerCluster_withNodeConfigScopeAlias(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckContainerClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccContainerCluster_withNodeConfigScopeAlias,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckContainerClusterExists(
|
|
|
|
"google_container_cluster.with_node_config_scope_alias"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-03 11:51:20 +02:00
|
|
|
func TestAccContainerCluster_network(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckContainerClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccContainerCluster_networkRef,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckContainerClusterExists(
|
|
|
|
"google_container_cluster.with_net_ref_by_url"),
|
|
|
|
testAccCheckContainerClusterExists(
|
|
|
|
"google_container_cluster.with_net_ref_by_name"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:39:01 +02:00
|
|
|
func testAccCheckContainerClusterDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "google_container_cluster" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes := rs.Primary.Attributes
|
|
|
|
_, err := config.clientContainer.Projects.Zones.Clusters.Get(
|
|
|
|
config.Project, attributes["zone"], attributes["name"]).Do()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Cluster still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckContainerClusterExists(n string) 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 ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
attributes := rs.Primary.Attributes
|
|
|
|
found, err := config.clientContainer.Projects.Zones.Clusters.Get(
|
|
|
|
config.Project, attributes["zone"], attributes["name"]).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.Name != attributes["name"] {
|
|
|
|
return fmt.Errorf("Cluster not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 02:21:34 +01:00
|
|
|
func testAccCheckContainerClusterAdditionalZonesExist(n string, num int) resource.TestCheckFunc {
|
2017-01-13 17:03:28 +01:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2017-02-07 02:21:34 +01:00
|
|
|
additionalZonesSize, err := strconv.Atoi(rs.Primary.Attributes["additional_zones.#"])
|
|
|
|
if err != nil {
|
2017-01-13 17:03:28 +01:00
|
|
|
return err
|
|
|
|
}
|
2017-02-07 02:21:34 +01:00
|
|
|
if additionalZonesSize != num {
|
|
|
|
return fmt.Errorf("number of additional zones did not match %d, was %d", num, additionalZonesSize)
|
2017-01-13 17:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 22:47:10 +01:00
|
|
|
var testAccContainerCluster_basic = fmt.Sprintf(`
|
2015-07-05 18:39:01 +02:00
|
|
|
resource "google_container_cluster" "primary" {
|
2016-01-05 22:47:10 +01:00
|
|
|
name = "cluster-test-%s"
|
2015-07-05 18:39:01 +02:00
|
|
|
zone = "us-central1-a"
|
|
|
|
initial_node_count = 3
|
|
|
|
|
|
|
|
master_auth {
|
|
|
|
username = "mr.yoda"
|
|
|
|
password = "adoy.rm"
|
|
|
|
}
|
2016-01-05 22:47:10 +01:00
|
|
|
}`, acctest.RandString(10))
|
2015-08-20 22:40:38 +02:00
|
|
|
|
2017-01-04 07:14:39 +01:00
|
|
|
var testAccContainerCluster_withAdditionalZones = fmt.Sprintf(`
|
|
|
|
resource "google_container_cluster" "with_additional_zones" {
|
|
|
|
name = "cluster-test-%s"
|
|
|
|
zone = "us-central1-a"
|
|
|
|
initial_node_count = 1
|
|
|
|
|
|
|
|
additional_zones = [
|
|
|
|
"us-central1-b",
|
|
|
|
"us-central1-c"
|
|
|
|
]
|
|
|
|
|
|
|
|
master_auth {
|
|
|
|
username = "mr.yoda"
|
|
|
|
password = "adoy.rm"
|
|
|
|
}
|
|
|
|
}`, acctest.RandString(10))
|
|
|
|
|
2016-12-18 14:50:46 +01:00
|
|
|
var testAccContainerCluster_withVersion = fmt.Sprintf(`
|
|
|
|
resource "google_container_cluster" "with_version" {
|
|
|
|
name = "cluster-test-%s"
|
|
|
|
zone = "us-central1-a"
|
|
|
|
node_version = "1.4.7"
|
|
|
|
initial_node_count = 1
|
|
|
|
|
|
|
|
master_auth {
|
|
|
|
username = "mr.yoda"
|
|
|
|
password = "adoy.rm"
|
|
|
|
}
|
|
|
|
}`, acctest.RandString(10))
|
|
|
|
|
2016-01-05 22:47:10 +01:00
|
|
|
var testAccContainerCluster_withNodeConfig = fmt.Sprintf(`
|
2015-08-20 22:40:38 +02:00
|
|
|
resource "google_container_cluster" "with_node_config" {
|
2016-01-05 22:47:10 +01:00
|
|
|
name = "cluster-test-%s"
|
2015-08-20 22:40:38 +02:00
|
|
|
zone = "us-central1-f"
|
|
|
|
initial_node_count = 1
|
|
|
|
|
|
|
|
master_auth {
|
|
|
|
username = "mr.yoda"
|
|
|
|
password = "adoy.rm"
|
|
|
|
}
|
|
|
|
|
|
|
|
node_config {
|
2015-10-15 05:16:58 +02:00
|
|
|
machine_type = "g1-small"
|
2015-08-20 22:40:38 +02:00
|
|
|
disk_size_gb = 15
|
|
|
|
oauth_scopes = [
|
|
|
|
"https://www.googleapis.com/auth/compute",
|
|
|
|
"https://www.googleapis.com/auth/devstorage.read_only",
|
|
|
|
"https://www.googleapis.com/auth/logging.write",
|
|
|
|
"https://www.googleapis.com/auth/monitoring"
|
|
|
|
]
|
|
|
|
}
|
2016-01-05 22:47:10 +01:00
|
|
|
}`, acctest.RandString(10))
|
2016-09-03 11:51:20 +02:00
|
|
|
|
2016-11-15 00:50:24 +01:00
|
|
|
var testAccContainerCluster_withNodeConfigScopeAlias = fmt.Sprintf(`
|
|
|
|
resource "google_container_cluster" "with_node_config_scope_alias" {
|
|
|
|
name = "cluster-test-%s"
|
|
|
|
zone = "us-central1-f"
|
|
|
|
initial_node_count = 1
|
|
|
|
|
|
|
|
master_auth {
|
|
|
|
username = "mr.yoda"
|
|
|
|
password = "adoy.rm"
|
|
|
|
}
|
|
|
|
|
|
|
|
node_config {
|
|
|
|
machine_type = "g1-small"
|
|
|
|
disk_size_gb = 15
|
|
|
|
oauth_scopes = [ "compute-rw", "storage-ro", "logging-write", "monitoring" ]
|
|
|
|
}
|
|
|
|
}`, acctest.RandString(10))
|
|
|
|
|
2016-09-03 11:51:20 +02:00
|
|
|
var testAccContainerCluster_networkRef = fmt.Sprintf(`
|
|
|
|
resource "google_compute_network" "container_network" {
|
|
|
|
name = "container-net-%s"
|
|
|
|
auto_create_subnetworks = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_container_cluster" "with_net_ref_by_url" {
|
|
|
|
name = "cluster-test-%s"
|
|
|
|
zone = "us-central1-a"
|
|
|
|
initial_node_count = 1
|
|
|
|
|
|
|
|
master_auth {
|
|
|
|
username = "mr.yoda"
|
|
|
|
password = "adoy.rm"
|
|
|
|
}
|
|
|
|
|
|
|
|
network = "${google_compute_network.container_network.self_link}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_container_cluster" "with_net_ref_by_name" {
|
|
|
|
name = "cluster-test-%s"
|
|
|
|
zone = "us-central1-a"
|
|
|
|
initial_node_count = 1
|
|
|
|
|
|
|
|
master_auth {
|
|
|
|
username = "mr.yoda"
|
|
|
|
password = "adoy.rm"
|
|
|
|
}
|
|
|
|
|
|
|
|
network = "${google_compute_network.container_network.name}"
|
|
|
|
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
|