Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tests for Reordering Network Interfaces #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions cloudscale/resource_cloudscale_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,49 @@ func TestAccCloudscaleNetwork_ServerWithPublicAndPrivateWithoutAddress(t *testin
})
}

func TestAccCloudscaleSubnet_ServerReorderNetworks(t *testing.T) {
count := 2
_ = make([]cloudscale.Network, count, count)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

var server cloudscale.Server

rInt1 := acctest.RandInt()
rInt2 := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudscaleNetworkDestroy,
Steps: []resource.TestStep{
{
Config: twoNetworkConfig(rInt1, rInt2, 0, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudscaleServerExists("cloudscale_server.web-worker01", &server),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.#", "2"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.type", "private"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.network_name", fmt.Sprintf("terraform-%d-0", rInt1)),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.addresses.#", "1"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.type", "private"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.network_name", fmt.Sprintf("terraform-%d-1", rInt1)),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.addresses.#", "1"),
),
},
{
Config: twoNetworkConfig(rInt1, rInt2, 1, 0),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudscaleServerExists("cloudscale_server.web-worker01", &server),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.#", "2"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.type", "private"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.network_name", fmt.Sprintf("terraform-%d-1", rInt1)),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.addresses.#", "1"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.type", "private"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.network_name", fmt.Sprintf("terraform-%d-0", rInt1)),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.addresses.#", "1"),
),
},
},
})
}

