|
| 1 | +package wait |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + |
| 10 | + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" |
| 11 | + "github.com/stackitcloud/stackit-sdk-go/services/scf" |
| 12 | +) |
| 13 | + |
| 14 | +var PROJECT_ID = uuid.New().String() |
| 15 | +var INSTANCE_ID = uuid.New().String() |
| 16 | + |
| 17 | +const REGION = "eu01" |
| 18 | + |
| 19 | +type apiClientMocked struct { |
| 20 | + getFails bool |
| 21 | + errorCode int |
| 22 | + returnInstance bool |
| 23 | + projectId string |
| 24 | + instanceId string |
| 25 | + getSCFResponse *scf.Organization |
| 26 | +} |
| 27 | + |
| 28 | +func (a *apiClientMocked) GetOrganizationExecute(_ context.Context, _, _, _ string) (*scf.Organization, error) { |
| 29 | + if a.getFails { |
| 30 | + return nil, &oapierror.GenericOpenAPIError{ |
| 31 | + StatusCode: a.errorCode, |
| 32 | + } |
| 33 | + } |
| 34 | + if !a.returnInstance { |
| 35 | + return nil, nil |
| 36 | + } |
| 37 | + return a.getSCFResponse, nil |
| 38 | +} |
| 39 | + |
| 40 | +func TestDeleteOrganizationWaitHandler(t *testing.T) { |
| 41 | + statusDeletingFailed := "deleting_failed" |
| 42 | + tests := []struct { |
| 43 | + desc string |
| 44 | + wantErr bool |
| 45 | + wantReturnedInstance bool |
| 46 | + getFails bool |
| 47 | + errorCode int |
| 48 | + returnInstance bool |
| 49 | + getOrgResponse *scf.Organization |
| 50 | + }{ |
| 51 | + { |
| 52 | + desc: "Instance deletion failed with error", |
| 53 | + wantErr: true, |
| 54 | + getFails: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + desc: "Instance is not found", |
| 58 | + wantErr: false, |
| 59 | + getFails: true, |
| 60 | + errorCode: 404, |
| 61 | + }, |
| 62 | + { |
| 63 | + desc: "Instance is in error state", |
| 64 | + wantErr: true, |
| 65 | + returnInstance: true, |
| 66 | + getOrgResponse: &scf.Organization{ |
| 67 | + Status: &statusDeletingFailed, |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + desc: "Instance is nil", |
| 72 | + wantErr: true, |
| 73 | + returnInstance: true, |
| 74 | + getOrgResponse: nil, |
| 75 | + }, |
| 76 | + } |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.desc, func(t *testing.T) { |
| 79 | + apiClient := &apiClientMocked{ |
| 80 | + projectId: PROJECT_ID, |
| 81 | + instanceId: INSTANCE_ID, |
| 82 | + getFails: tt.getFails, |
| 83 | + errorCode: tt.errorCode, |
| 84 | + returnInstance: tt.returnInstance, |
| 85 | + getSCFResponse: tt.getOrgResponse, |
| 86 | + } |
| 87 | + |
| 88 | + handler := DeleteOrganizationWaitHandler(context.Background(), apiClient, apiClient.projectId, REGION, apiClient.instanceId) |
| 89 | + response, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) |
| 90 | + |
| 91 | + if (err != nil) != tt.wantErr { |
| 92 | + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) |
| 93 | + } |
| 94 | + if (response != nil) != tt.wantReturnedInstance { |
| 95 | + t.Fatalf("handler gotRes = %v, want nil", response) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments