|
| 1 | +package wait |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/google/go-cmp/cmp" |
| 10 | + "github.com/google/uuid" |
| 11 | + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" |
| 12 | + "github.com/stackitcloud/stackit-sdk-go/core/utils" |
| 13 | + "github.com/stackitcloud/stackit-sdk-go/services/git" |
| 14 | +) |
| 15 | + |
| 16 | +type apiClientMocked struct { |
| 17 | + getFails bool |
| 18 | + errorCode int |
| 19 | + returnInstance bool |
| 20 | + projectId string |
| 21 | + instanceId string |
| 22 | + getGitResponse *git.Instance |
| 23 | +} |
| 24 | + |
| 25 | +func (a *apiClientMocked) GetInstanceExecute(_ context.Context, _, _ string) (*git.Instance, error) { |
| 26 | + if a.getFails { |
| 27 | + return nil, &oapierror.GenericOpenAPIError{ |
| 28 | + StatusCode: a.errorCode, |
| 29 | + } |
| 30 | + } |
| 31 | + if !a.returnInstance { |
| 32 | + return nil, nil |
| 33 | + } |
| 34 | + return a.getGitResponse, nil |
| 35 | +} |
| 36 | + |
| 37 | +var PROJECT_ID = uuid.New().String() |
| 38 | +var INSTANCE_ID = uuid.New().String() |
| 39 | + |
| 40 | +func TestCreateGitInstanceWaitHandler(t *testing.T) { |
| 41 | + tests := []struct { |
| 42 | + desc string |
| 43 | + getFails bool |
| 44 | + wantErr bool |
| 45 | + wantResp bool |
| 46 | + projectId string |
| 47 | + instanceId string |
| 48 | + returnInstance bool |
| 49 | + getGitResponse *git.Instance |
| 50 | + }{ |
| 51 | + { |
| 52 | + desc: "Creation of an instance succeeded", |
| 53 | + getFails: false, |
| 54 | + wantErr: false, |
| 55 | + wantResp: true, |
| 56 | + projectId: uuid.New().String(), |
| 57 | + instanceId: INSTANCE_ID, |
| 58 | + returnInstance: true, |
| 59 | + getGitResponse: &git.Instance{ |
| 60 | + Created: utils.Ptr(time.Now()), |
| 61 | + Id: utils.Ptr(INSTANCE_ID), |
| 62 | + Name: utils.Ptr("instance-test"), |
| 63 | + State: utils.Ptr(InstanceStateReady), |
| 64 | + Url: utils.Ptr("https://testing.git.onstackit.cloud"), |
| 65 | + Version: utils.Ptr("v1.6.0"), |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + desc: "Creation of an instance failed with error", |
| 70 | + getFails: true, |
| 71 | + wantErr: true, |
| 72 | + wantResp: false, |
| 73 | + projectId: uuid.New().String(), |
| 74 | + instanceId: INSTANCE_ID, |
| 75 | + returnInstance: true, |
| 76 | + getGitResponse: &git.Instance{ |
| 77 | + Created: utils.Ptr(time.Now()), |
| 78 | + Id: utils.Ptr(INSTANCE_ID), |
| 79 | + Name: utils.Ptr("instance-test"), |
| 80 | + State: utils.Ptr(InstanceStateReady), |
| 81 | + Url: utils.Ptr("https://testing.git.onstackit.cloud"), |
| 82 | + Version: utils.Ptr("v1.6.0"), |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + desc: "Creation of an instance with response failed and without error", |
| 87 | + getFails: false, |
| 88 | + wantErr: true, |
| 89 | + wantResp: true, |
| 90 | + projectId: uuid.New().String(), |
| 91 | + instanceId: INSTANCE_ID, |
| 92 | + returnInstance: true, |
| 93 | + getGitResponse: &git.Instance{ |
| 94 | + Created: utils.Ptr(time.Now()), |
| 95 | + Id: utils.Ptr(INSTANCE_ID), |
| 96 | + Name: utils.Ptr("instance-test"), |
| 97 | + State: utils.Ptr(InstanceStateError), |
| 98 | + Url: utils.Ptr("https://testing.git.onstackit.cloud"), |
| 99 | + Version: utils.Ptr("v1.6.0"), |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + desc: "Creation of an instance failed without id on the response", |
| 104 | + getFails: false, |
| 105 | + wantErr: true, |
| 106 | + wantResp: false, |
| 107 | + projectId: PROJECT_ID, |
| 108 | + instanceId: INSTANCE_ID, |
| 109 | + returnInstance: true, |
| 110 | + getGitResponse: &git.Instance{ |
| 111 | + Created: utils.Ptr(time.Now()), |
| 112 | + Name: utils.Ptr("instance-test"), |
| 113 | + State: utils.Ptr(InstanceStateError), |
| 114 | + Url: utils.Ptr("https://testing.git.onstackit.cloud"), |
| 115 | + Version: utils.Ptr("v1.6.0"), |
| 116 | + }, |
| 117 | + }, |
| 118 | + |
| 119 | + { |
| 120 | + desc: "Creation of an instance without state on the response", |
| 121 | + getFails: false, |
| 122 | + wantErr: true, |
| 123 | + wantResp: false, |
| 124 | + projectId: PROJECT_ID, |
| 125 | + instanceId: INSTANCE_ID, |
| 126 | + returnInstance: true, |
| 127 | + getGitResponse: &git.Instance{ |
| 128 | + Created: utils.Ptr(time.Now()), |
| 129 | + Id: utils.Ptr(INSTANCE_ID), |
| 130 | + Name: utils.Ptr("instance-test"), |
| 131 | + Url: utils.Ptr("https://testing.git.onstackit.cloud"), |
| 132 | + Version: utils.Ptr("v1.6.0"), |
| 133 | + }, |
| 134 | + }, |
| 135 | + } |
| 136 | + for _, tt := range tests { |
| 137 | + t.Run(tt.desc, func(t *testing.T) { |
| 138 | + apiClient := &apiClientMocked{ |
| 139 | + getFails: tt.getFails, |
| 140 | + projectId: tt.projectId, |
| 141 | + instanceId: tt.instanceId, |
| 142 | + getGitResponse: tt.getGitResponse, |
| 143 | + returnInstance: tt.returnInstance, |
| 144 | + } |
| 145 | + var instanceWanted *git.Instance |
| 146 | + if tt.wantResp { |
| 147 | + instanceWanted = tt.getGitResponse |
| 148 | + } |
| 149 | + |
| 150 | + handler := CreateGitInstanceWaitHandler(context.Background(), apiClient, apiClient.projectId, apiClient.instanceId) |
| 151 | + |
| 152 | + response, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) |
| 153 | + |
| 154 | + if (err != nil) != tt.wantErr { |
| 155 | + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) |
| 156 | + } |
| 157 | + if !cmp.Equal(response, instanceWanted) { |
| 158 | + t.Fatalf("handler gotRes = %v, want %v", response, instanceWanted) |
| 159 | + } |
| 160 | + }) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +func TestDeleteGitInstanceWaitHandler(t *testing.T) { |
| 165 | + tests := []struct { |
| 166 | + desc string |
| 167 | + wantErr bool |
| 168 | + wantReturnedInstance bool |
| 169 | + getFails bool |
| 170 | + errorCode int |
| 171 | + returnInstance bool |
| 172 | + getGitResponse *git.Instance |
| 173 | + }{ |
| 174 | + { |
| 175 | + desc: "Instance deletion failed with error", |
| 176 | + wantErr: true, |
| 177 | + getFails: true, |
| 178 | + }, |
| 179 | + { |
| 180 | + desc: "Instance deletion failed returning existing instance", |
| 181 | + wantErr: true, |
| 182 | + getFails: false, |
| 183 | + |
| 184 | + wantReturnedInstance: false, |
| 185 | + returnInstance: true, |
| 186 | + getGitResponse: &git.Instance{ |
| 187 | + Created: utils.Ptr(time.Now()), |
| 188 | + Id: utils.Ptr(INSTANCE_ID), |
| 189 | + Name: utils.Ptr("instance-test"), |
| 190 | + State: utils.Ptr(InstanceStateReady), |
| 191 | + Url: utils.Ptr("https://testing.git.onstackit.cloud"), |
| 192 | + Version: utils.Ptr("v1.6.0"), |
| 193 | + }, |
| 194 | + }, |
| 195 | + { |
| 196 | + desc: "Instance deletion successful", |
| 197 | + wantErr: false, |
| 198 | + getFails: true, |
| 199 | + errorCode: http.StatusNotFound, |
| 200 | + wantReturnedInstance: false, |
| 201 | + returnInstance: false, |
| 202 | + }, |
| 203 | + } |
| 204 | + for _, tt := range tests { |
| 205 | + t.Run(tt.desc, func(t *testing.T) { |
| 206 | + apiClient := &apiClientMocked{ |
| 207 | + |
| 208 | + projectId: uuid.New().String(), |
| 209 | + getFails: tt.getFails, |
| 210 | + errorCode: tt.errorCode, |
| 211 | + returnInstance: tt.returnInstance, |
| 212 | + getGitResponse: tt.getGitResponse, |
| 213 | + } |
| 214 | + |
| 215 | + handler := DeleteGitInstanceWaitHandler(context.Background(), apiClient, apiClient.projectId, apiClient.instanceId) |
| 216 | + response, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) |
| 217 | + |
| 218 | + if (err != nil) != tt.wantErr { |
| 219 | + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) |
| 220 | + } |
| 221 | + if (response != nil) != tt.wantReturnedInstance { |
| 222 | + t.Fatalf("handler gotRes = %v, want nil", response) |
| 223 | + } |
| 224 | + }) |
| 225 | + } |
| 226 | +} |
0 commit comments