From 56156325bbe0b35889fdef71a47d40ca608ab9d0 Mon Sep 17 00:00:00 2001 From: Alistair Yan <52126234+1riatsila1@users.noreply.github.com> Date: Wed, 6 Nov 2024 14:22:30 +0400 Subject: [PATCH] feat: acceptance tests | network group assignments (#64) --- test/live/config/config.go | 6 + test/live/resources/group_test.go | 2 +- .../network_group_assignment_test.go | 227 ++++++++++++++++++ test/live/util/util.go | 2 +- 4 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 test/live/config/config.go create mode 100644 test/live/resources/network_group_assignment_test.go diff --git a/test/live/config/config.go b/test/live/config/config.go new file mode 100644 index 00000000..42c9f542 --- /dev/null +++ b/test/live/config/config.go @@ -0,0 +1,6 @@ +package config + +const WiredNetworkUid = "ethernet-658f93b6ea49" +const WiredNetworkName = "tf-provider-acceptance-tests-ethernet-0" +const WirelessNetworkUid = "ssid-35e69ecab0c3" +const WirelessNetworkName = "tf-provider-acceptance-tests-ssid-0" diff --git a/test/live/resources/group_test.go b/test/live/resources/group_test.go index 91ceb4f9..feeb19f0 100644 --- a/test/live/resources/group_test.go +++ b/test/live/resources/group_test.go @@ -204,7 +204,7 @@ func TestRootGroupResource(t *testing.T) { } func checkGroupIsChildOfNode(actualParentGroupId, expectedParentName string) error { - expectedParentGroupId := util.GetNodeByName(expectedParentName).GetId() + expectedParentGroupId := util.GetGroupByName(expectedParentName).GetId() if expectedParentGroupId != actualParentGroupId { return fmt.Errorf( diff --git a/test/live/resources/network_group_assignment_test.go b/test/live/resources/network_group_assignment_test.go new file mode 100644 index 00000000..b57988b6 --- /dev/null +++ b/test/live/resources/network_group_assignment_test.go @@ -0,0 +1,227 @@ +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 TestNetworkGroupAssignmentResourceForWiredNetwork(t *testing.T) { + const groupName = "tf_provider_acceptance_test_network_association_test" + const group2Name = "tf_provider_acceptance_test_network_association_test_two" + + // Test Wired Network Group Assignment + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Creating a network group assignment + { + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "` + groupName + `" + } + + resource "uxi_wired_network" "my_network" { + name = "` + config.WiredNetworkName + `" + } + + import { + to = uxi_wired_network.my_network + id = "` + config.WiredNetworkUid + `" + } + + resource "uxi_network_group_assignment" "my_network_group_assignment" { + network_id = uxi_wired_network.my_network.id + group_id = uxi_group.my_group.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr( + "uxi_network_group_assignment.my_network_group_assignment", + "network_id", + config.WiredNetworkUid, + ), + resource.TestCheckResourceAttrWith( + "uxi_network_group_assignment.my_network_group_assignment", + "group_id", + func(group_id string) error { + st.Assert(t, group_id, util.GetGroupByName(groupName).Id) + return nil + }, + ), + ), + }, + // ImportState testing + { + ResourceName: "uxi_network_group_assignment.my_network_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_wired_network" "my_network" { + name = "` + config.WiredNetworkName + `" + } + + import { + to = uxi_wired_network.my_network + id = "` + config.WiredNetworkUid + `" + } + + // the new resources we wanna update the assignment to + resource "uxi_group" "my_group_2" { + name = "` + group2Name + `" + } + + // the assignment update, updated from network/group to network/group_2 + resource "uxi_network_group_assignment" "my_network_group_assignment" { + network_id = uxi_wired_network.my_network.id + group_id = uxi_group.my_group_2.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr( + "uxi_network_group_assignment.my_network_group_assignment", + "network_id", + config.WiredNetworkUid, + ), + resource.TestCheckResourceAttrWith( + "uxi_network_group_assignment.my_network_group_assignment", + "group_id", + func(group_id string) error { + st.Assert(t, group_id, util.GetGroupByName(group2Name).Id) + return nil + }, + ), + ), + }, + // Delete network-group-assignments and remove networks from state + { + Config: provider.ProviderConfig + ` + removed { + from = uxi_wired_network.my_network + + lifecycle { + destroy = false + } + }`, + }, + }, + }) +} + +func TestNetworkGroupAssignmentResourceForWirelessNetwork(t *testing.T) { + const groupName = "tf_provider_acceptance_test_network_association_test" + const group2Name = "tf_provider_acceptance_test_network_association_test_two" + + // Test Wired Network Group Assignment + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Creating a network group assignment + { + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "` + groupName + `" + } + + resource "uxi_wireless_network" "my_network" { + name = "` + config.WirelessNetworkName + `" + } + + import { + to = uxi_wireless_network.my_network + id = "` + config.WirelessNetworkUid + `" + } + + resource "uxi_network_group_assignment" "my_network_group_assignment" { + network_id = uxi_wireless_network.my_network.id + group_id = uxi_group.my_group.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr( + "uxi_network_group_assignment.my_network_group_assignment", + "network_id", + config.WirelessNetworkUid, + ), + resource.TestCheckResourceAttrWith( + "uxi_network_group_assignment.my_network_group_assignment", + "group_id", + func(group_id string) error { + st.Assert(t, group_id, util.GetGroupByName(groupName).Id) + return nil + }, + ), + ), + }, + // ImportState testing + { + ResourceName: "uxi_network_group_assignment.my_network_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_wireless_network" "my_network" { + name = "` + config.WirelessNetworkName + `" + } + + import { + to = uxi_wireless_network.my_network + id = "` + config.WirelessNetworkUid + `" + } + + // the new resources we wanna update the assignment to + resource "uxi_group" "my_group_2" { + name = "` + group2Name + `" + } + + // the assignment update, updated from network/group to network/group_2 + resource "uxi_network_group_assignment" "my_network_group_assignment" { + network_id = uxi_wireless_network.my_network.id + group_id = uxi_group.my_group_2.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr( + "uxi_network_group_assignment.my_network_group_assignment", + "network_id", + config.WirelessNetworkUid, + ), + resource.TestCheckResourceAttrWith( + "uxi_network_group_assignment.my_network_group_assignment", + "group_id", + func(group_id string) error { + st.Assert(t, group_id, util.GetGroupByName(group2Name).Id) + return nil + }, + ), + ), + }, + // Delete network-group-assignments and remove networks from state + { + Config: provider.ProviderConfig + ` + removed { + from = uxi_wireless_network.my_network + + lifecycle { + destroy = false + } + }`, + }, + }, + }) +} diff --git a/test/live/util/util.go b/test/live/util/util.go index f8a847dd..e866b77a 100644 --- a/test/live/util/util.go +++ b/test/live/util/util.go @@ -43,7 +43,7 @@ func GetRoot() *config_api_client.GroupsGetItem { return nil } -func GetNodeByName(name string) *config_api_client.GroupsGetItem { +func GetGroupByName(name string) *config_api_client.GroupsGetItem { groups, _, _ := Client.ConfigurationAPI.GroupsGet(context.Background()).Execute() for _, group := range groups.Items { if group.Name == name {