From 2016050a1cfe60ad3dd8f157cb6698c447411940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Cie=C5=9Blak?= Date: Wed, 20 Nov 2024 12:26:31 +0100 Subject: [PATCH 1/4] Add test that executes tsh with no env vars --- tool/tsh/common/tsh_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tool/tsh/common/tsh_test.go b/tool/tsh/common/tsh_test.go index 1bf5bd688ff66..e83948244d353 100644 --- a/tool/tsh/common/tsh_test.go +++ b/tool/tsh/common/tsh_test.go @@ -407,6 +407,29 @@ func TestAlias(t *testing.T) { } } +// TestNoEnvVars checks if tsh is able to work without any env vars provided. +// This is important for VNet on macOS. When launchd starts VNet's launch daemon, it executes "tsh +// vnet-daemon" with only the following env vars available: +// +// XPC_SERVICE_NAME=com.goteleport.tshdev.vnetd +// PATH=/usr/bin:/bin:/usr/sbin:/sbin +// XPC_FLAGS=1 +// +// …plus whatever is set in the launch daemon plist under the EnvironmentVariables key. +func TestNoEnvVars(t *testing.T) { + testExecutable, err := os.Executable() + require.NoError(t, err) + // Execute an actual command and not jut `tsh help` which goes through a different code path. + cmd := exec.Command(testExecutable, "version", "--client") + // Run the command with no env vars except tshBinMainTestEnv, otherwise the test would hang. + cmd.Env = []string{fmt.Sprintf("%s=1", tshBinMainTestEnv)} + + t.Logf("running command %v", cmd) + output, err := cmd.CombinedOutput() + t.Logf("executable output: %v", string(output)) + require.NoError(t, err) +} + func TestFailedLogin(t *testing.T) { t.Parallel() tmpHomePath := t.TempDir() From a9f0e0e1d5774be33ebc4c71e3bf02adfc811676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Cie=C5=9Blak?= Date: Wed, 20 Nov 2024 17:45:16 +0100 Subject: [PATCH 2/4] Use exec.CommandContext --- tool/tsh/common/tsh_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tool/tsh/common/tsh_test.go b/tool/tsh/common/tsh_test.go index e83948244d353..090e69b425843 100644 --- a/tool/tsh/common/tsh_test.go +++ b/tool/tsh/common/tsh_test.go @@ -417,17 +417,23 @@ func TestAlias(t *testing.T) { // // …plus whatever is set in the launch daemon plist under the EnvironmentVariables key. func TestNoEnvVars(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + t.Cleanup(cancel) + testExecutable, err := os.Executable() require.NoError(t, err) // Execute an actual command and not jut `tsh help` which goes through a different code path. - cmd := exec.Command(testExecutable, "version", "--client") + cmd := exec.CommandContext(ctx, testExecutable, "version", "--client") // Run the command with no env vars except tshBinMainTestEnv, otherwise the test would hang. cmd.Env = []string{fmt.Sprintf("%s=1", tshBinMainTestEnv)} t.Logf("running command %v", cmd) output, err := cmd.CombinedOutput() t.Logf("executable output: %v", string(output)) - require.NoError(t, err) + // By checking the ctx error together with err, the error report will include "context deadline + // exceeded" if the command doesn't complete within the timeout. Otherwise the error would be just + // "signal: killed". + require.NoError(t, trace.NewAggregate(err, ctx.Err())) } func TestFailedLogin(t *testing.T) { From f20679e849407323f9caf27de04756c34ecdf007 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Cie=C5=9Blak?= Date: Wed, 20 Nov 2024 17:45:27 +0100 Subject: [PATCH 3/4] Run test in parallel --- tool/tsh/common/tsh_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tool/tsh/common/tsh_test.go b/tool/tsh/common/tsh_test.go index 090e69b425843..1a3e0a2f3f63d 100644 --- a/tool/tsh/common/tsh_test.go +++ b/tool/tsh/common/tsh_test.go @@ -417,6 +417,7 @@ func TestAlias(t *testing.T) { // // …plus whatever is set in the launch daemon plist under the EnvironmentVariables key. func TestNoEnvVars(t *testing.T) { + t.Parallel() ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) t.Cleanup(cancel) From 104f81b8fefd521d73839a1e77511c752aff4d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Cie=C5=9Blak?= Date: Thu, 21 Nov 2024 08:46:00 +0100 Subject: [PATCH 4/4] Fix typo Co-authored-by: Bernard Kim --- tool/tsh/common/tsh_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tool/tsh/common/tsh_test.go b/tool/tsh/common/tsh_test.go index 1a3e0a2f3f63d..408d1b37fec3d 100644 --- a/tool/tsh/common/tsh_test.go +++ b/tool/tsh/common/tsh_test.go @@ -423,7 +423,7 @@ func TestNoEnvVars(t *testing.T) { testExecutable, err := os.Executable() require.NoError(t, err) - // Execute an actual command and not jut `tsh help` which goes through a different code path. + // Execute an actual command and not just `tsh help` which goes through a different code path. cmd := exec.CommandContext(ctx, testExecutable, "version", "--client") // Run the command with no env vars except tshBinMainTestEnv, otherwise the test would hang. cmd.Env = []string{fmt.Sprintf("%s=1", tshBinMainTestEnv)}