Skip to content

Commit

Permalink
Merge pull request #1278 from ksamoray/vpc-testfix
Browse files Browse the repository at this point in the history
Fix failing tests on vpc20 branch
  • Loading branch information
ksamoray authored Jul 25, 2024
2 parents 8b13758 + b7a2f96 commit e22edab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
13 changes: 7 additions & 6 deletions nsxt/data_source_nsxt_policy_ip_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"

"github.com/vmware/terraform-provider-nsxt/api/infra"
tf_api "github.com/vmware/terraform-provider-nsxt/api/utl"
)

func TestAccDataSourceNsxtPolicyIpBlock_basic(t *testing.T) {
Expand All @@ -37,12 +38,12 @@ func testAccDataSourceNsxtPolicyIPBlockBasic(t *testing.T, withContext bool, pre
PreCheck: preCheck,
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccDataSourceNsxtPolicyIPBlockDeleteByName(name)
return testAccDataSourceNsxtPolicyIPBlockDeleteByName(testAccGetSessionContext(), name)
},
Steps: []resource.TestStep{
{
PreConfig: func() {
if err := testAccDataSourceNsxtPolicyIPBlockCreate(name, newUUID(), "4001::/64", false); err != nil {
if err := testAccDataSourceNsxtPolicyIPBlockCreate(testAccGetSessionContext(), name, newUUID(), "4001::/64", false); err != nil {
t.Error(err)
}
},
Expand All @@ -57,12 +58,12 @@ func testAccDataSourceNsxtPolicyIPBlockBasic(t *testing.T, withContext bool, pre
})
}

func testAccDataSourceNsxtPolicyIPBlockCreate(name, id, cidr string, isPrivate bool) error {
func testAccDataSourceNsxtPolicyIPBlockCreate(context tf_api.SessionContext, name, id, cidr string, isPrivate bool) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}
client := infra.NewIpBlocksClient(testAccGetSessionContext(), connector)
client := infra.NewIpBlocksClient(context, connector)
if client == nil {
return policyResourceNotSupportedError()
}
Expand All @@ -86,12 +87,12 @@ func testAccDataSourceNsxtPolicyIPBlockCreate(name, id, cidr string, isPrivate b
return nil
}

func testAccDataSourceNsxtPolicyIPBlockDeleteByName(name string) error {
func testAccDataSourceNsxtPolicyIPBlockDeleteByName(context tf_api.SessionContext, name string) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}
client := infra.NewIpBlocksClient(testAccGetSessionContext(), connector)
client := infra.NewIpBlocksClient(context, connector)
if client == nil {
return policyResourceNotSupportedError()
}
Expand Down
7 changes: 4 additions & 3 deletions nsxt/data_source_nsxt_vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func testAccDataSourceNsxtVPCCreate(name string) error {
}

ipBlockID := newUUID()
err = testAccDataSourceNsxtPolicyIPBlockCreate(name, ipBlockID, "192.168.240.0/24", true)
err = testAccDataSourceNsxtPolicyIPBlockCreate(testAccGetProjectContext(), name, ipBlockID, "192.168.240.0/24", true)
if err != nil {
return err
}
Expand Down Expand Up @@ -107,14 +107,15 @@ func testAccDataSourceNsxtVPCDeleteByName(name string) error {
if err != nil {
return handleDeleteError("VPC", *objInList.Id, err)
}
return testAccDataSourceNsxtPolicyIPBlockDeleteByName(name)
return testAccDataSourceNsxtPolicyIPBlockDeleteByName(testAccGetProjectContext(), name)
}
}
return fmt.Errorf("error while deleting VPC '%s': resource not found", name)
}

func testAccNsxtVPCReadTemplate(name string) string {
context := testAccNsxtPolicyMultitenancyContext()
// We just need the project context as VPC is not under VPC, but the VPC itself
context := testAccNsxtProjectContext()
return fmt.Sprintf(`
data "nsxt_policy_ip_block" "test" {
%s
Expand Down
14 changes: 14 additions & 0 deletions nsxt/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ func testAccGetSessionContext() tf_api.SessionContext {
return tf_api.SessionContext{ProjectID: projectID, ClientType: clientType, VPCID: vpcID}
}

func testAccGetProjectContext() tf_api.SessionContext {
projectID := os.Getenv("NSXT_PROJECT_ID")
return tf_api.SessionContext{ProjectID: projectID, ClientType: tf_api.Multitenancy}
}

func testAccIsGlobalManager2() tf_api.ClientType {
if testAccIsVPC() {
return tf_api.VPC
Expand Down Expand Up @@ -719,6 +724,15 @@ func testAccNsxtPolicyMultitenancyContext() string {
return ""
}

func testAccNsxtProjectContext() string {
projectID := os.Getenv("NSXT_PROJECT_ID")
return fmt.Sprintf(`
context {
project_id = "%s"
}
`, projectID)
}

func testAccResourceNsxtPolicyImportIDRetriever(resourceID string) func(*terraform.State) (string, error) {
return func(s *terraform.State) (string, error) {

Expand Down

0 comments on commit e22edab

Please sign in to comment.