Skip to content

Commit

Permalink
More comprehensive resource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Oct 31, 2024
1 parent 8b5764b commit 349e73f
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions pkg/config-api-provider/test/resources/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"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"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
"github.com/nbio/st"
"regexp"
"testing"
)
Expand Down Expand Up @@ -102,3 +105,121 @@ func TestAgentResource(t *testing.T) {

mockOAuth.Mock.Disable()
}

func TestAgentResource429Handling(t *testing.T) {
defer gock.Off()
mockOAuth := util.MockOAuth()
var request429 *gock.Response

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
// we required terraform 1.7.0 and above for the `removed` block
tfversion.RequireAbove(tfversion.Version1_7_0),
},
Steps: []resource.TestStep{
// Importing a agent
{
PreConfig: func() {
request429 = gock.New("https://test.api.capenetworks.com").
Get("/networking-uxi/v1alpha1/agents").
Reply(429).
SetHeaders(util.RateLimitingHeaders)
util.MockGetAgent("uid", util.GeneratePaginatedResponse(
[]map[string]interface{}{util.GenerateAgentResponseModel("uid", "")}),
2,
)
},
Config: provider.ProviderConfig + `
resource "uxi_agent" "my_agent" {
name = "name"
notes = "notes"
pcap_mode = "light"
}
import {
to = uxi_agent.my_agent
id = "uid"
}`,

Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_agent.my_agent", "id", "uid"),
func(s *terraform.State) error {
st.Assert(t, request429.Mock.Request().Counter, 0)
return nil
},
),
},
// Deletion occurs automatically
},
})

mockOAuth.Mock.Disable()

}
func TestAgentResourceHttpErrorHandling(t *testing.T) {
defer gock.Off()
mockOAuth := util.MockOAuth()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
// we required terraform 1.7.0 and above for the `removed` block
tfversion.RequireAbove(tfversion.Version1_7_0),
},
Steps: []resource.TestStep{
// Read 5xx error
{
PreConfig: func() {
gock.New("https://test.api.capenetworks.com").
Get("/networking-uxi/v1alpha1/agents").
Reply(500).
JSON(map[string]interface{}{
"httpStatusCode": 500,
"errorCode": "HPE_GL_ERROR_INTERNAL_SERVER_ERROR",
"message": "Current request cannot be processed due to unknown issue",
"debugId": "12312-123123-123123-1231212",
})
},
Config: provider.ProviderConfig + `
resource "uxi_agent" "my_agent" {
name = "name"
notes = "notes"
pcap_mode = "light"
}
import {
to = uxi_agent.my_agent
id = "uid"
}`,

ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`),
},
// Read not found
{
PreConfig: func() {
util.MockGetAgent(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{}),
1,
)
},
Config: provider.ProviderConfig + `
resource "uxi_agent" "my_agent" {
name = "name"
notes = "notes"
pcap_mode = "light"
}
import {
to = uxi_agent.my_agent
id = "uid"
}`,

ExpectError: regexp.MustCompile(`Error: Cannot import non-existent remote object`),
},
},
})

mockOAuth.Mock.Disable()
}

0 comments on commit 349e73f

Please sign in to comment.