chore(iaas): extract mapping of labels into util func (#867)

This commit is contained in:
Ruben Hönle 2025-06-05 13:46:33 +02:00 committed by GitHub
parent ad24ebe52d
commit 281d31f615
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 156 additions and 171 deletions

View file

@ -6,6 +6,10 @@ import (
"reflect"
"testing"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-framework/diag"
sdkClients "github.com/stackitcloud/stackit-sdk-go/core/clients"
"github.com/stackitcloud/stackit-sdk-go/core/config"
@ -92,3 +96,84 @@ func TestConfigureClient(t *testing.T) {
})
}
}
func TestMapLabels(t *testing.T) {
type args struct {
responseLabels *map[string]interface{}
currentLabels types.Map
}
tests := []struct {
name string
args args
want basetypes.MapValue
wantErr bool
}{
{
name: "response labels is set",
args: args{
responseLabels: &map[string]interface{}{
"foo1": "bar1",
"foo2": "bar2",
},
currentLabels: types.MapUnknown(types.StringType),
},
wantErr: false,
want: types.MapValueMust(types.StringType, map[string]attr.Value{
"foo1": types.StringValue("bar1"),
"foo2": types.StringValue("bar2"),
}),
},
{
name: "response labels is set but empty",
args: args{
responseLabels: &map[string]interface{}{},
currentLabels: types.MapUnknown(types.StringType),
},
wantErr: false,
want: types.MapValueMust(types.StringType, map[string]attr.Value{}),
},
{
name: "response labels is nil and model labels is nil",
args: args{
responseLabels: nil,
currentLabels: types.MapNull(types.StringType),
},
wantErr: false,
want: types.MapNull(types.StringType),
},
{
name: "response labels is nil and model labels is set",
args: args{
responseLabels: nil,
currentLabels: types.MapValueMust(types.StringType, map[string]attr.Value{
"foo1": types.StringValue("bar1"),
"foo2": types.StringValue("bar2"),
}),
},
wantErr: false,
want: types.MapValueMust(types.StringType, map[string]attr.Value{}),
},
{
name: "response labels is nil and model labels is set but empty",
args: args{
responseLabels: nil,
currentLabels: types.MapValueMust(types.StringType, map[string]attr.Value{}),
},
wantErr: false,
want: types.MapValueMust(types.StringType, map[string]attr.Value{}),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
got, err := MapLabels(ctx, tt.args.responseLabels, tt.args.currentLabels)
if (err != nil) != tt.wantErr {
t.Errorf("MapLabels() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapLabels() got = %v, want %v", got, tt.want)
}
})
}
}