feat(cli): add hidden netcheck command (#8136)

This commit is contained in:
Colin Adler
2023-06-21 14:33:19 -05:00
committed by GitHub
parent 7703bb77a7
commit bc739bdfce
8 changed files with 189 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
package cli
import (
"context"
"encoding/json"
"fmt"
"time"
"golang.org/x/xerrors"
"github.com/coder/coder/cli/clibase"
"github.com/coder/coder/coderd/healthcheck"
"github.com/coder/coder/codersdk"
)
func (r *RootCmd) netcheck() *clibase.Cmd {
client := new(codersdk.Client)
cmd := &clibase.Cmd{
Use: "netcheck",
Short: "Print network debug information for DERP and STUN",
Hidden: true,
Middleware: clibase.Chain(
r.InitClient(client),
),
Handler: func(inv *clibase.Invocation) error {
ctx, cancel := context.WithTimeout(inv.Context(), 30*time.Second)
defer cancel()
connInfo, err := client.WorkspaceAgentConnectionInfo(ctx)
if err != nil {
return err
}
_, _ = fmt.Fprint(inv.Stderr, "Gathering a network report. This may take a few seconds...\n\n")
var report healthcheck.DERPReport
report.Run(ctx, &healthcheck.DERPReportOptions{
DERPMap: connInfo.DERPMap,
})
raw, err := json.MarshalIndent(report, "", " ")
if err != nil {
return err
}
n, err := inv.Stdout.Write(raw)
if err != nil {
return err
}
if n != len(raw) {
return xerrors.Errorf("failed to write all bytes to stdout; wrote %d, len %d", n, len(raw))
}
_, _ = inv.Stdout.Write([]byte("\n"))
return nil
},
}
cmd.Options = clibase.OptionSet{}
return cmd
}
+34
View File
@@ -0,0 +1,34 @@
package cli_test
import (
"bytes"
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/healthcheck"
"github.com/coder/coder/pty/ptytest"
)
func TestNetcheck(t *testing.T) {
t.Parallel()
pty := ptytest.New(t)
config := login(t, pty)
var out bytes.Buffer
inv, _ := clitest.New(t, "netcheck", "--global-config", string(config))
inv.Stdout = &out
clitest.StartWithWaiter(t, inv).RequireSuccess()
var report healthcheck.DERPReport
require.NoError(t, json.Unmarshal(out.Bytes(), &report))
assert.True(t, report.Healthy)
require.Len(t, report.Regions, 1)
require.Len(t, report.Regions[1].NodeReports, 1)
}
+1
View File
@@ -107,6 +107,7 @@ func (r *RootCmd) Core() []*clibase.Cmd {
// Hidden
r.gitssh(),
r.netcheck(),
r.vscodeSSH(),
r.workspaceAgent(),
}