## 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)
Reviewed-on: #58
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Co-committed-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
261 lines
6.8 KiB
Go
261 lines
6.8 KiB
Go
// Copyright (c) STACKIT
|
|
|
|
package sqlserverflexalpha
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
|
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
|
|
|
sqlserverflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
|
|
)
|
|
|
|
// Used for testing instance operations
|
|
type apiClientInstanceMocked struct {
|
|
instanceId string
|
|
instanceState string
|
|
instanceNetwork sqlserverflex.InstanceNetwork
|
|
instanceIsDeleted bool
|
|
instanceGetFails bool
|
|
}
|
|
|
|
func (a *apiClientInstanceMocked) GetInstanceRequestExecute(_ context.Context, _, _, _ string) (*sqlserverflex.GetInstanceResponse, error) {
|
|
if a.instanceGetFails {
|
|
return nil, &oapierror.GenericOpenAPIError{
|
|
StatusCode: 500,
|
|
}
|
|
}
|
|
|
|
if a.instanceIsDeleted {
|
|
return nil, &oapierror.GenericOpenAPIError{
|
|
StatusCode: 404,
|
|
}
|
|
}
|
|
|
|
return &sqlserverflex.GetInstanceResponse{
|
|
Id: &a.instanceId,
|
|
Status: sqlserverflex.GetInstanceResponseGetStatusAttributeType(&a.instanceState),
|
|
Network: &a.instanceNetwork,
|
|
}, nil
|
|
}
|
|
func TestCreateInstanceWaitHandler(t *testing.T) {
|
|
t.Skip("skipping - needs refactoring")
|
|
tests := []struct {
|
|
desc string
|
|
instanceGetFails bool
|
|
instanceState string
|
|
instanceNetwork sqlserverflex.InstanceNetwork
|
|
usersGetErrorStatus int
|
|
wantErr bool
|
|
wantRes *sqlserverflex.GetInstanceResponse
|
|
}{
|
|
{
|
|
desc: "create_succeeded",
|
|
instanceGetFails: false,
|
|
instanceState: InstanceStateSuccess,
|
|
instanceNetwork: sqlserverflex.InstanceNetwork{
|
|
AccessScope: nil,
|
|
Acl: nil,
|
|
InstanceAddress: utils.Ptr("10.0.0.1"),
|
|
RouterAddress: utils.Ptr("10.0.0.2"),
|
|
},
|
|
wantErr: false,
|
|
wantRes: &sqlserverflex.GetInstanceResponse{
|
|
BackupSchedule: nil,
|
|
Edition: nil,
|
|
Encryption: nil,
|
|
FlavorId: nil,
|
|
Id: nil,
|
|
IsDeletable: nil,
|
|
Name: nil,
|
|
Network: &sqlserverflex.InstanceNetwork{
|
|
AccessScope: nil,
|
|
Acl: nil,
|
|
InstanceAddress: utils.Ptr("10.0.0.1"),
|
|
RouterAddress: utils.Ptr("10.0.0.2"),
|
|
},
|
|
Replicas: nil,
|
|
RetentionDays: nil,
|
|
Status: nil,
|
|
Storage: nil,
|
|
Version: nil,
|
|
},
|
|
},
|
|
{
|
|
desc: "create_failed",
|
|
instanceGetFails: false,
|
|
instanceState: InstanceStateFailed,
|
|
wantErr: true,
|
|
wantRes: nil,
|
|
},
|
|
{
|
|
desc: "create_failed_2",
|
|
instanceGetFails: false,
|
|
instanceState: InstanceStateEmpty,
|
|
wantErr: true,
|
|
wantRes: nil,
|
|
},
|
|
{
|
|
desc: "instance_get_fails",
|
|
instanceGetFails: true,
|
|
wantErr: true,
|
|
wantRes: nil,
|
|
},
|
|
{
|
|
desc: "timeout",
|
|
instanceGetFails: false,
|
|
instanceState: InstanceStateProcessing,
|
|
wantErr: true,
|
|
wantRes: nil,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
instanceId := "foo-bar"
|
|
|
|
apiClient := &apiClientInstanceMocked{
|
|
instanceId: instanceId,
|
|
instanceState: tt.instanceState,
|
|
instanceGetFails: tt.instanceGetFails,
|
|
}
|
|
|
|
handler := CreateInstanceWaitHandler(context.Background(), apiClient, "", instanceId, "")
|
|
|
|
gotRes, err := handler.SetTimeout(10 * time.Millisecond).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) {
|
|
t.Skip("skipping - needs refactoring")
|
|
tests := []struct {
|
|
desc string
|
|
instanceGetFails bool
|
|
instanceState string
|
|
wantErr bool
|
|
wantResp bool
|
|
}{
|
|
{
|
|
desc: "update_succeeded",
|
|
instanceGetFails: false,
|
|
instanceState: InstanceStateSuccess,
|
|
wantErr: false,
|
|
wantResp: true,
|
|
},
|
|
{
|
|
desc: "update_failed",
|
|
instanceGetFails: false,
|
|
instanceState: InstanceStateFailed,
|
|
wantErr: true,
|
|
wantResp: true,
|
|
},
|
|
{
|
|
desc: "update_failed_2",
|
|
instanceGetFails: false,
|
|
instanceState: InstanceStateEmpty,
|
|
wantErr: true,
|
|
wantResp: true,
|
|
},
|
|
{
|
|
desc: "get_fails",
|
|
instanceGetFails: true,
|
|
wantErr: true,
|
|
wantResp: false,
|
|
},
|
|
{
|
|
desc: "timeout",
|
|
instanceGetFails: false,
|
|
instanceState: InstanceStateProcessing,
|
|
wantErr: true,
|
|
wantResp: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
instanceId := "foo-bar"
|
|
|
|
apiClient := &apiClientInstanceMocked{
|
|
instanceId: instanceId,
|
|
instanceState: tt.instanceState,
|
|
instanceGetFails: tt.instanceGetFails,
|
|
}
|
|
|
|
var wantRes *sqlserverflex.GetInstanceResponse
|
|
if tt.wantResp {
|
|
wantRes = &sqlserverflex.GetInstanceResponse{
|
|
Id: &instanceId,
|
|
Status: sqlserverflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr(tt.instanceState)),
|
|
}
|
|
}
|
|
|
|
handler := UpdateInstanceWaitHandler(context.Background(), apiClient, "", instanceId, "")
|
|
|
|
gotRes, err := handler.SetTimeout(10 * time.Millisecond).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, wantRes) {
|
|
t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDeleteInstanceWaitHandler(t *testing.T) {
|
|
tests := []struct {
|
|
desc string
|
|
instanceGetFails bool
|
|
instanceState string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
desc: "delete_succeeded",
|
|
instanceGetFails: false,
|
|
instanceState: InstanceStateSuccess,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
desc: "delete_failed",
|
|
instanceGetFails: false,
|
|
instanceState: InstanceStateFailed,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
desc: "get_fails",
|
|
instanceGetFails: true,
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
instanceId := "foo-bar"
|
|
|
|
apiClient := &apiClientInstanceMocked{
|
|
instanceGetFails: tt.instanceGetFails,
|
|
instanceIsDeleted: tt.instanceState == InstanceStateSuccess,
|
|
instanceId: instanceId,
|
|
instanceState: tt.instanceState,
|
|
}
|
|
|
|
handler := DeleteInstanceWaitHandler(context.Background(), apiClient, "", instanceId, "")
|
|
|
|
_, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|