chore: fixing tests

This commit is contained in:
Marcel_Henselin 2025-12-19 09:49:19 +01:00
parent a861661036
commit feef0b61d6
14 changed files with 1452 additions and 561 deletions

View file

@ -1,5 +1,3 @@
// Copyright (c) STACKIT
/*
PostgreSQL Flex API

View file

@ -1,5 +1,3 @@
// Copyright (c) STACKIT
package wait
import (
@ -29,7 +27,7 @@ type APIClientInstanceInterface interface {
// Interface needed for tests
type APIClientUserInterface interface {
GetUserExecute(ctx context.Context, projectId, region, instanceId, userId string) (*postgresflex.GetUserResponse, error)
GetUserRequestExecute(ctx context.Context, projectId, region, instanceId string, userId int64) (*postgresflex.GetUserResponse, error)
}
// CreateInstanceWaitHandler will wait for instance creation
@ -152,9 +150,9 @@ func ForceDeleteInstanceWaitHandler(ctx context.Context, a APIClientInstanceInte
}
// DeleteUserWaitHandler will wait for delete
func DeleteUserWaitHandler(ctx context.Context, a APIClientUserInterface, projectId, region, instanceId, userId string) *wait.AsyncActionHandler[struct{}] {
func DeleteUserWaitHandler(ctx context.Context, a APIClientUserInterface, projectId, region, instanceId string, userId int64) *wait.AsyncActionHandler[struct{}] {
handler := wait.New(func() (waitFinished bool, response *struct{}, err error) {
_, err = a.GetUserExecute(ctx, projectId, region, instanceId, userId)
_, err = a.GetUserRequestExecute(ctx, projectId, region, instanceId, userId)
if err == nil {
return false, nil, nil
}

View file

@ -1,5 +1,3 @@
// Copyright (c) STACKIT
package wait
import (
@ -10,7 +8,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex"
postgresflex "github.com/stackitcloud/terraform-provider-stackit/pkg/postgresflexalpha"
)
// Used for testing instance operations
@ -22,7 +20,7 @@ type apiClientInstanceMocked struct {
usersGetErrorStatus int
}
func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _, _ string) (*postgresflex.InstanceResponse, error) {
func (a *apiClientInstanceMocked) GetInstanceRequestExecute(_ context.Context, _, _, _ string) (*postgresflex.GetInstanceResponse, error) {
if a.instanceGetFails {
return nil, &oapierror.GenericOpenAPIError{
StatusCode: 500,
@ -35,15 +33,13 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _, _
}
}
return &postgresflex.InstanceResponse{
Item: &postgresflex.Instance{
Id: &a.instanceId,
Status: &a.instanceState,
},
return &postgresflex.GetInstanceResponse{
Id: &a.instanceId,
Status: postgresflex.GetInstanceResponseGetStatusAttributeType(&a.instanceState),
}, nil
}
func (a *apiClientInstanceMocked) ListUsersExecute(_ context.Context, _, _, _ string) (*postgresflex.ListUsersResponse, error) {
func (a *apiClientInstanceMocked) ListUsersRequestExecute(_ context.Context, _, _, _ string) (*postgresflex.ListUserResponse, error) {
if a.usersGetErrorStatus != 0 {
return nil, &oapierror.GenericOpenAPIError{
StatusCode: a.usersGetErrorStatus,
@ -51,20 +47,22 @@ func (a *apiClientInstanceMocked) ListUsersExecute(_ context.Context, _, _, _ st
}
aux := int64(0)
return &postgresflex.ListUsersResponse{
Count: &aux,
Items: &[]postgresflex.ListUsersResponseItem{},
return &postgresflex.ListUserResponse{
Pagination: &postgresflex.Pagination{
TotalRows: &aux,
},
Users: &[]postgresflex.ListUser{},
}, nil
}
// Used for testing user operations
type apiClientUserMocked struct {
getFails bool
userId string
userId int64
isUserDeleted bool
}
func (a *apiClientUserMocked) GetUserExecute(_ context.Context, _, _, _, _ string) (*postgresflex.GetUserResponse, error) {
func (a *apiClientUserMocked) GetUserRequestExecute(_ context.Context, _, _, _ string, _ int64) (*postgresflex.GetUserResponse, error) {
if a.getFails {
return nil, &oapierror.GenericOpenAPIError{
StatusCode: 500,
@ -78,9 +76,7 @@ func (a *apiClientUserMocked) GetUserExecute(_ context.Context, _, _, _, _ strin
}
return &postgresflex.GetUserResponse{
Item: &postgresflex.UserResponse{
Id: &a.userId,
},
Id: &a.userId,
}, nil
}
@ -155,13 +151,11 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
usersGetErrorStatus: tt.usersGetErrorStatus,
}
var wantRes *postgresflex.InstanceResponse
var wantRes *postgresflex.GetInstanceResponse
if tt.wantResp {
wantRes = &postgresflex.InstanceResponse{
Item: &postgresflex.Instance{
Id: &instanceId,
Status: utils.Ptr(tt.instanceState),
},
wantRes = &postgresflex.GetInstanceResponse{
Id: &instanceId,
Status: postgresflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr(tt.instanceState)),
}
}
@ -232,13 +226,11 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
instanceGetFails: tt.instanceGetFails,
}
var wantRes *postgresflex.InstanceResponse
var wantRes *postgresflex.GetInstanceResponse
if tt.wantResp {
wantRes = &postgresflex.InstanceResponse{
Item: &postgresflex.Instance{
Id: &instanceId,
Status: utils.Ptr(tt.instanceState),
},
wantRes = &postgresflex.GetInstanceResponse{
Id: &instanceId,
Status: postgresflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr(tt.instanceState)),
}
}
@ -377,7 +369,7 @@ func TestDeleteUserWaitHandler(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
userId := "foo-bar"
userId := int64(1001)
apiClient := &apiClientUserMocked{
getFails: tt.getFails,