func testAccCheckCloudscaleNetworkSubnetCount(n string, network *cloudscale.Network, expectedCount int) resource.TestCheckFunc {
return func(s *terraform.State) error {
if actualSubnetCount := len(network.Subnets); actualSubnetCount != expectedCount {
Expand Down Expand Up @@ -504,3 +547,32 @@ resource "cloudscale_server" "basic" {
}`
return fmt.Sprintf(template, rInt, DefaultImageSlug)
}

func twoNetworkConfig(rInt1 int, rInt2 int, networkIndex1 int, networkIndex2 int) string {
template := `
resource "cloudscale_network" "multi-net" {
count = 2
name = "terraform-%d-${count.index}"
auto_create_ipv4_subnet = true
}

resource "cloudscale_server" "web-worker01" {
name = "terraform-%d"
flavor_slug = "flex-4"
image_slug = "debian-9"
volume_size_gb = 50
interfaces {
type = "private"
network_uuid = cloudscale_network.multi-net[%d].id
}
interfaces {
type = "private"
network_uuid = cloudscale_network.multi-net[%d].id
}
ssh_keys = [
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIL2jzgla23DfRVLQr3KT20QQYovqCCN3clHrjm2ZuQFW [email protected]"
]
}`

return fmt.Sprintf(template, rInt1, rInt2, networkIndex1, networkIndex2)
}
107 changes: 101 additions & 6 deletions cloudscale/resource_cloudscale_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func TestAccCloudscaleSubnet_ServerAndMultipleSubnets(t *testing.T) {
CheckDestroy: testAccCheckCloudscaleNetworkDestroy,
Steps: []resource.TestStep{
{
Config: multipleSubnetConfig(rInt1, rInt2, 0, 0),
Config: networkAndSubnetWithAddressConfig(rInt1, rInt2, 0, 0),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudscaleNetworkExists("cloudscale_network.multi-net.0", &networks[0]),
testAccCheckCloudscaleNetworkExists("cloudscale_network.multi-net.1", &networks[1]),
Expand All @@ -213,7 +213,7 @@ func TestAccCloudscaleSubnet_ServerAndMultipleSubnets(t *testing.T) {
),
},
{
Config: multipleSubnetConfig(rInt1, rInt2, 1, 1),
Config: networkAndSubnetWithAddressConfig(rInt1, rInt2, 1, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudscaleNetworkExists("cloudscale_network.multi-net.0", &networks[0]),
testAccCheckCloudscaleNetworkExists("cloudscale_network.multi-net.1", &networks[1]),
Expand All @@ -233,7 +233,7 @@ func TestAccCloudscaleSubnet_ServerAndMultipleSubnets(t *testing.T) {
),
},
{
Config: multipleSubnetConfig(rInt1, rInt2, 0, -100),
Config: networkAndSubnetWithAddressConfig(rInt1, rInt2, 0, -100),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudscaleNetworkExists("cloudscale_network.multi-net.0", &networks[0]),
testAccCheckCloudscaleNetworkExists("cloudscale_network.multi-net.1", &networks[1]),
Expand All @@ -253,7 +253,7 @@ func TestAccCloudscaleSubnet_ServerAndMultipleSubnets(t *testing.T) {
),
},
{
Config: multipleSubnetConfig(rInt1, rInt2, 0, 0),
Config: networkAndSubnetWithAddressConfig(rInt1, rInt2, 0, 0),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudscaleNetworkExists("cloudscale_network.multi-net.0", &networks[0]),
testAccCheckCloudscaleNetworkExists("cloudscale_network.multi-net.1", &networks[1]),
Expand All @@ -273,7 +273,7 @@ func TestAccCloudscaleSubnet_ServerAndMultipleSubnets(t *testing.T) {
),
},
{
Config: multipleSubnetConfig(rInt1, rInt2, -100, 1),
Config: networkAndSubnetWithAddressConfig(rInt1, rInt2, -100, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudscaleNetworkExists("cloudscale_network.multi-net.0", &networks[0]),
testAccCheckCloudscaleNetworkExists("cloudscale_network.multi-net.1", &networks[1]),
Expand All @@ -296,6 +296,58 @@ func TestAccCloudscaleSubnet_ServerAndMultipleSubnets(t *testing.T) {
})
}

func TestAccCloudscaleSubnet_ServerReorderSubnet(t *testing.T) {
count := 2
_ = make([]cloudscale.Network, count, count)
_ = make([]cloudscale.Subnet, count, count)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

var server cloudscale.Server

rInt1 := acctest.RandInt()
rInt2 := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudscaleNetworkDestroy,
Steps: []resource.TestStep{
{
Config: twoSubnetWithAddressConfig(rInt1, rInt2, 0, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudscaleServerExists("cloudscale_server.web-worker01", &server),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.#", "2"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.type", "private"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.network_name", fmt.Sprintf("terraform-%d-0", rInt1)),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.addresses.#", "1"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.addresses.0.address", "192.168.0.124"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.addresses.0.subnet_cidr", "192.168.0.0/24"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.type", "private"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.network_name", fmt.Sprintf("terraform-%d-1", rInt1)),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.addresses.#", "1"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.addresses.0.address", "192.168.1.124"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.addresses.0.subnet_cidr", "192.168.1.0/24"),
),
},
{
Config: twoSubnetWithAddressConfig(rInt1, rInt2, 1, 0),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudscaleServerExists("cloudscale_server.web-worker01", &server),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.#", "2"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.type", "private"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.network_name", fmt.Sprintf("terraform-%d-1", rInt1)),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.addresses.#", "1"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.addresses.0.address", "192.168.1.124"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.0.addresses.0.subnet_cidr", "192.168.1.0/24"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.type", "private"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.network_name", fmt.Sprintf("terraform-%d-0", rInt1)),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.addresses.#", "1"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.addresses.0.address", "192.168.0.124"),
resource.TestCheckResourceAttr("cloudscale_server.web-worker01", "interfaces.1.addresses.0.subnet_cidr", "192.168.0.0/24"),
),
},
},
})
}

func testAccCheckCloudscaleSubnetOnNetwork(subnet *cloudscale.Subnet, network *cloudscale.Network) resource.TestCheckFunc {
return func(s *terraform.State) error {

Expand Down Expand Up @@ -436,7 +488,7 @@ resource "cloudscale_server" "basic" {
return fmt.Sprintf(template, rInt, DefaultImageSlug, fixedAddressLine)
}

func multipleSubnetConfig(rInt1 int, rInt2 int, networkIndex int, subnetIndex int) string {
func networkAndSubnetWithAddressConfig(rInt1 int, rInt2 int, networkIndex int, subnetIndex int) string {
template := `
resource "cloudscale_network" "multi-net" {
count = 2
Expand Down Expand Up @@ -483,3 +535,46 @@ resource "cloudscale_server" "web-worker01" {

return fmt.Sprintf(template, rInt1, rInt2, networkTemplate, addressTemplate)
}

func twoSubnetWithAddressConfig(rInt1 int, rInt2 int, subnetIndex1 int, subnetIndex2 int) string {
template := `
resource "cloudscale_network" "multi-net" {
count = 2
name = "terraform-%d-${count.index}"
auto_create_ipv4_subnet = false
}

resource "cloudscale_subnet" "multi-subnet" {
count = 2
cidr = "192.168.${count.index}.0/24"
network_uuid = cloudscale_network.multi-net[count.index].id
}

resource "cloudscale_server" "web-worker01" {
name = "terraform-%d"
flavor_slug = "flex-4"
image_slug = "debian-9"
volume_size_gb = 50
interfaces {
type = "private"
%s
}
interfaces {
type = "private"
%s
}
ssh_keys = [
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIL2jzgla23DfRVLQr3KT20QQYovqCCN3clHrjm2ZuQFW [email protected]"
]
}`
addressTemplate := `
addresses {
address = "192.168.%d.124"
subnet_uuid = cloudscale_subnet.multi-subnet[%d].id
}
`
address1 := fmt.Sprintf(addressTemplate, subnetIndex1, subnetIndex1)
address2 := fmt.Sprintf(addressTemplate, subnetIndex2, subnetIndex2)

return fmt.Sprintf(template, rInt1, rInt2, address1, address2)
}