diff --git a/test/live/resources/sensor_group_assignment_test.go b/test/live/resources/sensor_group_assignment_test.go new file mode 100644 index 00000000..b3c4ef97 --- /dev/null +++ b/test/live/resources/sensor_group_assignment_test.go @@ -0,0 +1,120 @@ +package resource_test + +import ( + "testing" + + "github.com/aruba-uxi/terraform-provider-configuration/test/live/config" + "github.com/aruba-uxi/terraform-provider-configuration/test/live/provider" + "github.com/aruba-uxi/terraform-provider-configuration/test/live/util" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/nbio/st" +) + +func TestSensorGroupAssignmentResource(t *testing.T) { + const groupName = "tf_provider_acceptance_test_sensor_association_test" + const group2Name = "tf_provider_acceptance_test_sensor_association_test_two" + existingSensorProperties := util.GetSensorProperties(config.SensorUid) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Creating a sensor group assignment + { + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "` + groupName + `" + } + + resource "uxi_sensor" "my_sensor" { + name = "` + existingSensorProperties.Name + `" + address_note = "` + *existingSensorProperties.AddressNotes + `" + notes = "` + *existingSensorProperties.Notes + `" + pcap_mode = "` + *existingSensorProperties.PcapMode + `" + } + + import { + to = uxi_sensor.my_sensor + id = "` + config.SensorUid + `" + } + + resource "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + sensor_id = uxi_sensor.my_sensor.id + group_id = uxi_group.my_group.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr( + "uxi_sensor_group_assignment.my_sensor_group_assignment", + "sensor_id", + config.SensorUid, + ), + resource.TestCheckResourceAttrWith( + "uxi_sensor_group_assignment.my_sensor_group_assignment", + "group_id", + func(value string) error { + st.Assert(t, value, util.GetGroupByName(groupName).Id) + return nil + }, + ), + ), + }, + // ImportState testing + { + ResourceName: "uxi_sensor_group_assignment.my_sensor_group_assignment", + ImportState: true, + ImportStateVerify: true, + }, + // Update and Read testing + { + Config: provider.ProviderConfig + ` + // the original resources + resource "uxi_group" "my_group" { + name = "` + groupName + `" + } + + resource "uxi_sensor" "my_sensor" { + name = "` + existingSensorProperties.Name + `" + address_note = "` + *existingSensorProperties.AddressNotes + `" + notes = "` + *existingSensorProperties.Notes + `" + pcap_mode = "` + *existingSensorProperties.PcapMode + `" + } + + // the new resources we wanna update the assignment to + resource "uxi_group" "my_group_2" { + name = "` + group2Name + `" + } + + // the assignment update, updated from sensor/group to sensor/group_2 + resource "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + sensor_id = uxi_sensor.my_sensor_2.id + group_id = uxi_group.my_group_2.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr( + "uxi_sensor_group_assignment.my_sensor_group_assignment", + "sensor_id", + config.SensorUid, + ), + resource.TestCheckResourceAttrWith( + "uxi_sensor_group_assignment.my_sensor_group_assignment", + "group_id", + func(value string) error { + st.Assert(t, value, util.GetGroupByName(group2Name).Id) + return nil + }, + ), + ), + }, + // Delete sensor-group assignments and remove sensors from state + { + Config: provider.ProviderConfig + ` + removed { + from = uxi_sensor.my_sensor + + lifecycle { + destroy = false + } + }`, + }, + }, + }) +} diff --git a/test/live/util/sensor.go b/test/live/util/sensor.go new file mode 100644 index 00000000..cbf81019 --- /dev/null +++ b/test/live/util/sensor.go @@ -0,0 +1,36 @@ +package util + +import ( + "context" +) + +type sensorProperties struct { + Id string + Name string + Notes *string + AddressNotes *string + PcapMode *string +} + +func GetSensorProperties(id string) sensorProperties { + result, _, err := Client.ConfigurationAPI. + SensorsGet(context.Background()). + Id(id). + Execute() + if err != nil { + panic(err) + } + if len(result.Items) != 1 { + panic("sensor with id `" + id + "` could not be found") + } + sensor := result.Items[0] + // Read these in, as they may not be always constant with the acceptance test + // customer + return sensorProperties{ + Id: sensor.Id, + Name: sensor.Name, + Notes: sensor.Notes.Get(), + AddressNotes: sensor.AddressNote.Get(), + PcapMode: sensor.PcapMode.Get(), + } +} diff --git a/test/mocked/util/utils.go b/test/mocked/util/utils.go index ed30959a..d170f0bf 100644 --- a/test/mocked/util/utils.go +++ b/test/mocked/util/utils.go @@ -25,6 +25,10 @@ func GenerateSensorResponseModel(uid string, postfix string) map[string]interfac } func GenerateMockedSensorResponseModel(uid string, postfix string) resources.SensorResponseModel { + addressNote := "address_note" + postfix + notes := "notes" + postfix + pcapMode := "light" + postfix + return resources.SensorResponseModel{ UID: uid, Serial: "serial" + postfix, @@ -32,11 +36,11 @@ func GenerateMockedSensorResponseModel(uid string, postfix string) resources.Sen ModelNumber: "model_number" + postfix, WifiMacAddress: "wifi_mac_address" + postfix, EthernetMacAddress: "ethernet_mac_address" + postfix, - AddressNote: "address_note" + postfix, + AddressNote: &addressNote, Longitude: 0.0, Latitude: 0.0, - Notes: "notes" + postfix, - PCapMode: "light" + postfix, + Notes: ¬es, + PCapMode: &pcapMode, } }