Skip to content

Commit

Permalink
updates, tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Sep 30, 2024
1 parent 78771ff commit 41842a3
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 55 deletions.
7 changes: 4 additions & 3 deletions pkg/config-api-provider/provider/resources/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
55 changes: 7 additions & 48 deletions pkg/config-api-provider/test/data-sources/group_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package data_source_test

import (
"fmt"
"regexp"
"testing"

Expand Down Expand Up @@ -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,
Expand All @@ -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{
Expand All @@ -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 + `
Expand All @@ -126,55 +126,14 @@ 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
},
),
},
},
})

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
}
}
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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()
}
48 changes: 48 additions & 0 deletions pkg/config-api-provider/test/data-sources/wired_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
}
51 changes: 50 additions & 1 deletion pkg/config-api-provider/test/data-sources/wireless_group_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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()
}
55 changes: 53 additions & 2 deletions pkg/config-api-provider/test/resources/group_test.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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()
}

0 comments on commit 41842a3

Please sign in to comment.