test: Fix GPG test so it does not inherit parent parallelism (#5820)

* test: Fix GPG test so it does not inherit parent parallelism

Running a subtest in a parent with `t.Parallel()` and using `t.Setenv`
is not allowed in Go 1.20, so we move it to a separate test function.

* Fix shadowed import
This commit is contained in:
Mathias Fredriksson
2023-01-23 13:40:41 +02:00
committed by GitHub
parent 73afdd7c09
commit 6a245ab1cc

View File

@ -293,9 +293,10 @@ func TestSSH(t *testing.T) {
pty.WriteLine("exit") pty.WriteLine("exit")
<-cmdDone <-cmdDone
}) })
}
//nolint:paralleltest // This test uses t.Setenv. //nolint:paralleltest // This test uses t.Setenv, parent test MUST NOT be parallel.
t.Run("ForwardGPG", func(t *testing.T) { func TestSSH_ForwardGPG(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
// While GPG forwarding from a Windows client works, we currently do // While GPG forwarding from a Windows client works, we currently do
// not support forwarding to a Windows workspace. Our tests use the // not support forwarding to a Windows workspace. Our tests use the
@ -464,10 +465,10 @@ Expire-Date: 0
"--forward-gpg", "--forward-gpg",
) )
clitest.SetupConfig(t, client, root) clitest.SetupConfig(t, client, root)
pty := ptytest.New(t) tpty := ptytest.New(t)
cmd.SetIn(pty.Input()) cmd.SetIn(tpty.Input())
cmd.SetOut(pty.Output()) cmd.SetOut(tpty.Output())
cmd.SetErr(pty.Output()) cmd.SetErr(tpty.Output())
cmdDone := tGo(t, func() { cmdDone := tGo(t, func() {
err := cmd.ExecuteContext(ctx) err := cmd.ExecuteContext(ctx)
assert.NoError(t, err, "ssh command failed") assert.NoError(t, err, "ssh command failed")
@ -481,40 +482,39 @@ Expire-Date: 0
// Wait for the prompt or any output really to indicate the command has // Wait for the prompt or any output really to indicate the command has
// started and accepting input on stdin. // started and accepting input on stdin.
_ = pty.Peek(ctx, 1) _ = tpty.Peek(ctx, 1)
pty.WriteLine("echo hello 'world'") tpty.WriteLine("echo hello 'world'")
pty.ExpectMatch("hello world") tpty.ExpectMatch("hello world")
// Check the GNUPGHOME was correctly inherited via shell. // Check the GNUPGHOME was correctly inherited via shell.
pty.WriteLine("env && echo env-''-command-done") tpty.WriteLine("env && echo env-''-command-done")
match := pty.ExpectMatch("env--command-done") match := tpty.ExpectMatch("env--command-done")
require.Contains(t, match, "GNUPGHOME="+gnupgHomeWorkspace, match) require.Contains(t, match, "GNUPGHOME="+gnupgHomeWorkspace, match)
// Get the agent extra socket path in the "workspace" via shell. // Get the agent extra socket path in the "workspace" via shell.
pty.WriteLine("gpgconf --list-dir agent-socket && echo gpgconf-''-agentsocket-command-done") tpty.WriteLine("gpgconf --list-dir agent-socket && echo gpgconf-''-agentsocket-command-done")
pty.ExpectMatch(workspaceAgentSocketPath) tpty.ExpectMatch(workspaceAgentSocketPath)
pty.ExpectMatch("gpgconf--agentsocket-command-done") tpty.ExpectMatch("gpgconf--agentsocket-command-done")
// List the keys in the "workspace". // List the keys in the "workspace".
pty.WriteLine("gpg --list-keys && echo gpg-''-listkeys-command-done") tpty.WriteLine("gpg --list-keys && echo gpg-''-listkeys-command-done")
listKeysOutput := pty.ExpectMatch("gpg--listkeys-command-done") listKeysOutput := tpty.ExpectMatch("gpg--listkeys-command-done")
require.Contains(t, listKeysOutput, "[ultimate] Coder Test <test@coder.com>") require.Contains(t, listKeysOutput, "[ultimate] Coder Test <test@coder.com>")
require.Contains(t, listKeysOutput, "[ultimate] Dean Sheather (work key) <dean@coder.com>") require.Contains(t, listKeysOutput, "[ultimate] Dean Sheather (work key) <dean@coder.com>")
// Try to sign something. This demonstrates that the forwarding is // Try to sign something. This demonstrates that the forwarding is
// working as expected, since the workspace doesn't have access to the // working as expected, since the workspace doesn't have access to the
// private key directly and must use the forwarded agent. // private key directly and must use the forwarded agent.
pty.WriteLine("echo 'hello world' | gpg --clearsign && echo gpg-''-sign-command-done") tpty.WriteLine("echo 'hello world' | gpg --clearsign && echo gpg-''-sign-command-done")
pty.ExpectMatch("BEGIN PGP SIGNED MESSAGE") tpty.ExpectMatch("BEGIN PGP SIGNED MESSAGE")
pty.ExpectMatch("Hash:") tpty.ExpectMatch("Hash:")
pty.ExpectMatch("hello world") tpty.ExpectMatch("hello world")
pty.ExpectMatch("gpg--sign-command-done") tpty.ExpectMatch("gpg--sign-command-done")
// And we're done. // And we're done.
pty.WriteLine("exit") tpty.WriteLine("exit")
<-cmdDone <-cmdDone
})
} }
// tGoContext runs fn in a goroutine passing a context that will be // tGoContext runs fn in a goroutine passing a context that will be