parent
6555a99a6d
commit
189fa1ece7
16 changed files with 388 additions and 175 deletions
|
|
@ -1,8 +1,11 @@
|
|||
package mongodbflex
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
|
|
@ -10,10 +13,21 @@ import (
|
|||
"github.com/stackitcloud/stackit-sdk-go/services/mongodbflex"
|
||||
)
|
||||
|
||||
const (
|
||||
testRegion = "eu02"
|
||||
)
|
||||
|
||||
var (
|
||||
projectId = uuid.NewString()
|
||||
instanceId = uuid.NewString()
|
||||
userId = uuid.NewString()
|
||||
)
|
||||
|
||||
func TestMapFieldsCreate(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *mongodbflex.CreateUserResponse
|
||||
region string
|
||||
expected Model
|
||||
isValid bool
|
||||
}{
|
||||
|
|
@ -21,15 +35,16 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
"default_values",
|
||||
&mongodbflex.CreateUserResponse{
|
||||
Item: &mongodbflex.User{
|
||||
Id: utils.Ptr("uid"),
|
||||
Id: utils.Ptr(userId),
|
||||
Password: utils.Ptr(""),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)),
|
||||
UserId: types.StringValue(userId),
|
||||
InstanceId: types.StringValue(instanceId),
|
||||
ProjectId: types.StringValue(projectId),
|
||||
Username: types.StringNull(),
|
||||
Database: types.StringNull(),
|
||||
Roles: types.SetNull(types.StringType),
|
||||
|
|
@ -37,6 +52,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Host: types.StringNull(),
|
||||
Port: types.Int64Null(),
|
||||
Uri: types.StringNull(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -44,7 +60,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
"simple_values",
|
||||
&mongodbflex.CreateUserResponse{
|
||||
Item: &mongodbflex.User{
|
||||
Id: utils.Ptr("uid"),
|
||||
Id: utils.Ptr(userId),
|
||||
Roles: &[]string{
|
||||
"role_1",
|
||||
"role_2",
|
||||
|
|
@ -58,11 +74,12 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Uri: utils.Ptr("uri"),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)),
|
||||
UserId: types.StringValue(userId),
|
||||
InstanceId: types.StringValue(instanceId),
|
||||
ProjectId: types.StringValue(projectId),
|
||||
Username: types.StringValue("username"),
|
||||
Database: types.StringValue("database"),
|
||||
Roles: types.SetValueMust(types.StringType, []attr.Value{
|
||||
|
|
@ -74,6 +91,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Host: types.StringValue("host"),
|
||||
Port: types.Int64Value(1234),
|
||||
Uri: types.StringValue("uri"),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -81,7 +99,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
"null_fields_and_int_conversions",
|
||||
&mongodbflex.CreateUserResponse{
|
||||
Item: &mongodbflex.User{
|
||||
Id: utils.Ptr("uid"),
|
||||
Id: utils.Ptr(userId),
|
||||
Roles: &[]string{},
|
||||
Username: nil,
|
||||
Database: nil,
|
||||
|
|
@ -91,11 +109,12 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Uri: nil,
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)),
|
||||
UserId: types.StringValue(userId),
|
||||
InstanceId: types.StringValue(instanceId),
|
||||
ProjectId: types.StringValue(projectId),
|
||||
Username: types.StringNull(),
|
||||
Database: types.StringNull(),
|
||||
Roles: types.SetValueMust(types.StringType, []attr.Value{}),
|
||||
|
|
@ -103,18 +122,21 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Host: types.StringNull(),
|
||||
Port: types.Int64Value(2123456789),
|
||||
Uri: types.StringNull(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_response",
|
||||
nil,
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_response_2",
|
||||
&mongodbflex.CreateUserResponse{},
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
|
|
@ -123,6 +145,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
&mongodbflex.CreateUserResponse{
|
||||
Item: &mongodbflex.User{},
|
||||
},
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
|
|
@ -130,9 +153,10 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
"no_password",
|
||||
&mongodbflex.CreateUserResponse{
|
||||
Item: &mongodbflex.User{
|
||||
Id: utils.Ptr("uid"),
|
||||
Id: utils.Ptr(userId),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
|
|
@ -143,7 +167,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
ProjectId: tt.expected.ProjectId,
|
||||
InstanceId: tt.expected.InstanceId,
|
||||
}
|
||||
err := mapFieldsCreate(tt.input, state)
|
||||
err := mapFieldsCreate(tt.input, state, tt.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
|
|
@ -164,6 +188,7 @@ func TestMapFields(t *testing.T) {
|
|||
tests := []struct {
|
||||
description string
|
||||
input *mongodbflex.GetUserResponse
|
||||
region string
|
||||
expected Model
|
||||
isValid bool
|
||||
}{
|
||||
|
|
@ -172,16 +197,18 @@ func TestMapFields(t *testing.T) {
|
|||
&mongodbflex.GetUserResponse{
|
||||
Item: &mongodbflex.InstanceResponseUser{},
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)),
|
||||
UserId: types.StringValue(userId),
|
||||
InstanceId: types.StringValue(instanceId),
|
||||
ProjectId: types.StringValue(projectId),
|
||||
Username: types.StringNull(),
|
||||
Database: types.StringNull(),
|
||||
Roles: types.SetNull(types.StringType),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Null(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -200,11 +227,12 @@ func TestMapFields(t *testing.T) {
|
|||
Port: utils.Ptr(int64(1234)),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)),
|
||||
UserId: types.StringValue(userId),
|
||||
InstanceId: types.StringValue(instanceId),
|
||||
ProjectId: types.StringValue(projectId),
|
||||
Username: types.StringValue("username"),
|
||||
Database: types.StringValue("database"),
|
||||
Roles: types.SetValueMust(types.StringType, []attr.Value{
|
||||
|
|
@ -212,8 +240,9 @@ func TestMapFields(t *testing.T) {
|
|||
types.StringValue("role_2"),
|
||||
types.StringValue(""),
|
||||
}),
|
||||
Host: types.StringValue("host"),
|
||||
Port: types.Int64Value(1234),
|
||||
Host: types.StringValue("host"),
|
||||
Port: types.Int64Value(1234),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -221,7 +250,7 @@ func TestMapFields(t *testing.T) {
|
|||
"null_fields_and_int_conversions",
|
||||
&mongodbflex.GetUserResponse{
|
||||
Item: &mongodbflex.InstanceResponseUser{
|
||||
Id: utils.Ptr("uid"),
|
||||
Id: utils.Ptr(userId),
|
||||
Roles: &[]string{},
|
||||
Username: nil,
|
||||
Database: nil,
|
||||
|
|
@ -229,28 +258,32 @@ func TestMapFields(t *testing.T) {
|
|||
Port: utils.Ptr(int64(2123456789)),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)),
|
||||
UserId: types.StringValue(userId),
|
||||
InstanceId: types.StringValue(instanceId),
|
||||
ProjectId: types.StringValue(projectId),
|
||||
Username: types.StringNull(),
|
||||
Database: types.StringNull(),
|
||||
Roles: types.SetValueMust(types.StringType, []attr.Value{}),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Value(2123456789),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_response",
|
||||
nil,
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_response_2",
|
||||
&mongodbflex.GetUserResponse{},
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
|
|
@ -259,6 +292,7 @@ func TestMapFields(t *testing.T) {
|
|||
&mongodbflex.GetUserResponse{
|
||||
Item: &mongodbflex.InstanceResponseUser{},
|
||||
},
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
|
|
@ -270,7 +304,7 @@ func TestMapFields(t *testing.T) {
|
|||
InstanceId: tt.expected.InstanceId,
|
||||
UserId: tt.expected.UserId,
|
||||
}
|
||||
err := mapFields(tt.input, state)
|
||||
err := mapFields(tt.input, state, tt.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue