From 41842a353bedd406b85c98b498a7a07a43674522 Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Mon, 30 Sep 2024 14:17:20 +0200 Subject: [PATCH] updates, tests and fixes --- .../provider/resources/group.go | 7 ++- .../test/data-sources/group_test.go | 55 +++---------------- .../sensor_group_assignment_test.go | 53 +++++++++++++++++- .../test/data-sources/wired_group_test.go | 48 ++++++++++++++++ .../test/data-sources/wireless_group_test.go | 51 ++++++++++++++++- .../test/resources/group_test.go | 55 ++++++++++++++++++- 6 files changed, 214 insertions(+), 55 deletions(-) diff --git a/pkg/config-api-provider/provider/resources/group.go b/pkg/config-api-provider/provider/resources/group.go index c78d8676..e75fec96 100644 --- a/pkg/config-api-provider/provider/resources/group.go +++ b/pkg/config-api-provider/provider/resources/group.go @@ -139,10 +139,11 @@ func (r *groupResource) Read(ctx context.Context, req resource.ReadRequest, resp return } - groupResponse, _, err := r.client.ConfigurationAPI. + request := r.client.ConfigurationAPI. GroupsGetConfigurationAppV1GroupsGet(context.Background()). - Uid(state.ID.ValueString()). - Execute() + Uid(state.ID.ValueString()) + + groupResponse, _, err := util.RetryFor429(request.Execute) if err != nil || len(groupResponse.Groups) != 1 { resp.Diagnostics.AddError( diff --git a/pkg/config-api-provider/test/data-sources/group_test.go b/pkg/config-api-provider/test/data-sources/group_test.go index 3a5a54cc..b1237111 100644 --- a/pkg/config-api-provider/test/data-sources/group_test.go +++ b/pkg/config-api-provider/test/data-sources/group_test.go @@ -1,7 +1,6 @@ package data_source_test import ( - "fmt" "regexp" "testing" @@ -95,6 +94,7 @@ func TestGroupDataSource(t *testing.T) { func TestGroupDataSource429Handling(t *testing.T) { defer gock.Off() mockOAuth := util.MockOAuth() + var mock429 *gock.Response resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, @@ -103,7 +103,7 @@ func TestGroupDataSource429Handling(t *testing.T) { // Test Read, is_root not set { PreConfig: func() { - gock.New("https://test.api.capenetworks.com"). + mock429 = gock.New("https://test.api.capenetworks.com"). Get("/configuration/app/v1/groups"). Reply(429). SetHeaders(map[string]string{ @@ -114,7 +114,7 @@ func TestGroupDataSource429Handling(t *testing.T) { util.MockGetGroup( "uid", util.GenerateGroupPaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateGroupResponseModel("uid", "", ""))}), - 2, + 3, ) }, Config: provider.ProviderConfig + ` @@ -126,7 +126,10 @@ func TestGroupDataSource429Handling(t *testing.T) { `, Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("data.uxi_group.my_group", "id", "uid"), - PendingMockCheck{t: t, mocksToExclude: []gock.Mock{mockOAuth.Mock}}.AssertNoPendingMocks(), + func(s *terraform.State) error { + st.Assert(t, mock429.Mock.Request().Counter, 0) + return nil + }, ), }, }, @@ -134,47 +137,3 @@ func TestGroupDataSource429Handling(t *testing.T) { mockOAuth.Mock.Disable() } - -func removeElements(slice1, slice2 []gock.Mock) []gock.Mock { - result := []gock.Mock{} - for _, v1 := range slice1 { - found := false - for _, v2 := range slice2 { - if v1 == v2 { - found = true - break - } - } - if !found { - result = append(result, v1) - } - } - return result -} - -type PendingMockCheck struct { - t st.Fatalf - mocksToExclude []gock.Mock -} - -func (p PendingMockCheck) AssertNoPendingMocks() resource.TestCheckFunc { - return func(s *terraform.State) error { - stillPending := removeElements(gock.Pending(), p.mocksToExclude) - - if len(stillPending) > 0 { - for _, mock := range stillPending { - fmt.Printf( - "Mock still pending: %s %s\tpending calls: %d, done: %t\n", - mock.Request().Method, - mock.Request().URLStruct, - mock.Request().Counter, - mock.Done(), - ) - } - } - - st.Assert(p.t, len(stillPending), 0) - - return nil - } -} diff --git a/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go b/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go index 3d147043..05606641 100644 --- a/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go +++ b/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go @@ -1,11 +1,14 @@ 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" + "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/nbio/st" ) func TestSensorGroupAssignmentDataSource(t *testing.T) { @@ -44,3 +47,51 @@ func TestSensorGroupAssignmentDataSource(t *testing.T) { mockOAuth.Mock.Disable() } + +func TestSensorGroupAssignmentSource429Handling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + var mock429 *gock.Response + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Read testing + { + PreConfig: func() { + mock429 = gock.New("https://test.api.capenetworks.com"). + Get("/configuration/app/v1/sensor-group-assignments"). + Reply(429). + SetHeaders(map[string]string{ + "X-RateLimit-Limit": "100", + "X-RateLimit-Remaining": "0", + "X-RateLimit-Reset": "1", + }) + util.MockGetSensorGroupAssignment( + "uid", + util.GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ + util.StructToMap(util.GenerateSensorGroupAssignmentResponse("uid", "")), + }), + 3, + ) + }, + Config: provider.ProviderConfig + ` + data "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + filter = { + sensor_group_assignment_id = "uid" + } + } + `, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.uxi_sensor_group_assignment.my_sensor_group_assignment", "id", "uid"), + func(s *terraform.State) error { + st.Assert(t, mock429.Mock.Request().Counter, 0) + return nil + }, + ), + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/data-sources/wired_group_test.go b/pkg/config-api-provider/test/data-sources/wired_group_test.go index 2fa33816..b92fccab 100644 --- a/pkg/config-api-provider/test/data-sources/wired_group_test.go +++ b/pkg/config-api-provider/test/data-sources/wired_group_test.go @@ -5,9 +5,11 @@ 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" + "github.com/nbio/st" "github.com/h2non/gock" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" ) func TestWiredNetworkDataSource(t *testing.T) { @@ -50,3 +52,49 @@ func TestWiredNetworkDataSource(t *testing.T) { mockOAuth.Mock.Disable() } + +func TestWiredNetworkDataSource429Handling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + var mock429 *gock.Response + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Read testing + { + PreConfig: func() { + mock429 = gock.New("https://test.api.capenetworks.com"). + Get("/configuration/app/v1/wired-networks"). + Reply(429). + SetHeaders(map[string]string{ + "X-RateLimit-Limit": "100", + "X-RateLimit-Remaining": "0", + "X-RateLimit-Reset": "1", + }) + util.MockGetWiredNetwork( + "uid", + util.GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("uid", "")}), + 3, + ) + }, + Config: provider.ProviderConfig + ` + data "uxi_wired_network" "my_wired_network" { + filter = { + wired_network_id = "uid" + } + } + `, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "id", "uid"), + func(s *terraform.State) error { + st.Assert(t, mock429.Mock.Request().Counter, 0) + return nil + }, + ), + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/data-sources/wireless_group_test.go b/pkg/config-api-provider/test/data-sources/wireless_group_test.go index 50a61d2c..3ad8d544 100644 --- a/pkg/config-api-provider/test/data-sources/wireless_group_test.go +++ b/pkg/config-api-provider/test/data-sources/wireless_group_test.go @@ -1,12 +1,15 @@ 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" - "testing" + "github.com/nbio/st" "github.com/h2non/gock" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" ) func TestWirelessNetworkDataSource(t *testing.T) { @@ -51,3 +54,49 @@ func TestWirelessNetworkDataSource(t *testing.T) { mockOAuth.Mock.Disable() } + +func TestWirelessNetworkDataSource429Handling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + var mock429 *gock.Response + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Read testing + { + PreConfig: func() { + mock429 = gock.New("https://test.api.capenetworks.com"). + Get("/configuration/app/v1/wireless-networks"). + Reply(429). + SetHeaders(map[string]string{ + "X-RateLimit-Limit": "100", + "X-RateLimit-Remaining": "0", + "X-RateLimit-Reset": "1", + }) + util.MockGetWirelessNetwork( + "uid", + util.GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}), + 3, + ) + }, + Config: provider.ProviderConfig + ` + data "uxi_wireless_network" "my_wireless_network" { + filter = { + wireless_network_id = "uid" + } + } + `, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "id", "uid"), + func(s *terraform.State) error { + st.Assert(t, mock429.Mock.Request().Counter, 0) + return nil + }, + ), + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/group_test.go b/pkg/config-api-provider/test/resources/group_test.go index 7a1351bd..91ee20ef 100644 --- a/pkg/config-api-provider/test/resources/group_test.go +++ b/pkg/config-api-provider/test/resources/group_test.go @@ -1,13 +1,16 @@ package resource_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" + "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/nbio/st" ) type Fetcher interface { @@ -144,3 +147,51 @@ func TestRootGroupResource(t *testing.T) { }, }) } + +func TestGroupResource429Handling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + var create429 *gock.Response + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Create and Read testing + { + PreConfig: func() { + create429 = gock.New("https://test.api.capenetworks.com"). + Post("/configuration/app/v1/groups"). + Reply(429). + SetHeaders(map[string]string{ + "X-RateLimit-Limit": "100", + "X-RateLimit-Remaining": "0", + "X-RateLimit-Reset": "1", + }) + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("uid", "", "")), 1) + util.MockGetGroup("uid", util.GenerateGroupPaginatedResponse( + []map[string]interface{}{ + util.StructToMap(util.GenerateGroupResponseModel("uid", "", "")), + }), + 1, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_group.my_group", "id", "uid"), + func(s *terraform.State) error { + st.Assert(t, create429.Mock.Request().Counter, 0) + return nil + }, + ), + }, + // TODO: Test Updating 429s + // TODO: Test Deleting 429s + }, + }) + + mockOAuth.Mock.Disable() +}