terraform-provider-stackitp.../stackit/internal/wait/postgresflexalpha/wait_test.go
Marcel_Henselin dd77da71dd
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
feat: more_tests (#85)
## 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
2026-03-17 15:02:08 +00:00

374 lines
10 KiB
Go

// Copyright (c) STACKIT
package postgresflexalpha
import (
"context"
"os"
"testing"
"time"
"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/v3alpha1api"
)
func TestCreateInstanceWaitHandler(t *testing.T) {
tests := []struct {
desc string
instanceGetFails bool
instanceState string
instanceNetwork v3alpha1api.InstanceNetwork
usersGetErrorStatus int
wantErr bool
wantRes *v3alpha1api.GetInstanceResponse
timeout time.Duration
onlyOnLong bool
}{
{
desc: "create_succeeded",
instanceGetFails: false,
instanceState: InstanceStateSuccess,
instanceNetwork: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
wantErr: false,
wantRes: &v3alpha1api.GetInstanceResponse{
Id: "foo-bar",
Status: InstanceStateSuccess,
Network: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
},
},
{
onlyOnLong: true,
desc: "create_failed",
instanceGetFails: false,
instanceState: InstanceStateFailed,
instanceNetwork: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
wantErr: true,
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",
instanceGetFails: false,
instanceState: InstanceStateEmpty,
instanceNetwork: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
wantErr: true,
wantRes: nil,
},
{
desc: "instance_get_fails",
instanceGetFails: true,
wantErr: true,
wantRes: nil,
},
{
desc: "users_get_fails",
instanceGetFails: false,
instanceState: InstanceStateSuccess,
instanceNetwork: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
usersGetErrorStatus: 500,
wantErr: true,
wantRes: nil,
},
{
desc: "users_get_fails_2",
instanceGetFails: false,
instanceState: InstanceStateSuccess,
instanceNetwork: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
usersGetErrorStatus: 400,
wantErr: true,
wantRes: &v3alpha1api.GetInstanceResponse{
Id: "foo-bar",
Status: InstanceStateSuccess,
Network: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
},
},
{
desc: "fail when response has no instance address",
instanceGetFails: false,
instanceState: InstanceStateSuccess,
instanceNetwork: v3alpha1api.InstanceNetwork{
AccessScope: (*v3alpha1api.InstanceNetworkAccessScope)(utils.Ptr("SNA")),
Acl: nil,
InstanceAddress: nil,
RouterAddress: utils.Ptr("10.0.0.1"),
},
wantErr: true,
wantRes: nil,
},
{
desc: "timeout",
instanceGetFails: false,
instanceState: InstanceStateProgressing,
instanceNetwork: v3alpha1api.InstanceNetwork{
AccessScope: (*v3alpha1api.InstanceNetworkAccessScope)(utils.Ptr("SNA")),
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
wantErr: true,
wantRes: nil,
},
}
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"
listUsersMock := func(_ v3alpha1api.ApiListUsersRequestRequest) (*v3alpha1api.ListUserResponse, error) {
if tt.usersGetErrorStatus != 0 {
return nil, &oapierror.GenericOpenAPIError{
StatusCode: tt.usersGetErrorStatus,
}
}
aux := int32(0)
return &v3alpha1api.ListUserResponse{
Pagination: v3alpha1api.Pagination{
TotalRows: aux,
},
Users: []v3alpha1api.ListUser{},
}, nil
}
getInstanceMock := func(_ v3alpha1api.ApiGetInstanceRequestRequest) (*v3alpha1api.GetInstanceResponse, error) {
if tt.instanceGetFails {
return nil, &oapierror.GenericOpenAPIError{
StatusCode: 500,
}
}
return &v3alpha1api.GetInstanceResponse{
Id: instanceID,
Status: v3alpha1api.Status(tt.instanceState),
Network: tt.instanceNetwork,
}, nil
}
apiClientMock := v3alpha1api.DefaultAPIServiceMock{
GetInstanceRequestExecuteMock: &getInstanceMock,
ListUsersRequestExecuteMock: &listUsersMock,
}
handler := CreateInstanceWaitHandler(context.Background(), apiClientMock, "", "", instanceID).
SetTimeout(10 * time.Millisecond)
if tt.timeout != 0 {
handler.SetTimeout(tt.timeout)
}
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)
}
if !cmp.Equal(gotRes, tt.wantRes) {
t.Fatalf("handler gotRes = %v, want %v", gotRes, tt.wantRes)
}
},
)
}
}
func TestUpdateInstanceWaitHandler(t *testing.T) {
tests := []struct {
desc string
instanceGetFails bool
instanceState string
instanceNetwork v3alpha1api.InstanceNetwork
wantErr bool
wantRes *v3alpha1api.GetInstanceResponse
timeout time.Duration
onlyOnLong bool
}{
{
desc: "update_succeeded",
instanceGetFails: false,
instanceState: InstanceStateSuccess,
instanceNetwork: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
wantErr: false,
wantRes: &v3alpha1api.GetInstanceResponse{
Id: "foo-bar",
Status: v3alpha1api.Status(InstanceStateSuccess),
Network: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
},
},
{
onlyOnLong: true,
desc: "update_failed",
instanceGetFails: false,
instanceState: InstanceStateFailed,
instanceNetwork: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
wantErr: true,
wantRes: &v3alpha1api.GetInstanceResponse{
Id: "foo-bar",
Status: v3alpha1api.Status(InstanceStateFailed),
Network: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
},
timeout: 300 * time.Second,
},
{
desc: "update_failed_2",
instanceGetFails: false,
instanceState: InstanceStateEmpty,
instanceNetwork: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
wantErr: true,
wantRes: nil,
},
{
desc: "get_fails",
instanceGetFails: true,
wantErr: true,
wantRes: nil,
},
{
desc: "timeout",
instanceGetFails: false,
instanceState: InstanceStateProgressing,
instanceNetwork: v3alpha1api.InstanceNetwork{
AccessScope: nil,
Acl: nil,
InstanceAddress: utils.Ptr("10.0.0.1"),
RouterAddress: utils.Ptr("10.0.0.1"),
},
wantErr: true,
wantRes: nil,
},
}
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"
listUsersMock := func(_ v3alpha1api.ApiListUsersRequestRequest) (*v3alpha1api.ListUserResponse, error) {
aux := int32(0)
return &v3alpha1api.ListUserResponse{
Pagination: v3alpha1api.Pagination{
TotalRows: aux,
},
Users: []v3alpha1api.ListUser{},
}, nil
}
getInstanceMock := func(_ v3alpha1api.ApiGetInstanceRequestRequest) (*v3alpha1api.GetInstanceResponse, error) {
if tt.instanceGetFails {
return nil, &oapierror.GenericOpenAPIError{
StatusCode: 500,
}
}
return &v3alpha1api.GetInstanceResponse{
Id: instanceID,
Status: v3alpha1api.Status(tt.instanceState),
Network: tt.instanceNetwork,
}, nil
}
apiClientMock := v3alpha1api.DefaultAPIServiceMock{
GetInstanceRequestExecuteMock: &getInstanceMock,
ListUsersRequestExecuteMock: &listUsersMock,
}
handler := PartialUpdateInstanceWaitHandler(context.Background(), apiClientMock, "", "", instanceID).
SetTimeout(10 * time.Millisecond)
if tt.timeout > 0 {
handler.SetTimeout(tt.timeout)
}
gotRes, err := handler.WaitWithContext(context.Background())
if (err != nil) != tt.wantErr {
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
}
if !cmp.Equal(gotRes, tt.wantRes) {
t.Fatalf("handler gotRes = %v, want %v", gotRes, tt.wantRes)
}
},
)
}
}