From 128cf80c119761fcf2a13ae4c8df52e345a29a58 Mon Sep 17 00:00:00 2001 From: Adam Babik Date: Mon, 8 Jul 2024 00:28:56 +0200 Subject: [PATCH] Disable encryption in tests better --- internal/command/command_test.go | 7 ++----- internal/command/env_collector.go | 14 ++++++++++---- internal/command/env_shell.go | 4 ++-- internal/runnerv2service/service_execute_test.go | 2 +- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/internal/command/command_test.go b/internal/command/command_test.go index 5578fcc3..ff26b535 100644 --- a/internal/command/command_test.go +++ b/internal/command/command_test.go @@ -13,15 +13,12 @@ import ( func init() { // Switch from "runme env" to "env -0" for the tests. - // This is because the "runme env" command is not available + // This is because the "runme" program is not available // in the test environment. // // TODO(adamb): this can be changed. runme must be built // in the test environment and put into the PATH. - EnvDumpCommand = "env -0" - - // We don't have a way to test encryption using overriden [EnvDumpCommand]. - envCollectorEnableEncryption = false + SetEnvDumpCommand("env -0") } func testExecuteCommand( diff --git a/internal/command/env_collector.go b/internal/command/env_collector.go index 0d9abd72..b6acc443 100644 --- a/internal/command/env_collector.go +++ b/internal/command/env_collector.go @@ -20,10 +20,8 @@ const ( envCollectorEncNonceEnvName = "RUNME_ENCRYPTION_NONCE" ) -// EnvDumpCommand is a command that dumps the environment variables. -// It is declared as a var, because it must be replaced in tests. -// Equivalent is `env -0`. -var EnvDumpCommand = func() string { +// envDumpCommand is a command that dumps the environment variables. +var envDumpCommand = func() string { path, err := os.Executable() if err != nil { panic(errors.WithMessage(err, "failed to get the executable path")) @@ -31,6 +29,14 @@ var EnvDumpCommand = func() string { return strings.Join([]string{path, "env", "dump", "--insecure"}, " ") }() +func SetEnvDumpCommand(cmd string) { + envDumpCommand = cmd + // When overriding [envDumpCommand], we disable the encryption. + // There is no way to test the encryption if the dump command + // is not controlled. + envCollectorEnableEncryption = false +} + type envCollectorFactoryOptions struct { encryptionEnabled bool useFifo bool diff --git a/internal/command/env_shell.go b/internal/command/env_shell.go index 1acd6a2a..2a217eec 100644 --- a/internal/command/env_shell.go +++ b/internal/command/env_shell.go @@ -8,14 +8,14 @@ import ( func setOnShell(shell io.Writer, prePath, postPath string) error { var err error // First, dump all env at the beginning, so that a diff can be calculated. - _, err = shell.Write([]byte(EnvDumpCommand + " > " + prePath + "\n")) + _, err = shell.Write([]byte(envDumpCommand + " > " + prePath + "\n")) if err != nil { return err } // Then, set a trap on EXIT to dump all env at the end. _, err = shell.Write(bytes.Join( [][]byte{ - []byte("__cleanup() {\nrv=$?\n" + (EnvDumpCommand + " > " + postPath) + "\nexit $rv\n}"), + []byte("__cleanup() {\nrv=$?\n" + (envDumpCommand + " > " + postPath) + "\nexit $rv\n}"), []byte("trap -- \"__cleanup\" EXIT"), nil, // add a new line at the end }, diff --git a/internal/runnerv2service/service_execute_test.go b/internal/runnerv2service/service_execute_test.go index f8b18d60..15ccf8f6 100644 --- a/internal/runnerv2service/service_execute_test.go +++ b/internal/runnerv2service/service_execute_test.go @@ -30,7 +30,7 @@ func init() { // This can be turned on if setSysProcAttrPgid() is called in Start(). command.SignalToProcessGroup = false - command.EnvDumpCommand = "env -0" + command.SetEnvDumpCommand("env -0") // Server uses autoconfig to get necessary dependencies. // One of them, implicit, is [config.Config]. With the default