feat: more_tests (#85)
Some checks failed
Publish / Check GoReleaser config (push) Successful in 7s
Publish / Publish provider (push) Successful in 7m41s
CI Workflow / Check GoReleaser config (pull_request) Successful in 6s
CI Workflow / Prepare GO cache (pull_request) Successful in 10m18s
CI Workflow / Test readiness for publishing provider (pull_request) Has been cancelled
CI Workflow / Code coverage report (pull_request) Has been cancelled
CI Workflow / CI run build and linting (pull_request) Has been cancelled
CI Workflow / CI run tests (pull_request) Has been cancelled
TF Acceptance Tests Workflow / Acceptance Tests (pull_request) Failing after 26m13s

## 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>
Reviewed-on: #85
This commit is contained in:
Marcel_Henselin 2026-03-17 15:02:08 +00:00
parent 3790894563
commit dd77da71dd
Signed by: tf-provider.git.onstackit.cloud
GPG key ID: 6D7E8A1ED8955A9C
37 changed files with 2473 additions and 1742 deletions

View file

@ -2,9 +2,11 @@ package postgresflexalpha
import (
"context"
"crypto/rand"
"errors"
"fmt"
"math"
"math/big"
"net/http"
"time"
@ -29,45 +31,47 @@ const (
// APIClientInstanceInterface Interface needed for tests
type APIClientInstanceInterface interface {
GetInstanceRequest(ctx context.Context, projectId, region, instanceId string) v3alpha1api.ApiGetInstanceRequestRequest
GetInstanceRequest(ctx context.Context, projectID, region, instanceID string) v3alpha1api.ApiGetInstanceRequestRequest
ListUsersRequest(
ctx context.Context,
projectId string,
projectID string,
region string,
instanceId string,
instanceID string,
) v3alpha1api.ApiListUsersRequestRequest
}
// APIClientUserInterface Interface needed for tests
type APIClientUserInterface interface {
GetUserRequest(ctx context.Context, projectId, region, instanceId string, userId int32) v3alpha1api.ApiGetUserRequestRequest
GetUserRequest(ctx context.Context, projectID, region, instanceID string, userID int32) v3alpha1api.ApiGetUserRequestRequest
}
// APIClientDatabaseInterface Interface needed for tests
type APIClientDatabaseInterface interface {
GetDatabaseRequest(ctx context.Context, projectId string, region string, instanceId string, databaseId int32) v3alpha1api.ApiGetDatabaseRequestRequest
GetDatabaseRequest(ctx context.Context, projectID string, region string, instanceID string, databaseID int32) v3alpha1api.ApiGetDatabaseRequestRequest
}
// CreateInstanceWaitHandler will wait for instance creation
func CreateInstanceWaitHandler(
ctx context.Context, a APIClientInstanceInterface, projectId, region,
instanceId string,
ctx context.Context, a APIClientInstanceInterface, projectID, region,
instanceID string,
) *wait.AsyncActionHandler[v3alpha1api.GetInstanceResponse] {
instanceCreated := false
var instanceGetResponse *v3alpha1api.GetInstanceResponse
maxWait := time.Minute * 45
startTime := time.Now()
extendedTimeout := 0
maxFailedCount := 3
failedCount := 0
handler := wait.New(
func() (waitFinished bool, response *v3alpha1api.GetInstanceResponse, err error) {
if !instanceCreated {
s, err := a.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
if err != nil {
return false, nil, err
s, getErr := a.GetInstanceRequest(ctx, projectID, region, instanceID).Execute()
if getErr != nil {
return false, nil, getErr
}
if s == nil || s.Id != instanceId {
if s == nil || s.Id != instanceID {
return false, nil, nil
}
tflog.Debug(
@ -77,7 +81,7 @@ func CreateInstanceWaitHandler(
)
switch s.Status {
default:
return true, s, fmt.Errorf("instance with id %s has unexpected status %s", instanceId, s.Status)
return true, s, fmt.Errorf("instance with id %s has unexpected status %s", instanceID, s.Status)
case InstanceStateEmpty:
return false, nil, nil
case InstanceStatePending:
@ -94,30 +98,15 @@ func CreateInstanceWaitHandler(
"Wait handler still got status %s after %v for instance: %s",
InstanceStateProgressing,
maxWait,
instanceId,
instanceID,
),
)
if extendedTimeout < 3 {
maxWait += time.Minute * 5
extendedTimeout++
if *s.Network.AccessScope == "SNA" {
ready := true
if s.Network.InstanceAddress == nil {
tflog.Warn(ctx, "Waiting for instance_address")
ready = false
}
if s.Network.RouterAddress == nil {
tflog.Warn(ctx, "Waiting for router_address")
ready = false
}
if !ready {
return false, nil, nil
}
}
return false, nil, nil
}
instanceCreated = true
instanceGetResponse = s
return false, nil, fmt.Errorf("instance after max timeout still in state %s", InstanceStateProgressing)
case InstanceStateSuccess:
if s.Network.AccessScope != nil && *s.Network.AccessScope == "SNA" {
if s.Network.InstanceAddress == nil {
@ -132,8 +121,27 @@ func CreateInstanceWaitHandler(
instanceCreated = true
instanceGetResponse = s
case InstanceStateFailed:
tflog.Warn(ctx, fmt.Sprintf("Wait handler got status FAILURE for instance: %s", instanceId))
return false, nil, nil
if failedCount < maxFailedCount {
failedCount++
tflog.Warn(
ctx, "got failed status from API retry", map[string]interface{}{
"failedCount": failedCount,
},
)
var waitCounter int64 = 1
maxWaitInt := big.NewInt(7)
n, randErr := rand.Int(rand.Reader, maxWaitInt)
if randErr == nil {
waitCounter = n.Int64() + 1
}
time.Sleep(time.Duration(waitCounter*30) * time.Second) //nolint:gosec // not that important and temporary
return false, nil, nil
}
return true, s, fmt.Errorf(
"update got status FAILURE for instance with id %s after %d retries",
instanceID,
failedCount,
)
// API responds with FAILURE for some seconds and then the instance goes to READY
// return true, s, fmt.Errorf("create failed for instance with id %s", instanceId)
}
@ -142,7 +150,7 @@ func CreateInstanceWaitHandler(
tflog.Info(ctx, "Waiting for instance (calling list users")
// // User operations aren't available right after an instance is deemed successful
// // To check if they are, perform a users request
_, err = a.ListUsersRequest(ctx, projectId, region, instanceId).Execute()
_, err = a.ListUsersRequest(ctx, projectID, region, instanceID).Execute()
if err == nil {
return true, instanceGetResponse, nil
}
@ -150,7 +158,7 @@ func CreateInstanceWaitHandler(
if !ok {
return false, nil, err
}
// TODO: refactor and cooperate with api guys to mitigate
// TODO: refactor and cooperate with api guys to mitigate // nolint: // reason upfront
if oapiErr.StatusCode < 500 {
return true, instanceGetResponse, fmt.Errorf(
"users request after instance creation returned %d status code",
@ -160,8 +168,6 @@ func CreateInstanceWaitHandler(
return false, nil, nil
},
)
// Sleep before wait is set because sometimes API returns 404 right after creation request
handler.SetTimeout(90 * time.Minute).SetSleepBeforeWait(30 * time.Second)
return handler
}
@ -170,6 +176,8 @@ func PartialUpdateInstanceWaitHandler(
ctx context.Context, a APIClientInstanceInterface, projectID, region,
instanceID string,
) *wait.AsyncActionHandler[v3alpha1api.GetInstanceResponse] {
maxFailedCount := 3
failedCount := 0
handler := wait.New(
func() (waitFinished bool, response *v3alpha1api.GetInstanceResponse, err error) {
s, err := a.GetInstanceRequest(ctx, projectID, region, instanceID).Execute()
@ -195,11 +203,30 @@ func PartialUpdateInstanceWaitHandler(
case InstanceStateUnknown:
return false, nil, nil
case InstanceStateFailed:
return true, s, fmt.Errorf("update got status FAILURE for instance with id %s", instanceID)
if failedCount < maxFailedCount {
failedCount++
tflog.Warn(
ctx, "got failed status from API retry", map[string]interface{}{
"failedCount": failedCount,
},
)
var waitCounter int64 = 1
maxWait := big.NewInt(7)
n, err := rand.Int(rand.Reader, maxWait)
if err == nil {
waitCounter = n.Int64() + 1
}
time.Sleep(time.Duration(waitCounter*30) * time.Second) //nolint:gosec // not that important and temporary
return false, nil, nil
}
return true, s, fmt.Errorf(
"update got status FAILURE for instance with id %s after %d retries",
instanceID,
failedCount,
)
}
},
)
handler.SetTimeout(45 * time.Minute).SetSleepBeforeWait(30 * time.Second)
return handler
}
@ -295,6 +322,8 @@ func DeleteInstanceWaitHandler(
instanceID string,
timeout, sleepBeforeWait time.Duration,
) error {
maxFailedCount := 3
failedCount := 0
handler := wait.New(
func() (waitFinished bool, response *v3alpha1api.GetInstanceResponse, err error) {
s, err := a.GetInstanceRequest(ctx, projectID, region, instanceID).Execute()
@ -314,6 +343,22 @@ func DeleteInstanceWaitHandler(
case InstanceStateEmpty, InstanceStatePending, InstanceStateUnknown, InstanceStateProgressing, InstanceStateSuccess:
return false, nil, nil
case InstanceStateFailed:
if failedCount < maxFailedCount {
failedCount++
tflog.Warn(
ctx, "got failed status from API retry", map[string]interface{}{
"failedCount": failedCount,
},
)
var waitCounter int64 = 1
maxWait := big.NewInt(7)
n, err := rand.Int(rand.Reader, maxWait)
if err == nil {
waitCounter = n.Int64() + 1
}
time.Sleep(time.Duration(waitCounter*30) * time.Second) //nolint:gosec // not that important and temporary
return false, nil, nil
}
return true, nil, fmt.Errorf("wait handler got status FAILURE for instance: %s", instanceID)
default:
return true, s, fmt.Errorf("instance with id %s has unexpected status %s", instanceID, s.Status)

View file

@ -4,6 +4,7 @@ package postgresflexalpha
import (
"context"
"os"
"testing"
"time"
@ -22,6 +23,8 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
usersGetErrorStatus int
wantErr bool
wantRes *v3alpha1api.GetInstanceResponse
timeout time.Duration
onlyOnLong bool
}{
{
desc: "create_succeeded",
@ -46,6 +49,7 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
},
},
{
onlyOnLong: true,
desc: "create_failed",
instanceGetFails: false,
instanceState: InstanceStateFailed,
@ -56,7 +60,18 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
RouterAddress: utils.Ptr("10.0.0.1"),
},
wantErr: true,
wantRes: nil,
wantRes: &v3alpha1api.GetInstanceResponse{
Id: "foo-bar",
Status: InstanceStateFailed,
Network: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
},
// waiter uses random timeouts up to 8 times 30 secs
timeout: 300 * time.Second,
},
{
desc: "create_failed_2",
@ -142,6 +157,13 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
},
}
for _, tt := range tests {
if tt.onlyOnLong {
_, ok := os.LookupEnv("TF_RUN_LONG_TESTS")
if !ok {
t.Logf("skipping test '%s' because TF_RUN_LONG_TESTS env var is missing", tt.desc)
continue
}
}
t.Run(
tt.desc, func(t *testing.T) {
instanceID := "foo-bar"
@ -181,9 +203,13 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
ListUsersRequestExecuteMock: &listUsersMock,
}
handler := CreateInstanceWaitHandler(context.Background(), apiClientMock, "", "", instanceID)
handler := CreateInstanceWaitHandler(context.Background(), apiClientMock, "", "", instanceID).
SetTimeout(10 * time.Millisecond)
if tt.timeout != 0 {
handler.SetTimeout(tt.timeout)
}
gotRes, err := handler.SetTimeout(10 * time.Millisecond).SetSleepBeforeWait(1 * time.Millisecond).WaitWithContext(context.Background())
gotRes, err := handler.SetSleepBeforeWait(1 * time.Millisecond).WaitWithContext(context.Background())
if (err != nil) != tt.wantErr {
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
}
@ -204,6 +230,8 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
instanceNetwork v3alpha1api.InstanceNetwork
wantErr bool
wantRes *v3alpha1api.GetInstanceResponse
timeout time.Duration
onlyOnLong bool
}{
{
desc: "update_succeeded",
@ -228,6 +256,7 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
},
},
{
onlyOnLong: true,
desc: "update_failed",
instanceGetFails: false,
instanceState: InstanceStateFailed,
@ -248,6 +277,7 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
RouterAddress: utils.Ptr("10.0.0.1"),
},
},
timeout: 300 * time.Second,
},
{
desc: "update_failed_2",
@ -283,6 +313,14 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
},
}
for _, tt := range tests {
if tt.onlyOnLong {
_, ok := os.LookupEnv("TF_RUN_LONG_TESTS")
if !ok {
t.Logf("skipping test '%s' because TF_RUN_LONG_TESTS env var is missing", tt.desc)
continue
}
}
t.Run(
tt.desc, func(t *testing.T) {
instanceID := "foo-bar"
@ -316,9 +354,13 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
ListUsersRequestExecuteMock: &listUsersMock,
}
handler := PartialUpdateInstanceWaitHandler(context.Background(), apiClientMock, "", "", instanceID)
handler := PartialUpdateInstanceWaitHandler(context.Background(), apiClientMock, "", "", instanceID).
SetTimeout(10 * time.Millisecond)
if tt.timeout > 0 {
handler.SetTimeout(tt.timeout)
}
gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
gotRes, err := handler.WaitWithContext(context.Background())
if (err != nil) != tt.wantErr {
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
}

View file

@ -30,35 +30,35 @@ const (
type APIClientInterface interface {
GetInstanceRequest(
ctx context.Context,
projectId, region, instanceId string,
projectID, region, instanceID string,
) sqlserverflex.ApiGetInstanceRequestRequest
GetDatabaseRequest(
ctx context.Context,
projectId string,
projectID string,
region string,
instanceId string,
instanceID string,
databaseName string,
) sqlserverflex.ApiGetDatabaseRequestRequest
GetUserRequest(
ctx context.Context,
projectId string,
projectID string,
region string,
instanceId string,
userId int64,
instanceID string,
userID int64,
) sqlserverflex.ApiGetUserRequestRequest
ListRolesRequest(
ctx context.Context,
projectId string,
projectID string,
region string,
instanceId string,
instanceID string,
) sqlserverflex.ApiListRolesRequestRequest
ListUsersRequest(
ctx context.Context,
projectId string,
projectID string,
region string,
instanceId string,
instanceID string,
) sqlserverflex.ApiListUsersRequestRequest
}
@ -66,10 +66,10 @@ type APIClientInterface interface {
type APIClientUserInterface interface {
DeleteUserRequestExecute(
ctx context.Context,
projectId string,
projectID string,
region string,
instanceId string,
userId int64,
instanceID string,
userID int64,
) error
}
@ -77,11 +77,11 @@ type APIClientUserInterface interface {
func CreateInstanceWaitHandler(
ctx context.Context,
a APIClientInterface,
projectId, instanceId, region string,
projectID, instanceID, region string,
) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] {
handler := wait.New(
func() (waitFinished bool, response *sqlserverflex.GetInstanceResponse, err error) {
s, err := a.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
s, err := a.GetInstanceRequest(ctx, projectID, region, instanceID).Execute()
if err != nil {
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(err, &oapiErr)
@ -95,7 +95,7 @@ func CreateInstanceWaitHandler(
return false, nil, fmt.Errorf("api error: %w", err)
}
}
if s == nil || s.Id != instanceId {
if s == nil || s.Id != instanceID {
return false, nil, nil
}
switch strings.ToLower(string(s.Status)) {
@ -113,7 +113,7 @@ func CreateInstanceWaitHandler(
tflog.Info(ctx, "trying to get roles")
time.Sleep(10 * time.Second)
_, rolesErr := a.ListRolesRequest(ctx, projectId, region, instanceId).Execute()
_, rolesErr := a.ListRolesRequest(ctx, projectID, region, instanceID).Execute()
if rolesErr != nil {
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(rolesErr, &oapiErr)
@ -137,7 +137,7 @@ func CreateInstanceWaitHandler(
tflog.Info(ctx, "trying to get users")
time.Sleep(10 * time.Second)
_, usersErr := a.ListUsersRequest(ctx, projectId, region, instanceId).Execute()
_, usersErr := a.ListUsersRequest(ctx, projectID, region, instanceID).Execute()
if usersErr != nil {
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(usersErr, &oapiErr)
@ -162,13 +162,13 @@ func CreateInstanceWaitHandler(
case strings.ToLower(InstanceStateUnknown):
return true, nil, fmt.Errorf(
"create failed for instance %s with status %s",
instanceId,
instanceID,
InstanceStateUnknown,
)
case strings.ToLower(InstanceStateFailed):
return true, nil, fmt.Errorf(
"create failed for instance %s with status %s",
instanceId,
instanceID,
InstanceStateFailed,
)
case strings.ToLower(InstanceStatePending), strings.ToLower(InstanceStateProcessing):
@ -182,7 +182,7 @@ func CreateInstanceWaitHandler(
default:
tflog.Info(
ctx, "Wait (create) received unknown status", map[string]interface{}{
"instanceId": instanceId,
"instanceId": instanceID,
"status": s.Status,
},
)
@ -197,22 +197,22 @@ func CreateInstanceWaitHandler(
func UpdateInstanceWaitHandler(
ctx context.Context,
a APIClientInterface,
projectId, instanceId, region string,
projectID, instanceID, region string,
) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] {
handler := wait.New(
func() (waitFinished bool, response *sqlserverflex.GetInstanceResponse, err error) {
s, err := a.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
s, err := a.GetInstanceRequest(ctx, projectID, region, instanceID).Execute()
if err != nil {
return false, nil, err
}
if s == nil || s.Id != instanceId {
if s == nil || s.Id != instanceID {
return false, nil, nil
}
switch strings.ToLower(string(s.Status)) {
case strings.ToLower(InstanceStateSuccess):
return true, s, nil
case strings.ToLower(InstanceStateUnknown), strings.ToLower(InstanceStateFailed):
return true, s, fmt.Errorf("update failed for instance with id %s", instanceId)
return true, s, fmt.Errorf("update failed for instance with id %s", instanceID)
case strings.ToLower(InstanceStatePending), strings.ToLower(InstanceStateProcessing):
tflog.Info(
ctx, "request is being handled", map[string]interface{}{
@ -223,7 +223,7 @@ func UpdateInstanceWaitHandler(
default:
tflog.Info(
ctx, "Wait (update) received unknown status", map[string]interface{}{
"instanceId": instanceId,
"instanceId": instanceID,
"status": s.Status,
},
)
@ -238,11 +238,11 @@ func UpdateInstanceWaitHandler(
func DeleteInstanceWaitHandler(
ctx context.Context,
a APIClientInterface,
projectId, instanceId, region string,
projectID, instanceID, region string,
) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] {
handler := wait.New(
func() (waitFinished bool, response *sqlserverflex.GetInstanceResponse, err error) {
s, err := a.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
s, err := a.GetInstanceRequest(ctx, projectID, region, instanceID).Execute()
if err == nil {
return false, s, nil
}
@ -265,11 +265,11 @@ func DeleteInstanceWaitHandler(
func CreateDatabaseWaitHandler(
ctx context.Context,
a APIClientInterface,
projectId, instanceId, region, databaseName string,
projectID, instanceID, region, databaseName string,
) *wait.AsyncActionHandler[sqlserverflex.GetDatabaseResponse] {
handler := wait.New(
func() (waitFinished bool, response *sqlserverflex.GetDatabaseResponse, err error) {
s, err := a.GetDatabaseRequest(ctx, projectId, region, instanceId, databaseName).Execute()
s, err := a.GetDatabaseRequest(ctx, projectID, region, instanceID, databaseName).Execute()
if err != nil {
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(err, &oapiErr)
@ -297,12 +297,12 @@ func CreateDatabaseWaitHandler(
func CreateUserWaitHandler(
ctx context.Context,
a APIClientInterface,
projectId, instanceId, region string,
userId int64,
projectID, instanceID, region string,
userID int64,
) *wait.AsyncActionHandler[sqlserverflex.GetUserResponse] {
handler := wait.New(
func() (waitFinished bool, response *sqlserverflex.GetUserResponse, err error) {
s, err := a.GetUserRequest(ctx, projectId, region, instanceId, userId).Execute()
s, err := a.GetUserRequest(ctx, projectID, region, instanceID, userID).Execute()
if err != nil {
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(err, &oapiErr)
@ -324,7 +324,7 @@ func CreateUserWaitHandler(
func WaitForUserWaitHandler(
ctx context.Context,
a APIClientInterface,
projectId, instanceId, region, userName string,
projectID, instanceID, region, userName string,
) *wait.AsyncActionHandler[sqlserverflex.ListUserResponse] {
startTime := time.Now()
timeOut := 2 * time.Minute
@ -334,7 +334,7 @@ func WaitForUserWaitHandler(
if time.Since(startTime) > timeOut {
return false, nil, errors.New("ran into timeout")
}
s, err := a.ListUsersRequest(ctx, projectId, region, instanceId).Size(100).Execute()
s, err := a.ListUsersRequest(ctx, projectID, region, instanceID).Size(100).Execute()
if err != nil {
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(err, &oapiErr)
@ -376,12 +376,12 @@ func WaitForUserWaitHandler(
func DeleteUserWaitHandler(
ctx context.Context,
a APIClientInterface,
projectId, region, instanceId string,
userId int64,
projectID, region, instanceID string,
userID int64,
) *wait.AsyncActionHandler[struct{}] {
handler := wait.New(
func() (waitFinished bool, response *struct{}, err error) {
_, err = a.GetUserRequest(ctx, projectId, region, instanceId, userId).Execute()
_, err = a.GetUserRequest(ctx, projectID, region, instanceID, userID).Execute()
if err == nil {
return false, nil, nil
}