From a34c3266a7c35113a3f5206f7c92fe2b62615170 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 10 Dec 2024 14:14:46 +0200 Subject: [PATCH 01/39] test: extract helpers --- test/mocked/resources/group_test.go | 44 +++++++++++++++++------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 89963de..6147370 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -31,18 +31,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - util.MockPostGroup( - util.GenerateNonRootGroupPostRequest("id", "", ""), - util.GenerateGroupPostResponse("id", "", ""), - 1, - ) - util.MockGetGroup("id", util.GenerateGroupGetResponse("id", "", ""), 2) - // to indicate the group has a parent - util.MockGetGroup( - "parent_id", - util.GenerateGroupGetResponse("parent_id", "", ""), - 1, - ) + setupGroupCreateMocks("id", "parent_id") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -84,12 +73,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - util.MockGetGroup( - "id", - util.GenerateGroupGetResponse("id", "", ""), - 1, - ) - util.MockDeleteGroup("id", 1) + setupGroupDeleteMocks("id") }, Config: provider.ProviderConfig, }, @@ -99,6 +83,30 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { mockOAuth.Mock.Disable() } +func setupGroupDeleteMocks(groupID string) { + util.MockGetGroup( + groupID, + util.GenerateGroupGetResponse(groupID, "", ""), + 1, + ) + util.MockDeleteGroup(groupID, 1) +} + +func setupGroupCreateMocks(groupID string, parentID string) { + util.MockPostGroup( + util.GenerateNonRootGroupPostRequest(groupID, "", ""), + util.GenerateGroupPostResponse(groupID, "", ""), + 1, + ) + util.MockGetGroup(groupID, util.GenerateGroupGetResponse(groupID, "", ""), 2) + + util.MockGetGroup( + parentID, + util.GenerateGroupGetResponse(parentID, "", ""), + 1, + ) +} + func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { defer gock.Off() mockOAuth := util.MockOAuth() From 07b1fe6bc7742ba9af93141b9e5b1121cba7a10c Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 10 Dec 2024 15:50:25 +0200 Subject: [PATCH 02/39] test: remove unused params and mock setups --- test/mocked/resources/group_test.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 6147370..d9db3ed 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -31,7 +31,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - setupGroupCreateMocks("id", "parent_id") + setupGroupCreateMocks("id") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -83,28 +83,22 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { mockOAuth.Mock.Disable() } -func setupGroupDeleteMocks(groupID string) { - util.MockGetGroup( - groupID, - util.GenerateGroupGetResponse(groupID, "", ""), - 1, - ) - util.MockDeleteGroup(groupID, 1) -} - -func setupGroupCreateMocks(groupID string, parentID string) { +func setupGroupCreateMocks(groupID string) { util.MockPostGroup( util.GenerateNonRootGroupPostRequest(groupID, "", ""), util.GenerateGroupPostResponse(groupID, "", ""), 1, ) util.MockGetGroup(groupID, util.GenerateGroupGetResponse(groupID, "", ""), 2) +} +func setupGroupDeleteMocks(groupID string) { util.MockGetGroup( - parentID, - util.GenerateGroupGetResponse(parentID, "", ""), + groupID, + util.GenerateGroupGetResponse(groupID, "", ""), 1, ) + util.MockDeleteGroup(groupID, 1) } func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { From ad06a5ab126cf7fa0455d8ffcb7e81c3b48c5bb7 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 10 Dec 2024 16:52:33 +0200 Subject: [PATCH 03/39] test: extract setting up mock for create --- test/mocked/resources/group_test.go | 46 +++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index d9db3ed..b2cd9a8 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -17,8 +17,10 @@ import ( "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" "github.com/stretchr/testify/assert" + config_api_client "github.com/aruba-uxi/terraform-provider-hpeuxi/pkg/config-api-client" "github.com/aruba-uxi/terraform-provider-hpeuxi/test/mocked/provider" "github.com/aruba-uxi/terraform-provider-hpeuxi/test/mocked/util" + "github.com/aruba-uxi/terraform-provider-hpeuxi/test/shared" ) func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { @@ -31,7 +33,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - setupGroupCreateMocks("id") + setupGroupCreateMocks("id", "parent_id", "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -83,13 +85,47 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { mockOAuth.Mock.Disable() } -func setupGroupCreateMocks(groupID string) { +func setupGroupCreateMocks(groupID string, parentID string, name string) { + groupPath := parentID + "." + groupID + + setupCreateGroupPostMock(groupID, parentID, groupPath, name) + setupCreateGroupGetMock(groupID, parentID, groupPath, name) +} + +func setupCreateGroupGetMock(groupID string, parentID string, groupPath string, name string) { + getResponseItems := []config_api_client.GroupsGetItem{*config_api_client.NewGroupsGetItem( + groupID, + name, + *config_api_client.NewNullableGroupsGetParent(config_api_client.NewGroupsGetParent(parentID)), + groupPath, + shared.GroupType, + )} + getResponse := config_api_client.NewGroupsGetResponse( + getResponseItems, + 1, + *config_api_client.NewNullableString(nil), + ) + + util.MockGetGroup(groupID, getResponse, 1) +} + +func setupCreateGroupPostMock(groupID string, parentID string, groupPath string, name string) { + postRequest := config_api_client.NewGroupPostRequest(name) + postRequest.SetParentId(parentID) + + postResponse := config_api_client.NewGroupPostResponse( + groupID, + name, + groupPath, + *config_api_client.NewGroupPostParent(parentID), + shared.GroupType, + ) + util.MockPostGroup( - util.GenerateNonRootGroupPostRequest(groupID, "", ""), - util.GenerateGroupPostResponse(groupID, "", ""), + *postRequest, + *postResponse, 1, ) - util.MockGetGroup(groupID, util.GenerateGroupGetResponse(groupID, "", ""), 2) } func setupGroupDeleteMocks(groupID string) { From 1341fe7180cbf27e027177f6f42006ae46202f42 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 10 Dec 2024 17:04:16 +0200 Subject: [PATCH 04/39] test: shuffle group post mock setup --- test/mocked/resources/group_test.go | 56 +++++++++++++++++------------ 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index b2cd9a8..888bbc7 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -88,44 +88,56 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { func setupGroupCreateMocks(groupID string, parentID string, name string) { groupPath := parentID + "." + groupID - setupCreateGroupPostMock(groupID, parentID, groupPath, name) - setupCreateGroupGetMock(groupID, parentID, groupPath, name) -} - -func setupCreateGroupGetMock(groupID string, parentID string, groupPath string, name string) { - getResponseItems := []config_api_client.GroupsGetItem{*config_api_client.NewGroupsGetItem( - groupID, - name, - *config_api_client.NewNullableGroupsGetParent(config_api_client.NewGroupsGetParent(parentID)), - groupPath, - shared.GroupType, - )} - getResponse := config_api_client.NewGroupsGetResponse( - getResponseItems, - 1, - *config_api_client.NewNullableString(nil), - ) + postRequest := createGroupPostRequest(name, parentID) + postResponse := createGroupPostResponse(groupID, groupPath, name, parentID) + util.MockPostGroup(postRequest, postResponse, 1) + getResponse := createGroupGetResponse(groupID, parentID, groupPath, name) util.MockGetGroup(groupID, getResponse, 1) } -func setupCreateGroupPostMock(groupID string, parentID string, groupPath string, name string) { +func createGroupPostRequest(name string, parentID string) config_api_client.GroupPostRequest { postRequest := config_api_client.NewGroupPostRequest(name) postRequest.SetParentId(parentID) - postResponse := config_api_client.NewGroupPostResponse( + return *postRequest +} + +func createGroupPostResponse( + groupID string, + groupPath string, + name string, + parentID string, +) config_api_client.GroupPostResponse { + return *config_api_client.NewGroupPostResponse( groupID, name, groupPath, *config_api_client.NewGroupPostParent(parentID), shared.GroupType, ) +} - util.MockPostGroup( - *postRequest, - *postResponse, +func createGroupGetResponse( + groupID string, + parentID string, + groupPath string, + name string, +) config_api_client.GroupsGetResponse { + getResponseItems := []config_api_client.GroupsGetItem{*config_api_client.NewGroupsGetItem( + groupID, + name, + *config_api_client.NewNullableGroupsGetParent(config_api_client.NewGroupsGetParent(parentID)), + groupPath, + shared.GroupType, + )} + getResponse := config_api_client.NewGroupsGetResponse( + getResponseItems, 1, + *config_api_client.NewNullableString(nil), ) + + return *getResponse } func setupGroupDeleteMocks(groupID string) { From f8da4fd68bb8714bb4837476bba90c8b4203a370 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Wed, 11 Dec 2024 15:14:34 +0200 Subject: [PATCH 05/39] test: use shared helpers for setting up delete mocks --- test/mocked/resources/group_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 888bbc7..fa8aaf4 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -75,7 +75,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks("id") + setupGroupDeleteMocks("id", "parent_id", "name") }, Config: provider.ProviderConfig, }, @@ -140,10 +140,12 @@ func createGroupGetResponse( return *getResponse } -func setupGroupDeleteMocks(groupID string) { +func setupGroupDeleteMocks(groupID string, parentID string, name string) { + groupPath := parentID + "." + groupID + getResponse := createGroupGetResponse(groupID, parentID, groupPath, name) util.MockGetGroup( groupID, - util.GenerateGroupGetResponse(groupID, "", ""), + getResponse, 1, ) util.MockDeleteGroup(groupID, 1) From 21f03bb8118968bf88e626d16f542a37440ce0b8 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Wed, 11 Dec 2024 15:31:23 +0200 Subject: [PATCH 06/39] test: creat helper for import mocks --- test/mocked/resources/group_test.go | 36 +++++++++-------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index fa8aaf4..a542e59 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -151,6 +151,14 @@ func setupGroupDeleteMocks(groupID string, parentID string, name string) { util.MockDeleteGroup(groupID, 1) } +func setupGroupImportMocks(groupID string, parentID string, name string) { + groupPath := parentID + "." + groupID + getResponse := createGroupGetResponse(groupID, parentID, groupPath, name) + util.MockGetGroup(groupID, getResponse, 1) + getParentResponse := createGroupGetResponse(parentID, "fake_parent_id", "fake_parent_id."+parentID, "fake parent") + util.MockGetGroup(parentID, getParentResponse, 1) +} + func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { defer gock.Off() mockOAuth := util.MockOAuth() @@ -161,18 +169,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - util.MockPostGroup( - util.GenerateNonRootGroupPostRequest("id", "", ""), - util.GenerateGroupPostResponse("id", "", ""), - 1, - ) - util.MockGetGroup("id", util.GenerateGroupGetResponse("id", "", ""), 2) - // to indicate the group has a parent - util.MockGetGroup( - "parent_id", - util.GenerateGroupGetResponse("parent_id", "", ""), - 1, - ) + setupGroupCreateMocks("id", "parent_id", "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -183,13 +180,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // ImportState { PreConfig: func() { - util.MockGetGroup("id", util.GenerateGroupGetResponse("id", "", ""), 1) - // to indicate the group has a parent - util.MockGetGroup( - "parent_id", - util.GenerateGroupGetResponse("parent_id", "", ""), - 1, - ) + setupGroupImportMocks("id", "parent_id", "name") }, ResourceName: "hpeuxi_group.my_group", ImportState: true, @@ -198,12 +189,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - util.MockGetGroup( - "id", - util.GenerateGroupGetResponse("id", "", ""), - 1, - ) - util.MockDeleteGroup("id", 1) + setupGroupDeleteMocks("id", "parent_id", "name") }, Config: provider.ProviderConfig, }, From bd0cdc04617cb96b3fa63b138d524e48dcc6a0f5 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Wed, 11 Dec 2024 15:48:41 +0200 Subject: [PATCH 07/39] test: add nillable parent to group mock setup --- test/mocked/resources/group_test.go | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index a542e59..81ccc52 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -33,7 +33,9 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - setupGroupCreateMocks("id", "parent_id", "name") + var parentID = new(string) + *parentID = "parent_id" + setupGroupCreateMocks("id", parentID, "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -85,20 +87,28 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { mockOAuth.Mock.Disable() } -func setupGroupCreateMocks(groupID string, parentID string, name string) { - groupPath := parentID + "." + groupID +func setupGroupCreateMocks(groupID string, parentID *string, name string) { + groupPath := util.MockRootGroupID + "." + groupID + realParent := util.MockRootGroupID + if parentID != nil { + realParent := *parentID + groupPath = realParent + "." + groupID + } postRequest := createGroupPostRequest(name, parentID) - postResponse := createGroupPostResponse(groupID, groupPath, name, parentID) + postResponse := createGroupPostResponse(groupID, groupPath, name, realParent) util.MockPostGroup(postRequest, postResponse, 1) - getResponse := createGroupGetResponse(groupID, parentID, groupPath, name) + getResponse := createGroupGetResponse(groupID, realParent, groupPath, name) util.MockGetGroup(groupID, getResponse, 1) } -func createGroupPostRequest(name string, parentID string) config_api_client.GroupPostRequest { +func createGroupPostRequest(name string, parentID *string) config_api_client.GroupPostRequest { postRequest := config_api_client.NewGroupPostRequest(name) - postRequest.SetParentId(parentID) + if parentID != nil { + realParent := *parentID + postRequest.SetParentId(realParent) + } return *postRequest } @@ -169,7 +179,9 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - setupGroupCreateMocks("id", "parent_id", "name") + var parentID = new(string) + *parentID = "parent_id" + setupGroupCreateMocks("id", parentID, "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -221,6 +233,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { util.GenerateGroupAttachedToRootGroupGetResponse("id", ""), 1, ) + setupGroupCreateMocks("id", nil, "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { From 9fcfbc32b2493cb367e1556f9afbaf9ea49d60ed Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Wed, 11 Dec 2024 16:23:45 +0200 Subject: [PATCH 08/39] test: allow group delete to have empty parent (root) --- test/mocked/resources/group_test.go | 49 ++++++++++++++--------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 81ccc52..599079b 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -33,7 +33,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - var parentID = new(string) + parentID := new(string) *parentID = "parent_id" setupGroupCreateMocks("id", parentID, "name") }, @@ -77,7 +77,9 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks("id", "parent_id", "name") + parentID := new(string) + *parentID = "parent_id" + setupGroupDeleteMocks("id", parentID, "name") }, Config: provider.ProviderConfig, }, @@ -150,9 +152,14 @@ func createGroupGetResponse( return *getResponse } -func setupGroupDeleteMocks(groupID string, parentID string, name string) { - groupPath := parentID + "." + groupID - getResponse := createGroupGetResponse(groupID, parentID, groupPath, name) +func setupGroupDeleteMocks(groupID string, parentID *string, name string) { + groupPath := util.MockRootGroupID + "." + groupID + realParent := util.MockRootGroupID + if parentID != nil { + realParent := *parentID + groupPath = realParent + "." + groupID + } + getResponse := createGroupGetResponse(groupID, realParent, groupPath, name) util.MockGetGroup( groupID, getResponse, @@ -165,7 +172,12 @@ func setupGroupImportMocks(groupID string, parentID string, name string) { groupPath := parentID + "." + groupID getResponse := createGroupGetResponse(groupID, parentID, groupPath, name) util.MockGetGroup(groupID, getResponse, 1) - getParentResponse := createGroupGetResponse(parentID, "fake_parent_id", "fake_parent_id."+parentID, "fake parent") + getParentResponse := createGroupGetResponse( + parentID, + "fake_parent_id", + "fake_parent_id."+parentID, + "fake parent", + ) util.MockGetGroup(parentID, getParentResponse, 1) } @@ -179,7 +191,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - var parentID = new(string) + parentID := new(string) *parentID = "parent_id" setupGroupCreateMocks("id", parentID, "name") }, @@ -201,7 +213,9 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks("id", "parent_id", "name") + parentID := new(string) + *parentID = "parent_id" + setupGroupDeleteMocks("id", parentID, "name") }, Config: provider.ProviderConfig, }, @@ -221,18 +235,6 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { // Creating a group attached to the root { PreConfig: func() { - util.MockPostGroup( - util.GenerateGroupAttachedToRootGroupPostRequest("id", ""), - util.GenerateGroupAttachedToRootGroupPostResponse("id", ""), - 1, - ) - // to indicate the group has the root group as a parent - util.MockGetGroup(util.MockRootGroupID, util.GenerateRootGroupGetResponse(), 1) - util.MockGetGroup( - "id", - util.GenerateGroupAttachedToRootGroupGetResponse("id", ""), - 1, - ) setupGroupCreateMocks("id", nil, "name") }, Config: provider.ProviderConfig + ` @@ -271,12 +273,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { { PreConfig: func() { // existing group - util.MockGetGroup( - "id", - util.GenerateGroupAttachedToRootGroupGetResponse("id", ""), - 1, - ) - util.MockDeleteGroup("id", 1) + setupGroupDeleteMocks("id", nil, "name") }, Config: provider.ProviderConfig, }, From 73841edca93157dd9b30bf6bad1296ffad177bf5 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Thu, 12 Dec 2024 10:40:42 +0200 Subject: [PATCH 09/39] test: use new helpers in UpdateGroupsResource test --- test/mocked/resources/group_test.go | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 599079b..cc559e0 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -283,7 +283,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { mockOAuth.Mock.Disable() } -func Test_ImportGroupResource_WithRootParent_ShouldFail(t *testing.T) { +func Test_ImportGroupResource_WithRoot_ShouldFail(t *testing.T) { defer gock.Off() mockOAuth := util.MockOAuth() @@ -322,18 +322,9 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - util.MockPostGroup( - util.GenerateNonRootGroupPostRequest("id", "", ""), - util.GenerateGroupPostResponse("id", "", ""), - 1, - ) - util.MockGetGroup("id", util.GenerateGroupGetResponse("id", "", ""), 2) - // to indicate the group has a parent - util.MockGetGroup( - "parent_id", - util.GenerateGroupGetResponse("parent_id", "", ""), - 1, - ) + parentID := new(string) + *parentID = "parent_id" + setupGroupCreateMocks("id", parentID, "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -391,12 +382,9 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - util.MockGetGroup( - "id", - util.GenerateGroupGetResponse("id", "", "_2"), - 1, - ) - util.MockDeleteGroup("id", 1) + parentID := new(string) + *parentID = "parent_id" + setupGroupDeleteMocks("id", parentID, "name_2") }, Config: provider.ProviderConfig, }, From bbc434429711f965ccda5f727eac57632350c4b0 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Thu, 12 Dec 2024 10:53:06 +0200 Subject: [PATCH 10/39] test: use create and delete helpers all over --- test/mocked/resources/group_test.go | 43 ++++++----------------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index cc559e0..9fffc18 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -404,17 +404,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - util.MockPostGroup( - util.GenerateGroupAttachedToRootGroupPostRequest("id", ""), - util.GenerateGroupAttachedToRootGroupPostResponse("id", ""), - 1, - ) - util.MockGetGroup(util.MockRootGroupID, util.GenerateRootGroupGetResponse(), 1) - util.MockGetGroup( - "id", - util.GenerateGroupAttachedToRootGroupGetResponse("id", ""), - 1, - ) + setupGroupCreateMocks("id", nil, "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -470,12 +460,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - util.MockGetGroup( - "id", - util.GenerateGroupAttachedToRootGroupGetResponse("id", "_2"), - 1, - ) - util.MockDeleteGroup("id", 1) + setupGroupDeleteMocks("id", nil, "name_2") }, Config: provider.ProviderConfig, }, @@ -495,18 +480,9 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - util.MockPostGroup( - util.GenerateNonRootGroupPostRequest("id", "", ""), - util.GenerateGroupPostResponse("id", "", ""), - 1, - ) - util.MockGetGroup("id", util.GenerateGroupGetResponse("id", "", ""), 2) - // to indicate the group has a parent - util.MockGetGroup( - "parent_id", - util.GenerateGroupGetResponse("parent_id", "", ""), - 1, - ) + parentID := new(string) + *parentID = "parent_id" + setupGroupCreateMocks("id", parentID, "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -573,12 +549,9 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - util.MockGetGroup( - "new_id", - util.GenerateGroupGetResponse("new_id", "", "_2"), - 1, - ) - util.MockDeleteGroup("new_id", 1) + parentID := new(string) + *parentID = "parent_id_2" + setupGroupDeleteMocks("new_id", parentID, "name_2") }, Config: provider.ProviderConfig, }, From a2390fb282342e89571d49fb06590588643d7a5d Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Thu, 12 Dec 2024 11:06:21 +0200 Subject: [PATCH 11/39] test: update createImportMocks to deal with nil parent --- test/mocked/resources/group_test.go | 42 ++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 9fffc18..31a1555 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -168,17 +168,31 @@ func setupGroupDeleteMocks(groupID string, parentID *string, name string) { util.MockDeleteGroup(groupID, 1) } -func setupGroupImportMocks(groupID string, parentID string, name string) { - groupPath := parentID + "." + groupID - getResponse := createGroupGetResponse(groupID, parentID, groupPath, name) - util.MockGetGroup(groupID, getResponse, 1) - getParentResponse := createGroupGetResponse( - parentID, - "fake_parent_id", - "fake_parent_id."+parentID, - "fake parent", - ) - util.MockGetGroup(parentID, getParentResponse, 1) +func setupGroupImportMocks(groupID string, parentID *string, name string) { + if parentID != nil { + realParent := *parentID + groupPath := realParent + "." + groupID + + getParentResponse := createGroupGetResponse( + realParent, + "fake_parent_id", + "fake_parent_id."+realParent, + "fake parent", + ) + + util.MockGetGroup(realParent, getParentResponse, 1) + + getResponse := createGroupGetResponse(groupID, realParent, groupPath, name) + util.MockGetGroup(groupID, getResponse, 1) + } else { + groupPath := util.MockRootGroupID + "." + groupID + + rootResponse := util.GenerateRootGroupGetResponse() + util.MockGetGroup(util.MockRootGroupID, rootResponse, 1) + + getResponse := createGroupGetResponse(groupID, util.MockRootGroupID, groupPath, name) + util.MockGetGroup(groupID, getResponse, 1) + } } func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { @@ -204,7 +218,9 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // ImportState { PreConfig: func() { - setupGroupImportMocks("id", "parent_id", "name") + parentID := new(string) + *parentID = "parent_id" + setupGroupImportMocks("id", parentID, "name") }, ResourceName: "hpeuxi_group.my_group", ImportState: true, @@ -293,7 +309,7 @@ func Test_ImportGroupResource_WithRoot_ShouldFail(t *testing.T) { // Importing the root group does not work { PreConfig: func() { - util.MockGetGroup(util.MockRootGroupID, util.GenerateRootGroupGetResponse(), 1) + setupGroupImportMocks(util.MockRootGroupID, nil, "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_root_group" { From 5c8702220f71c82015b23f20c47d3be37ababce6 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Thu, 12 Dec 2024 11:24:11 +0200 Subject: [PATCH 12/39] test: use new helpers for recreate updates --- test/mocked/resources/group_test.go | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 31a1555..efc8138 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -353,6 +353,7 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { PreConfig: func() { // existing group util.MockGetGroup("id", util.GenerateGroupGetResponse("id", "", ""), 1) + // updated group util.MockPatchGroup( "id", @@ -436,6 +437,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { util.GenerateGroupAttachedToRootGroupGetResponse("id", ""), 1, ) + // updated group util.MockPatchGroup( "id", @@ -509,21 +511,14 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { // Update that does trigger a recreate { PreConfig: func() { - // existing group - util.MockGetGroup("id", util.GenerateGroupGetResponse("id", "", ""), 1) - // new group (replacement) - util.MockPostGroup( - util.GenerateNonRootGroupPostRequest("new_id", "", "_2"), - util.GenerateGroupPostResponse("new_id", "", "_2"), - 1, - ) - util.MockGetGroup( - "new_id", - util.GenerateGroupGetResponse("new_id", "", "_2"), - 1, - ) - // delete old group (being replaced) - util.MockDeleteGroup("id", 1) + oldParentID := new(string) + *oldParentID = "parent_id" + setupGroupImportMocks("id", oldParentID, "name") + setupGroupDeleteMocks("id", oldParentID, "name") + + newParentID := new(string) + *newParentID = "parent_id_2" + setupGroupCreateMocks("new_id", newParentID, "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -567,7 +562,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { PreConfig: func() { parentID := new(string) *parentID = "parent_id_2" - setupGroupDeleteMocks("new_id", parentID, "name_2") + setupGroupDeleteMocks("new_id", parentID, "name") }, Config: provider.ProviderConfig, }, From a075f55fa83f867a3c7f4d0ac0c5d9f6f3e8905b Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Thu, 12 Dec 2024 14:13:21 +0200 Subject: [PATCH 13/39] test: add helper to set up GroupUpdate mocks --- test/mocked/resources/group_test.go | 70 +++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index efc8138..42770ad 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -152,6 +152,58 @@ func createGroupGetResponse( return *getResponse } +func createGroupPatchRequest(name string) config_api_client.GroupPatchRequest { + request := config_api_client.NewGroupPatchRequest() + request.SetName(name) + + return *request +} + +func createGroupPatchResponse( + groupID string, + name string, + parentID string, + path string, +) config_api_client.GroupPatchResponse { + response := config_api_client.NewGroupPatchResponse( + groupID, + name, + path, + *config_api_client.NewGroupPatchParent(parentID), + shared.GroupType, + ) + + return *response +} + +func setupGroupUpdateMocks(groupID string, parentID string, name string) { + path := parentID + "." + groupID + + util.MockPatchGroup( + "id", + createGroupPatchRequest(name), + createGroupPatchResponse(groupID, name, path, parentID), + 1, + ) + + util.MockGetGroup( + "id", + createGroupGetResponse(groupID, parentID, path, name), + 1, + ) + + util.MockGetGroup( + parentID, + createGroupGetResponse( + parentID, + "fake_parent_id", + "fake_parent_id."+parentID, + "fake_parent", + ), + 1, + ) +} + func setupGroupDeleteMocks(groupID string, parentID *string, name string) { groupPath := util.MockRootGroupID + "." + groupID realParent := util.MockRootGroupID @@ -351,22 +403,12 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { // Update that does not trigger a recreate { PreConfig: func() { - // existing group - util.MockGetGroup("id", util.GenerateGroupGetResponse("id", "", ""), 1) + parentID := new(string) + *parentID = "parent_id" + setupGroupImportMocks("id", parentID, "name") // updated group - util.MockPatchGroup( - "id", - util.GenerateGroupPatchRequest("_2"), - util.GenerateGroupPatchResponse("id", "_2", ""), - 1, - ) - util.MockGetGroup("id", util.GenerateGroupGetResponse("id", "_2", ""), 3) - util.MockGetGroup( - "parent_id", - util.GenerateGroupGetResponse("parent_id", "", ""), - 1, - ) + setupGroupUpdateMocks("id", *parentID, "name_2") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { From 7f63574b950ff06359aee2b8b7f95fe7a90a4ee7 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Fri, 13 Dec 2024 10:06:44 +0200 Subject: [PATCH 14/39] test: gock.OffAll and root parent update helpers --- test/mocked/resources/group_test.go | 64 ++++++++++++++--------------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 42770ad..d3b2d6d 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -24,7 +24,7 @@ import ( ) func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { - defer gock.Off() + defer gock.OffAll() mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ @@ -178,6 +178,20 @@ func createGroupPatchResponse( func setupGroupUpdateMocks(groupID string, parentID string, name string) { path := parentID + "." + groupID + if parentID == util.MockRootGroupID { + util.MockGetGroup(util.MockRootGroupID, util.GenerateRootGroupGetResponse(), 1) + } else { + util.MockGetGroup( + parentID, + createGroupGetResponse( + parentID, + "fake_parent_id", + "fake_parent_id."+parentID, + "fake_parent", + ), + 1, + ) + } util.MockPatchGroup( "id", @@ -191,17 +205,6 @@ func setupGroupUpdateMocks(groupID string, parentID string, name string) { createGroupGetResponse(groupID, parentID, path, name), 1, ) - - util.MockGetGroup( - parentID, - createGroupGetResponse( - parentID, - "fake_parent_id", - "fake_parent_id."+parentID, - "fake_parent", - ), - 1, - ) } func setupGroupDeleteMocks(groupID string, parentID *string, name string) { @@ -248,7 +251,7 @@ func setupGroupImportMocks(groupID string, parentID *string, name string) { } func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { - defer gock.Off() + defer gock.OffAll() mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ @@ -294,7 +297,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { } func Test_CreateGroupResource_WithRootParent(t *testing.T) { - defer gock.Off() + defer gock.OffAll() mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ @@ -352,7 +355,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { } func Test_ImportGroupResource_WithRoot_ShouldFail(t *testing.T) { - defer gock.Off() + defer gock.OffAll() mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ @@ -381,7 +384,7 @@ func Test_ImportGroupResource_WithRoot_ShouldFail(t *testing.T) { } func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { - defer gock.Off() + defer gock.OffAll() mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ @@ -454,7 +457,7 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { } func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { - defer gock.Off() + defer gock.OffAll() mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ @@ -476,23 +479,16 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // existing group util.MockGetGroup( "id", - util.GenerateGroupAttachedToRootGroupGetResponse("id", ""), + createGroupGetResponse( + "id", + util.MockRootGroupID, + "id."+util.MockRootGroupID, + "name", + ), 1, ) - // updated group - util.MockPatchGroup( - "id", - util.GenerateGroupPatchRequest("_2"), - util.GenerateGroupPatchResponse("id", "_2", ""), - 1, - ) - util.MockGetGroup(util.MockRootGroupID, util.GenerateRootGroupGetResponse(), 2) - util.MockGetGroup( - "id", - util.GenerateGroupAttachedToRootGroupGetResponse("id", "_2"), - 2, - ) + setupGroupUpdateMocks("id", util.MockRootGroupID, "name_2") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -531,7 +527,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { } func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { - defer gock.Off() + defer gock.OffAll() mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ @@ -615,7 +611,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { } func TestGroupResourceTooManyRequestsHandling(t *testing.T) { - defer gock.Off() + defer gock.OffAll() mockOAuth := util.MockOAuth() var mockTooManyRequests *gock.Response @@ -736,7 +732,7 @@ func TestGroupResourceTooManyRequestsHandling(t *testing.T) { } func TestGroupResourceHttpErrorHandling(t *testing.T) { - defer gock.Off() + defer gock.OffAll() mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ From 8deabb8c34e86c45b92d5bb68fc3eb73530421af Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Fri, 13 Dec 2024 10:14:05 +0200 Subject: [PATCH 15/39] test: use import setup for update pre-plan test --- test/mocked/resources/group_test.go | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index d3b2d6d..656f577 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -476,18 +476,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // Update that does not trigger a recreate { PreConfig: func() { - // existing group - util.MockGetGroup( - "id", - createGroupGetResponse( - "id", - util.MockRootGroupID, - "id."+util.MockRootGroupID, - "name", - ), - 1, - ) - + setupGroupImportMocks("id", nil, "name") setupGroupUpdateMocks("id", util.MockRootGroupID, "name_2") }, Config: provider.ProviderConfig + ` From fab3ff7b7e66a07c9565dec699cf7c2b48b7b4bf Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Fri, 13 Dec 2024 11:13:29 +0200 Subject: [PATCH 16/39] test: change ids and names for basic create --- test/mocked/resources/group_test.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 656f577..560c33b 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -34,13 +34,13 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { { PreConfig: func() { parentID := new(string) - *parentID = "parent_id" - setupGroupCreateMocks("id", parentID, "name") + *parentID = "create_parent" + setupGroupCreateMocks("create_id", parentID, "test_name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { - name = "name" - parent_group_id = "parent_id" + name = "test_name" + parent_group_id = "create_parent" }`, ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ @@ -55,31 +55,31 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { plancheck.ExpectKnownValue( "hpeuxi_group.my_group", tfjsonpath.New("parent_group_id"), - knownvalue.StringExact("parent_id"), + knownvalue.StringExact("create_parent"), ), plancheck.ExpectKnownValue( "hpeuxi_group.my_group", tfjsonpath.New("name"), - knownvalue.StringExact("name"), + knownvalue.StringExact("test_name"), ), }, }, Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", "name"), + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", "test_name"), resource.TestCheckResourceAttr( "hpeuxi_group.my_group", "parent_group_id", - "parent_id", + "create_parent", ), - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", "id"), + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", "create_id"), ), }, // Delete { PreConfig: func() { parentID := new(string) - *parentID = "parent_id" - setupGroupDeleteMocks("id", parentID, "name") + *parentID = "create_parent" + setupGroupDeleteMocks("create_id", parentID, "test_name") }, Config: provider.ProviderConfig, }, From 85b58908de43975bbd2daef5a85980966dd82209 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Fri, 13 Dec 2024 11:16:32 +0200 Subject: [PATCH 17/39] test: update parent and id for import group --- test/mocked/resources/group_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 560c33b..c3e58ca 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -261,21 +261,21 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { { PreConfig: func() { parentID := new(string) - *parentID = "parent_id" - setupGroupCreateMocks("id", parentID, "name") + *parentID = "import_parent" + setupGroupCreateMocks("import_id", parentID, "import_name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { - name = "name" - parent_group_id = "parent_id" + name = "import_name" + parent_group_id = "import_parent" }`, }, // ImportState { PreConfig: func() { parentID := new(string) - *parentID = "parent_id" - setupGroupImportMocks("id", parentID, "name") + *parentID = "import_parent" + setupGroupImportMocks("import_id", parentID, "import_name") }, ResourceName: "hpeuxi_group.my_group", ImportState: true, @@ -285,8 +285,8 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { { PreConfig: func() { parentID := new(string) - *parentID = "parent_id" - setupGroupDeleteMocks("id", parentID, "name") + *parentID = "import_parent" + setupGroupDeleteMocks("import_id", parentID, "import_name") }, Config: provider.ProviderConfig, }, From e98ee65260441a34121035d11d1c89f9448625e9 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Fri, 13 Dec 2024 11:25:31 +0200 Subject: [PATCH 18/39] test: re organize functions --- test/mocked/resources/group_test.go | 291 ++++++++++++++-------------- 1 file changed, 146 insertions(+), 145 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index c3e58ca..ccd6d38 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -23,72 +23,6 @@ import ( "github.com/aruba-uxi/terraform-provider-hpeuxi/test/shared" ) -func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { - defer gock.OffAll() - mockOAuth := util.MockOAuth() - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, - Steps: []resource.TestStep{ - // Create - { - PreConfig: func() { - parentID := new(string) - *parentID = "create_parent" - setupGroupCreateMocks("create_id", parentID, "test_name") - }, - Config: provider.ProviderConfig + ` - resource "hpeuxi_group" "my_group" { - name = "test_name" - parent_group_id = "create_parent" - }`, - ConfigPlanChecks: resource.ConfigPlanChecks{ - PreApply: []plancheck.PlanCheck{ - plancheck.ExpectResourceAction( - "hpeuxi_group.my_group", - plancheck.ResourceActionCreate, - ), - plancheck.ExpectUnknownValue( - "hpeuxi_group.my_group", - tfjsonpath.New("id"), - ), - plancheck.ExpectKnownValue( - "hpeuxi_group.my_group", - tfjsonpath.New("parent_group_id"), - knownvalue.StringExact("create_parent"), - ), - plancheck.ExpectKnownValue( - "hpeuxi_group.my_group", - tfjsonpath.New("name"), - knownvalue.StringExact("test_name"), - ), - }, - }, - Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", "test_name"), - resource.TestCheckResourceAttr( - "hpeuxi_group.my_group", - "parent_group_id", - "create_parent", - ), - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", "create_id"), - ), - }, - // Delete - { - PreConfig: func() { - parentID := new(string) - *parentID = "create_parent" - setupGroupDeleteMocks("create_id", parentID, "test_name") - }, - Config: provider.ProviderConfig, - }, - }, - }) - - mockOAuth.Mock.Disable() -} - func setupGroupCreateMocks(groupID string, parentID *string, name string) { groupPath := util.MockRootGroupID + "." + groupID realParent := util.MockRootGroupID @@ -105,6 +39,80 @@ func setupGroupCreateMocks(groupID string, parentID *string, name string) { util.MockGetGroup(groupID, getResponse, 1) } +func setupGroupUpdateMocks(groupID string, parentID string, name string) { + path := parentID + "." + groupID + if parentID == util.MockRootGroupID { + util.MockGetGroup(util.MockRootGroupID, util.GenerateRootGroupGetResponse(), 1) + } else { + util.MockGetGroup( + parentID, + createGroupGetResponse( + parentID, + "fake_parent_id", + "fake_parent_id."+parentID, + "fake_parent", + ), + 1, + ) + } + + util.MockPatchGroup( + "id", + createGroupPatchRequest(name), + createGroupPatchResponse(groupID, name, path, parentID), + 1, + ) + + util.MockGetGroup( + "id", + createGroupGetResponse(groupID, parentID, path, name), + 1, + ) +} + +func setupGroupDeleteMocks(groupID string, parentID *string, name string) { + groupPath := util.MockRootGroupID + "." + groupID + realParent := util.MockRootGroupID + if parentID != nil { + realParent := *parentID + groupPath = realParent + "." + groupID + } + getResponse := createGroupGetResponse(groupID, realParent, groupPath, name) + util.MockGetGroup( + groupID, + getResponse, + 1, + ) + util.MockDeleteGroup(groupID, 1) +} + +func setupGroupImportMocks(groupID string, parentID *string, name string) { + if parentID != nil { + realParent := *parentID + groupPath := realParent + "." + groupID + + getParentResponse := createGroupGetResponse( + realParent, + "fake_parent_id", + "fake_parent_id."+realParent, + "fake parent", + ) + + util.MockGetGroup(realParent, getParentResponse, 1) + + getResponse := createGroupGetResponse(groupID, realParent, groupPath, name) + util.MockGetGroup(groupID, getResponse, 1) + } else { + groupPath := util.MockRootGroupID + "." + groupID + + rootResponse := util.GenerateRootGroupGetResponse() + util.MockGetGroup(util.MockRootGroupID, rootResponse, 1) + + getResponse := createGroupGetResponse(groupID, util.MockRootGroupID, groupPath, name) + util.MockGetGroup(groupID, getResponse, 1) + } +} + func createGroupPostRequest(name string, parentID *string) config_api_client.GroupPostRequest { postRequest := config_api_client.NewGroupPostRequest(name) if parentID != nil { @@ -176,83 +184,82 @@ func createGroupPatchResponse( return *response } -func setupGroupUpdateMocks(groupID string, parentID string, name string) { - path := parentID + "." + groupID - if parentID == util.MockRootGroupID { - util.MockGetGroup(util.MockRootGroupID, util.GenerateRootGroupGetResponse(), 1) - } else { - util.MockGetGroup( - parentID, - createGroupGetResponse( - parentID, - "fake_parent_id", - "fake_parent_id."+parentID, - "fake_parent", - ), - 1, - ) - } - - util.MockPatchGroup( - "id", - createGroupPatchRequest(name), - createGroupPatchResponse(groupID, name, path, parentID), - 1, - ) - - util.MockGetGroup( - "id", - createGroupGetResponse(groupID, parentID, path, name), - 1, - ) -} - -func setupGroupDeleteMocks(groupID string, parentID *string, name string) { - groupPath := util.MockRootGroupID + "." + groupID - realParent := util.MockRootGroupID - if parentID != nil { - realParent := *parentID - groupPath = realParent + "." + groupID - } - getResponse := createGroupGetResponse(groupID, realParent, groupPath, name) - util.MockGetGroup( - groupID, - getResponse, - 1, - ) - util.MockDeleteGroup(groupID, 1) -} - -func setupGroupImportMocks(groupID string, parentID *string, name string) { - if parentID != nil { - realParent := *parentID - groupPath := realParent + "." + groupID - - getParentResponse := createGroupGetResponse( - realParent, - "fake_parent_id", - "fake_parent_id."+realParent, - "fake parent", - ) - - util.MockGetGroup(realParent, getParentResponse, 1) - - getResponse := createGroupGetResponse(groupID, realParent, groupPath, name) - util.MockGetGroup(groupID, getResponse, 1) - } else { - groupPath := util.MockRootGroupID + "." + groupID +func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { + defer gock.OffAll() + mockOAuth := util.MockOAuth() + parentID := new(string) + *parentID = "create_parent" + testGroupID := "create_id" - rootResponse := util.GenerateRootGroupGetResponse() - util.MockGetGroup(util.MockRootGroupID, rootResponse, 1) + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Create + { + PreConfig: func() { + parentID := new(string) + *parentID = "create_parent" + setupGroupCreateMocks("create_id", parentID, "test_name") + }, + Config: provider.ProviderConfig + ` + resource "hpeuxi_group" "my_group" { + name = "test_name" + parent_group_id = "create_parent" + }`, + ConfigPlanChecks: resource.ConfigPlanChecks{ + PreApply: []plancheck.PlanCheck{ + plancheck.ExpectResourceAction( + "hpeuxi_group.my_group", + plancheck.ResourceActionCreate, + ), + plancheck.ExpectUnknownValue( + "hpeuxi_group.my_group", + tfjsonpath.New("id"), + ), + plancheck.ExpectKnownValue( + "hpeuxi_group.my_group", + tfjsonpath.New("parent_group_id"), + knownvalue.StringExact("create_parent"), + ), + plancheck.ExpectKnownValue( + "hpeuxi_group.my_group", + tfjsonpath.New("name"), + knownvalue.StringExact("test_name"), + ), + }, + }, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", "test_name"), + resource.TestCheckResourceAttr( + "hpeuxi_group.my_group", + "parent_group_id", + "create_parent", + ), + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", "create_id"), + ), + }, + // Delete + { + PreConfig: func() { + parentID := new(string) + *parentID = "create_parent" + setupGroupDeleteMocks("create_id", parentID, "test_name") + }, + Config: provider.ProviderConfig, + }, + }, + }) - getResponse := createGroupGetResponse(groupID, util.MockRootGroupID, groupPath, name) - util.MockGetGroup(groupID, getResponse, 1) - } + mockOAuth.Mock.Disable() } func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() + parentID := new(string) + *parentID = "import_parent" + testName := "import_name" + testID := "import_id" resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -260,9 +267,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - parentID := new(string) - *parentID = "import_parent" - setupGroupCreateMocks("import_id", parentID, "import_name") + setupGroupCreateMocks(testID, parentID, testName) }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -273,9 +278,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // ImportState { PreConfig: func() { - parentID := new(string) - *parentID = "import_parent" - setupGroupImportMocks("import_id", parentID, "import_name") + setupGroupImportMocks(testID, parentID, testName) }, ResourceName: "hpeuxi_group.my_group", ImportState: true, @@ -284,9 +287,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - parentID := new(string) - *parentID = "import_parent" - setupGroupDeleteMocks("import_id", parentID, "import_name") + setupGroupDeleteMocks(testID, parentID, testName) }, Config: provider.ProviderConfig, }, From 2f82521be38873457e29052cbab189099d165974 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Fri, 13 Dec 2024 11:29:31 +0200 Subject: [PATCH 19/39] test: pull up test values --- test/mocked/resources/group_test.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index ccd6d38..710dc2c 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -190,6 +190,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { parentID := new(string) *parentID = "create_parent" testGroupID := "create_id" + testName := "test_name" resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -197,9 +198,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - parentID := new(string) - *parentID = "create_parent" - setupGroupCreateMocks("create_id", parentID, "test_name") + setupGroupCreateMocks(testGroupID, parentID, testName) }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -224,26 +223,24 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { plancheck.ExpectKnownValue( "hpeuxi_group.my_group", tfjsonpath.New("name"), - knownvalue.StringExact("test_name"), + knownvalue.StringExact(testName), ), }, }, Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", "test_name"), + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", testName), resource.TestCheckResourceAttr( "hpeuxi_group.my_group", "parent_group_id", "create_parent", ), - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", "create_id"), + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", testGroupID), ), }, // Delete { PreConfig: func() { - parentID := new(string) - *parentID = "create_parent" - setupGroupDeleteMocks("create_id", parentID, "test_name") + setupGroupDeleteMocks(testGroupID, parentID, testName) }, Config: provider.ProviderConfig, }, From 6edf0d7ca9370a6eaec11513b34e06dfdc55d9e7 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Fri, 13 Dec 2024 13:37:41 +0200 Subject: [PATCH 20/39] test: update root values --- test/mocked/resources/group_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 710dc2c..8cfa863 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -362,11 +362,11 @@ func Test_ImportGroupResource_WithRoot_ShouldFail(t *testing.T) { // Importing the root group does not work { PreConfig: func() { - setupGroupImportMocks(util.MockRootGroupID, nil, "name") + setupGroupImportMocks(util.MockRootGroupID, nil, "root") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_root_group" { - name = "name" + name = "root" } import { From 77efd65eb346748d5177f340fe5384c96c16e9e8 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Fri, 13 Dec 2024 13:41:16 +0200 Subject: [PATCH 21/39] test: extract values for root child creation --- test/mocked/resources/group_test.go | 32 +++++++++++++++-------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 8cfa863..0668cd4 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -187,8 +187,8 @@ func createGroupPatchResponse( func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() - parentID := new(string) - *parentID = "create_parent" + testParentID := new(string) + *testParentID = "create_parent" testGroupID := "create_id" testName := "test_name" @@ -198,7 +198,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - setupGroupCreateMocks(testGroupID, parentID, testName) + setupGroupCreateMocks(testGroupID, testParentID, testName) }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -240,7 +240,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(testGroupID, parentID, testName) + setupGroupDeleteMocks(testGroupID, testParentID, testName) }, Config: provider.ProviderConfig, }, @@ -253,8 +253,8 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() - parentID := new(string) - *parentID = "import_parent" + testParentID := new(string) + *testParentID = "import_parent" testName := "import_name" testID := "import_id" @@ -264,7 +264,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - setupGroupCreateMocks(testID, parentID, testName) + setupGroupCreateMocks(testID, testParentID, testName) }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -275,7 +275,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // ImportState { PreConfig: func() { - setupGroupImportMocks(testID, parentID, testName) + setupGroupImportMocks(testID, testParentID, testName) }, ResourceName: "hpeuxi_group.my_group", ImportState: true, @@ -284,7 +284,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(testID, parentID, testName) + setupGroupDeleteMocks(testID, testParentID, testName) }, Config: provider.ProviderConfig, }, @@ -297,6 +297,8 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { func Test_CreateGroupResource_WithRootParent(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() + testGroupID := "root_child" + testName := "child of root" resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -304,7 +306,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { // Creating a group attached to the root { PreConfig: func() { - setupGroupCreateMocks("id", nil, "name") + setupGroupCreateMocks(testGroupID, nil, testName) }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -318,7 +320,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { ), plancheck.ExpectUnknownValue( "hpeuxi_group.my_group", - tfjsonpath.New("id"), + tfjsonpath.New(testGroupID), ), plancheck.ExpectKnownValue( "hpeuxi_group.my_group", @@ -328,13 +330,13 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { plancheck.ExpectKnownValue( "hpeuxi_group.my_group", tfjsonpath.New("name"), - knownvalue.StringExact("name"), + knownvalue.StringExact(testName), ), }, }, Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", "id"), - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", "name"), + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", testGroupID), + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", testName), resource.TestCheckNoResourceAttr("hpeuxi_group.my_group", "parent_group_id"), ), }, @@ -342,7 +344,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { { PreConfig: func() { // existing group - setupGroupDeleteMocks("id", nil, "name") + setupGroupDeleteMocks(testGroupID, nil, testName) }, Config: provider.ProviderConfig, }, From ec89a8788ce00e09cbaaa150d1820dcab58acfeb Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 09:42:17 +0200 Subject: [PATCH 22/39] test: extract constants for update test --- test/mocked/resources/group_test.go | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 0668cd4..9d7d1c1 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -310,7 +310,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { - name = "name" + name = "child of root" }`, ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ @@ -320,7 +320,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { ), plancheck.ExpectUnknownValue( "hpeuxi_group.my_group", - tfjsonpath.New(testGroupID), + tfjsonpath.New("id"), ), plancheck.ExpectKnownValue( "hpeuxi_group.my_group", @@ -386,6 +386,9 @@ func Test_ImportGroupResource_WithRoot_ShouldFail(t *testing.T) { func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() + parentID := new(string) + *parentID = "parent_id" + testGroupID := "no_recreate" resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -393,9 +396,7 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - parentID := new(string) - *parentID = "parent_id" - setupGroupCreateMocks("id", parentID, "name") + setupGroupCreateMocks(testGroupID, parentID, "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -406,12 +407,10 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { // Update that does not trigger a recreate { PreConfig: func() { - parentID := new(string) - *parentID = "parent_id" - setupGroupImportMocks("id", parentID, "name") + setupGroupImportMocks(testGroupID, parentID, "name") // updated group - setupGroupUpdateMocks("id", *parentID, "name_2") + setupGroupUpdateMocks(testGroupID, *parentID, "name_2") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -436,17 +435,15 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { resource.TestCheckResourceAttr( "hpeuxi_group.my_group", "parent_group_id", - "parent_id", + *parentID, ), - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", "id"), + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", testGroupID), ), }, // Delete { PreConfig: func() { - parentID := new(string) - *parentID = "parent_id" - setupGroupDeleteMocks("id", parentID, "name_2") + setupGroupDeleteMocks(testGroupID, parentID, "name_2") }, Config: provider.ProviderConfig, }, From 9296dc9a3b74b322dab27e256f18297e0bfd8542 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 09:50:52 +0200 Subject: [PATCH 23/39] test: fix tests for update not passing groupID --- test/mocked/resources/group_test.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 9d7d1c1..12d106e 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -57,14 +57,14 @@ func setupGroupUpdateMocks(groupID string, parentID string, name string) { } util.MockPatchGroup( - "id", + groupID, createGroupPatchRequest(name), createGroupPatchResponse(groupID, name, path, parentID), 1, ) util.MockGetGroup( - "id", + groupID, createGroupGetResponse(groupID, parentID, path, name), 1, ) @@ -456,6 +456,7 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() + testGroupID := "id" resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -463,7 +464,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - setupGroupCreateMocks("id", nil, "name") + setupGroupCreateMocks(testGroupID, nil, "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -473,8 +474,8 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // Update that does not trigger a recreate { PreConfig: func() { - setupGroupImportMocks("id", nil, "name") - setupGroupUpdateMocks("id", util.MockRootGroupID, "name_2") + setupGroupImportMocks(testGroupID, nil, "name") + setupGroupUpdateMocks(testGroupID, util.MockRootGroupID, "name_2") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -496,13 +497,13 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", "name_2"), resource.TestCheckNoResourceAttr("hpeuxi_group.my_group", "parent_group_id"), - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", "id"), + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", testGroupID), ), }, // Delete { PreConfig: func() { - setupGroupDeleteMocks("id", nil, "name_2") + setupGroupDeleteMocks(testGroupID, nil, "name_2") }, Config: provider.ProviderConfig, }, From 18f99f80c67339e662b841e43d3bbcaffbdb1526 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 09:58:04 +0200 Subject: [PATCH 24/39] test: extract values for recreate update test --- test/mocked/resources/group_test.go | 32 ++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 12d106e..1b27c9e 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -517,15 +517,21 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() + oldParentID := new(string) + *oldParentID = "parent_id" + oldGroupID := "id" + + newParentID := new(string) + *newParentID = "parent_id_2" + newGroupID := "new_id" + resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Create { PreConfig: func() { - parentID := new(string) - *parentID = "parent_id" - setupGroupCreateMocks("id", parentID, "name") + setupGroupCreateMocks(oldGroupID, oldParentID, "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -536,14 +542,10 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { // Update that does trigger a recreate { PreConfig: func() { - oldParentID := new(string) - *oldParentID = "parent_id" - setupGroupImportMocks("id", oldParentID, "name") - setupGroupDeleteMocks("id", oldParentID, "name") + setupGroupImportMocks(oldGroupID, oldParentID, "name") + setupGroupDeleteMocks(oldGroupID, oldParentID, "name") - newParentID := new(string) - *newParentID = "parent_id_2" - setupGroupCreateMocks("new_id", newParentID, "name") + setupGroupCreateMocks(newGroupID, newParentID, "name") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -568,7 +570,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { plancheck.ExpectKnownValue( "hpeuxi_group.my_group", tfjsonpath.New("parent_group_id"), - knownvalue.StringExact("parent_id_2"), + knownvalue.StringExact(*newParentID), ), }, }, @@ -577,17 +579,15 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { resource.TestCheckResourceAttr( "hpeuxi_group.my_group", "parent_group_id", - "parent_id_2", + *newParentID, ), - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", "new_id"), + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", newGroupID), ), }, // Delete { PreConfig: func() { - parentID := new(string) - *parentID = "parent_id_2" - setupGroupDeleteMocks("new_id", parentID, "name") + setupGroupDeleteMocks(newGroupID, newParentID, "name") }, Config: provider.ProviderConfig, }, From 6b2384f3f64ae0b85598e1ae8318545ed2fd46fc Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 10:16:06 +0200 Subject: [PATCH 25/39] test: extract testName to variable --- test/mocked/resources/group_test.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 1b27c9e..eba8965 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -525,13 +525,15 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { *newParentID = "parent_id_2" newGroupID := "new_id" + testName := "name" + resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Create { PreConfig: func() { - setupGroupCreateMocks(oldGroupID, oldParentID, "name") + setupGroupCreateMocks(oldGroupID, oldParentID, testName) }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -542,10 +544,10 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { // Update that does trigger a recreate { PreConfig: func() { - setupGroupImportMocks(oldGroupID, oldParentID, "name") - setupGroupDeleteMocks(oldGroupID, oldParentID, "name") + setupGroupImportMocks(oldGroupID, oldParentID, testName) + setupGroupDeleteMocks(oldGroupID, oldParentID, testName) - setupGroupCreateMocks(newGroupID, newParentID, "name") + setupGroupCreateMocks(newGroupID, newParentID, testName) }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_group" { @@ -565,7 +567,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { plancheck.ExpectKnownValue( "hpeuxi_group.my_group", tfjsonpath.New("name"), - knownvalue.StringExact("name"), + knownvalue.StringExact(testName), ), plancheck.ExpectKnownValue( "hpeuxi_group.my_group", @@ -575,7 +577,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { }, }, Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", "name"), + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", testName), resource.TestCheckResourceAttr( "hpeuxi_group.my_group", "parent_group_id", @@ -587,7 +589,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(newGroupID, newParentID, "name") + setupGroupDeleteMocks(newGroupID, newParentID, testName) }, Config: provider.ProviderConfig, }, From 6b384ffe088e7a685fe6b1105b25603d879b5b43 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 10:22:23 +0200 Subject: [PATCH 26/39] test: use sprintf on the test manifest --- test/mocked/resources/group_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index eba8965..39d3683 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -5,6 +5,7 @@ Copyright 2024 Hewlett Packard Enterprise Development LP. package resource_test import ( + "fmt" "net/http" "regexp" "testing" @@ -200,11 +201,11 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { PreConfig: func() { setupGroupCreateMocks(testGroupID, testParentID, testName) }, - Config: provider.ProviderConfig + ` + Config: fmt.Sprintf(`%s resource "hpeuxi_group" "my_group" { - name = "test_name" - parent_group_id = "create_parent" - }`, + name = "%s" + parent_group_id = "%s" + }`, provider.ProviderConfig, testName, *testParentID), ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ plancheck.ExpectResourceAction( From 6319592cf1980cef940ea9be36e8a38bcaa87fae Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 10:24:03 +0200 Subject: [PATCH 27/39] test: use interpolation for another manifest --- test/mocked/resources/group_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 39d3683..0526095 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -267,11 +267,11 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { PreConfig: func() { setupGroupCreateMocks(testID, testParentID, testName) }, - Config: provider.ProviderConfig + ` + Config: fmt.Sprintf(`%s resource "hpeuxi_group" "my_group" { - name = "import_name" - parent_group_id = "import_parent" - }`, + name = "%s" + parent_group_id = "%s" + }`, provider.ProviderConfig, testName, *testParentID), }, // ImportState { From f1c9401401abed47720cae9a54660da26f1962ab Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 10:25:30 +0200 Subject: [PATCH 28/39] test: use string interpolation for another manifest --- test/mocked/resources/group_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 0526095..f6f38bf 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -309,10 +309,10 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { PreConfig: func() { setupGroupCreateMocks(testGroupID, nil, testName) }, - Config: provider.ProviderConfig + ` + Config: fmt.Sprintf(`%s resource "hpeuxi_group" "my_group" { - name = "child of root" - }`, + name = "%s" + }`, provider.ProviderConfig, testName), ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ plancheck.ExpectResourceAction( From b0049265f5004963474e058952398dc176a1049e Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 10:30:02 +0200 Subject: [PATCH 29/39] test: use string interpolation for yet another manifest --- test/mocked/resources/group_test.go | 39 ++++++++++++++++------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index f6f38bf..6ddc596 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -387,37 +387,40 @@ func Test_ImportGroupResource_WithRoot_ShouldFail(t *testing.T) { func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() - parentID := new(string) - *parentID = "parent_id" + testParentID := new(string) + *testParentID = "parent_id" testGroupID := "no_recreate" + oldTestName := "name" + newTestName := "name_2" + resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Create { PreConfig: func() { - setupGroupCreateMocks(testGroupID, parentID, "name") + setupGroupCreateMocks(testGroupID, testParentID, oldTestName) }, - Config: provider.ProviderConfig + ` + Config: fmt.Sprintf(`%s resource "hpeuxi_group" "my_group" { - name = "name" - parent_group_id = "parent_id" - }`, + name = "%s" + parent_group_id = "%s" + }`, provider.ProviderConfig, oldTestName, *testParentID), }, // Update that does not trigger a recreate { PreConfig: func() { - setupGroupImportMocks(testGroupID, parentID, "name") + setupGroupImportMocks(testGroupID, testParentID, oldTestName) // updated group - setupGroupUpdateMocks(testGroupID, *parentID, "name_2") + setupGroupUpdateMocks(testGroupID, *testParentID, newTestName) }, - Config: provider.ProviderConfig + ` - resource "hpeuxi_group" "my_group" { - name = "name_2" - parent_group_id = "parent_id" - }`, + Config: fmt.Sprintf(`%s + resource "hpeuxi_group" "my_group" { + name = "%s" + parent_group_id = "%s" + }`, provider.ProviderConfig, newTestName, *testParentID), ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ plancheck.ExpectResourceAction( @@ -427,16 +430,16 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { plancheck.ExpectKnownValue( "hpeuxi_group.my_group", tfjsonpath.New("name"), - knownvalue.StringExact("name_2"), + knownvalue.StringExact(newTestName), ), }, }, Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", "name_2"), + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", newTestName), resource.TestCheckResourceAttr( "hpeuxi_group.my_group", "parent_group_id", - *parentID, + *testParentID, ), resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", testGroupID), ), @@ -444,7 +447,7 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(testGroupID, parentID, "name_2") + setupGroupDeleteMocks(testGroupID, testParentID, newTestName) }, Config: provider.ProviderConfig, }, From ae9adbcd5644a480509c2cddd39f72f344289b1a Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 10:33:43 +0200 Subject: [PATCH 30/39] test: interpolate more manifests --- test/mocked/resources/group_test.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 6ddc596..06ce64a 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -461,6 +461,8 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() testGroupID := "id" + oldTestName := "name" + newTestName := "name_2" resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -468,23 +470,23 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - setupGroupCreateMocks(testGroupID, nil, "name") + setupGroupCreateMocks(testGroupID, nil, oldTestName) }, - Config: provider.ProviderConfig + ` + Config: fmt.Sprintf(`%s resource "hpeuxi_group" "my_group" { - name = "name" - }`, + name = "%s" + }`, provider.ProviderConfig, oldTestName), }, // Update that does not trigger a recreate { PreConfig: func() { - setupGroupImportMocks(testGroupID, nil, "name") - setupGroupUpdateMocks(testGroupID, util.MockRootGroupID, "name_2") + setupGroupImportMocks(testGroupID, nil, oldTestName) + setupGroupUpdateMocks(testGroupID, util.MockRootGroupID, newTestName) }, - Config: provider.ProviderConfig + ` - resource "hpeuxi_group" "my_group" { - name = "name_2" - }`, + Config: fmt.Sprintf(`%s + resource "hpeuxi_group" "my_group" { + name = "%s" + }`, provider.ProviderConfig, newTestName), ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ plancheck.ExpectResourceAction( @@ -494,12 +496,12 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { plancheck.ExpectKnownValue( "hpeuxi_group.my_group", tfjsonpath.New("name"), - knownvalue.StringExact("name_2"), + knownvalue.StringExact(newTestName), ), }, }, Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", "name_2"), + resource.TestCheckResourceAttr("hpeuxi_group.my_group", "name", newTestName), resource.TestCheckNoResourceAttr("hpeuxi_group.my_group", "parent_group_id"), resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", testGroupID), ), @@ -507,7 +509,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(testGroupID, nil, "name_2") + setupGroupDeleteMocks(testGroupID, nil, newTestName) }, Config: provider.ProviderConfig, }, From 91cb8a90cbd56b022413807a1d34cf016b580049 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 10:37:39 +0200 Subject: [PATCH 31/39] test: interpolate one more manifest --- test/mocked/resources/group_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 06ce64a..cc61907 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -541,11 +541,11 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { PreConfig: func() { setupGroupCreateMocks(oldGroupID, oldParentID, testName) }, - Config: provider.ProviderConfig + ` + Config: fmt.Sprintf(`%s resource "hpeuxi_group" "my_group" { - name = "name" - parent_group_id = "parent_id" - }`, + name = "%s" + parent_group_id = "%s" + }`, provider.ProviderConfig, testName, *oldParentID), }, // Update that does trigger a recreate { @@ -555,11 +555,11 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { setupGroupCreateMocks(newGroupID, newParentID, testName) }, - Config: provider.ProviderConfig + ` - resource "hpeuxi_group" "my_group" { - name = "name" - parent_group_id = "parent_id_2" - }`, + Config: fmt.Sprintf(`%s + resource "hpeuxi_group" "my_group" { + name = "%s" + parent_group_id = "%s" + }`, provider.ProviderConfig, testName, *newParentID), ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ plancheck.ExpectResourceAction( From 4f9d97c63e83372b167ca8842c13465b5881d4cf Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 14:39:55 +0200 Subject: [PATCH 32/39] test: use "real" parent_id for all requests --- test/mocked/resources/group_test.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index cc61907..6a98759 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -28,13 +28,19 @@ func setupGroupCreateMocks(groupID string, parentID *string, name string) { groupPath := util.MockRootGroupID + "." + groupID realParent := util.MockRootGroupID if parentID != nil { - realParent := *parentID + realParent = *parentID groupPath = realParent + "." + groupID } - postRequest := createGroupPostRequest(name, parentID) - postResponse := createGroupPostResponse(groupID, groupPath, name, realParent) - util.MockPostGroup(postRequest, postResponse, 1) + if realParent == util.MockRootGroupID { + postRequest := createGroupPostRequest(name, nil) + postResponse := createGroupPostResponse(groupID, groupPath, name, realParent) + util.MockPostGroup(postRequest, postResponse, 1) + } else { + postRequest := createGroupPostRequest(name, parentID) + postResponse := createGroupPostResponse(groupID, groupPath, name, realParent) + util.MockPostGroup(postRequest, postResponse, 1) + } getResponse := createGroupGetResponse(groupID, realParent, groupPath, name) util.MockGetGroup(groupID, getResponse, 1) @@ -300,6 +306,8 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { mockOAuth := util.MockOAuth() testGroupID := "root_child" testName := "child of root" + testParentID := new(string) + *testParentID = util.MockRootGroupID resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -307,7 +315,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { // Creating a group attached to the root { PreConfig: func() { - setupGroupCreateMocks(testGroupID, nil, testName) + setupGroupCreateMocks(testGroupID, testParentID, testName) }, Config: fmt.Sprintf(`%s resource "hpeuxi_group" "my_group" { @@ -463,6 +471,8 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { testGroupID := "id" oldTestName := "name" newTestName := "name_2" + testParentID := new(string) + *testParentID = util.MockRootGroupID resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -470,7 +480,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - setupGroupCreateMocks(testGroupID, nil, oldTestName) + setupGroupCreateMocks(testGroupID, testParentID, oldTestName) }, Config: fmt.Sprintf(`%s resource "hpeuxi_group" "my_group" { From 911912aafe81a1b351c8204032158a7cef69ada6 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 14:50:45 +0200 Subject: [PATCH 33/39] test: extract group create request creation --- test/mocked/resources/group_test.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 6a98759..b8bd12d 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -32,15 +32,14 @@ func setupGroupCreateMocks(groupID string, parentID *string, name string) { groupPath = realParent + "." + groupID } + var postRequest config_api_client.GroupPostRequest if realParent == util.MockRootGroupID { - postRequest := createGroupPostRequest(name, nil) - postResponse := createGroupPostResponse(groupID, groupPath, name, realParent) - util.MockPostGroup(postRequest, postResponse, 1) + postRequest = createGroupPostRequest(name, nil) } else { - postRequest := createGroupPostRequest(name, parentID) - postResponse := createGroupPostResponse(groupID, groupPath, name, realParent) - util.MockPostGroup(postRequest, postResponse, 1) + postRequest = createGroupPostRequest(name, parentID) } + postResponse := createGroupPostResponse(groupID, groupPath, name, realParent) + util.MockPostGroup(postRequest, postResponse, 1) getResponse := createGroupGetResponse(groupID, realParent, groupPath, name) util.MockGetGroup(groupID, getResponse, 1) From ca91fcbae59bde64be8eb8882098415e74dd1487 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 15:03:56 +0200 Subject: [PATCH 34/39] test: use str for parentID instead of pointer --- test/mocked/resources/group_test.go | 76 ++++++++++++----------------- 1 file changed, 32 insertions(+), 44 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index b8bd12d..da34ae2 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -24,24 +24,19 @@ import ( "github.com/aruba-uxi/terraform-provider-hpeuxi/test/shared" ) -func setupGroupCreateMocks(groupID string, parentID *string, name string) { - groupPath := util.MockRootGroupID + "." + groupID - realParent := util.MockRootGroupID - if parentID != nil { - realParent = *parentID - groupPath = realParent + "." + groupID - } - +func setupGroupCreateMocks(groupID string, parentID string, name string) { var postRequest config_api_client.GroupPostRequest - if realParent == util.MockRootGroupID { + if parentID == util.MockRootGroupID { postRequest = createGroupPostRequest(name, nil) } else { - postRequest = createGroupPostRequest(name, parentID) + postRequest = createGroupPostRequest(name, &parentID) } - postResponse := createGroupPostResponse(groupID, groupPath, name, realParent) + + groupPath := parentID + "." + groupID + postResponse := createGroupPostResponse(groupID, groupPath, name, parentID) util.MockPostGroup(postRequest, postResponse, 1) - getResponse := createGroupGetResponse(groupID, realParent, groupPath, name) + getResponse := createGroupGetResponse(groupID, parentID, groupPath, name) util.MockGetGroup(groupID, getResponse, 1) } @@ -193,8 +188,7 @@ func createGroupPatchResponse( func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() - testParentID := new(string) - *testParentID = "create_parent" + testParentID := "create_parent" testGroupID := "create_id" testName := "test_name" @@ -210,7 +204,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { resource "hpeuxi_group" "my_group" { name = "%s" parent_group_id = "%s" - }`, provider.ProviderConfig, testName, *testParentID), + }`, provider.ProviderConfig, testName, testParentID), ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ plancheck.ExpectResourceAction( @@ -246,7 +240,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(testGroupID, testParentID, testName) + setupGroupDeleteMocks(testGroupID, &testParentID, testName) }, Config: provider.ProviderConfig, }, @@ -259,8 +253,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() - testParentID := new(string) - *testParentID = "import_parent" + testParentID := "import_parent" testName := "import_name" testID := "import_id" @@ -276,12 +269,12 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { resource "hpeuxi_group" "my_group" { name = "%s" parent_group_id = "%s" - }`, provider.ProviderConfig, testName, *testParentID), + }`, provider.ProviderConfig, testName, testParentID), }, // ImportState { PreConfig: func() { - setupGroupImportMocks(testID, testParentID, testName) + setupGroupImportMocks(testID, &testParentID, testName) }, ResourceName: "hpeuxi_group.my_group", ImportState: true, @@ -290,7 +283,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(testID, testParentID, testName) + setupGroupDeleteMocks(testID, &testParentID, testName) }, Config: provider.ProviderConfig, }, @@ -305,8 +298,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { mockOAuth := util.MockOAuth() testGroupID := "root_child" testName := "child of root" - testParentID := new(string) - *testParentID = util.MockRootGroupID + testParentID := util.MockRootGroupID resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -394,8 +386,7 @@ func Test_ImportGroupResource_WithRoot_ShouldFail(t *testing.T) { func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() - testParentID := new(string) - *testParentID = "parent_id" + testParentID := "parent_id" testGroupID := "no_recreate" oldTestName := "name" @@ -413,21 +404,21 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { resource "hpeuxi_group" "my_group" { name = "%s" parent_group_id = "%s" - }`, provider.ProviderConfig, oldTestName, *testParentID), + }`, provider.ProviderConfig, oldTestName, testParentID), }, // Update that does not trigger a recreate { PreConfig: func() { - setupGroupImportMocks(testGroupID, testParentID, oldTestName) + setupGroupImportMocks(testGroupID, &testParentID, oldTestName) // updated group - setupGroupUpdateMocks(testGroupID, *testParentID, newTestName) + setupGroupUpdateMocks(testGroupID, testParentID, newTestName) }, Config: fmt.Sprintf(`%s resource "hpeuxi_group" "my_group" { name = "%s" parent_group_id = "%s" - }`, provider.ProviderConfig, newTestName, *testParentID), + }`, provider.ProviderConfig, newTestName, testParentID), ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ plancheck.ExpectResourceAction( @@ -446,7 +437,7 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { resource.TestCheckResourceAttr( "hpeuxi_group.my_group", "parent_group_id", - *testParentID, + testParentID, ), resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", testGroupID), ), @@ -454,7 +445,7 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(testGroupID, testParentID, newTestName) + setupGroupDeleteMocks(testGroupID, &testParentID, newTestName) }, Config: provider.ProviderConfig, }, @@ -470,8 +461,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { testGroupID := "id" oldTestName := "name" newTestName := "name_2" - testParentID := new(string) - *testParentID = util.MockRootGroupID + testParentID := util.MockRootGroupID resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -532,12 +522,10 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() - oldParentID := new(string) - *oldParentID = "parent_id" + oldParentID := "parent_id" oldGroupID := "id" - newParentID := new(string) - *newParentID = "parent_id_2" + newParentID := "parent_id_2" newGroupID := "new_id" testName := "name" @@ -554,13 +542,13 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { resource "hpeuxi_group" "my_group" { name = "%s" parent_group_id = "%s" - }`, provider.ProviderConfig, testName, *oldParentID), + }`, provider.ProviderConfig, testName, oldParentID), }, // Update that does trigger a recreate { PreConfig: func() { - setupGroupImportMocks(oldGroupID, oldParentID, testName) - setupGroupDeleteMocks(oldGroupID, oldParentID, testName) + setupGroupImportMocks(oldGroupID, &oldParentID, testName) + setupGroupDeleteMocks(oldGroupID, &oldParentID, testName) setupGroupCreateMocks(newGroupID, newParentID, testName) }, @@ -568,7 +556,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { resource "hpeuxi_group" "my_group" { name = "%s" parent_group_id = "%s" - }`, provider.ProviderConfig, testName, *newParentID), + }`, provider.ProviderConfig, testName, newParentID), ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ plancheck.ExpectResourceAction( @@ -587,7 +575,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { plancheck.ExpectKnownValue( "hpeuxi_group.my_group", tfjsonpath.New("parent_group_id"), - knownvalue.StringExact(*newParentID), + knownvalue.StringExact(newParentID), ), }, }, @@ -596,7 +584,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { resource.TestCheckResourceAttr( "hpeuxi_group.my_group", "parent_group_id", - *newParentID, + newParentID, ), resource.TestCheckResourceAttr("hpeuxi_group.my_group", "id", newGroupID), ), @@ -604,7 +592,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(newGroupID, newParentID, testName) + setupGroupDeleteMocks(newGroupID, &newParentID, testName) }, Config: provider.ProviderConfig, }, From 9ae7b1939642525be28d967b218dd2bb82b346e6 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 15:18:59 +0200 Subject: [PATCH 35/39] test: use string for delete group mock setup --- test/mocked/resources/group_test.go | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index da34ae2..2c25c4e 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -71,14 +71,9 @@ func setupGroupUpdateMocks(groupID string, parentID string, name string) { ) } -func setupGroupDeleteMocks(groupID string, parentID *string, name string) { - groupPath := util.MockRootGroupID + "." + groupID - realParent := util.MockRootGroupID - if parentID != nil { - realParent := *parentID - groupPath = realParent + "." + groupID - } - getResponse := createGroupGetResponse(groupID, realParent, groupPath, name) +func setupGroupDeleteMocks(groupID string, parentID string, name string) { + groupPath := parentID + "." + groupID + getResponse := createGroupGetResponse(groupID, parentID, groupPath, name) util.MockGetGroup( groupID, getResponse, @@ -240,7 +235,7 @@ func Test_CreateGroupResource_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(testGroupID, &testParentID, testName) + setupGroupDeleteMocks(testGroupID, testParentID, testName) }, Config: provider.ProviderConfig, }, @@ -283,7 +278,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(testID, &testParentID, testName) + setupGroupDeleteMocks(testID, testParentID, testName) }, Config: provider.ProviderConfig, }, @@ -344,7 +339,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { { PreConfig: func() { // existing group - setupGroupDeleteMocks(testGroupID, nil, testName) + setupGroupDeleteMocks(testGroupID, testParentID, testName) }, Config: provider.ProviderConfig, }, @@ -445,7 +440,7 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(testGroupID, &testParentID, newTestName) + setupGroupDeleteMocks(testGroupID, testParentID, newTestName) }, Config: provider.ProviderConfig, }, @@ -508,7 +503,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(testGroupID, nil, newTestName) + setupGroupDeleteMocks(testGroupID, testParentID, newTestName) }, Config: provider.ProviderConfig, }, @@ -548,7 +543,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { { PreConfig: func() { setupGroupImportMocks(oldGroupID, &oldParentID, testName) - setupGroupDeleteMocks(oldGroupID, &oldParentID, testName) + setupGroupDeleteMocks(oldGroupID, oldParentID, testName) setupGroupCreateMocks(newGroupID, newParentID, testName) }, @@ -592,7 +587,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(newGroupID, &newParentID, testName) + setupGroupDeleteMocks(newGroupID, newParentID, testName) }, Config: provider.ProviderConfig, }, From 4e919ae299cb4c4b3d50a7ab8d4f2bd25c739daf Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 15:25:23 +0200 Subject: [PATCH 36/39] test: use string in update group mock setup --- test/mocked/resources/group_test.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 2c25c4e..1cd28de 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -82,9 +82,9 @@ func setupGroupDeleteMocks(groupID string, parentID string, name string) { util.MockDeleteGroup(groupID, 1) } -func setupGroupImportMocks(groupID string, parentID *string, name string) { - if parentID != nil { - realParent := *parentID +func setupGroupImportMocks(groupID string, parentID string, name string) { + if parentID != util.MockRootGroupID { + realParent := parentID groupPath := realParent + "." + groupID getParentResponse := createGroupGetResponse( @@ -269,7 +269,7 @@ func Test_ImportGroupResource_ShouldSucceed(t *testing.T) { // ImportState { PreConfig: func() { - setupGroupImportMocks(testID, &testParentID, testName) + setupGroupImportMocks(testID, testParentID, testName) }, ResourceName: "hpeuxi_group.my_group", ImportState: true, @@ -352,6 +352,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { func Test_ImportGroupResource_WithRoot_ShouldFail(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() + testParentID := util.MockRootGroupID resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -359,7 +360,7 @@ func Test_ImportGroupResource_WithRoot_ShouldFail(t *testing.T) { // Importing the root group does not work { PreConfig: func() { - setupGroupImportMocks(util.MockRootGroupID, nil, "root") + setupGroupImportMocks(util.MockRootGroupID, testParentID, "root") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_root_group" { @@ -404,7 +405,7 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { // Update that does not trigger a recreate { PreConfig: func() { - setupGroupImportMocks(testGroupID, &testParentID, oldTestName) + setupGroupImportMocks(testGroupID, testParentID, oldTestName) // updated group setupGroupUpdateMocks(testGroupID, testParentID, newTestName) @@ -474,7 +475,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // Update that does not trigger a recreate { PreConfig: func() { - setupGroupImportMocks(testGroupID, nil, oldTestName) + setupGroupImportMocks(testGroupID, testParentID, oldTestName) setupGroupUpdateMocks(testGroupID, util.MockRootGroupID, newTestName) }, Config: fmt.Sprintf(`%s @@ -542,7 +543,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { // Update that does trigger a recreate { PreConfig: func() { - setupGroupImportMocks(oldGroupID, &oldParentID, testName) + setupGroupImportMocks(oldGroupID, oldParentID, testName) setupGroupDeleteMocks(oldGroupID, oldParentID, testName) setupGroupCreateMocks(newGroupID, newParentID, testName) From fba9181c467d905858c18b3693945e8e827020de Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 15:44:02 +0200 Subject: [PATCH 37/39] test: rename root test groups --- test/mocked/resources/group_test.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index 1cd28de..ce20602 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -293,7 +293,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { mockOAuth := util.MockOAuth() testGroupID := "root_child" testName := "child of root" - testParentID := util.MockRootGroupID + rootParentID := util.MockRootGroupID resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -301,7 +301,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { // Creating a group attached to the root { PreConfig: func() { - setupGroupCreateMocks(testGroupID, testParentID, testName) + setupGroupCreateMocks(testGroupID, rootParentID, testName) }, Config: fmt.Sprintf(`%s resource "hpeuxi_group" "my_group" { @@ -339,7 +339,7 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { { PreConfig: func() { // existing group - setupGroupDeleteMocks(testGroupID, testParentID, testName) + setupGroupDeleteMocks(testGroupID, rootParentID, testName) }, Config: provider.ProviderConfig, }, @@ -352,7 +352,6 @@ func Test_CreateGroupResource_WithRootParent(t *testing.T) { func Test_ImportGroupResource_WithRoot_ShouldFail(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() - testParentID := util.MockRootGroupID resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -360,7 +359,7 @@ func Test_ImportGroupResource_WithRoot_ShouldFail(t *testing.T) { // Importing the root group does not work { PreConfig: func() { - setupGroupImportMocks(util.MockRootGroupID, testParentID, "root") + setupGroupImportMocks(util.MockRootGroupID, util.MockRootGroupID, "root") }, Config: provider.ProviderConfig + ` resource "hpeuxi_group" "my_root_group" { @@ -385,8 +384,8 @@ func Test_UpdateGroupResource_WithoutRecreate_ShouldSucceed(t *testing.T) { testParentID := "parent_id" testGroupID := "no_recreate" - oldTestName := "name" - newTestName := "name_2" + oldTestName := "create_name" + newTestName := "update_name" resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -455,8 +454,8 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { defer gock.OffAll() mockOAuth := util.MockOAuth() testGroupID := "id" - oldTestName := "name" - newTestName := "name_2" + oldTestName := "create_name" + newTestName := "update_name" testParentID := util.MockRootGroupID resource.Test(t, resource.TestCase{ @@ -524,7 +523,7 @@ func Test_UpdateGroupResource_WithRecreate_ShouldSucceed(t *testing.T) { newParentID := "parent_id_2" newGroupID := "new_id" - testName := "name" + testName := "constant_name" resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, From d326fd6f4fc701ed044ec9527f0979204305d2bf Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Tue, 17 Dec 2024 15:49:56 +0200 Subject: [PATCH 38/39] test: shrink if conditional body --- test/mocked/resources/group_test.go | 36 +++++++++++------------------ 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index ce20602..c703d44 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -83,30 +83,22 @@ func setupGroupDeleteMocks(groupID string, parentID string, name string) { } func setupGroupImportMocks(groupID string, parentID string, name string) { + var getParentResponse config_api_client.GroupsGetResponse if parentID != util.MockRootGroupID { - realParent := parentID - groupPath := realParent + "." + groupID - - getParentResponse := createGroupGetResponse( - realParent, + getParentResponse = createGroupGetResponse( + parentID, "fake_parent_id", - "fake_parent_id."+realParent, + "fake_parent_id."+parentID, "fake parent", ) - - util.MockGetGroup(realParent, getParentResponse, 1) - - getResponse := createGroupGetResponse(groupID, realParent, groupPath, name) - util.MockGetGroup(groupID, getResponse, 1) } else { - groupPath := util.MockRootGroupID + "." + groupID - - rootResponse := util.GenerateRootGroupGetResponse() - util.MockGetGroup(util.MockRootGroupID, rootResponse, 1) - - getResponse := createGroupGetResponse(groupID, util.MockRootGroupID, groupPath, name) - util.MockGetGroup(groupID, getResponse, 1) + getParentResponse = util.GenerateRootGroupGetResponse() } + + util.MockGetGroup(parentID, getParentResponse, 1) + groupPath := parentID + "." + groupID + getResponse := createGroupGetResponse(groupID, parentID, groupPath, name) + util.MockGetGroup(groupID, getResponse, 1) } func createGroupPostRequest(name string, parentID *string) config_api_client.GroupPostRequest { @@ -456,7 +448,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { testGroupID := "id" oldTestName := "create_name" newTestName := "update_name" - testParentID := util.MockRootGroupID + rootParentID := util.MockRootGroupID resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -464,7 +456,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // Create { PreConfig: func() { - setupGroupCreateMocks(testGroupID, testParentID, oldTestName) + setupGroupCreateMocks(testGroupID, rootParentID, oldTestName) }, Config: fmt.Sprintf(`%s resource "hpeuxi_group" "my_group" { @@ -474,7 +466,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // Update that does not trigger a recreate { PreConfig: func() { - setupGroupImportMocks(testGroupID, testParentID, oldTestName) + setupGroupImportMocks(testGroupID, rootParentID, oldTestName) setupGroupUpdateMocks(testGroupID, util.MockRootGroupID, newTestName) }, Config: fmt.Sprintf(`%s @@ -503,7 +495,7 @@ func Test_UpdateGroupResource_WithoutParent_ShouldSucceed(t *testing.T) { // Delete { PreConfig: func() { - setupGroupDeleteMocks(testGroupID, testParentID, newTestName) + setupGroupDeleteMocks(testGroupID, rootParentID, newTestName) }, Config: provider.ProviderConfig, }, From 9afa80d7268efc7464582db945d9a0e7f8add353 Mon Sep 17 00:00:00 2001 From: Sias Mey Date: Wed, 18 Dec 2024 09:08:24 +0200 Subject: [PATCH 39/39] test: make root parent check consistent --- test/mocked/resources/group_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/mocked/resources/group_test.go b/test/mocked/resources/group_test.go index c703d44..05ef8e9 100644 --- a/test/mocked/resources/group_test.go +++ b/test/mocked/resources/group_test.go @@ -84,15 +84,15 @@ func setupGroupDeleteMocks(groupID string, parentID string, name string) { func setupGroupImportMocks(groupID string, parentID string, name string) { var getParentResponse config_api_client.GroupsGetResponse - if parentID != util.MockRootGroupID { + if parentID == util.MockRootGroupID { + getParentResponse = util.GenerateRootGroupGetResponse() + } else { getParentResponse = createGroupGetResponse( parentID, "fake_parent_id", "fake_parent_id."+parentID, "fake parent", ) - } else { - getParentResponse = util.GenerateRootGroupGetResponse() } util.MockGetGroup(parentID, getParentResponse, 1)