Skip to content

Commit

Permalink
Changes to fix V0 state migration test
Browse files Browse the repository at this point in the history
Signed-off-by: Dom Del Nano <[email protected]>
  • Loading branch information
ddelnano committed Mar 18, 2024
1 parent 9dd2b9b commit 13f1b84
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 66 deletions.
80 changes: 79 additions & 1 deletion xoa/internal/state/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"log"
"net"

"github.com/vatesfr/terraform-provider-xenorchestra/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vatesfr/terraform-provider-xenorchestra/client"
)

var validVga = []string{
Expand Down Expand Up @@ -362,3 +362,81 @@ func suppressAttachedDiffWhenHalted(k, old, new string, d *schema.ResourceData)
log.Printf("[DEBUG] VM '%s' attribute has transitioned from '%s' to '%s' when PowerState '%s'. Suppress diff: %t", k, old, new, powerState, suppress)
return
}

// Used for tests that require the schema of the provider <= v0.28.1
func V1TestAccVmConfigWithWaitForIp(testPrefix, vmName, templateName, netName, poolId, srId string) string {
return fmt.Sprintf(`
resource "xenorchestra_cloud_config" "bar" {
name = "%s-vm-template-%s"
template = "template" # should this be templated?
}
data "xenorchestra_template" "template" {
name_label = "%s"
pool_id = "%s"
}
data "xenorchestra_network" "network" {
name_label = "%s"
pool_id = "%s"
}
resource "xenorchestra_vm" "bar" {
memory_max = 4295000000
cpus = 1
cloud_config = xenorchestra_cloud_config.bar.template
name_label = "%s"
name_description = "description"
template = data.xenorchestra_template.template.id
network {
network_id = data.xenorchestra_network.network.id
}
disk {
sr_id = "%s"
name_label = "disk 1"
size = 10001317888
}
wait_for_ip = true
}
`, testPrefix, vmName, templateName, poolId, netName, poolId, vmName, srId)
}

// terraform configuration that can be used to block changes that should not destroy a VM.
// While this doesn't integrate nicely with the sdk's test helpers (failure is vague), there
// are some cases were options are limited (testing pinned provider versions).
func TestAccV1VmConfigWithDeletionBlocked(testPrefix, vmName, templateName, netName, poolId, srId, waitForIp string) string {
return fmt.Sprintf(`
resource "xenorchestra_cloud_config" "bar" {
name = "%s-vm-template-%s"
template = "template" # should this be templated?
}
data "xenorchestra_template" "template" {
name_label = "%s"
pool_id = "%s"
}
data "xenorchestra_network" "network" {
name_label = "%s"
pool_id = "%s"
}
resource "xenorchestra_vm" "bar" {
memory_max = 4295000000
cpus = 1
cloud_config = xenorchestra_cloud_config.bar.template
name_label = "%s"
name_description = "description"
template = data.xenorchestra_template.template.id
network {
network_id = data.xenorchestra_network.network.id
}
disk {
sr_id = "%s"
name_label = "disk 1"
size = 10001317888
}
wait_for_ip = %s
blocked_operations = ["destroy"]
}
`, testPrefix, vmName, templateName, poolId, netName, poolId, vmName, srId, waitForIp)
}
72 changes: 7 additions & 65 deletions xoa/resource_xenorchestra_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/vatesfr/terraform-provider-xenorchestra/client"
"github.com/vatesfr/terraform-provider-xenorchestra/xoa/internal"
"github.com/vatesfr/terraform-provider-xenorchestra/xoa/internal/state"
)

