terraform-provider-stackitp.../stackit/internal/services/secretsmanager/instance/resource_test.go
Henrique Santos 6372434e56
Implement Secrets Manager instance (#85)
* Implement secrets manager instance resource

* Implement secrets manager data source

* Implement secrets manager acc tests

* Add secrets manager to provider

* Fix typos

* Fix test name

* Fix typo

* Fix instance name

* Lint fix

* Remove unnecessary modifier

---------

Co-authored-by: Henrique Santos <henrique.santos@freiheit.com>
2023-10-16 11:37:34 +01:00

136 lines
2.7 KiB
Go

package secretsmanager
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/secretsmanager"
)
func TestMapFields(t *testing.T) {
tests := []struct {
description string
input *secretsmanager.Instance
expected Model
isValid bool
}{
{
"default_values",
&secretsmanager.Instance{},
Model{
Id: types.StringValue("pid,iid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringNull(),
},
true,
},
{
"simple_values",
&secretsmanager.Instance{
Name: utils.Ptr("name"),
},
Model{
Id: types.StringValue("pid,iid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringValue("name"),
},
true,
},
{
"nil_response",
nil,
Model{},
false,
},
{
"no_resource_id",
&secretsmanager.Instance{},
Model{},
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
state := &Model{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
}
err := mapFields(tt.input, state)
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(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 *secretsmanager.CreateInstancePayload
isValid bool
}{
{
"default_values",
&Model{},
&secretsmanager.CreateInstancePayload{},
true,
},
{
"simple_values",
&Model{
Name: types.StringValue("name"),
},
&secretsmanager.CreateInstancePayload{
Name: utils.Ptr("name"),
},
true,
},
{
"null_fields_and_int_conversions",
&Model{
Name: types.StringValue(""),
},
&secretsmanager.CreateInstancePayload{
Name: utils.Ptr(""),
},
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)
}
}
})
}
}