-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into ay/feat/acceptance-tests/data-source/service…
…-test-group-assignment
- Loading branch information
Showing
6 changed files
with
261 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package data_source_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aruba-uxi/terraform-provider-hpeuxi/test/live/config" | ||
"github.com/aruba-uxi/terraform-provider-hpeuxi/test/live/provider" | ||
"github.com/aruba-uxi/terraform-provider-hpeuxi/test/live/util" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/terraform" | ||
) | ||
|
||
func TestNetworkGroupAssignmentDataSource(t *testing.T) { | ||
const groupName = "tf_provider_acceptance_test_network_group_assignment_datasource" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: provider.ProviderConfig + ` | ||
// create the resource to be used as a datasource | ||
resource "uxi_group" "my_group" { | ||
name = "` + groupName + `" | ||
} | ||
data "uxi_wired_network" "my_network" { | ||
filter = { | ||
wired_network_id = "` + config.WiredNetworkUid + `" | ||
} | ||
} | ||
resource "uxi_network_group_assignment" "my_network_group_assignment" { | ||
network_id = data.uxi_wired_network.my_network.id | ||
group_id = uxi_group.my_group.id | ||
} | ||
// the actual datasource | ||
data "uxi_network_group_assignment" "my_network_group_assignment" { | ||
filter = { | ||
network_group_assignment_id = uxi_network_group_assignment.my_network_group_assignment.id | ||
} | ||
} | ||
`, | ||
Check: resource.ComposeTestCheckFunc( | ||
func(s *terraform.State) error { | ||
resourceName := "uxi_network_group_assignment.my_network_group_assignment" | ||
rs := s.RootModule().Resources[resourceName] | ||
return util.CheckStateAgainstNetworkGroupAssignment( | ||
t, | ||
"data.uxi_network_group_assignment.my_network_group_assignment", | ||
util.GetNetworkGroupAssignment(rs.Primary.ID), | ||
)(s) | ||
}, | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package resource_test | ||
|
||
import ( | ||
config_api_client "github.com/aruba-uxi/terraform-provider-hpeuxi/pkg/config-api-client" | ||
"github.com/aruba-uxi/terraform-provider-hpeuxi/test/live/config" | ||
"github.com/aruba-uxi/terraform-provider-hpeuxi/test/live/provider" | ||
"github.com/aruba-uxi/terraform-provider-hpeuxi/test/live/util" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
|
||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestSensorResource(t *testing.T) { | ||
originalSensor := util.GetSensorProperties(config.SensorUid) | ||
updatedNotes := "tf_provider_acceptance_test_update_notes" | ||
updatedAddressNote := "tf_provider_acceptance_test_update_address_note" | ||
updatedPcapMode := "off" | ||
updatedSensor := config_api_client.SensorItem{ | ||
Id: config.SensorUid, | ||
Name: "tf_provider_acceptance_test_update_name", | ||
Notes: *config_api_client.NewNullableString(&updatedNotes), | ||
AddressNote: *config_api_client.NewNullableString(&updatedAddressNote), | ||
PcapMode: *config_api_client.NewNullableString(&updatedPcapMode), | ||
} | ||
|
||
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{ | ||
// Creating a sensor is not allowed | ||
{ | ||
Config: provider.ProviderConfig + ` | ||
resource "uxi_sensor" "my_sensor" { | ||
name = "` + originalSensor.Name + `" | ||
}`, | ||
|
||
ExpectError: regexp.MustCompile( | ||
`creating a sensor is not supported; sensors can only be imported`, | ||
), | ||
}, | ||
// Importing a sensor | ||
{ | ||
Config: provider.ProviderConfig + ` | ||
resource "uxi_sensor" "my_sensor" { | ||
name = "` + originalSensor.Name + `" | ||
} | ||
import { | ||
to = uxi_sensor.my_sensor | ||
id = "` + config.SensorUid + `" | ||
}`, | ||
|
||
Check: resource.ComposeAggregateTestCheckFunc(), | ||
}, | ||
// ImportState testing | ||
{ | ||
ResourceName: "uxi_sensor.my_sensor", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
// Update testing | ||
{ | ||
Config: provider.ProviderConfig + ` | ||
resource "uxi_sensor" "my_sensor" { | ||
name = "` + updatedSensor.Name + `" | ||
address_note = "` + updatedSensor.GetAddressNote() + `" | ||
notes = "` + updatedSensor.GetNotes() + `" | ||
pcap_mode = "` + updatedSensor.GetPcapMode() + `" | ||
}`, | ||
Check: util.CheckResourceStateAgainstSensor( | ||
t, | ||
"uxi_sensor.my_sensor", | ||
updatedSensor, | ||
), | ||
}, | ||
// Update sensor back to original | ||
{ | ||
Config: provider.ProviderConfig + ` | ||
resource "uxi_sensor" "my_sensor" { | ||
name = "` + originalSensor.Name + `" | ||
address_note = "` + originalSensor.GetAddressNote() + `" | ||
notes = "` + originalSensor.GetNotes() + `" | ||
pcap_mode = "` + originalSensor.GetPcapMode() + `" | ||
}`, | ||
Check: util.CheckResourceStateAgainstSensor( | ||
t, | ||
"uxi_sensor.my_sensor", | ||
originalSensor, | ||
), | ||
}, | ||
// Deleting a sensor is not allowed | ||
{ | ||
Config: provider.ProviderConfig + ``, | ||
ExpectError: regexp.MustCompile( | ||
`deleting a sensor is not supported; sensors can only removed from state`, | ||
), | ||
}, | ||
// Remove sensor from state | ||
{ | ||
Config: provider.ProviderConfig + ` | ||
removed { | ||
from = uxi_sensor.my_sensor | ||
lifecycle { | ||
destroy = false | ||
} | ||
}`, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
|
||
config_api_client "github.com/aruba-uxi/terraform-provider-hpeuxi/pkg/config-api-client" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/nbio/st" | ||
) | ||
|
||
func GetNetworkGroupAssignment(id string) config_api_client.NetworkGroupAssignmentsItem { | ||
result, _, err := Client.ConfigurationAPI. | ||
NetworkGroupAssignmentsGet(context.Background()). | ||
Id(id). | ||
Execute() | ||
if err != nil { | ||
panic(err) | ||
} | ||
if len(result.Items) != 1 { | ||
panic("network_group_assignment with id `" + id + "` could not be found") | ||
} | ||
return result.Items[0] | ||
} | ||
|
||
func CheckStateAgainstNetworkGroupAssignment( | ||
t st.Fatalf, | ||
entity string, | ||
networkGroupAssignment config_api_client.NetworkGroupAssignmentsItem, | ||
) resource.TestCheckFunc { | ||
return resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(entity, "id", networkGroupAssignment.Id), | ||
resource.TestCheckResourceAttr(entity, "group_id", networkGroupAssignment.Group.Id), | ||
resource.TestCheckResourceAttr(entity, "network_id", networkGroupAssignment.Network.Id), | ||
) | ||
} |
Oops, something went wrong.