## Description
<!-- **Please link some issue here describing what you are trying to achieve.**
In case there is no issue present for your PR, please consider creating one.
At least please give us some description what you are trying to achieve and why your change is needed. -->
relates to #1234
## Checklist
- [ ] Issue was linked above
- [ ] Code format was applied: `make fmt`
- [ ] Examples were added / adjusted (see `examples/` directory)
- [x] Docs are up-to-date: `make generate-docs` (will be checked by CI)
- [ ] Unit tests got implemented or updated
- [ ] Acceptance tests got implemented or updated (see e.g. [here](f5f99d1709/stackit/internal/services/dns/dns_acc_test.go))
- [x] Unit tests are passing: `make test` (will be checked by CI)
- [x] No linter issues: `make lint` (will be checked by CI)
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Co-authored-by: marcel.henselin <marcel.henselin@stackit.cloud>
Reviewed-on: #81
540 lines
13 KiB
Go
540 lines
13 KiB
Go
package sqlserverflexalpha
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/hashicorp/terraform-plugin-framework/attr"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
|
|
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v3alpha1api"
|
|
)
|
|
|
|
func TestMapDataSourceFields(t *testing.T) {
|
|
const testRegion = "region"
|
|
tests := []struct {
|
|
description string
|
|
input *v3alpha1api.GetUserResponse
|
|
region string
|
|
expected dataSourceModel
|
|
isValid bool
|
|
}{
|
|
{
|
|
"default_values",
|
|
&v3alpha1api.GetUserResponse{},
|
|
testRegion,
|
|
dataSourceModel{
|
|
Id: types.StringValue("pid,region,iid,1"),
|
|
UserId: types.Int64Value(1),
|
|
InstanceId: types.StringValue("iid"),
|
|
ProjectId: types.StringValue("pid"),
|
|
Username: types.StringValue(""),
|
|
Roles: types.List(types.SetNull(types.StringType)),
|
|
Host: types.StringValue(""),
|
|
Port: types.Int64Value(0),
|
|
Region: types.StringValue(testRegion),
|
|
Status: types.StringValue(""),
|
|
DefaultDatabase: types.StringValue(""),
|
|
},
|
|
true,
|
|
},
|
|
{
|
|
"simple_values",
|
|
&v3alpha1api.GetUserResponse{
|
|
Roles: []string{
|
|
"##STACKIT_SQLAgentUser##",
|
|
"##STACKIT_DatabaseManager##",
|
|
"##STACKIT_LoginManager##",
|
|
"##STACKIT_SQLAgentManager##",
|
|
"##STACKIT_ProcessManager##",
|
|
"##STACKIT_ServerManager##",
|
|
},
|
|
Username: "username",
|
|
Host: "host",
|
|
Port: int32(1234),
|
|
Status: "active",
|
|
DefaultDatabase: "default_db",
|
|
},
|
|
testRegion,
|
|
dataSourceModel{
|
|
Id: types.StringValue("pid,region,iid,1"),
|
|
UserId: types.Int64Value(1),
|
|
InstanceId: types.StringValue("iid"),
|
|
ProjectId: types.StringValue("pid"),
|
|
Username: types.StringValue("username"),
|
|
Roles: types.List(
|
|
types.SetValueMust(
|
|
types.StringType, []attr.Value{
|
|
types.StringValue("##STACKIT_DatabaseManager##"),
|
|
types.StringValue("##STACKIT_LoginManager##"),
|
|
types.StringValue("##STACKIT_ProcessManager##"),
|
|
types.StringValue("##STACKIT_SQLAgentManager##"),
|
|
types.StringValue("##STACKIT_SQLAgentUser##"),
|
|
types.StringValue("##STACKIT_ServerManager##"),
|
|
},
|
|
),
|
|
),
|
|
Host: types.StringValue("host"),
|
|
Port: types.Int64Value(1234),
|
|
Region: types.StringValue(testRegion),
|
|
Status: types.StringValue("active"),
|
|
DefaultDatabase: types.StringValue("default_db"),
|
|
},
|
|
true,
|
|
},
|
|
{
|
|
"null_fields_and_int_conversions",
|
|
&v3alpha1api.GetUserResponse{
|
|
Id: int64(1),
|
|
Roles: []string{},
|
|
Username: "",
|
|
Host: "",
|
|
Port: int32(2123456789),
|
|
},
|
|
testRegion,
|
|
dataSourceModel{
|
|
Id: types.StringValue("pid,region,iid,1"),
|
|
UserId: types.Int64Value(1),
|
|
InstanceId: types.StringValue("iid"),
|
|
ProjectId: types.StringValue("pid"),
|
|
Username: types.StringValue(""),
|
|
Roles: types.List(types.SetValueMust(types.StringType, []attr.Value{})),
|
|
Host: types.StringValue(""),
|
|
Port: types.Int64Value(2123456789),
|
|
Region: types.StringValue(testRegion),
|
|
DefaultDatabase: types.StringValue(""),
|
|
Status: types.StringValue(""),
|
|
},
|
|
true,
|
|
},
|
|
{
|
|
"nil_response",
|
|
nil,
|
|
testRegion,
|
|
dataSourceModel{},
|
|
false,
|
|
},
|
|
{
|
|
"nil_response_2",
|
|
&v3alpha1api.GetUserResponse{},
|
|
testRegion,
|
|
dataSourceModel{},
|
|
false,
|
|
},
|
|
{
|
|
"no_resource_id",
|
|
&v3alpha1api.GetUserResponse{},
|
|
testRegion,
|
|
dataSourceModel{},
|
|
false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(
|
|
tt.description, func(t *testing.T) {
|
|
state := &dataSourceModel{
|
|
ProjectId: tt.expected.ProjectId,
|
|
InstanceId: tt.expected.InstanceId,
|
|
UserId: tt.expected.UserId,
|
|
}
|
|
err := mapDataSourceFields(tt.input, state, tt.region)
|
|
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.expected, state)
|
|
if diff != "" {
|
|
t.Fatalf("Data does not match: %s", diff)
|
|
}
|
|
}
|
|
},
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestMapFieldsCreate(t *testing.T) {
|
|
const testRegion = "region"
|
|
tests := []struct {
|
|
description string
|
|
input *v3alpha1api.CreateUserResponse
|
|
region string
|
|
expected resourceModel
|
|
isValid bool
|
|
}{
|
|
{
|
|
"default_values",
|
|
&v3alpha1api.CreateUserResponse{
|
|
Id: int64(1),
|
|
Password: "xy",
|
|
},
|
|
testRegion,
|
|
resourceModel{
|
|
Id: types.Int64Value(1),
|
|
UserId: types.Int64Value(1),
|
|
InstanceId: types.StringValue("iid"),
|
|
ProjectId: types.StringValue("pid"),
|
|
Username: types.StringValue(""),
|
|
Roles: types.List(types.SetNull(types.StringType)),
|
|
Password: types.StringValue("xy"),
|
|
Host: types.StringValue(""),
|
|
Port: types.Int64Value(0),
|
|
Region: types.StringValue(testRegion),
|
|
DefaultDatabase: types.StringValue(""),
|
|
Status: types.StringValue(""),
|
|
Uri: types.StringValue(""),
|
|
},
|
|
true,
|
|
},
|
|
{
|
|
"simple_values",
|
|
&v3alpha1api.CreateUserResponse{
|
|
Id: int64(2),
|
|
Roles: []string{
|
|
"role_2",
|
|
"role_1",
|
|
"",
|
|
},
|
|
Username: "username",
|
|
Password: "password",
|
|
Host: "host",
|
|
Port: int32(1234),
|
|
Status: "status",
|
|
DefaultDatabase: "default_db",
|
|
Uri: "myURI",
|
|
},
|
|
testRegion,
|
|
resourceModel{
|
|
Id: types.Int64Value(2),
|
|
UserId: types.Int64Value(2),
|
|
InstanceId: types.StringValue("iid"),
|
|
ProjectId: types.StringValue("pid"),
|
|
Username: types.StringValue("username"),
|
|
Roles: types.List(
|
|
types.SetValueMust(
|
|
types.StringType, []attr.Value{
|
|
types.StringValue(""),
|
|
types.StringValue("role_1"),
|
|
types.StringValue("role_2"),
|
|
},
|
|
),
|
|
),
|
|
Password: types.StringValue("password"),
|
|
Host: types.StringValue("host"),
|
|
Port: types.Int64Value(1234),
|
|
Region: types.StringValue(testRegion),
|
|
Status: types.StringValue("status"),
|
|
DefaultDatabase: types.StringValue("default_db"),
|
|
Uri: types.StringValue("myURI"),
|
|
},
|
|
true,
|
|
},
|
|
{
|
|
"null_fields_and_int_conversions",
|
|
&v3alpha1api.CreateUserResponse{
|
|
Id: int64(3),
|
|
Roles: []string{},
|
|
Username: "",
|
|
Password: "xy",
|
|
Host: "",
|
|
Port: int32(256789),
|
|
},
|
|
testRegion,
|
|
resourceModel{
|
|
Id: types.Int64Value(3),
|
|
UserId: types.Int64Value(3),
|
|
InstanceId: types.StringValue("iid"),
|
|
ProjectId: types.StringValue("pid"),
|
|
Username: types.StringValue(""),
|
|
Roles: types.ListNull(types.StringType),
|
|
Password: types.StringValue("xy"),
|
|
Host: types.StringValue(""),
|
|
Port: types.Int64Value(256789),
|
|
Region: types.StringValue(testRegion),
|
|
DefaultDatabase: types.StringValue(""),
|
|
Status: types.StringValue(""),
|
|
Uri: types.StringValue(""),
|
|
},
|
|
true,
|
|
},
|
|
{
|
|
"nil_response",
|
|
nil,
|
|
testRegion,
|
|
resourceModel{},
|
|
false,
|
|
},
|
|
{
|
|
"nil_response_2",
|
|
&v3alpha1api.CreateUserResponse{},
|
|
testRegion,
|
|
resourceModel{},
|
|
false,
|
|
},
|
|
{
|
|
"no_resource_id",
|
|
&v3alpha1api.CreateUserResponse{},
|
|
testRegion,
|
|
resourceModel{},
|
|
false,
|
|
},
|
|
{
|
|
"no_password",
|
|
&v3alpha1api.CreateUserResponse{
|
|
Id: int64(1),
|
|
},
|
|
testRegion,
|
|
resourceModel{},
|
|
false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(
|
|
tt.description, func(t *testing.T) {
|
|
state := &resourceModel{
|
|
ProjectId: tt.expected.ProjectId,
|
|
InstanceId: tt.expected.InstanceId,
|
|
}
|
|
err := mapFieldsCreate(tt.input, state, tt.region)
|
|
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.expected, state)
|
|
if diff != "" {
|
|
t.Fatalf("Data does not match: %s", diff)
|
|
}
|
|
}
|
|
},
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestMapFields(t *testing.T) {
|
|
const testRegion = "region"
|
|
tests := []struct {
|
|
description string
|
|
input *v3alpha1api.GetUserResponse
|
|
region string
|
|
expected resourceModel
|
|
isValid bool
|
|
}{
|
|
{
|
|
"default_values",
|
|
&v3alpha1api.GetUserResponse{},
|
|
testRegion,
|
|
resourceModel{
|
|
Id: types.Int64Value(1),
|
|
UserId: types.Int64Value(1),
|
|
InstanceId: types.StringValue("iid"),
|
|
ProjectId: types.StringValue("pid"),
|
|
Username: types.StringValue(""),
|
|
Roles: types.List(types.SetNull(types.StringType)),
|
|
Host: types.StringValue(""),
|
|
Port: types.Int64Value(0),
|
|
Region: types.StringValue(testRegion),
|
|
},
|
|
true,
|
|
},
|
|
{
|
|
"simple_values",
|
|
&v3alpha1api.GetUserResponse{
|
|
Roles: []string{
|
|
"role_2",
|
|
"role_1",
|
|
"",
|
|
},
|
|
Username: ("username"),
|
|
Host: ("host"),
|
|
Port: (int32(1234)),
|
|
},
|
|
testRegion,
|
|
resourceModel{
|
|
Id: types.Int64Value(2),
|
|
UserId: types.Int64Value(2),
|
|
InstanceId: types.StringValue("iid"),
|
|
ProjectId: types.StringValue("pid"),
|
|
Username: types.StringValue("username"),
|
|
Roles: types.List(
|
|
types.SetValueMust(
|
|
types.StringType, []attr.Value{
|
|
types.StringValue(""),
|
|
types.StringValue("role_1"),
|
|
types.StringValue("role_2"),
|
|
},
|
|
),
|
|
),
|
|
Host: types.StringValue("host"),
|
|
Port: types.Int64Value(1234),
|
|
Region: types.StringValue(testRegion),
|
|
},
|
|
true,
|
|
},
|
|
{
|
|
"null_fields_and_int_conversions",
|
|
&v3alpha1api.GetUserResponse{
|
|
Id: int64(1),
|
|
Roles: []string{},
|
|
Username: "",
|
|
Host: "",
|
|
Port: int32(2123456789),
|
|
},
|
|
testRegion,
|
|
resourceModel{
|
|
Id: types.Int64Value(1),
|
|
UserId: types.Int64Value(1),
|
|
InstanceId: types.StringValue("iid"),
|
|
ProjectId: types.StringValue("pid"),
|
|
Username: types.StringValue(""),
|
|
Roles: types.List(types.SetValueMust(types.StringType, []attr.Value{})),
|
|
Host: types.StringValue(""),
|
|
Port: types.Int64Value(2123456789),
|
|
Region: types.StringValue(testRegion),
|
|
},
|
|
true,
|
|
},
|
|
{
|
|
"nil_response",
|
|
nil,
|
|
testRegion,
|
|
resourceModel{},
|
|
false,
|
|
},
|
|
{
|
|
"nil_response_2",
|
|
&v3alpha1api.GetUserResponse{},
|
|
testRegion,
|
|
resourceModel{},
|
|
false,
|
|
},
|
|
{
|
|
"no_resource_id",
|
|
&v3alpha1api.GetUserResponse{},
|
|
testRegion,
|
|
resourceModel{},
|
|
false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(
|
|
tt.description, func(t *testing.T) {
|
|
state := &resourceModel{
|
|
ProjectId: tt.expected.ProjectId,
|
|
InstanceId: tt.expected.InstanceId,
|
|
UserId: tt.expected.UserId,
|
|
}
|
|
err := mapFields(tt.input, state, tt.region)
|
|
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.expected, state)
|
|
if diff != "" {
|
|
t.Fatalf("Data does not match: %s", diff)
|
|
}
|
|
}
|
|
},
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestToCreatePayload(t *testing.T) {
|
|
tests := []struct {
|
|
description string
|
|
input *resourceModel
|
|
inputRoles []string
|
|
expected *v3alpha1api.CreateUserRequestPayload
|
|
isValid bool
|
|
}{
|
|
{
|
|
"default_values",
|
|
&resourceModel{},
|
|
[]string{},
|
|
&v3alpha1api.CreateUserRequestPayload{
|
|
Roles: []string{},
|
|
Username: "",
|
|
},
|
|
true,
|
|
},
|
|
{
|
|
"default_values",
|
|
&resourceModel{
|
|
Username: types.StringValue("username"),
|
|
},
|
|
[]string{
|
|
"role_1",
|
|
"role_2",
|
|
},
|
|
&v3alpha1api.CreateUserRequestPayload{
|
|
Roles: []string{
|
|
"role_1",
|
|
"role_2",
|
|
},
|
|
Username: "username",
|
|
},
|
|
true,
|
|
},
|
|
{
|
|
"null_fields_and_int_conversions",
|
|
&resourceModel{
|
|
Username: types.StringValue(""),
|
|
},
|
|
[]string{
|
|
"",
|
|
},
|
|
&v3alpha1api.CreateUserRequestPayload{
|
|
Roles: []string{
|
|
"",
|
|
},
|
|
Username: "",
|
|
},
|
|
true,
|
|
},
|
|
{
|
|
"nil_model",
|
|
nil,
|
|
[]string{},
|
|
nil,
|
|
false,
|
|
},
|
|
{
|
|
"nil_roles",
|
|
&resourceModel{
|
|
Username: types.StringValue("username"),
|
|
},
|
|
[]string{},
|
|
&v3alpha1api.CreateUserRequestPayload{
|
|
Roles: []string{},
|
|
Username: "username",
|
|
},
|
|
true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(
|
|
tt.description, func(t *testing.T) {
|
|
output, err := toCreatePayload(tt.input, tt.inputRoles)
|
|
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.expected, output)
|
|
if diff != "" {
|
|
t.Fatalf("Data does not match: %s", diff)
|
|
}
|
|
}
|
|
},
|
|
)
|
|
}
|
|
}
|