From 97a8d9cb3f790263d509509d6a202b1d0feb7510 Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Thu, 19 Sep 2024 10:09:00 +0200 Subject: [PATCH] feat: 429 retries --- .../provider/config/config.go | 5 + .../provider/data-sources/group.go | 3 +- .../data-sources/sensor_group_assignment.go | 7 +- .../provider/data-sources/wired_network.go | 7 +- .../provider/data-sources/wireless_network.go | 7 +- .../provider/resources/group.go | 4 +- .../resources/sensor_group_assignment.go | 7 +- .../provider/resources/wired_network.go | 7 +- .../provider/resources/wireless_network.go | 7 +- .../provider/util/retry.go | 34 +++++ .../group_test.go} | 26 ++-- .../sensor_group_assignment_test.go} | 21 +-- .../wired_group_test.go} | 17 ++- .../wireless_group_test.go} | 16 +- .../provider.go} | 8 +- .../agent_group_assignment_test.go | 42 ++--- .../test/{ => resources}/agent_test.go | 23 +-- .../test/{ => resources}/group_test.go | 49 +++--- .../network_group_assignment_test.go | 144 +++++++++--------- .../sensor_group_assignment_test.go | 88 +++++------ .../test/{ => resources}/sensor_test.go | 22 +-- .../test/{ => resources}/service_test.go | 20 ++- .../service_test_group_assignment_test.go | 52 ++++--- .../{ => resources}/wired_network_test.go | 42 ++--- .../{ => resources}/wireless_network_test.go | 38 ++--- .../test/{ => util}/utils.go | 6 +- 26 files changed, 398 insertions(+), 304 deletions(-) create mode 100644 pkg/config-api-provider/provider/config/config.go create mode 100644 pkg/config-api-provider/provider/util/retry.go rename pkg/config-api-provider/test/{data_source_group_test.go => data-sources/group_test.go} (65%) rename pkg/config-api-provider/test/{data_source_sensor_group_assignment_test.go => data-sources/sensor_group_assignment_test.go} (60%) rename pkg/config-api-provider/test/{data_source_wired_group_test.go => data-sources/wired_group_test.go} (72%) rename pkg/config-api-provider/test/{data_source_wireless_group_test.go => data-sources/wireless_group_test.go} (75%) rename pkg/config-api-provider/test/{provider_test.go => provider/provider.go} (79%) rename pkg/config-api-provider/test/{ => resources}/agent_group_assignment_test.go (73%) rename pkg/config-api-provider/test/{ => resources}/agent_test.go (78%) rename pkg/config-api-provider/test/{ => resources}/group_test.go (67%) rename pkg/config-api-provider/test/{ => resources}/network_group_assignment_test.go (63%) rename pkg/config-api-provider/test/{ => resources}/sensor_group_assignment_test.go (62%) rename pkg/config-api-provider/test/{ => resources}/sensor_test.go (81%) rename pkg/config-api-provider/test/{ => resources}/service_test.go (78%) rename pkg/config-api-provider/test/{ => resources}/service_test_group_assignment_test.go (70%) rename pkg/config-api-provider/test/{ => resources}/wired_network_test.go (66%) rename pkg/config-api-provider/test/{ => resources}/wireless_network_test.go (64%) rename pkg/config-api-provider/test/{ => util}/utils.go (98%) diff --git a/pkg/config-api-provider/provider/config/config.go b/pkg/config-api-provider/provider/config/config.go new file mode 100644 index 00000000..e2f140c9 --- /dev/null +++ b/pkg/config-api-provider/provider/config/config.go @@ -0,0 +1,5 @@ +package config + +const ( + MaxRetriesFor429 = 10 +) diff --git a/pkg/config-api-provider/provider/data-sources/group.go b/pkg/config-api-provider/provider/data-sources/group.go index bfe2e41f..ddc699da 100644 --- a/pkg/config-api-provider/provider/data-sources/group.go +++ b/pkg/config-api-provider/provider/data-sources/group.go @@ -4,6 +4,7 @@ import ( "context" config_api_client "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/util" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" @@ -91,7 +92,7 @@ func (d *groupDataSource) Read(ctx context.Context, req datasource.ReadRequest, request = request.Uid(*state.Filter.GroupID) } - groupResponse, _, err := request.Execute() + groupResponse, _, err := util.RetryFor429(request.Execute) if err != nil || len(groupResponse.Groups) != 1 { resp.Diagnostics.AddError( diff --git a/pkg/config-api-provider/provider/data-sources/sensor_group_assignment.go b/pkg/config-api-provider/provider/data-sources/sensor_group_assignment.go index c90b8380..13fa2772 100644 --- a/pkg/config-api-provider/provider/data-sources/sensor_group_assignment.go +++ b/pkg/config-api-provider/provider/data-sources/sensor_group_assignment.go @@ -4,6 +4,7 @@ import ( "context" config_api_client "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/util" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" @@ -70,10 +71,10 @@ func (d *sensorGroupAssignmentDataSource) Read(ctx context.Context, req datasour return } - sensorGroupAssignmentResponse, _, err := d.client.ConfigurationAPI. + request := d.client.ConfigurationAPI. GetConfigurationAppV1SensorGroupAssignmentsGet(context.Background()). - Uid(state.Filter.SensorGroupAssignmentID). - Execute() + Uid(state.Filter.SensorGroupAssignmentID) + sensorGroupAssignmentResponse, _, err := util.RetryFor429(request.Execute) if err != nil || len(sensorGroupAssignmentResponse.SensorGroupAssignments) != 1 { resp.Diagnostics.AddError( diff --git a/pkg/config-api-provider/provider/data-sources/wired_network.go b/pkg/config-api-provider/provider/data-sources/wired_network.go index c8829c86..8c0e898d 100644 --- a/pkg/config-api-provider/provider/data-sources/wired_network.go +++ b/pkg/config-api-provider/provider/data-sources/wired_network.go @@ -4,6 +4,7 @@ import ( "context" config_api_client "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/util" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" @@ -95,10 +96,10 @@ func (d *wiredNetworkDataSource) Read(ctx context.Context, req datasource.ReadRe return } - networkResponse, _, err := d.client.ConfigurationAPI. + request := d.client.ConfigurationAPI. GetConfigurationAppV1WiredNetworksGet(context.Background()). - Uid(state.Filter.WiredNetworkID). - Execute() + Uid(state.Filter.WiredNetworkID) + networkResponse, _, err := util.RetryFor429(request.Execute) if err != nil || len(networkResponse.WiredNetworks) != 1 { resp.Diagnostics.AddError( diff --git a/pkg/config-api-provider/provider/data-sources/wireless_network.go b/pkg/config-api-provider/provider/data-sources/wireless_network.go index 89ac478e..4223ec4a 100644 --- a/pkg/config-api-provider/provider/data-sources/wireless_network.go +++ b/pkg/config-api-provider/provider/data-sources/wireless_network.go @@ -4,6 +4,7 @@ import ( "context" config_api_client "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/util" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" @@ -103,10 +104,10 @@ func (d *wirelessNetworkDataSource) Read(ctx context.Context, req datasource.Rea return } - networkResponse, _, err := d.client.ConfigurationAPI. + request := d.client.ConfigurationAPI. GetConfigurationAppV1WirelessNetworksGet(context.Background()). - Uid(state.Filter.WirelessNetworkID). - Execute() + Uid(state.Filter.WirelessNetworkID) + networkResponse, _, err := util.RetryFor429(request.Execute) if err != nil || len(networkResponse.WirelessNetworks) != 1 { resp.Diagnostics.AddError( diff --git a/pkg/config-api-provider/provider/resources/group.go b/pkg/config-api-provider/provider/resources/group.go index 75552c39..c78d8676 100644 --- a/pkg/config-api-provider/provider/resources/group.go +++ b/pkg/config-api-provider/provider/resources/group.go @@ -4,6 +4,7 @@ import ( "context" "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/util" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" @@ -103,7 +104,8 @@ func (r *groupResource) Create(ctx context.Context, req resource.CreateRequest, } groups_post_request := config_api_client.NewGroupsPostRequest(plan.ParentGroupId.ValueString(), plan.Name.ValueString()) - group, _, err := r.client.ConfigurationAPI.GroupsPostConfigurationAppV1GroupsPost(context.Background()).GroupsPostRequest(*groups_post_request).Execute() + request := r.client.ConfigurationAPI.GroupsPostConfigurationAppV1GroupsPost(context.Background()).GroupsPostRequest(*groups_post_request) + group, _, err := util.RetryFor429(request.Execute) if err != nil { resp.Diagnostics.AddError( "Error creating group", diff --git a/pkg/config-api-provider/provider/resources/sensor_group_assignment.go b/pkg/config-api-provider/provider/resources/sensor_group_assignment.go index da917d89..5833bb11 100644 --- a/pkg/config-api-provider/provider/resources/sensor_group_assignment.go +++ b/pkg/config-api-provider/provider/resources/sensor_group_assignment.go @@ -4,6 +4,7 @@ import ( "context" config_api_client "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/util" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -130,10 +131,10 @@ func (r *sensorGroupAssignmentResource) Read(ctx context.Context, req resource.R return } - sensorGroupAssignmentResponse, _, err := r.client.ConfigurationAPI. + request := r.client.ConfigurationAPI. GetConfigurationAppV1SensorGroupAssignmentsGet(context.Background()). - Uid(state.ID.ValueString()). - Execute() + Uid(state.ID.ValueString()) + sensorGroupAssignmentResponse, _, err := util.RetryFor429(request.Execute) if err != nil || len(sensorGroupAssignmentResponse.SensorGroupAssignments) != 1 { resp.Diagnostics.AddError( diff --git a/pkg/config-api-provider/provider/resources/wired_network.go b/pkg/config-api-provider/provider/resources/wired_network.go index b7b0a7ad..3e31d7cc 100644 --- a/pkg/config-api-provider/provider/resources/wired_network.go +++ b/pkg/config-api-provider/provider/resources/wired_network.go @@ -4,6 +4,7 @@ import ( "context" "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/util" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -88,10 +89,10 @@ func (r *wiredNetworkResource) Read(ctx context.Context, req resource.ReadReques return } - networkResponse, _, err := r.client.ConfigurationAPI. + request := r.client.ConfigurationAPI. GetConfigurationAppV1WiredNetworksGet(context.Background()). - Uid(state.ID.ValueString()). - Execute() + Uid(state.ID.ValueString()) + networkResponse, _, err := util.RetryFor429(request.Execute) if err != nil || len(networkResponse.WiredNetworks) != 1 { resp.Diagnostics.AddError( diff --git a/pkg/config-api-provider/provider/resources/wireless_network.go b/pkg/config-api-provider/provider/resources/wireless_network.go index 20a238df..b456f018 100644 --- a/pkg/config-api-provider/provider/resources/wireless_network.go +++ b/pkg/config-api-provider/provider/resources/wireless_network.go @@ -4,6 +4,7 @@ import ( "context" "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/util" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -88,10 +89,10 @@ func (r *wirelessNetworkResource) Read(ctx context.Context, req resource.ReadReq return } - networkResponse, _, err := r.client.ConfigurationAPI. + request := r.client.ConfigurationAPI. GetConfigurationAppV1WirelessNetworksGet(context.Background()). - Uid(state.ID.ValueString()). - Execute() + Uid(state.ID.ValueString()) + networkResponse, _, err := util.RetryFor429(request.Execute) if err != nil || len(networkResponse.WirelessNetworks) != 1 { resp.Diagnostics.AddError( diff --git a/pkg/config-api-provider/provider/util/retry.go b/pkg/config-api-provider/provider/util/retry.go new file mode 100644 index 00000000..6ebb0c4e --- /dev/null +++ b/pkg/config-api-provider/provider/util/retry.go @@ -0,0 +1,34 @@ +package util + +import ( + "errors" + "net/http" + "strconv" + "time" + + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/config" +) + +func RetryFor429[T any](f func() (T, *http.Response, error)) (T, *http.Response, error) { + var result T + var err error + var httpResponse *http.Response + + for i := 0; i < config.MaxRetriesFor429; i++ { + result, httpResponse, err = f() + + if httpResponse != nil && httpResponse.StatusCode != 429 { + return result, httpResponse, err + } + + waitForSeconds := httpResponse.Header.Get("X-Ratelimit-Reset") + if waitForSeconds == "" { + return result, httpResponse, errors.Join(err, errors.New("header X-Ratelimit-Reset is missing or contains non valid value")) + } + + rateLimitedFor, _ := strconv.Atoi(httpResponse.Header.Get("X-Ratelimit-Reset")) + time.Sleep(time.Duration(rateLimitedFor) * time.Second) + } + + return result, httpResponse, errors.Join(err, errors.New("number of retries exceeded for 429 retries")) +} diff --git a/pkg/config-api-provider/test/data_source_group_test.go b/pkg/config-api-provider/test/data-sources/group_test.go similarity index 65% rename from pkg/config-api-provider/test/data_source_group_test.go rename to pkg/config-api-provider/test/data-sources/group_test.go index cae0f0a6..f23244e9 100644 --- a/pkg/config-api-provider/test/data_source_group_test.go +++ b/pkg/config-api-provider/test/data-sources/group_test.go @@ -1,23 +1,25 @@ -package test +package data_source import ( "regexp" "testing" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" "github.com/h2non/gock" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestGroupDataSource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Test no filters set { - Config: providerConfig + ` + Config: provider.ProviderConfig + ` data "uxi_group" "my_group" { filter = {} } @@ -26,7 +28,7 @@ func TestGroupDataSource(t *testing.T) { }, // Test too many filters set { - Config: providerConfig + ` + Config: provider.ProviderConfig + ` data "uxi_group" "my_group" { filter = { is_root = true @@ -42,13 +44,13 @@ func TestGroupDataSource(t *testing.T) { // Test Read, is_root not set { PreConfig: func() { - MockGetGroup( + util.MockGetGroup( "uid", - GenerateGroupPaginatedResponse([]map[string]interface{}{StructToMap(GenerateGroupResponseModel("uid", "", ""))}), + util.GenerateGroupPaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateGroupResponseModel("uid", "", ""))}), 3, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` data "uxi_group" "my_group" { filter = { group_id = "uid" @@ -62,13 +64,13 @@ func TestGroupDataSource(t *testing.T) { // Test Read, is_root is false { PreConfig: func() { - MockGetGroup( + util.MockGetGroup( "uid", - GenerateGroupPaginatedResponse([]map[string]interface{}{StructToMap(GenerateGroupResponseModel("uid", "", ""))}), + util.GenerateGroupPaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateGroupResponseModel("uid", "", ""))}), 3, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` data "uxi_group" "my_group" { filter = { is_root = false @@ -83,4 +85,6 @@ func TestGroupDataSource(t *testing.T) { // TODO: Test retrieving the root group }, }) + + mockOAuth.Mock.Disable() } diff --git a/pkg/config-api-provider/test/data_source_sensor_group_assignment_test.go b/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go similarity index 60% rename from pkg/config-api-provider/test/data_source_sensor_group_assignment_test.go rename to pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go index a05515f0..3d147043 100644 --- a/pkg/config-api-provider/test/data_source_sensor_group_assignment_test.go +++ b/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go @@ -1,31 +1,32 @@ -package test +package data_source_test import ( - "testing" - + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" "github.com/h2non/gock" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "testing" ) func TestSensorGroupAssignmentDataSource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Read testing { PreConfig: func() { - MockGetSensorGroupAssignment( + util.MockGetSensorGroupAssignment( "uid", - GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ - StructToMap(GenerateSensorGroupAssignmentResponse("uid", "")), + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ + util.StructToMap(util.GenerateSensorGroupAssignmentResponse("uid", "")), }), 3, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` data "uxi_sensor_group_assignment" "my_sensor_group_assignment" { filter = { sensor_group_assignment_id = "uid" @@ -40,4 +41,6 @@ func TestSensorGroupAssignmentDataSource(t *testing.T) { }, }, }) + + mockOAuth.Mock.Disable() } diff --git a/pkg/config-api-provider/test/data_source_wired_group_test.go b/pkg/config-api-provider/test/data-sources/wired_group_test.go similarity index 72% rename from pkg/config-api-provider/test/data_source_wired_group_test.go rename to pkg/config-api-provider/test/data-sources/wired_group_test.go index 257d3e6d..2fa33816 100644 --- a/pkg/config-api-provider/test/data_source_wired_group_test.go +++ b/pkg/config-api-provider/test/data-sources/wired_group_test.go @@ -1,29 +1,32 @@ -package test +package data_source_test import ( "testing" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "github.com/h2non/gock" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestWiredNetworkDataSource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Read testing { PreConfig: func() { - MockGetWiredNetwork( + util.MockGetWiredNetwork( "uid", - GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{GenerateWiredNetworkResponse("uid", "")}), + util.GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("uid", "")}), 3, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` data "uxi_wired_network" "my_wired_network" { filter = { wired_network_id = "uid" @@ -44,4 +47,6 @@ func TestWiredNetworkDataSource(t *testing.T) { }, }, }) + + mockOAuth.Mock.Disable() } diff --git a/pkg/config-api-provider/test/data_source_wireless_group_test.go b/pkg/config-api-provider/test/data-sources/wireless_group_test.go similarity index 75% rename from pkg/config-api-provider/test/data_source_wireless_group_test.go rename to pkg/config-api-provider/test/data-sources/wireless_group_test.go index c2c79fb8..50a61d2c 100644 --- a/pkg/config-api-provider/test/data_source_wireless_group_test.go +++ b/pkg/config-api-provider/test/data-sources/wireless_group_test.go @@ -1,6 +1,8 @@ -package test +package data_source_test import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" "testing" "github.com/h2non/gock" @@ -9,21 +11,21 @@ import ( func TestWirelessNetworkDataSource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Read testing { PreConfig: func() { - MockGetWirelessNetwork( + util.MockGetWirelessNetwork( "uid", - GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{GenerateWirelessNetworkResponse("uid", "")}), + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}), 3, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` data "uxi_wireless_network" "my_wireless_network" { filter = { wireless_network_id = "uid" @@ -46,4 +48,6 @@ func TestWirelessNetworkDataSource(t *testing.T) { }, }, }) + + mockOAuth.Mock.Disable() } diff --git a/pkg/config-api-provider/test/provider_test.go b/pkg/config-api-provider/test/provider/provider.go similarity index 79% rename from pkg/config-api-provider/test/provider_test.go rename to pkg/config-api-provider/test/provider/provider.go index 59710ad6..dc04bb91 100644 --- a/pkg/config-api-provider/test/provider_test.go +++ b/pkg/config-api-provider/test/provider/provider.go @@ -1,4 +1,4 @@ -package test +package provider import ( "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider" @@ -7,7 +7,7 @@ import ( ) const ( - providerConfig = `provider "uxi" { + ProviderConfig = `provider "uxi" { host = "test.api.capenetworks.com" client_id = "client_id" client_secret = "client_secret" @@ -16,11 +16,11 @@ const ( ) var ( - // testAccProtoV6ProviderFactories are used to instantiate a provider during + // TestAccProtoV6ProviderFactories are used to instantiate a provider during // acceptance testing. The factory function will be invoked for every Terraform // CLI command executed to create a provider server to which the CLI can // reattach. - testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){ + TestAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){ "uxi": providerserver.NewProtocol6WithError(provider.New("test")()), } ) diff --git a/pkg/config-api-provider/test/agent_group_assignment_test.go b/pkg/config-api-provider/test/resources/agent_group_assignment_test.go similarity index 73% rename from pkg/config-api-provider/test/agent_group_assignment_test.go rename to pkg/config-api-provider/test/resources/agent_group_assignment_test.go index c83d0d32..d2116f3c 100644 --- a/pkg/config-api-provider/test/agent_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/agent_group_assignment_test.go @@ -1,6 +1,8 @@ package test import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" "testing" "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" @@ -10,30 +12,30 @@ import ( func TestAgentGroupAssignmentResource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Creating a agent group assignment { PreConfig: func() { // required for agent import resources.GetAgent = func(uid string) resources.AgentResponseModel { - return GenerateAgentResponseModel(uid, "") + return util.GenerateAgentResponseModel(uid, "") } // required for group create - MockPostGroup(StructToMap(GenerateGroupResponseModel("group_uid", "", "")), 1) - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), 1, ) // required for agent group assignment create - agentGroupAssignmentResponse := GenerateAgentGroupAssignmentResponse("agent_group_assignment_uid", "") + agentGroupAssignmentResponse := util.GenerateAgentGroupAssignmentResponse("agent_group_assignment_uid", "") resources.CreateAgentGroupAssignment = func(request resources.AgentGroupAssignmentRequestModel) resources.AgentGroupAssignmentResponseModel { return agentGroupAssignmentResponse } @@ -42,7 +44,7 @@ func TestAgentGroupAssignmentResource(t *testing.T) { } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_group" "my_group" { name = "name" parent_group_id = "parent_uid" @@ -80,40 +82,40 @@ func TestAgentGroupAssignmentResource(t *testing.T) { PreConfig: func() { resources.GetAgent = func(uid string) resources.AgentResponseModel { if uid == "agent_uid" { - return GenerateAgentResponseModel(uid, "") + return util.GenerateAgentResponseModel(uid, "") } else { - return GenerateAgentResponseModel(uid, "_2") + return util.GenerateAgentResponseModel(uid, "_2") } } - MockGetGroup("group_uid_2", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), }), 1, ) - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), 2, ) // required for creating another group - MockPostGroup(StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) // required for agent group assignment create resources.GetAgentGroupAssignment = func(uid string) resources.AgentGroupAssignmentResponseModel { if uid == "agent_group_assignment_uid" { - return GenerateAgentGroupAssignmentResponse(uid, "") + return util.GenerateAgentGroupAssignmentResponse(uid, "") } else { - return GenerateAgentGroupAssignmentResponse(uid, "_2") + return util.GenerateAgentGroupAssignmentResponse(uid, "_2") } } resources.CreateAgentGroupAssignment = func(request resources.AgentGroupAssignmentRequestModel) resources.AgentGroupAssignmentResponseModel { - return GenerateAgentGroupAssignmentResponse("agent_group_assignment_uid_2", "_2") + return util.GenerateAgentGroupAssignmentResponse("agent_group_assignment_uid_2", "_2") } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` // the original resources resource "uxi_group" "my_group" { name = "name" @@ -162,4 +164,6 @@ func TestAgentGroupAssignmentResource(t *testing.T) { // Delete testing automatically occurs in TestCase }, }) + + mockOAuth.Mock.Disable() } diff --git a/pkg/config-api-provider/test/agent_test.go b/pkg/config-api-provider/test/resources/agent_test.go similarity index 78% rename from pkg/config-api-provider/test/agent_test.go rename to pkg/config-api-provider/test/resources/agent_test.go index 71288cca..5db7bc45 100644 --- a/pkg/config-api-provider/test/agent_test.go +++ b/pkg/config-api-provider/test/resources/agent_test.go @@ -1,24 +1,25 @@ package test import ( - "regexp" - "testing" - "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" "github.com/h2non/gock" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "regexp" + "testing" ) func TestAgentResource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Creating an agent is not allowed { - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_agent" "my_agent" { name = "name" notes = "note" @@ -31,10 +32,10 @@ func TestAgentResource(t *testing.T) { { PreConfig: func() { resources.GetAgent = func(uid string) resources.AgentResponseModel { - return GenerateAgentResponseModel(uid, "") + return util.GenerateAgentResponseModel(uid, "") } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_agent" "my_agent" { name = "name" notes = "notes" @@ -63,10 +64,10 @@ func TestAgentResource(t *testing.T) { { PreConfig: func() { resources.GetAgent = func(uid string) resources.AgentResponseModel { - return GenerateAgentResponseModel(uid, "_2") + return util.GenerateAgentResponseModel(uid, "_2") } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_agent" "my_agent" { name = "name_2" notes = "notes_2" @@ -81,4 +82,6 @@ func TestAgentResource(t *testing.T) { // Delete testing automatically occurs in TestCase }, }) + + mockOAuth.Mock.Disable() } diff --git a/pkg/config-api-provider/test/group_test.go b/pkg/config-api-provider/test/resources/group_test.go similarity index 67% rename from pkg/config-api-provider/test/group_test.go rename to pkg/config-api-provider/test/resources/group_test.go index d4fbe88d..9f153c85 100644 --- a/pkg/config-api-provider/test/group_test.go +++ b/pkg/config-api-provider/test/resources/group_test.go @@ -1,12 +1,13 @@ package test import ( - "regexp" - "testing" - "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" "github.com/h2non/gock" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "regexp" + "testing" ) type Fetcher interface { @@ -15,23 +16,23 @@ type Fetcher interface { func TestGroupResource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Create and Read testing { PreConfig: func() { - MockPostGroup(StructToMap(GenerateGroupResponseModel("uid", "", "")), 1) - MockGetGroup("uid", GenerateGroupPaginatedResponse( + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("uid", "", "")), 1) + util.MockGetGroup("uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("uid", "", "")), }), 1, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_group" "my_group" { name = "name" parent_group_id = "parent_uid" @@ -45,9 +46,9 @@ func TestGroupResource(t *testing.T) { // ImportState testing { PreConfig: func() { - MockGetGroup("uid", GenerateGroupPaginatedResponse( + util.MockGetGroup("uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("uid", "", "")), }), 1, ) @@ -60,16 +61,16 @@ func TestGroupResource(t *testing.T) { { PreConfig: func() { resources.UpdateGroup = func(request resources.GroupUpdateRequestModel) resources.GroupResponseModel { - return GenerateGroupResponseModel("uid", "_2", "") + return util.GenerateGroupResponseModel("uid", "_2", "") } - MockGetGroup("uid", GenerateGroupPaginatedResponse( + util.MockGetGroup("uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("uid", "_2", "")), + util.StructToMap(util.GenerateGroupResponseModel("uid", "_2", "")), }), 2, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_group" "my_group" { name = "name_2" parent_group_id = "parent_uid" @@ -85,22 +86,22 @@ func TestGroupResource(t *testing.T) { { PreConfig: func() { // existing group - MockGetGroup("uid", GenerateGroupPaginatedResponse( + util.MockGetGroup("uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("uid", "", "")), }), 1, ) // new group (replacement) - MockPostGroup(StructToMap(GenerateGroupResponseModel("new_uid", "", "_2")), 1) - MockGetGroup("new_uid", GenerateGroupPaginatedResponse( + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("new_uid", "", "_2")), 1) + util.MockGetGroup("new_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("new_uid", "", "_2")), + util.StructToMap(util.GenerateGroupResponseModel("new_uid", "", "_2")), }), 1, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_group" "my_group" { name = "name" parent_group_id = "parent_uid_2" @@ -114,19 +115,21 @@ func TestGroupResource(t *testing.T) { // Delete testing automatically occurs in TestCase }, }) + + mockOAuth.Mock.Disable() } func TestRootGroupResource(t *testing.T) { resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Importing the root group does not work { PreConfig: func() { resources.GetRootGroupUID = func() string { return "my_root_group_uid" } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_group" "my_root_group" { name = "name" parent_group_id = "some_random_string" diff --git a/pkg/config-api-provider/test/network_group_assignment_test.go b/pkg/config-api-provider/test/resources/network_group_assignment_test.go similarity index 63% rename from pkg/config-api-provider/test/network_group_assignment_test.go rename to pkg/config-api-provider/test/resources/network_group_assignment_test.go index 3e45da7e..64c1d4f4 100644 --- a/pkg/config-api-provider/test/network_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/network_group_assignment_test.go @@ -1,49 +1,51 @@ package test import ( - "github.com/h2non/gock" "testing" "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" + "github.com/h2non/gock" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestNetworkGroupAssignmentResource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() // Test Wired Network Group Assignment resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Creating a network group assignment { PreConfig: func() { - MockGetWiredNetwork( + util.MockGetWiredNetwork( "network_uid", - GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{GenerateWiredNetworkResponse("network_uid", "")}), + util.GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid", "")}), 2, ) // required for group create - MockPostGroup(StructToMap(GenerateGroupResponseModel("group_uid", "", "")), 1) - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), - 1, + 2, ) // required for network group assignment create resources.CreateNetworkGroupAssignment = func(request resources.NetworkGroupAssignmentRequestModel) resources.NetworkGroupAssignmentResponseModel { - return GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "") + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "") } resources.GetNetworkGroupAssignment = func(uid string) resources.NetworkGroupAssignmentResponseModel { - return GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "") + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "") } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_group" "my_group" { name = "name" parent_group_id = "parent_uid" @@ -77,45 +79,45 @@ func TestNetworkGroupAssignmentResource(t *testing.T) { // Update and Read testing { PreConfig: func() { - MockGetWiredNetwork( + util.MockGetWiredNetwork( "network_uid_2", - GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{GenerateWiredNetworkResponse("network_uid_2", "_2")}), + util.GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid_2", "_2")}), 2, ) - MockGetWiredNetwork( + util.MockGetWiredNetwork( "network_uid", - GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{GenerateWiredNetworkResponse("network_uid", "")}), + util.GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid", "")}), 2, ) - MockGetGroup("group_uid_2", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), }), 1, ) - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), 3, ) // required for creating another group - MockPostGroup(StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) // required for network group assignment create resources.CreateNetworkGroupAssignment = func(request resources.NetworkGroupAssignmentRequestModel) resources.NetworkGroupAssignmentResponseModel { - return GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid_2", "_2") + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid_2", "_2") } resources.GetNetworkGroupAssignment = func(uid string) resources.NetworkGroupAssignmentResponseModel { if uid == "network_group_assignment_uid" { - return GenerateNetworkGroupAssignmentResponse(uid, "") + return util.GenerateNetworkGroupAssignmentResponse(uid, "") } else { - return GenerateNetworkGroupAssignmentResponse(uid, "_2") + return util.GenerateNetworkGroupAssignmentResponse(uid, "_2") } } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` // the original resources resource "uxi_group" "my_group" { name = "name" @@ -160,30 +162,30 @@ func TestNetworkGroupAssignmentResource(t *testing.T) { // Remove networks from state { PreConfig: func() { - MockGetWiredNetwork( + util.MockGetWiredNetwork( "network_uid_2", - GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{GenerateWiredNetworkResponse("network_uid_2", "_2")}), + util.GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid_2", "_2")}), 1, ) - MockGetWiredNetwork( + util.MockGetWiredNetwork( "network_uid", - GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{GenerateWiredNetworkResponse("network_uid", "")}), + util.GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid", "")}), 1, ) - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), - 1, + 2, ) - MockGetGroup("group_uid_2", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), }), 1, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` removed { from = uxi_wired_network.my_network @@ -206,36 +208,36 @@ func TestNetworkGroupAssignmentResource(t *testing.T) { // Test Wireless Network Group Assignment resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Creating a network group assignment { PreConfig: func() { // required for network import - MockGetWirelessNetwork( + util.MockGetWirelessNetwork( "network_uid", - GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{GenerateWirelessNetworkResponse("network_uid", "")}), + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("network_uid", "")}), 2, ) // required for group create - MockPostGroup(StructToMap(GenerateGroupResponseModel("group_uid", "", "")), 1) - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), 1, ) // required for network group assignment create resources.CreateNetworkGroupAssignment = func(request resources.NetworkGroupAssignmentRequestModel) resources.NetworkGroupAssignmentResponseModel { - return GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "") + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "") } resources.GetNetworkGroupAssignment = func(uid string) resources.NetworkGroupAssignmentResponseModel { - return GenerateNetworkGroupAssignmentResponse(uid, "") + return util.GenerateNetworkGroupAssignmentResponse(uid, "") } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_group" "my_group" { name = "name" parent_group_id = "parent_uid" @@ -269,46 +271,46 @@ func TestNetworkGroupAssignmentResource(t *testing.T) { // Update and Read testing { PreConfig: func() { - MockGetWirelessNetwork( + util.MockGetWirelessNetwork( "network_uid_2", - GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{GenerateWirelessNetworkResponse("network_uid_2", "_2")}), + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("network_uid_2", "_2")}), 2, ) - MockGetWirelessNetwork( + util.MockGetWirelessNetwork( "network_uid", - GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{GenerateWirelessNetworkResponse("network_uid", "")}), + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("network_uid", "")}), 2, ) - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + // required for creating another group + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), }), - 2, + 1, ) - // required for creating another group - MockPostGroup(StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) - MockGetGroup("group_uid_2", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), - 1, + 3, ) // required for network group assignment create resources.GetNetworkGroupAssignment = func(uid string) resources.NetworkGroupAssignmentResponseModel { if uid == "network_group_assignment_uid" { - return GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "") + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "") } else { - return GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid_2", "_2") + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid_2", "_2") } } resources.CreateNetworkGroupAssignment = func(request resources.NetworkGroupAssignmentRequestModel) resources.NetworkGroupAssignmentResponseModel { - return GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid_2", "_2") + return util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid_2", "_2") } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` // the original resources resource "uxi_group" "my_group" { name = "name" @@ -353,30 +355,30 @@ func TestNetworkGroupAssignmentResource(t *testing.T) { // Remove networks from state { PreConfig: func() { - MockGetWirelessNetwork( + util.MockGetWirelessNetwork( "network_uid_2", - GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{GenerateWirelessNetworkResponse("network_uid_2", "_2")}), + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("network_uid_2", "_2")}), 1, ) - MockGetWirelessNetwork( + util.MockGetWirelessNetwork( "network_uid", - GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{GenerateWirelessNetworkResponse("network_uid", "")}), + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("network_uid", "")}), 1, ) - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), - 1, + 2, ) - MockGetGroup("group_uid_2", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), }), 1, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` removed { from = uxi_wireless_network.my_network @@ -396,4 +398,6 @@ func TestNetworkGroupAssignmentResource(t *testing.T) { // Delete testing automatically occurs in TestCase }, }) + + mockOAuth.Mock.Disable() } diff --git a/pkg/config-api-provider/test/sensor_group_assignment_test.go b/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go similarity index 62% rename from pkg/config-api-provider/test/sensor_group_assignment_test.go rename to pkg/config-api-provider/test/resources/sensor_group_assignment_test.go index 81067f34..3005721f 100644 --- a/pkg/config-api-provider/test/sensor_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go @@ -4,48 +4,50 @@ import ( "testing" "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" "github.com/h2non/gock" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestSensorGroupAssignmentResource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Creating a sensor group assignment { PreConfig: func() { // required for sensor import resources.GetSensor = func(uid string) resources.SensorResponseModel { - return GenerateSensorResponseModel(uid, "") + return util.GenerateSensorResponseModel(uid, "") } // required for group create - MockPostGroup(StructToMap(GenerateGroupResponseModel("group_uid", "", "")), 1) - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), 1, ) // required for sensor group assignment create resources.CreateSensorGroupAssignment = func(request resources.SensorGroupAssignmentRequestModel) resources.SensorGroupAssignmentResponseModel { - return GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "") + return util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "") } - MockGetSensorGroupAssignment( + util.MockGetSensorGroupAssignment( "sensor_group_assignment_uid", - GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ - StructToMap(GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")), + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ + util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")), }), 1, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_group" "my_group" { name = "name" parent_group_id = "parent_uid" @@ -76,10 +78,10 @@ func TestSensorGroupAssignmentResource(t *testing.T) { // ImportState testing { PreConfig: func() { - MockGetSensorGroupAssignment( + util.MockGetSensorGroupAssignment( "sensor_group_assignment_uid", - GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ - StructToMap(GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")), + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ + util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")), }), 1, ) @@ -93,48 +95,48 @@ func TestSensorGroupAssignmentResource(t *testing.T) { PreConfig: func() { resources.GetSensor = func(uid string) resources.SensorResponseModel { if uid == "sensor_uid" { - return GenerateSensorResponseModel("sensor_uid", "") + return util.GenerateSensorResponseModel("sensor_uid", "") } else { - return GenerateSensorResponseModel("sensor_uid", "_2") + return util.GenerateSensorResponseModel("sensor_uid", "_2") } } - MockGetGroup("group_uid_2", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), }), 1, ) - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), 2, ) // required for creating another group - MockPostGroup(StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) // required for sensor group assignment create - MockGetSensorGroupAssignment( + util.MockGetSensorGroupAssignment( "sensor_group_assignment_uid", - GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ - StructToMap(GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")), + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ + util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")), }), 1, ) - MockGetSensorGroupAssignment( + util.MockGetSensorGroupAssignment( "sensor_group_assignment_uid_2", - GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ - StructToMap(GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid_2", "_2")), + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ + util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid_2", "_2")), }), 1, ) resources.CreateSensorGroupAssignment = func(request resources.SensorGroupAssignmentRequestModel) resources.SensorGroupAssignmentResponseModel { - return GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid_2", "_2") + return util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid_2", "_2") } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` // the original resources resource "uxi_group" "my_group" { name = "name" @@ -185,35 +187,27 @@ func TestSensorGroupAssignmentResource(t *testing.T) { // Remove sensors from state { PreConfig: func() { - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), }), - 2, + 1, ) - MockGetGroup("group_uid_2", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), 1, ) - MockGetSensorGroupAssignment( + util.MockGetSensorGroupAssignment( "sensor_group_assignment_uid", - GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ - StructToMap(GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")), - }), - 1, - ) - MockGetSensorGroupAssignment( - "sensor_group_assignment_uid_2", - GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ - StructToMap(GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid_2", "_2")), + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ + util.StructToMap(util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")), }), 1, ) - }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` removed { from = uxi_sensor.my_sensor @@ -233,4 +227,6 @@ func TestSensorGroupAssignmentResource(t *testing.T) { // Delete testing automatically occurs in TestCase }, }) + + mockOAuth.Mock.Disable() } diff --git a/pkg/config-api-provider/test/sensor_test.go b/pkg/config-api-provider/test/resources/sensor_test.go similarity index 81% rename from pkg/config-api-provider/test/sensor_test.go rename to pkg/config-api-provider/test/resources/sensor_test.go index 80013e53..6e456c64 100644 --- a/pkg/config-api-provider/test/sensor_test.go +++ b/pkg/config-api-provider/test/resources/sensor_test.go @@ -1,6 +1,8 @@ package test import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" "regexp" "testing" @@ -12,10 +14,10 @@ import ( func TestSensorResource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, TerraformVersionChecks: []tfversion.TerraformVersionCheck{ // we required terraform 1.7.0 and above for the `removed` block tfversion.RequireAbove(tfversion.Version1_7_0), @@ -23,7 +25,7 @@ func TestSensorResource(t *testing.T) { Steps: []resource.TestStep{ // Creating a sensor is not allowed { - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_sensor" "my_sensor" { name = "name" address_note = "address_note" @@ -37,10 +39,10 @@ func TestSensorResource(t *testing.T) { { PreConfig: func() { resources.GetSensor = func(uid string) resources.SensorResponseModel { - return GenerateSensorResponseModel(uid, "") + return util.GenerateSensorResponseModel(uid, "") } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_sensor" "my_sensor" { name = "name" address_note = "address_note" @@ -71,10 +73,10 @@ func TestSensorResource(t *testing.T) { { PreConfig: func() { resources.GetSensor = func(uid string) resources.SensorResponseModel { - return GenerateSensorResponseModel(uid, "_2") + return util.GenerateSensorResponseModel(uid, "_2") } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_sensor" "my_sensor" { name = "name_2" address_note = "address_note_2" @@ -91,12 +93,12 @@ func TestSensorResource(t *testing.T) { }, // Deleting a sensor is not allowed { - Config: providerConfig + ``, + Config: provider.ProviderConfig + ``, ExpectError: regexp.MustCompile(`deleting a sensor is not supported; sensors can only removed from state`), }, // Remove sensor from state { - Config: providerConfig + ` + Config: provider.ProviderConfig + ` removed { from = uxi_sensor.my_sensor @@ -107,4 +109,6 @@ func TestSensorResource(t *testing.T) { }, }, }) + + mockOAuth.Mock.Disable() } diff --git a/pkg/config-api-provider/test/service_test.go b/pkg/config-api-provider/test/resources/service_test.go similarity index 78% rename from pkg/config-api-provider/test/service_test.go rename to pkg/config-api-provider/test/resources/service_test.go index b09c5ecc..0149e88e 100644 --- a/pkg/config-api-provider/test/service_test.go +++ b/pkg/config-api-provider/test/resources/service_test.go @@ -1,6 +1,8 @@ package test import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" "regexp" "testing" @@ -12,10 +14,10 @@ import ( func TestServiceTestResource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, TerraformVersionChecks: []tfversion.TerraformVersionCheck{ // we required terraform 1.7.0 and above for the `removed` block tfversion.RequireAbove(tfversion.Version1_7_0), @@ -23,7 +25,7 @@ func TestServiceTestResource(t *testing.T) { Steps: []resource.TestStep{ // Creating a service_test is not allowed { - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_service_test" "my_service_test" { title = "title" }`, @@ -34,10 +36,10 @@ func TestServiceTestResource(t *testing.T) { { PreConfig: func() { resources.GetServiceTest = func(uid string) resources.ServiceTestResponseModel { - return GenerateServiceTestResponseModel(uid, "") + return util.GenerateServiceTestResponseModel(uid, "") } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_service_test" "my_service_test" { title = "title" } @@ -60,7 +62,7 @@ func TestServiceTestResource(t *testing.T) { }, // Updating a service_test is not allowed { - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_service_test" "my_service_test" { title = "updated_title" }`, @@ -68,12 +70,12 @@ func TestServiceTestResource(t *testing.T) { }, // Deleting a service_test is not allowed { - Config: providerConfig + ``, + Config: provider.ProviderConfig + ``, ExpectError: regexp.MustCompile(`(?s)deleting a service_test is not supported; service_tests can only removed from\s*state`), }, // Remove service_test from state { - Config: providerConfig + ` + Config: provider.ProviderConfig + ` removed { from = uxi_service_test.my_service_test @@ -84,4 +86,6 @@ func TestServiceTestResource(t *testing.T) { }, }, }) + + mockOAuth.Mock.Disable() } diff --git a/pkg/config-api-provider/test/service_test_group_assignment_test.go b/pkg/config-api-provider/test/resources/service_test_group_assignment_test.go similarity index 70% rename from pkg/config-api-provider/test/service_test_group_assignment_test.go rename to pkg/config-api-provider/test/resources/service_test_group_assignment_test.go index 7d93bd62..57bd71c4 100644 --- a/pkg/config-api-provider/test/service_test_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/service_test_group_assignment_test.go @@ -1,6 +1,8 @@ package test import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" "testing" "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" @@ -10,30 +12,30 @@ import ( func TestServiceTestGroupAssignmentResource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Creating a serviceTest group assignment { PreConfig: func() { // required for serviceTest import resources.GetServiceTest = func(uid string) resources.ServiceTestResponseModel { - return GenerateServiceTestResponseModel(uid, "") + return util.GenerateServiceTestResponseModel(uid, "") } // required for group create - MockPostGroup(StructToMap(GenerateGroupResponseModel("group_uid", "", "")), 1) - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), 1, ) // required for serviceTest group assignment create - serviceTestGroupAssignmentResponse := GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid", "") + serviceTestGroupAssignmentResponse := util.GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid", "") resources.CreateServiceTestGroupAssignment = func(request resources.ServiceTestGroupAssignmentRequestModel) resources.ServiceTestGroupAssignmentResponseModel { return serviceTestGroupAssignmentResponse } @@ -42,7 +44,7 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_group" "my_group" { name = "name" parent_group_id = "parent_uid" @@ -78,40 +80,40 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { PreConfig: func() { resources.GetServiceTest = func(uid string) resources.ServiceTestResponseModel { if uid == "service_test_uid" { - return GenerateServiceTestResponseModel("service_test_uid", "") + return util.GenerateServiceTestResponseModel("service_test_uid", "") } else { - return GenerateServiceTestResponseModel("service_test_uid", "_2") + return util.GenerateServiceTestResponseModel("service_test_uid", "_2") } } - MockGetGroup("group_uid_2", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), }), 1, ) - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), 3, ) // required for creating another group - MockPostGroup(StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), 1) // required for serviceTest group assignment create resources.GetServiceTestGroupAssignment = func(uid string) resources.ServiceTestGroupAssignmentResponseModel { if uid == "service_test_group_assignment_uid" { - return GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid", "") + return util.GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid", "") } else { - return GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid_2", "_2") + return util.GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid_2", "_2") } } resources.CreateServiceTestGroupAssignment = func(request resources.ServiceTestGroupAssignmentRequestModel) resources.ServiceTestGroupAssignmentResponseModel { - return GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid_2", "_2") + return util.GenerateServiceTestGroupAssignmentResponse("service_test_group_assignment_uid_2", "_2") } }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` // the original resources resource "uxi_group" "my_group" { name = "name" @@ -156,20 +158,20 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { // Remove serviceTests from state { PreConfig: func() { - MockGetGroup("group_uid", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid", "", "")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), }), 1, ) - MockGetGroup("group_uid_2", GenerateGroupPaginatedResponse( + util.MockGetGroup("group_uid_2", util.GenerateGroupPaginatedResponse( []map[string]interface{}{ - StructToMap(GenerateGroupResponseModel("group_uid_2", "_2", "_2")), + util.StructToMap(util.GenerateGroupResponseModel("group_uid_2", "_2", "_2")), }), 1, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` removed { from = uxi_service_test.my_service_test @@ -189,4 +191,6 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { // Delete testing automatically occurs in TestCase }, }) + + mockOAuth.Mock.Disable() } diff --git a/pkg/config-api-provider/test/wired_network_test.go b/pkg/config-api-provider/test/resources/wired_network_test.go similarity index 66% rename from pkg/config-api-provider/test/wired_network_test.go rename to pkg/config-api-provider/test/resources/wired_network_test.go index e64bb516..b5b5d815 100644 --- a/pkg/config-api-provider/test/wired_network_test.go +++ b/pkg/config-api-provider/test/resources/wired_network_test.go @@ -1,6 +1,8 @@ package test import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" "regexp" "testing" @@ -11,10 +13,10 @@ import ( func TestWiredNetworkResource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, TerraformVersionChecks: []tfversion.TerraformVersionCheck{ // we required terraform 1.7.0 and above for the `removed` block tfversion.RequireAbove(tfversion.Version1_7_0), @@ -22,7 +24,7 @@ func TestWiredNetworkResource(t *testing.T) { Steps: []resource.TestStep{ // Creating a wired_network is not allowed { - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_wired_network" "my_wired_network" { alias = "alias" }`, @@ -32,15 +34,15 @@ func TestWiredNetworkResource(t *testing.T) { // Importing a wired_network { PreConfig: func() { - MockGetWiredNetwork( + util.MockGetWiredNetwork( "uid", - GenerateWiredNetworkPaginatedResponse( - []map[string]interface{}{GenerateWiredNetworkResponse("uid", "")}, + util.GenerateWiredNetworkPaginatedResponse( + []map[string]interface{}{util.GenerateWiredNetworkResponse("uid", "")}, ), 2, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_wired_network" "my_wired_network" { alias = "alias" } @@ -58,10 +60,10 @@ func TestWiredNetworkResource(t *testing.T) { // ImportState testing { PreConfig: func() { - MockGetWiredNetwork( + util.MockGetWiredNetwork( "uid", - GenerateWiredNetworkPaginatedResponse( - []map[string]interface{}{GenerateWiredNetworkResponse("uid", "")}, + util.GenerateWiredNetworkPaginatedResponse( + []map[string]interface{}{util.GenerateWiredNetworkResponse("uid", "")}, ), 1, ) @@ -73,15 +75,15 @@ func TestWiredNetworkResource(t *testing.T) { // Updating a wired_network is not allowed { PreConfig: func() { - MockGetWiredNetwork( + util.MockGetWiredNetwork( "uid", - GenerateWiredNetworkPaginatedResponse( - []map[string]interface{}{GenerateWiredNetworkResponse("uid", "")}, + util.GenerateWiredNetworkPaginatedResponse( + []map[string]interface{}{util.GenerateWiredNetworkResponse("uid", "")}, ), 1, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_wired_network" "my_wired_network" { alias = "updated_alias" }`, @@ -90,20 +92,20 @@ func TestWiredNetworkResource(t *testing.T) { // Deleting a wired_network is not allowed { PreConfig: func() { - MockGetWiredNetwork( + util.MockGetWiredNetwork( "uid", - GenerateWiredNetworkPaginatedResponse( - []map[string]interface{}{GenerateWiredNetworkResponse("uid", "")}, + util.GenerateWiredNetworkPaginatedResponse( + []map[string]interface{}{util.GenerateWiredNetworkResponse("uid", "")}, ), 2, ) }, - Config: providerConfig + ``, + Config: provider.ProviderConfig + ``, ExpectError: regexp.MustCompile(`(?s)deleting a wired_network is not supported; wired_networks can only removed\s*from state`), }, // Remove wired_network from state { - Config: providerConfig + ` + Config: provider.ProviderConfig + ` removed { from = uxi_wired_network.my_wired_network @@ -114,4 +116,6 @@ func TestWiredNetworkResource(t *testing.T) { }, }, }) + + mockOAuth.Mock.Disable() } diff --git a/pkg/config-api-provider/test/wireless_network_test.go b/pkg/config-api-provider/test/resources/wireless_network_test.go similarity index 64% rename from pkg/config-api-provider/test/wireless_network_test.go rename to pkg/config-api-provider/test/resources/wireless_network_test.go index 0493ec15..dddf913b 100644 --- a/pkg/config-api-provider/test/wireless_network_test.go +++ b/pkg/config-api-provider/test/resources/wireless_network_test.go @@ -1,6 +1,8 @@ package test import ( + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util" "regexp" "testing" @@ -11,10 +13,10 @@ import ( func TestWirelessNetworkResource(t *testing.T) { defer gock.Off() - MockOAuth() + mockOAuth := util.MockOAuth() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, TerraformVersionChecks: []tfversion.TerraformVersionCheck{ // we required terraform 1.7.0 and above for the `removed` block tfversion.RequireAbove(tfversion.Version1_7_0), @@ -22,7 +24,7 @@ func TestWirelessNetworkResource(t *testing.T) { Steps: []resource.TestStep{ // Creating a wireless_network is not allowed { - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_wireless_network" "my_wireless_network" { alias = "alias" }`, @@ -32,13 +34,13 @@ func TestWirelessNetworkResource(t *testing.T) { // Importing a wireless_network { PreConfig: func() { - MockGetWirelessNetwork( + util.MockGetWirelessNetwork( "uid", - GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{GenerateWirelessNetworkResponse("uid", "")}), + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}), 2, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_wireless_network" "my_wireless_network" { alias = "alias" } @@ -56,9 +58,9 @@ func TestWirelessNetworkResource(t *testing.T) { // ImportState testing { PreConfig: func() { - MockGetWirelessNetwork( + util.MockGetWirelessNetwork( "uid", - GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{GenerateWirelessNetworkResponse("uid", "")}), + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}), 1, ) }, @@ -69,13 +71,13 @@ func TestWirelessNetworkResource(t *testing.T) { // Updating a wireless_network is not allowed { PreConfig: func() { - MockGetWirelessNetwork( + util.MockGetWirelessNetwork( "uid", - GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{GenerateWirelessNetworkResponse("uid", "")}), + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}), 1, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` resource "uxi_wireless_network" "my_wireless_network" { alias = "updated_alias" }`, @@ -84,25 +86,25 @@ func TestWirelessNetworkResource(t *testing.T) { // Deleting a wireless_network is not allowed { PreConfig: func() { - MockGetWirelessNetwork( + util.MockGetWirelessNetwork( "uid", - GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{GenerateWirelessNetworkResponse("uid", "")}), + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}), 1, ) }, - Config: providerConfig + ``, + Config: provider.ProviderConfig + ``, ExpectError: regexp.MustCompile(`(?s)deleting a wireless_network is not supported; wireless_networks can only\s*removed from state`), }, // Remove wireless_network from state { PreConfig: func() { - MockGetWirelessNetwork( + util.MockGetWirelessNetwork( "uid", - GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{GenerateWirelessNetworkResponse("uid", "")}), + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}), 1, ) }, - Config: providerConfig + ` + Config: provider.ProviderConfig + ` removed { from = uxi_wireless_network.my_wireless_network @@ -113,4 +115,6 @@ func TestWirelessNetworkResource(t *testing.T) { }, }, }) + + mockOAuth.Mock.Disable() } diff --git a/pkg/config-api-provider/test/utils.go b/pkg/config-api-provider/test/util/utils.go similarity index 98% rename from pkg/config-api-provider/test/utils.go rename to pkg/config-api-provider/test/util/utils.go index 7d807447..6bf8a822 100644 --- a/pkg/config-api-provider/test/utils.go +++ b/pkg/config-api-provider/test/util/utils.go @@ -1,4 +1,4 @@ -package test +package util import ( "encoding/json" @@ -178,8 +178,8 @@ func StructToMap(obj interface{}) map[string]interface{} { return newMap } -func MockOAuth() { - gock.New("https://test.sso.common.cloud.hpe.com"). +func MockOAuth() *gock.Response { + return gock.New("https://test.sso.common.cloud.hpe.com"). Post("/as/token.oauth2"). MatchHeader("Content-Type", "application/x-www-form-urlencoded"). Persist().