Skip to content

Commit

Permalink
Disable encryption in tests better
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Jul 7, 2024
1 parent 3557492 commit 128cf80
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
7 changes: 2 additions & 5 deletions internal/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 10 additions & 4 deletions internal/command/env_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@ 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"))
}
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
Expand Down
4 changes: 2 additions & 2 deletions internal/command/env_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
2 changes: 1 addition & 1 deletion internal/runnerv2service/service_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 128cf80

Please sign in to comment.