-
Notifications
You must be signed in to change notification settings - Fork 144
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
[WIP] chore: restructure CLI init and begin logging refactor #2350
Draft
nathan-nicholson
wants to merge
11
commits into
main
Choose a base branch
from
nathan-nicholson/restructure-cli-init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
691a656
chore: refactor CLI initialization
nathan-nicholson 048690c
chore: adding new Printer implementation
nathan-nicholson c4816c2
chore: set output for rootCmd
nathan-nicholson af8f242
chore: begin logging refactor
nathan-nicholson 9089cbc
chore: remove empty space in conditionals
nathan-nicholson 9bc31f8
chore: add space to comment
nathan-nicholson 2584761
chore: rename AwsService to service
nathan-nicholson ed42c4f
chore: more lint fixes
nathan-nicholson 541d8f4
chore: ran gofumpt
nathan-nicholson dcb0deb
chore: delete unused printer struct
nathan-nicholson 23e5eef
chore: extract options to struct among other things
nathan-nicholson 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 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 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 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 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,92 @@ | ||
package cluster | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
apiTypes "github.com/konstructio/kubefirst-api/pkg/types" | ||
) | ||
|
||
type mockHTTPClient struct { | ||
DoFunc func(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
func (m *mockHTTPClient) Do(req *http.Request) (*http.Response, error) { | ||
return m.DoFunc(req) | ||
} | ||
|
||
func TestClusterClient_CreateCluster(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cluster apiTypes.ClusterDefinition | ||
mockResp *http.Response | ||
mockErr error | ||
wantErr bool | ||
errContains string | ||
}{ | ||
{ | ||
name: "successful cluster creation", | ||
cluster: apiTypes.ClusterDefinition{ | ||
ClusterName: "test-cluster", | ||
}, | ||
mockResp: &http.Response{ | ||
StatusCode: http.StatusAccepted, | ||
Body: io.NopCloser(bytes.NewReader([]byte(`{"status":"accepted"}`))), | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "error - unexpected status code", | ||
cluster: apiTypes.ClusterDefinition{ | ||
ClusterName: "test-cluster", | ||
}, | ||
mockResp: &http.Response{ | ||
StatusCode: http.StatusBadRequest, | ||
Status: "400 Bad Request", | ||
Body: io.NopCloser(bytes.NewReader([]byte(`{"error":"bad request"}`))), | ||
}, | ||
wantErr: true, | ||
errContains: "unexpected status code", | ||
}, | ||
{ | ||
name: "error - failed to make request", | ||
cluster: apiTypes.ClusterDefinition{ | ||
ClusterName: "test-cluster", | ||
}, | ||
mockErr: http.ErrHandlerTimeout, | ||
wantErr: true, | ||
errContains: "failed to execute request", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
mockClient := &mockHTTPClient{ | ||
DoFunc: func(req *http.Request) (*http.Response, error) { | ||
if tt.mockErr != nil { | ||
return nil, tt.mockErr | ||
} | ||
return tt.mockResp, nil | ||
}, | ||
} | ||
|
||
c := &ClusterClient{ | ||
hostURL: "http://test.local", | ||
httpClient: mockClient, | ||
} | ||
|
||
err := c.CreateCluster(tt.cluster) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("CreateCluster() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
|
||
if tt.wantErr && !strings.Contains(err.Error(), tt.errContains) { | ||
t.Errorf("CreateCluster() error = %v, should contain %v", err, tt.errContains) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not something that needs change but don't forget there's a whole
httptest
package that adds support for mocking stuff out of the box (including http requests and responses).See here for reference: https://github.com/konstructio/dropkick/blob/main/internal/civo/sdk/json/json_fetch_test.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. This was from the robot and I haven't got back to this yet.