terraform-provider-stackitp.../stackit/internal/services/iaasalpha/routingtable/routes/datasource_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

199 lines
6.4 KiB
Go

package routes
import (
"context"
"fmt"
"testing"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaasalpha/routingtable/shared"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"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"
)
const (
testRegion = "eu02"
)
var (
testOrganizationId = uuid.NewString()
testNetworkAreaId = uuid.NewString()
testRoutingTableId = uuid.NewString()
testRouteId1 = uuid.NewString()
testRouteId2 = uuid.NewString()
)
func Test_mapDataSourceRoutingTableRoutes(t *testing.T) {
type args struct {
routes *iaasalpha.RouteListResponse
model *RoutingTableRoutesDataSourceModel
region string
}
tests := []struct {
name string
args args
wantErr bool
expectedModel *RoutingTableRoutesDataSourceModel
}{
{
name: "model is nil",
args: args{
model: nil,
routes: &iaasalpha.RouteListResponse{
Items: &[]iaasalpha.Route{},
},
},
wantErr: true,
},
{
name: "response is nil",
args: args{
model: &RoutingTableRoutesDataSourceModel{},
routes: nil,
},
wantErr: true,
},
{
name: "response items is nil",
args: args{
model: nil,
routes: &iaasalpha.RouteListResponse{
Items: nil,
},
},
wantErr: true,
},
{
name: "response items is empty",
args: args{
model: &RoutingTableRoutesDataSourceModel{
OrganizationId: types.StringValue(testOrganizationId),
NetworkAreaId: types.StringValue(testNetworkAreaId),
RoutingTableId: types.StringValue(testRoutingTableId),
Region: types.StringValue(testRegion),
},
routes: &iaasalpha.RouteListResponse{
Items: &[]iaasalpha.Route{},
},
region: testRegion,
},
wantErr: false,
expectedModel: &RoutingTableRoutesDataSourceModel{
Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", testOrganizationId, testRegion, testNetworkAreaId, testRoutingTableId)),
OrganizationId: types.StringValue(testOrganizationId),
NetworkAreaId: types.StringValue(testNetworkAreaId),
RoutingTableId: types.StringValue(testRoutingTableId),
Region: types.StringValue(testRegion),
Routes: types.ListValueMust(
types.ObjectType{AttrTypes: shared.RouteReadModelTypes()}, []attr.Value{},
),
},
},
{
name: "response items has items",
args: args{
model: &RoutingTableRoutesDataSourceModel{
OrganizationId: types.StringValue(testOrganizationId),
NetworkAreaId: types.StringValue(testNetworkAreaId),
RoutingTableId: types.StringValue(testRoutingTableId),
Region: types.StringValue(testRegion),
},
routes: &iaasalpha.RouteListResponse{
Items: &[]iaasalpha.Route{
{
Id: utils.Ptr(testRouteId1),
Destination: utils.Ptr(iaasalpha.DestinationCIDRv4AsRouteDestination(
iaasalpha.NewDestinationCIDRv4("cidrv4", "58.251.236.138/32"),
)),
Nexthop: utils.Ptr(iaasalpha.NexthopIPv4AsRouteNexthop(
iaasalpha.NewNexthopIPv4("ipv4", "10.20.42.2"),
)),
Labels: &map[string]interface{}{
"foo": "bar",
},
CreatedAt: nil,
UpdatedAt: nil,
},
{
Id: utils.Ptr(testRouteId2),
Destination: utils.Ptr(iaasalpha.DestinationCIDRv6AsRouteDestination(
iaasalpha.NewDestinationCIDRv6("cidrv6", "2001:0db8:3c4d:1a2b::/64"),
)),
Nexthop: utils.Ptr(iaasalpha.NexthopIPv6AsRouteNexthop(
iaasalpha.NewNexthopIPv6("ipv6", "172b:f881:46fe:d89a:9332:90f7:3485:236d"),
)),
Labels: &map[string]interface{}{
"key": "value",
},
CreatedAt: nil,
UpdatedAt: nil,
},
},
},
region: testRegion,
},
wantErr: false,
expectedModel: &RoutingTableRoutesDataSourceModel{
Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", testOrganizationId, testRegion, testNetworkAreaId, testRoutingTableId)),
OrganizationId: types.StringValue(testOrganizationId),
NetworkAreaId: types.StringValue(testNetworkAreaId),
RoutingTableId: types.StringValue(testRoutingTableId),
Region: types.StringValue(testRegion),
Routes: types.ListValueMust(
types.ObjectType{AttrTypes: shared.RouteReadModelTypes()}, []attr.Value{
types.ObjectValueMust(shared.RouteReadModelTypes(), map[string]attr.Value{
"route_id": types.StringValue(testRouteId1),
"created_at": types.StringNull(),
"updated_at": types.StringNull(),
"labels": types.MapValueMust(types.StringType, map[string]attr.Value{
"foo": types.StringValue("bar"),
}),
"destination": types.ObjectValueMust(shared.RouteDestinationTypes, map[string]attr.Value{
"type": types.StringValue("cidrv4"),
"value": types.StringValue("58.251.236.138/32"),
}),
"next_hop": types.ObjectValueMust(shared.RouteNextHopTypes, map[string]attr.Value{
"type": types.StringValue("ipv4"),
"value": types.StringValue("10.20.42.2"),
}),
}),
types.ObjectValueMust(shared.RouteReadModelTypes(), map[string]attr.Value{
"route_id": types.StringValue(testRouteId2),
"created_at": types.StringNull(),
"updated_at": types.StringNull(),
"labels": types.MapValueMust(types.StringType, map[string]attr.Value{
"key": types.StringValue("value"),
}),
"destination": types.ObjectValueMust(shared.RouteDestinationTypes, map[string]attr.Value{
"type": types.StringValue("cidrv6"),
"value": types.StringValue("2001:0db8:3c4d:1a2b::/64"),
}),
"next_hop": types.ObjectValueMust(shared.RouteNextHopTypes, map[string]attr.Value{
"type": types.StringValue("ipv6"),
"value": types.StringValue("172b:f881:46fe:d89a:9332:90f7:3485:236d"),
}),
}),
},
),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
if err := mapDataSourceRoutingTableRoutes(ctx, tt.args.routes, tt.args.model, tt.args.region); (err != nil) != tt.wantErr {
t.Errorf("mapDataSourceRoutingTableRoutes() error = %v, wantErr %v", err, tt.wantErr)
return
}
diff := cmp.Diff(tt.args.model, tt.expectedModel)
if diff != "" && !tt.wantErr {
t.Fatalf("mapFieldsFromList(): %s", diff)
}
})
}
}