Skip to content

Commit

Permalink
Update test to reproduce the issue with destroy_cloud_config_vdi_afte…
Browse files Browse the repository at this point in the history
…r_boot
  • Loading branch information
ddelnano committed Oct 6, 2023
1 parent 48141b9 commit 9728a80
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
24 changes: 24 additions & 0 deletions xoa/internal/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,27 @@ func newFailToStartAndHaltClient(config client.Config) (client.XOClient, error)
func GetFailToStartAndHaltXOClient(d *schema.ResourceData) (interface{}, error) {
return newFailToStartAndHaltClient(client.GetConfigFromEnv())
}

type failToDeleteVmXOClient struct {
*client.Client
}

func (c *failToDeleteVmXOClient) DeleteVm(id string) error {
return errors.New("client.DeleteVm should not be called")
}

func newFailToDeleteVmClient(config client.Config) (client.XOClient, error) {
xoClient, err := client.NewClient(config)

if err != nil {
return nil, err
}

c := xoClient.(*client.Client)

return &failToDeleteVmXOClient{c}, nil
}

func GetFailToDeleteVmXOClient(d *schema.ResourceData) (interface{}, error) {
return newFailToDeleteVmClient(client.GetConfigFromEnv())
}
7 changes: 7 additions & 0 deletions xoa/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (

var testAccProviders map[string]*schema.Provider
var testAccFailToStartAndHaltProviders map[string]*schema.Provider
var testAccFailToDeleteVmProviders map[string]*schema.Provider

var testAccProvider *schema.Provider
var testAccFailToStartHaltVmProvider *schema.Provider
var testAccFailToDeleteVmProvider *schema.Provider

func init() {
testAccProvider = Provider()
Expand All @@ -25,6 +27,11 @@ func init() {
testAccFailToStartAndHaltProviders = map[string]*schema.Provider{
"xenorchestra": testAccFailToStartHaltVmProvider,
}
testAccFailToDeleteVmProvider = Provider()
testAccFailToDeleteVmProvider.ConfigureFunc = internal.GetFailToDeleteVmXOClient
testAccFailToDeleteVmProviders = map[string]*schema.Provider{
"xenorchestra": testAccFailToDeleteVmProvider,
}
}

func testAccPreCheck(t *testing.T) {
Expand Down
33 changes: 32 additions & 1 deletion xoa/resource_xenorchestra_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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 @@ -1492,6 +1493,7 @@ func TestAccXenorchestraVm_createWithV0StateMigration(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckNoResourceAttr(resourceName, "destroy_cloud_config_vdi_after_boot"),
),
},
{
Expand All @@ -1501,14 +1503,43 @@ func TestAccXenorchestraVm_createWithV0StateMigration(t *testing.T) {
VersionConstraint: "0.25.0",
},
},
Config: testAccVmConfig(vmName),
Config: testAccVmConfigWithWaitForIp(vmName, "true"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckNoResourceAttr(resourceName, "destroy_cloud_config_vdi_after_boot"),
),
PlanOnly: true,
ExpectNonEmptyPlan: true,
},
{
ProviderFactories: map[string]func() (*schema.Provider, error){
"xenorchestra": func() (*schema.Provider, error) {
return testAccFailToDeleteVmProvider, nil
},
},
Config: testAccVmConfigWithWaitForIp(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
},
},
Config: testAccVmConfigWithWaitForIp(vmName, "true"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccVmExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttr(resourceName, "destroy_cloud_config_vdi_after_boot", "false"),
),
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
},
})
}
Expand Down

0 comments on commit 9728a80

Please sign in to comment.