func init() {
Expand Down Expand Up @@ -1760,7 +1761,7 @@ func TestAccXenorchestraVm_createWithV0StateMigration(t *testing.T) {
VersionConstraint: "0.24.2",
},
},
Config: testAccVmConfigWithWaitForIp(vmName, "false"),
Config: state.V1TestAccVmConfigWithWaitForIp(accTestPrefix, vmName, testTemplate.NameLabel, accDefaultNetwork.NameLabel, accTestPool.Id, accDefaultSr.Id),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
Expand All @@ -1774,7 +1775,7 @@ func TestAccXenorchestraVm_createWithV0StateMigration(t *testing.T) {
VersionConstraint: "0.24.2",
},
},
Config: testAccVmConfigWithDeletionBlocked(vmName, "false"),
Config: state.TestAccV1VmConfigWithDeletionBlocked(accTestPrefix, vmName, testTemplate.NameLabel, accDefaultNetwork.NameLabel, accTestPool.Id, accDefaultSr.Id, "false"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
Expand All @@ -1788,7 +1789,7 @@ func TestAccXenorchestraVm_createWithV0StateMigration(t *testing.T) {
VersionConstraint: "0.25.0",
},
},
Config: testAccVmConfigWithDeletionBlocked(vmName, "true"),
Config: state.TestAccV1VmConfigWithDeletionBlocked(accTestPrefix, vmName, testTemplate.NameLabel, accDefaultNetwork.NameLabel, accTestPool.Id, accDefaultSr.Id, "true"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
Expand All @@ -1805,7 +1806,7 @@ func TestAccXenorchestraVm_createWithV0StateMigration(t *testing.T) {
VersionConstraint: "0.25.1",
},
},
Config: testAccVmConfigWithDeletionBlocked(vmName, "true"),
Config: state.TestAccV1VmConfigWithDeletionBlocked(accTestPrefix, vmName, testTemplate.NameLabel, accDefaultNetwork.NameLabel, accTestPool.Id, accDefaultSr.Id, "true"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
Expand All @@ -1819,7 +1820,7 @@ func TestAccXenorchestraVm_createWithV0StateMigration(t *testing.T) {
VersionConstraint: "0.25.1",
},
},
Config: testAccVmConfigWithDeletionBlocked(vmName, "true"),
Config: state.TestAccV1VmConfigWithDeletionBlocked(accTestPrefix, vmName, testTemplate.NameLabel, accDefaultNetwork.NameLabel, accTestPool.Id, accDefaultSr.Id, "true"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
Expand All @@ -1835,7 +1836,7 @@ func TestAccXenorchestraVm_createWithV0StateMigration(t *testing.T) {
VersionConstraint: "0.25.1",
},
},
Config: testAccVmConfigWithWaitForIp(vmName, "0.0.0.0/0"),
Config: state.V1TestAccVmConfigWithWaitForIp(accTestPrefix, vmName, testTemplate.NameLabel, accDefaultNetwork.NameLabel, accTestPool.Id, accDefaultSr.Id),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
Expand Down Expand Up @@ -2069,65 +2070,6 @@ func testAccVmConfig(vmName string) string {
return testAccVmConfigWithWaitForIp(vmName, "false")
}

// terraform configuration that can be used to block changes that should not destroy a VM.
// While this doesn't integrate nicely with the sdk's test helpers (failure is vague), there
// are some cases were options are limited (testing pinned provider versions).
func testAccVmConfigWithDeletionBlocked(vmName, waitForIp string) string {
return testAccCloudConfigConfig(fmt.Sprintf("vm-template-%s", vmName), "template") + testAccTemplateConfig() + fmt.Sprintf(`
data "xenorchestra_network" "network" {
name_label = "%s"
pool_id = "%s"
}
resource "xenorchestra_vm" "bar" {
memory_max = 4295000000
cpus = 1
cloud_config = xenorchestra_cloud_config.bar.template
name_label = "%s"
name_description = "description"
template = data.xenorchestra_template.template.id
network {
network_id = data.xenorchestra_network.network.id
}
disk {
sr_id = "%s"
name_label = "disk 1"
size = 10001317888
}
wait_for_ip = %s
blocked_operations = ["destroy"]
}
`, accDefaultNetwork.NameLabel, accTestPool.Id, vmName, accDefaultSr.Id, waitForIp)
}

func testAccVmConfigWithWaitForIp(vmName, waitForIp string) string {
return testAccCloudConfigConfig(fmt.Sprintf("vm-template-%s", vmName), "template") + testAccTemplateConfig() + fmt.Sprintf(`
data "xenorchestra_network" "network" {
name_label = "%s"
pool_id = "%s"
}
resource "xenorchestra_vm" "bar" {
memory_max = 4295000000
cpus = 1
cloud_config = xenorchestra_cloud_config.bar.template
name_label = "%s"
name_description = "description"
template = data.xenorchestra_template.template.id
network {
network_id = data.xenorchestra_network.network.id
}
disk {
sr_id = "%s"
name_label = "disk 1"
size = 10001317888
}
}
`, accDefaultNetwork.NameLabel, accTestPool.Id, vmName, accDefaultSr.Id)
}

func testAccVmConfigWithWaitForIp(vmName, expectedIpCidr string) string {
return testAccCloudConfigConfig(fmt.Sprintf("vm-template-%s", vmName), "template") + testAccTemplateConfig() + fmt.Sprintf(`
data "xenorchestra_network" "network" {
Expand Down

0 comments on commit 13f1b84

Please sign in to comment.