Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: neaten up test directory #31

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package test
package data_source_test

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 = {}
}
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -83,4 +85,6 @@ func TestGroupDataSource(t *testing.T) {
// TODO: Test retrieving the root group
},
})

mockOAuth.Mock.Disable()
}
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -40,4 +41,6 @@ func TestSensorGroupAssignmentDataSource(t *testing.T) {
},
},
})

mockOAuth.Mock.Disable()
}
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -44,4 +47,6 @@ func TestWiredNetworkDataSource(t *testing.T) {
},
},
})

mockOAuth.Mock.Disable()
}
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand All @@ -46,4 +48,6 @@ func TestWirelessNetworkDataSource(t *testing.T) {
},
},
})

mockOAuth.Mock.Disable()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test
package provider

import (
"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider"
Expand All @@ -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"
Expand All @@ -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")()),
}
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package test
package resource_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"
Expand All @@ -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
}
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -162,4 +164,6 @@ func TestAgentGroupAssignmentResource(t *testing.T) {
// Delete testing automatically occurs in TestCase
},
})

mockOAuth.Mock.Disable()
}
Loading
Loading