terraform-provider-stackitp.../stackit/internal/services/loadbalancer/credential/resource_test.go
João Palet d8734270f5
Add new resource and datasource for Load Balancer credentials (#255)
* Implement and test credential resource and data source

* Fix descriptions in load balancer instance schema

* Extend acceptance tests

* Add acceptance test requirements to README

* Generate updated docs

* Fix linter issues

* Add examples and update docs

* Fix examples

* Improvements from review

* Remove credential data source
2024-02-08 12:49:05 +01:00

152 lines
3.4 KiB
Go

package loadbalancer
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer"
)
func TestToCreatePayload(t *testing.T) {
tests := []struct {
description string
input *Model
expected *loadbalancer.CreateCredentialsPayload
isValid bool
}{
{
"default_values_ok",
&Model{},
&loadbalancer.CreateCredentialsPayload{
DisplayName: nil,
Username: nil,
Password: nil,
},
true,
},
{
"simple_values_ok",
&Model{
DisplayName: types.StringValue("display_name"),
Username: types.StringValue("username"),
Password: types.StringValue("password"),
},
&loadbalancer.CreateCredentialsPayload{
DisplayName: utils.Ptr("display_name"),
Username: utils.Ptr("username"),
Password: utils.Ptr("password"),
},
true,
},
{
"nil_model",
nil,
nil,
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
output, err := toCreatePayload(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 TestMapFields(t *testing.T) {
tests := []struct {
description string
input *loadbalancer.CredentialsResponse
expected *Model
isValid bool
}{
{
"default_values_ok",
&loadbalancer.CredentialsResponse{
CredentialsRef: utils.Ptr("credentials_ref"),
Username: utils.Ptr("username"),
},
&Model{
Id: types.StringValue("pid,credentials_ref"),
ProjectId: types.StringValue("pid"),
CredentialsRef: types.StringValue("credentials_ref"),
Username: types.StringValue("username"),
},
true,
},
{
"simple_values_ok",
&loadbalancer.CredentialsResponse{
CredentialsRef: utils.Ptr("credentials_ref"),
DisplayName: utils.Ptr("display_name"),
Username: utils.Ptr("username"),
},
&Model{
Id: types.StringValue("pid,credentials_ref"),
ProjectId: types.StringValue("pid"),
CredentialsRef: types.StringValue("credentials_ref"),
DisplayName: types.StringValue("display_name"),
Username: types.StringValue("username"),
},
true,
},
{
"nil_response",
nil,
&Model{},
false,
},
{
"no_username",
&loadbalancer.CredentialsResponse{
CredentialsRef: utils.Ptr("credentials_ref"),
DisplayName: utils.Ptr("display_name"),
},
&Model{},
false,
},
{
"no_credentials_ref",
&loadbalancer.CredentialsResponse{
DisplayName: utils.Ptr("display_name"),
Username: utils.Ptr("username"),
},
&Model{},
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
model := &Model{
ProjectId: tt.expected.ProjectId,
}
err := mapFields(tt.input, model)
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(model, tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}