From 52c9f4d55e19dfecee34639a750791f3280b15aa 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 a6dc3c55bc14f..626e5204fed78 100644 --- a/tool/tsh/common/tsh_test.go +++ b/tool/tsh/common/tsh_test.go @@ -390,6 +390,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) { tmpHomePath := t.TempDir() From 9dca56eaa8e466d418b5de96a69e6d3a041654b0 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 626e5204fed78..aaef37a3d50d2 100644 --- a/tool/tsh/common/tsh_test.go +++ b/tool/tsh/common/tsh_test.go @@ -400,17 +400,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 02c14ab9a9d24aebbf964db1b731f39de2a14477 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 aaef37a3d50d2..039a72190ca7e 100644 --- a/tool/tsh/common/tsh_test.go +++ b/tool/tsh/common/tsh_test.go @@ -400,6 +400,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 a9f593ebc59234c4e543da440f01a7f0fb234765 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 039a72190ca7e..185915028f18b 100644 --- a/tool/tsh/common/tsh_test.go +++ b/tool/tsh/common/tsh_test.go @@ -406,7 +406,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)}