Skip to content

Commit

Permalink
Use previous terraform releases for V0 to V1 state migration test.
Browse files Browse the repository at this point in the history
This prevents issues with future breaking changes from causing impact to
the test's implementation

Signed-off-by: Dom Del Nano <[email protected]>
  • Loading branch information
ddelnano committed Mar 18, 2024
1 parent e77ee5b commit 1d91c71
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 20 deletions.
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type XOClient interface {
GetObjectsWithTags(tags []string) ([]Object, error)
GetAllObjectsOfType(obj XoObject, response interface{}) error

Call(method string, params, result interface{}) error

CreateVm(vmReq Vm, d time.Duration) (*Vm, error)
GetVm(vmReq Vm) (*Vm, error)
GetVms(vm Vm) ([]Vm, error)
Expand Down
28 changes: 28 additions & 0 deletions client/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,19 @@ func FindOrCreateVmForTests(vm *Vm, poolId, srId, templateName, tag string) {
*vm = *vmRes
}

func checkBlockDestroyOperation(vm *Vm) bool {
fmt.Printf("Found VM with blocked_operations=%v", vm.BlockedOperations)

for k, _ := range vm.BlockedOperations {

if k == "destroy" {
return true
}

}
return false
}

func RemoveVmsWithNamePrefix(prefix string) func(string) error {
return func(_ string) error {
fmt.Println("[DEBUG] Running vm sweeper")
Expand All @@ -748,6 +761,21 @@ func RemoveVmsWithNamePrefix(prefix string) func(string) error {
}
for _, vm := range vmsMap {
if strings.HasPrefix(vm.NameLabel, prefix) {
if checkBlockDestroyOperation(&vm) {
var success bool
blockedOperations := map[string]interface{}{
"destroy": nil,
}
params := map[string]interface{}{
"id": vm.Id,
"blockedOperations": blockedOperations,
}
err := c.Call("vm.set", params, &success)

if err != nil {
log.Printf("error removing destroy block on vm `%s` during sweep: %s", vm.NameLabel, err)
}
}
fmt.Printf("[DEBUG] Deleting vm `%s`\n", vm.NameLabel)
err := c.DeleteVm(vm.Id)
if err != nil {
Expand Down
15 changes: 7 additions & 8 deletions xoa/resource_xenorchestra_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/ddelnano/terraform-provider-xenorchestra/client"
"github.com/ddelnano/terraform-provider-xenorchestra/xoa/internal"
"github.com/ddelnano/terraform-provider-xenorchestra/xoa/internal/state"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand Down Expand Up @@ -443,13 +442,13 @@ This does not work in terraform since that is applied on Xen Orchestra's client
Update: resourceVmUpdate,
Delete: resourceVmDelete,
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: state.ResourceVmResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: state.VmStateUpgradeV0,
Version: 0,
},
},
// StateUpgraders: []schema.StateUpgrader{
// {
// Type: state.ResourceVmResourceV0().CoreConfigSchema().ImpliedType(),
// Upgrade: state.VmStateUpgradeV0,
// Version: 0,
// },
// },
Importer: &schema.ResourceImporter{
State: RecordImport,
},
Expand Down
113 changes: 101 additions & 12 deletions xoa/resource_xenorchestra_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/ddelnano/terraform-provider-xenorchestra/client"
"github.com/ddelnano/terraform-provider-xenorchestra/xoa/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

Expand Down Expand Up @@ -1725,18 +1724,33 @@ func TestAccXenorchestraVm_diskAndNetworkAttachmentIgnoredWhenHalted(t *testing.
func TestAccXenorchestraVm_createWithV0StateMigration(t *testing.T) {
resourceName := "xenorchestra_vm.bar"
vmName := fmt.Sprintf("%s - %s", accTestPrefix, t.Name())
resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
CheckDestroy: testAccCheckXenorchestraVmDestroy,
Steps: []resource.TestStep{
// TODO(ddelnano): Remove this once blocked_operations can work on create
{
ExternalProviders: map[string]resource.ExternalProvider{
"xenorchestra": {
Source: "terra-farm/xenorchestra",
VersionConstraint: "0.24.2",
},
},
Config: testAccVmConfig(vmName),
Config: testAccVmConfigWithDeletionBlockedCommented(vmName, "false"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckNoResourceAttr(resourceName, "destroy_cloud_config_vdi_after_boot"),
),
},
{
ExternalProviders: map[string]resource.ExternalProvider{
"xenorchestra": {
Source: "terra-farm/xenorchestra",
VersionConstraint: "0.24.2",
},
},
Config: testAccVmConfigWithDeletionBlocked(vmName, "false"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
Expand All @@ -1750,7 +1764,7 @@ func TestAccXenorchestraVm_createWithV0StateMigration(t *testing.T) {
VersionConstraint: "0.25.0",
},
},
Config: testAccVmConfigWithWaitForIp(vmName, "true"),
Config: testAccVmConfigWithDeletionBlocked(vmName, "true"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
Expand All @@ -1759,26 +1773,29 @@ func TestAccXenorchestraVm_createWithV0StateMigration(t *testing.T) {
PlanOnly: true,
ExpectNonEmptyPlan: true,
},
// This step should fail if the state upgrade does not happen
{
ProviderFactories: map[string]func() (*schema.Provider, error){
"xenorchestra": func() (*schema.Provider, error) {
return testAccFailToDeleteVmProvider, nil
ExternalProviders: map[string]resource.ExternalProvider{
"xenorchestra": {
Source: "terra-farm/xenorchestra",
VersionConstraint: "0.25.1",
},
},
Config: testAccVmConfigWithWaitForIp(vmName, "true"),
Config: testAccVmConfigWithDeletionBlocked(vmName, "true"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttr(resourceName, "destroy_cloud_config_vdi_after_boot", "false"),
),
},
{
ProviderFactories: map[string]func() (*schema.Provider, error){
"xenorchestra": func() (*schema.Provider, error) {
return Provider(), nil
ExternalProviders: map[string]resource.ExternalProvider{
"xenorchestra": {
Source: "terra-farm/xenorchestra",
VersionConstraint: "0.25.1",
},
},
Config: testAccVmConfigWithWaitForIp(vmName, "true"),
Config: testAccVmConfigWithDeletionBlocked(vmName, "true"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
Expand All @@ -1787,6 +1804,20 @@ func TestAccXenorchestraVm_createWithV0StateMigration(t *testing.T) {
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
{
ExternalProviders: map[string]resource.ExternalProvider{
"xenorchestra": {
Source: "terra-farm/xenorchestra",
VersionConstraint: "0.25.1",
},
},
Config: testAccVmConfigWithDeletionBlockedCommented(vmName, "true"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttr(resourceName, "destroy_cloud_config_vdi_after_boot", "false"),
),
},
},
})
}
Expand Down Expand Up @@ -2014,6 +2045,64 @@ func testAccVmConfig(vmName string) string {
return testAccVmConfigWithWaitForIp(vmName, "false")
}

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 testAccVmConfigWithDeletionBlockedCommented(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" {
Expand Down

0 comments on commit 1d91c71

Please sign in to comment.