-
Notifications
You must be signed in to change notification settings - Fork 894
fix(cli): ensure that the support bundle command does not panic on zero values #14392
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
Changes from 1 commit
157164c
b10a361
99e396a
7fac003
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…ro values
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ import ( | |
"bytes" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
@@ -14,6 +17,7 @@ import ( | |
"tailscale.com/ipn/ipnstate" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/v2/agent" | ||
|
@@ -156,6 +160,42 @@ func TestSupportBundle(t *testing.T) { | |
err := inv.Run() | ||
require.ErrorContains(t, err, "failed authorization check") | ||
}) | ||
|
||
// This ensures that the CLI does not panic when trying to generate a support bundle | ||
// against a fake server that returns a 200 OK for all requests. This essentially | ||
// ensures that (almost) all of the support bundle generating code paths get a zero value. | ||
t.Run("DontPanic", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Start up a fake server that will return a blank 200 OK response for everything. | ||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
t.Logf("received request: %s %s", r.Method, r.URL) | ||
switch r.URL.Path { | ||
case "/api/v2/authcheck": | ||
// Fake auth check | ||
resp := codersdk.AuthorizationResponse{ | ||
"Read DeploymentValues": true, | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
assert.NoError(t, json.NewEncoder(w).Encode(resp)) | ||
default: | ||
// Simply return a 200 OK for everything else. | ||
w.WriteHeader(http.StatusOK) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would a non-200 response also trigger this problem? For example: eg.Go(func() error {
hr, err := healthsdk.New(client).DebugHealth(ctx)
if err != nil {
return xerrors.Errorf("fetch health report: %w", err)
}
d.HealthReport = &hr
return nil
}) I think it might be more idiomatic to have all calls fail rather than return empty values, although both are worthwhile testing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point, I'll test against a range of status codes. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated! |
||
} | ||
})) | ||
u, err := url.Parse(srv.URL) | ||
require.NoError(t, err) | ||
client := codersdk.New(u) | ||
defer srv.Close() | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
d := t.TempDir() | ||
path := filepath.Join(d, "bundle.zip") | ||
|
||
inv, root := clitest.New(t, "support", "bundle", "--url-override", srv.URL, "--output-file", path, "--yes") | ||
clitest.SetupConfig(t, client, root) | ||
err = inv.Run() | ||
require.NoError(t, err) | ||
}) | ||
} | ||
|
||
// nolint:revive // It's a control flag, but this is just a test. | ||
|
Uh oh!
There was an error while loading. Please reload this page.