Skip to content

Commit

Permalink
feat: acceptance test for sensor group assignment resource
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Nov 11, 2024
1 parent a23b170 commit 3871bf0
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 3 deletions.
120 changes: 120 additions & 0 deletions test/live/resources/sensor_group_assignment_test.go
Original file line number Diff line number Diff line change
@@ -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
}
}`,
},
},
})
}
36 changes: 36 additions & 0 deletions test/live/util/sensor.go
Original file line number Diff line number Diff line change
@@ -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(),
}
}
10 changes: 7 additions & 3 deletions test/mocked/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@ 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,
Name: "name" + postfix,
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: &notes,
PCapMode: &pcapMode,
}
}

Expand Down

0 comments on commit 3871bf0

Please sign in to comment.