Skip to content

Commit

Permalink
Fix flaky kubeexec unit tests using environment variables (redhat-dev…
Browse files Browse the repository at this point in the history
…eloper#5845)

Using a map for storing such data does not guarantee
an iteration order.
As we are matching an exact command-line string in the test,
we need to use a type that guarantees the iteration order.
  • Loading branch information
rm3l authored and cdrage committed Aug 31, 2022
1 parent 82fda10 commit 855473f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/devfile/adapters/kubernetes/component/commandhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ func devfileCommandToRemoteCmdDefinition(devfileCmd devfilev1.Command) (remotecm
return remotecmd.CommandDefinition{}, errors.New(" only Exec commands are supported")
}

envVars := make(map[string]string, len(devfileCmd.Exec.Env))
envVars := make([]remotecmd.CommandEnvVar, 0, len(devfileCmd.Exec.Env))
for _, e := range devfileCmd.Exec.Env {
envVars[e.Name] = e.Value
envVars = append(envVars, remotecmd.CommandEnvVar{Key: e.Name, Value: e.Value})
}

return remotecmd.CommandDefinition{
Expand Down
4 changes: 2 additions & 2 deletions pkg/remotecmd/kubeexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func (k *kubeExecProcessHandler) StartProcessForCommand(
// deal with environment variables
cmdLine := def.CmdLine
envCommands := make([]string, 0, len(def.EnvVars))
for key, val := range def.EnvVars {
envCommands = append(envCommands, fmt.Sprintf("%s='%s'", key, val))
for _, envVar := range def.EnvVars {
envCommands = append(envCommands, fmt.Sprintf("%s='%s'", envVar.Key, envVar.Value))
}
var setEnvCmd string
if len(envCommands) != 0 {
Expand Down
12 changes: 9 additions & 3 deletions pkg/remotecmd/kubexec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,15 @@ func TestKubeExecProcessHandler_StartProcessForCommand(t *testing.T) {
Id: "my-exec-cmd",
CmdLine: "tail -f /path/to/a/file",
WorkingDir: "/path/to/working/dir",
EnvVars: map[string]string{
"ENV_VAR1": "value1",
"ENV_VAR2": "value2",
EnvVars: []CommandEnvVar{
{
Key: "ENV_VAR1",
Value: "value1",
},
{
Key: "ENV_VAR2",
Value: "value2",
},
},
}
for _, tt := range []struct {
Expand Down
14 changes: 10 additions & 4 deletions pkg/remotecmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,20 @@ type CommandDefinition struct {
WorkingDir string

// EnvVars are environment variables to set.
EnvVars map[string]string
EnvVars []CommandEnvVar

// CmdLine is the command-line that will get executed.
CmdLine string
}

// CommandEnvVar represents an environment variable used as part of running any CommandDefinition.
type CommandEnvVar struct {

// Key of the environment variable.
Key string

//// RedirectOutputToMain indicates whether to redirect this command output streams to the main (PID 1) one.
//// This can be useful to have the output of this command show up in `odo logs` or `kubectl logs`.
//RedirectOutputToMain bool
// Value of the environment variable.
Value string
}

// CommandOutputHandler is a function that is expected to handle the output and error returned by a command executed.
Expand Down

0 comments on commit 855473f

Please sign in to comment.