Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e991a40

Browse files
coadlerkylecarbs
authored andcommitted
fix: log after test exit in TestAgent/StartupScript (#1726)
``` $ go test ./agent/ -v -run TestAgent/StartupScript -count 1 === RUN TestAgent === PAUSE TestAgent === CONT TestAgent === RUN TestAgent/StartupScript === PAUSE TestAgent/StartupScript === CONT TestAgent/StartupScript t.go:56: 2022-05-24 20:22:39.648 [INFO] <agent.go:112> connected --- PASS: TestAgent (0.00s) --- PASS: TestAgent/StartupScript (0.17s) PASS panic: Log in goroutine after TestAgent/StartupScript has completed: 2022-05-24 20:22:39.651 [WARN] <agent.go:130> agent script failed ... "error": run: github.com/coder/coder/agent.(*agent).runStartupScript /home/colin/Projects/coder/coder/agent/agent.go:183 - signal: killed ```
1 parent d940a19 commit e991a40

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

agent/agent.go

+22-11
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,25 @@ import (
2121
"time"
2222

2323
"github.com/armon/circbuf"
24+
"github.com/gliderlabs/ssh"
2425
"github.com/google/uuid"
25-
26+
"github.com/pkg/sftp"
2627
"go.uber.org/atomic"
28+
gossh "golang.org/x/crypto/ssh"
29+
"golang.org/x/xerrors"
2730

2831
"cdr.dev/slog"
2932
"github.com/coder/coder/agent/usershell"
3033
"github.com/coder/coder/peer"
3134
"github.com/coder/coder/peerbroker"
3235
"github.com/coder/coder/pty"
3336
"github.com/coder/retry"
37+
)
3438

35-
"github.com/pkg/sftp"
36-
37-
"github.com/gliderlabs/ssh"
38-
gossh "golang.org/x/crypto/ssh"
39-
"golang.org/x/xerrors"
39+
const (
40+
ProtocolReconnectingPTY = "reconnecting-pty"
41+
ProtocolSSH = "ssh"
42+
ProtocolDial = "dial"
4043
)
4144

4245
type Options struct {
@@ -174,17 +177,25 @@ func (*agent) runStartupScript(ctx context.Context, script string) error {
174177
defer func() {
175178
_ = writer.Close()
176179
}()
180+
177181
caller := "-c"
178182
if runtime.GOOS == "windows" {
179183
caller = "/c"
180184
}
185+
181186
cmd := exec.CommandContext(ctx, shell, caller, script)
182187
cmd.Stdout = writer
183188
cmd.Stderr = writer
184189
err = cmd.Run()
185190
if err != nil {
191+
// cmd.Run does not return a context canceled error, it returns "signal: killed".
192+
if ctx.Err() != nil {
193+
return ctx.Err()
194+
}
195+
186196
return xerrors.Errorf("run: %w", err)
187197
}
198+
188199
return nil
189200
}
190201

@@ -208,11 +219,11 @@ func (a *agent) handlePeerConn(ctx context.Context, conn *peer.Conn) {
208219
}
209220

210221
switch channel.Protocol() {
211-
case "ssh":
222+
case ProtocolSSH:
212223
go a.sshServer.HandleConn(channel.NetConn())
213-
case "reconnecting-pty":
224+
case ProtocolReconnectingPTY:
214225
go a.handleReconnectingPTY(ctx, channel.Label(), channel.NetConn())
215-
case "dial":
226+
case ProtocolDial:
216227
go a.handleDial(ctx, channel.Label(), channel.NetConn())
217228
default:
218229
a.logger.Warn(ctx, "unhandled protocol from channel",
@@ -478,8 +489,8 @@ func (a *agent) handleReconnectingPTY(ctx context.Context, rawID string, conn ne
478489
a.logger.Warn(ctx, "start reconnecting pty command", slog.F("id", id))
479490
}
480491

481-
// Default to buffer 64KB.
482-
circularBuffer, err := circbuf.NewBuffer(64 * 1024)
492+
// Default to buffer 64KiB.
493+
circularBuffer, err := circbuf.NewBuffer(64 << 10)
483494
if err != nil {
484495
a.logger.Warn(ctx, "create circular buffer", slog.Error(err))
485496
return

agent/agent_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,9 @@ func TestAgent(t *testing.T) {
172172
tempPath := filepath.Join(os.TempDir(), "content.txt")
173173
content := "somethingnice"
174174
setupAgent(t, agent.Metadata{
175-
StartupScript: "echo " + content + " > " + tempPath,
175+
StartupScript: fmt.Sprintf("echo %s > %s", content, tempPath),
176176
}, 0)
177+
177178
var gotContent string
178179
require.Eventually(t, func() bool {
179180
content, err := os.ReadFile(tempPath)
@@ -202,6 +203,7 @@ func TestAgent(t *testing.T) {
202203
// it seems like it could be either.
203204
t.Skip("ConPTY appears to be inconsistent on Windows.")
204205
}
206+
205207
conn := setupAgent(t, agent.Metadata{}, 0)
206208
id := uuid.NewString()
207209
netConn, err := conn.ReconnectingPTY(id, 100, 100)
@@ -228,6 +230,7 @@ func TestAgent(t *testing.T) {
228230
}
229231
}
230232
}
233+
231234
matchEchoCommand := func(line string) bool {
232235
return strings.Contains(line, "echo test")
233236
}

agent/conn.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Conn struct {
3636
// be reconnected to via ID.
3737
func (c *Conn) ReconnectingPTY(id string, height, width uint16) (net.Conn, error) {
3838
channel, err := c.CreateChannel(context.Background(), fmt.Sprintf("%s:%d:%d", id, height, width), &peer.ChannelOptions{
39-
Protocol: "reconnecting-pty",
39+
Protocol: ProtocolReconnectingPTY,
4040
})
4141
if err != nil {
4242
return nil, xerrors.Errorf("pty: %w", err)
@@ -47,7 +47,7 @@ func (c *Conn) ReconnectingPTY(id string, height, width uint16) (net.Conn, error
4747
// SSH dials the built-in SSH server.
4848
func (c *Conn) SSH() (net.Conn, error) {
4949
channel, err := c.CreateChannel(context.Background(), "ssh", &peer.ChannelOptions{
50-
Protocol: "ssh",
50+
Protocol: ProtocolSSH,
5151
})
5252
if err != nil {
5353
return nil, xerrors.Errorf("dial: %w", err)
@@ -87,7 +87,7 @@ func (c *Conn) DialContext(ctx context.Context, network string, addr string) (ne
8787
}
8888

8989
channel, err := c.CreateChannel(ctx, u.String(), &peer.ChannelOptions{
90-
Protocol: "dial",
90+
Protocol: ProtocolDial,
9191
Unordered: strings.HasPrefix(network, "udp"),
9292
})
9393
if err != nil {

0 commit comments

Comments
 (0)