From 8668c06af3be9a614f67d97422edd442275b1ea6 Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Tue, 12 Nov 2024 15:43:05 +0400 Subject: [PATCH] feat: acceptance tests | data source | sensor group assignment --- .../sensor_group_assignment_test.go | 58 +++++++++++++++++++ .../network_group_assignment_test.go | 8 +-- .../resources/sensor_group_assignment_test.go | 4 +- test/live/util/sensor_group_assignment.go | 35 +++++++++++ 4 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 test/live/datasources/sensor_group_assignment_test.go create mode 100644 test/live/util/sensor_group_assignment.go diff --git a/test/live/datasources/sensor_group_assignment_test.go b/test/live/datasources/sensor_group_assignment_test.go new file mode 100644 index 00000000..64831361 --- /dev/null +++ b/test/live/datasources/sensor_group_assignment_test.go @@ -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 TestSensorGroupAssignmentDataSource(t *testing.T) { + const groupName = "tf_provider_acceptance_test_sensor_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_sensor" "my_sensor" { + filter = { + sensor_id = "` + config.SensorUid + `" + } + } + + resource "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + sensor_id = data.uxi_sensor.my_sensor.id + group_id = uxi_group.my_group.id + } + + // the actual datasource + data "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + filter = { + sensor_group_assignment_id = uxi_sensor_group_assignment.my_sensor_group_assignment.id + } + } + `, + Check: resource.ComposeTestCheckFunc( + func(s *terraform.State) error { + resourceName := "uxi_sensor_group_assignment.my_sensor_group_assignment" + rs := s.RootModule().Resources[resourceName] + return util.CheckStateAgainstSensorGroupAssignment( + t, + "data.uxi_sensor_group_assignment.my_sensor_group_assignment", + util.GetSensorGroupAssignment(rs.Primary.ID), + )(s) + }, + ), + }, + }, + }) +} diff --git a/test/live/resources/network_group_assignment_test.go b/test/live/resources/network_group_assignment_test.go index d1628c65..46e80cae 100644 --- a/test/live/resources/network_group_assignment_test.go +++ b/test/live/resources/network_group_assignment_test.go @@ -11,8 +11,8 @@ import ( ) func TestNetworkGroupAssignmentResourceForWiredNetwork(t *testing.T) { - const groupName = "tf_provider_acceptance_test_network_association_test" - const group2Name = "tf_provider_acceptance_test_network_association_test_two" + const groupName = "tf_provider_acceptance_test_network_assignment_test" + const group2Name = "tf_provider_acceptance_test_network_assignment_test_two" // Test Wired Network Group Assignment resource.Test(t, resource.TestCase{ @@ -119,8 +119,8 @@ func TestNetworkGroupAssignmentResourceForWiredNetwork(t *testing.T) { } func TestNetworkGroupAssignmentResourceForWirelessNetwork(t *testing.T) { - const groupName = "tf_provider_acceptance_test_network_association_test" - const group2Name = "tf_provider_acceptance_test_network_association_test_two" + const groupName = "tf_provider_acceptance_test_network_assignment_test" + const group2Name = "tf_provider_acceptance_test_network_assignment_test_two" // Test Wired Network Group Assignment resource.Test(t, resource.TestCase{ diff --git a/test/live/resources/sensor_group_assignment_test.go b/test/live/resources/sensor_group_assignment_test.go index bda1e361..80a33c08 100644 --- a/test/live/resources/sensor_group_assignment_test.go +++ b/test/live/resources/sensor_group_assignment_test.go @@ -11,8 +11,8 @@ import ( ) func TestSensorGroupAssignmentResource(t *testing.T) { - const groupName = "tf_provider_acceptance_test_sensor_association_test" - const group2Name = "tf_provider_acceptance_test_sensor_association_test_two" + const groupName = "tf_provider_acceptance_test_sensor_assignment_test" + const group2Name = "tf_provider_acceptance_test_sensor_assignment_test_two" existingSensorProperties := util.GetSensorProperties(config.SensorUid) resource.Test(t, resource.TestCase{ diff --git a/test/live/util/sensor_group_assignment.go b/test/live/util/sensor_group_assignment.go new file mode 100644 index 00000000..801dae2c --- /dev/null +++ b/test/live/util/sensor_group_assignment.go @@ -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 GetSensorGroupAssignment(id string) config_api_client.SensorGroupAssignmentsItem { + result, _, err := Client.ConfigurationAPI. + SensorGroupAssignmentsGet(context.Background()). + Id(id). + Execute() + if err != nil { + panic(err) + } + if len(result.Items) != 1 { + panic("sensor_group_assignment with id `" + id + "` could not be found") + } + return result.Items[0] +} + +func CheckStateAgainstSensorGroupAssignment( + t st.Fatalf, + entity string, + sensorGroupAssignment config_api_client.SensorGroupAssignmentsItem, +) resource.TestCheckFunc { + return resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(entity, "id", sensorGroupAssignment.Id), + resource.TestCheckResourceAttr(entity, "group_id", sensorGroupAssignment.Group.Id), + resource.TestCheckResourceAttr(entity, "sensor_id", sensorGroupAssignment.Sensor.Id), + ) +}