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

175 lines
5.8 KiB
Go

package tables
import (
"context"
"fmt"
"testing"
"time"
"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 = "eu01"
)
var (
organizationId = uuid.New()
networkAreaId = uuid.New()
routingTableId = uuid.New()
secondRoutingTableId = uuid.New()
)
func TestMapDataFields(t *testing.T) {
terraformId := fmt.Sprintf("%s,%s,%s", organizationId.String(), testRegion, networkAreaId.String())
createdAt := time.Now()
updatedAt := time.Now().Add(5 * time.Minute)
tests := []struct {
description string
state DataSourceModelTables
input *iaasalpha.RoutingTableListResponse
expected DataSourceModelTables
isValid bool
}{
{
"default_values",
DataSourceModelTables{
OrganizationId: types.StringValue(organizationId.String()),
NetworkAreaId: types.StringValue(networkAreaId.String()),
Region: types.StringValue(testRegion),
},
&iaasalpha.RoutingTableListResponse{
Items: &[]iaasalpha.RoutingTable{
{
Id: utils.Ptr(routingTableId.String()),
Name: utils.Ptr("test"),
Description: utils.Ptr("description"),
Default: utils.Ptr(true),
CreatedAt: &createdAt,
UpdatedAt: &updatedAt,
SystemRoutes: utils.Ptr(false),
},
},
},
DataSourceModelTables{
Id: types.StringValue(terraformId),
OrganizationId: types.StringValue(organizationId.String()),
NetworkAreaId: types.StringValue(networkAreaId.String()),
Region: types.StringValue(testRegion),
Items: types.ListValueMust(types.ObjectType{AttrTypes: shared.RoutingTableReadModelTypes()}, []attr.Value{
types.ObjectValueMust(shared.RoutingTableReadModelTypes(), map[string]attr.Value{
"routing_table_id": types.StringValue(routingTableId.String()),
"name": types.StringValue("test"),
"description": types.StringValue("description"),
"default": types.BoolValue(true),
"system_routes": types.BoolValue(false),
"created_at": types.StringValue(createdAt.Format(time.RFC3339)),
"updated_at": types.StringValue(updatedAt.Format(time.RFC3339)),
"labels": types.MapNull(types.StringType),
}),
}),
},
true,
},
{
"two routing tables",
DataSourceModelTables{
OrganizationId: types.StringValue(organizationId.String()),
NetworkAreaId: types.StringValue(networkAreaId.String()),
Region: types.StringValue(testRegion),
},
&iaasalpha.RoutingTableListResponse{
Items: &[]iaasalpha.RoutingTable{
{
Id: utils.Ptr(routingTableId.String()),
Name: utils.Ptr("test"),
Description: utils.Ptr("description"),
Default: utils.Ptr(true),
CreatedAt: &createdAt,
UpdatedAt: &updatedAt,
SystemRoutes: utils.Ptr(false),
},
{
Id: utils.Ptr(secondRoutingTableId.String()),
Name: utils.Ptr("test2"),
Description: utils.Ptr("description2"),
Default: utils.Ptr(false),
CreatedAt: &createdAt,
UpdatedAt: &updatedAt,
SystemRoutes: utils.Ptr(false),
},
},
},
DataSourceModelTables{
Id: types.StringValue(terraformId),
OrganizationId: types.StringValue(organizationId.String()),
NetworkAreaId: types.StringValue(networkAreaId.String()),
Region: types.StringValue(testRegion),
Items: types.ListValueMust(types.ObjectType{AttrTypes: shared.RoutingTableReadModelTypes()}, []attr.Value{
types.ObjectValueMust(shared.RoutingTableReadModelTypes(), map[string]attr.Value{
"routing_table_id": types.StringValue(routingTableId.String()),
"name": types.StringValue("test"),
"description": types.StringValue("description"),
"default": types.BoolValue(true),
"system_routes": types.BoolValue(false),
"created_at": types.StringValue(createdAt.Format(time.RFC3339)),
"updated_at": types.StringValue(updatedAt.Format(time.RFC3339)),
"labels": types.MapNull(types.StringType),
}),
types.ObjectValueMust(shared.RoutingTableReadModelTypes(), map[string]attr.Value{
"routing_table_id": types.StringValue(secondRoutingTableId.String()),
"name": types.StringValue("test2"),
"description": types.StringValue("description2"),
"default": types.BoolValue(false),
"system_routes": types.BoolValue(false),
"created_at": types.StringValue(createdAt.Format(time.RFC3339)),
"updated_at": types.StringValue(updatedAt.Format(time.RFC3339)),
"labels": types.MapNull(types.StringType),
}),
}),
},
true,
},
{
"response_fields_items_nil_fail",
DataSourceModelTables{},
&iaasalpha.RoutingTableListResponse{
Items: nil,
},
DataSourceModelTables{},
false,
},
{
"response_nil_fail",
DataSourceModelTables{},
nil,
DataSourceModelTables{},
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
err := mapDataSourceRoutingTables(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)
}
}
})
}
}