terraform-provider-stackitp.../stackit/internal/services/iaasalpha/routingtable/table/resource_test.go
Ruben Hönle 9ff9b8f610
feat(iaas): add experimental support for routing tables and routes (#896)
* Merged PR 788126: feat(iaas): Onboard routing tables

feat(iaas): Onboard routing tables

Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>

* Merged PR 793350: fix(routingtable): region attribute is missing in scheme

fix(routingtable): region attribute is missing in scheme

Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>

* Merged PR 797968: feat(iaas): onboarding of routing table routes

relates to STACKITTPR-241

* use iaasalpha sdk from github

* resolve todos

* remove routes from routing table model

* restructure packages

* acc tests routing tables

* add acc tests for routes

* chore(iaas): mark routing table resources as experimental

* chore(deps): use iaasalpha sdk v0.1.19-alpha

* Review feedback

Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>

---------

Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
Co-authored-by: Alexander Dahmen (EXT) <Alexander.Dahmen_ext@external.mail.schwarz>
Co-authored-by: Alexander Dahmen <alexander.dahmen@inovex.de>
2025-07-02 10:30:50 +02:00

212 lines
5.2 KiB
Go

package table
import (
"context"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaasalpha"
)
func TestMapFields(t *testing.T) {
const testRegion = "eu01"
id := fmt.Sprintf("%s,%s,%s,%s", "oid", testRegion, "aid", "rtid")
tests := []struct {
description string
state Model
input *iaasalpha.RoutingTable
expected Model
isValid bool
}{
{
"default_values",
Model{
OrganizationId: types.StringValue("oid"),
NetworkAreaId: types.StringValue("aid"),
},
&iaasalpha.RoutingTable{
Id: utils.Ptr("rtid"),
Name: utils.Ptr("default_values"),
},
Model{
Id: types.StringValue(id),
OrganizationId: types.StringValue("oid"),
RoutingTableId: types.StringValue("rtid"),
Name: types.StringValue("default_values"),
NetworkAreaId: types.StringValue("aid"),
Labels: types.MapNull(types.StringType),
Region: types.StringValue(testRegion),
},
true,
},
{
"values_ok",
Model{
OrganizationId: types.StringValue("oid"),
NetworkAreaId: types.StringValue("aid"),
},
&iaasalpha.RoutingTable{
Id: utils.Ptr("rtid"),
Name: utils.Ptr("values_ok"),
Description: utils.Ptr("Description"),
Labels: &map[string]interface{}{
"key": "value",
},
},
Model{
Id: types.StringValue(id),
OrganizationId: types.StringValue("oid"),
RoutingTableId: types.StringValue("rtid"),
Name: types.StringValue("values_ok"),
Description: types.StringValue("Description"),
NetworkAreaId: types.StringValue("aid"),
Region: types.StringValue(testRegion),
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
"key": types.StringValue("value"),
}),
},
true,
},
{
"response_fields_nil_fail",
Model{},
&iaasalpha.RoutingTable{
Id: nil,
},
Model{},
false,
},
{
"response_nil_fail",
Model{},
nil,
Model{},
false,
},
{
"no_resource_id",
Model{
OrganizationId: types.StringValue("oid"),
NetworkAreaId: types.StringValue("naid"),
},
&iaasalpha.RoutingTable{},
Model{},
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
err := mapFields(context.Background(), tt.input, &tt.state, testRegion)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(tt.state, tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}
func TestToCreatePayload(t *testing.T) {
tests := []struct {
description string
input *Model
expected *iaasalpha.AddRoutingTableToAreaPayload
isValid bool
}{
{
description: "default_ok",
input: &Model{
Description: types.StringValue("Description"),
Name: types.StringValue("default_ok"),
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
"key": types.StringValue("value"),
}),
SystemRoutes: types.BoolValue(true),
},
expected: &iaasalpha.AddRoutingTableToAreaPayload{
Description: utils.Ptr("Description"),
Name: utils.Ptr("default_ok"),
Labels: &map[string]interface{}{
"key": "value",
},
SystemRoutes: utils.Ptr(true),
},
isValid: true,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
output, err := toCreatePayload(context.Background(), tt.input)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(output, tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}
func TestToUpdatePayload(t *testing.T) {
tests := []struct {
description string
input *Model
expected *iaasalpha.UpdateRoutingTableOfAreaPayload
isValid bool
}{
{
"default_ok",
&Model{
Description: types.StringValue("Description"),
Name: types.StringValue("default_ok"),
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
"key1": types.StringValue("value1"),
"key2": types.StringValue("value2"),
}),
},
&iaasalpha.UpdateRoutingTableOfAreaPayload{
Description: utils.Ptr("Description"),
Name: utils.Ptr("default_ok"),
Labels: &map[string]interface{}{
"key1": "value1",
"key2": "value2",
},
},
true,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
output, err := toUpdatePayload(context.Background(), tt.input, types.MapNull(types.StringType))
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(output, tt.expected, cmp.AllowUnexported(iaasalpha.NullableString{}))
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}