-
Notifications
You must be signed in to change notification settings - Fork 24
Add waiters in git service for Create and Delete git instances #1890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0ba3edf
Add waiters in git service for Create and Delete git instances
peremanent b9038ea
Modify Waiters tests for Create and Delete git instances
peremanent 6c1a1ae
Improving waiters tests in git package
peremanent 315a21e
fix: misspelling of
peremanent 967675d
Merge branch 'main' into main
Fyusel 1cca183
Improve waiters test and adding changelog to the git service
peremanent 8da96e0
Fix merge conflicts with root Changelog
peremanent 62152a3
Improve waiters test and adding changelog to the git service
peremanent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package wait | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/stackitcloud/stackit-sdk-go/core/wait" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/git" | ||
| ) | ||
|
|
||
| const ( | ||
| CreateSuccess = "Ready" | ||
| Creating = "Creating" | ||
| CreateFail = "Error" | ||
| ) | ||
|
|
||
| // APIClientInterface Interfaces needed for tests | ||
| type APIClientInterface interface { | ||
| GetGitExecute(ctx context.Context, projectId string, instanceId string) (*git.Instance, error) | ||
peremanent marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func CreateGitInstanceWaitHandler(ctx context.Context, a APIClientInterface, projectId, instanceId string) *wait.AsyncActionHandler[git.Instance] { | ||
| handler := wait.New(func() (waitFinished bool, response *git.Instance, err error) { | ||
| instance, err := a.GetGitExecute(ctx, projectId, instanceId) | ||
| if err != nil { | ||
| return false, nil, err | ||
| } | ||
| if *instance.Id == "" || *instance.State == "" { | ||
peremanent marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return false, nil, fmt.Errorf("could not get Instance id or State from response for project %s and instanceId %s", projectId, instanceId) | ||
| } | ||
| if *instance.Id == instanceId && *instance.State == CreateSuccess { | ||
| return true, instance, nil | ||
| } | ||
| if *instance.Id == instanceId && *instance.State == CreateFail { | ||
| return true, instance, fmt.Errorf("create failed for Instance with id %s", instanceId) | ||
| } | ||
| return false, nil, nil | ||
| }) | ||
| handler.SetTimeout(10 * time.Minute) | ||
| return handler | ||
| } | ||
|
|
||
| func DeleteGitInstanceWaitHandler(ctx context.Context, a APIClientInterface, projectId, instanceId string) *wait.AsyncActionHandler[git.Instance] { | ||
| handler := wait.New(func() (waitFinished bool, response *git.Instance, err error) { | ||
| instance, err := a.GetGitExecute(ctx, projectId, instanceId) | ||
| if err != nil { | ||
| return true, nil, err | ||
| } | ||
peremanent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if instance != nil { | ||
| return true, instance, nil | ||
| } | ||
| return true, nil, nil | ||
| }) | ||
| handler.SetTimeout(10 * time.Minute) | ||
| return handler | ||
| } | ||
peremanent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| package wait | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/uuid" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/git" | ||
| ) | ||
|
|
||
| type apiClientMocked struct { | ||
| desc string | ||
peremanent marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| getFails bool | ||
| returnInstance bool | ||
| wantErr bool | ||
peremanent marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| created time.Time | ||
| id string | ||
| projectId string | ||
| name string | ||
| state string | ||
| url string | ||
| version string | ||
peremanent marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func (a *apiClientMocked) GetGitExecute(_ context.Context, _, _ string) (*git.Instance, error) { | ||
Fyusel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if a.getFails { | ||
| return nil, &oapierror.GenericOpenAPIError{ | ||
| StatusCode: http.StatusInternalServerError, | ||
| } | ||
| } | ||
| if !a.returnInstance { | ||
| return nil, nil | ||
| } | ||
| return &git.Instance{ | ||
| Created: utils.Ptr(a.created), | ||
| Id: utils.Ptr(a.id), | ||
| Name: utils.Ptr(a.name), | ||
| State: utils.Ptr(a.state), | ||
| Url: utils.Ptr(a.url), | ||
| Version: utils.Ptr(a.version), | ||
| }, nil | ||
| } | ||
|
|
||
| func TestCreateGitInstanceWaitHandler(t *testing.T) { | ||
| tests := []struct { | ||
| desc string | ||
| getFails bool | ||
| wantErr bool | ||
| wantResp bool | ||
| created time.Time | ||
| id string | ||
| projectId string | ||
| name string | ||
| state string | ||
| url string | ||
| version string | ||
| returnInstance bool | ||
| }{ | ||
| { | ||
| desc: "Creation of an instance succeeded", | ||
| getFails: false, | ||
| wantErr: false, | ||
| wantResp: true, | ||
| created: time.Now(), | ||
| id: uuid.New().String(), | ||
| projectId: uuid.New().String(), | ||
| name: "instance-test", | ||
| state: CreateSuccess, | ||
| url: "https://testing.git.onstackit.cloud", | ||
| version: "v1.6.0", | ||
| returnInstance: true, | ||
| }, | ||
| { | ||
| desc: "Creation of an instance Failed With Error", | ||
peremanent marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| getFails: true, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| created: time.Now(), | ||
| id: uuid.New().String(), | ||
| projectId: uuid.New().String(), | ||
| name: "instance-test", | ||
| state: CreateFail, | ||
| url: "https://testing.git.onstackit.cloud", | ||
| version: "v1.6.0", | ||
| returnInstance: true, | ||
| }, | ||
| { | ||
| desc: "Creation of an instance with response failed and without error", | ||
| getFails: false, | ||
| wantErr: true, | ||
| wantResp: true, | ||
| created: time.Now(), | ||
| id: uuid.New().String(), | ||
| projectId: uuid.New().String(), | ||
| name: "instance-test", | ||
| state: CreateFail, | ||
| url: "https://testing.git.onstackit.cloud", | ||
| version: "v1.6.0", | ||
| returnInstance: true, | ||
| }, | ||
| { | ||
| desc: "Creation of an instance failed without id on the response", | ||
| getFails: false, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| created: time.Now(), | ||
| projectId: uuid.New().String(), | ||
| name: "instance-test", | ||
| state: CreateFail, | ||
| url: "https://testing.git.onstackit.cloud", | ||
| version: "v1.6.0", | ||
| returnInstance: true, | ||
peremanent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
|
|
||
| { | ||
| desc: "Creation of an instance without state on the response", | ||
| getFails: false, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| created: time.Now(), | ||
| id: uuid.New().String(), | ||
| projectId: uuid.New().String(), | ||
| name: "instance-test", | ||
| url: "https://testing.git.onstackit.cloud", | ||
| version: "v1.6.0", | ||
| returnInstance: true, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.desc, func(t *testing.T) { | ||
| apiClient := &apiClientMocked{ | ||
| desc: tt.desc, | ||
| getFails: tt.getFails, | ||
| wantErr: tt.wantErr, | ||
| created: tt.created, | ||
| id: tt.id, | ||
| projectId: tt.projectId, | ||
| name: tt.name, | ||
| state: tt.state, | ||
| url: tt.url, | ||
| version: tt.version, | ||
| returnInstance: tt.returnInstance, | ||
| } | ||
| var instanceWanted *git.Instance | ||
| if tt.wantResp { | ||
| instanceWanted = &git.Instance{ | ||
| Created: utils.Ptr(tt.created), | ||
| Id: utils.Ptr(tt.id), | ||
| Name: utils.Ptr(tt.name), | ||
| State: utils.Ptr(tt.state), | ||
| Url: utils.Ptr(tt.url), | ||
| Version: utils.Ptr(tt.version), | ||
| } | ||
| } | ||
|
|
||
| handler := CreateGitInstanceWaitHandler(context.Background(), apiClient, apiClient.projectId, apiClient.id) | ||
|
|
||
| response, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) | ||
|
|
||
| if (err != nil) != tt.wantErr { | ||
| t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) | ||
| } | ||
| if !cmp.Equal(response, instanceWanted) { | ||
| t.Fatalf("handler gotRes = %v, want %v", response, instanceWanted) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDeleteGitInstanceWaitHandler(t *testing.T) { | ||
| tests := []struct { | ||
| desc string | ||
| wantErr bool | ||
| wantReturnedInstance bool | ||
| getFails bool | ||
| returnInstance bool | ||
| created time.Time | ||
| id string | ||
| name string | ||
| state string | ||
| url string | ||
| version string | ||
| }{ | ||
| { | ||
| desc: "Instance deletion failed with error", | ||
| wantErr: true, | ||
| getFails: true, | ||
| }, | ||
| { | ||
| desc: "Instance deletion failed returning existing instance", | ||
| wantErr: false, | ||
| getFails: false, | ||
| wantReturnedInstance: true, | ||
| created: time.Now(), | ||
| id: uuid.New().String(), | ||
| name: "instance-test", | ||
| state: CreateSuccess, | ||
| returnInstance: true, | ||
| url: "https://testing.git.onstackit.cloud", | ||
| version: "v1.6.0", | ||
| }, | ||
| { | ||
| desc: "Instance deletion succesfull", | ||
Fyusel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| wantErr: false, | ||
| getFails: false, | ||
| wantReturnedInstance: false, | ||
| returnInstance: false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.desc, func(t *testing.T) { | ||
| apiClient := &apiClientMocked{ | ||
|
|
||
| projectId: uuid.New().String(), | ||
| getFails: tt.getFails, | ||
| created: tt.created, | ||
| id: tt.id, | ||
| name: tt.name, | ||
| state: tt.state, | ||
| url: tt.url, | ||
| version: tt.version, | ||
| returnInstance: tt.returnInstance, | ||
| } | ||
|
|
||
| handler := DeleteGitInstanceWaitHandler(context.Background(), apiClient, apiClient.projectId, apiClient.id) | ||
| response, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) | ||
|
|
||
| if (err != nil) != tt.wantErr { | ||
| t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) | ||
| } | ||
| if (response != nil) != tt.wantReturnedInstance { | ||
| t.Fatalf("handler gotRes = %v, want nil", response) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.