From 8d0cd85118bb63032c87726c89a06649e3d78b94 Mon Sep 17 00:00:00 2001 From: Armel Soro Date: Mon, 20 Jun 2022 15:28:52 +0200 Subject: [PATCH] Fix flaky kubeexec unit tests using environment variables 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. --- .../kubernetes/component/commandhandler.go | 4 ++-- pkg/remotecmd/kubeexec.go | 4 ++-- pkg/remotecmd/kubexec_test.go | 12 +++++++++--- pkg/remotecmd/types.go | 14 ++++++++++---- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/pkg/devfile/adapters/kubernetes/component/commandhandler.go b/pkg/devfile/adapters/kubernetes/component/commandhandler.go index 182212387d3..02152d61677 100644 --- a/pkg/devfile/adapters/kubernetes/component/commandhandler.go +++ b/pkg/devfile/adapters/kubernetes/component/commandhandler.go @@ -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{ diff --git a/pkg/remotecmd/kubeexec.go b/pkg/remotecmd/kubeexec.go index 385a2834a1c..9f9217f26d9 100644 --- a/pkg/remotecmd/kubeexec.go +++ b/pkg/remotecmd/kubeexec.go @@ -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 { diff --git a/pkg/remotecmd/kubexec_test.go b/pkg/remotecmd/kubexec_test.go index 38af9b7e220..8c36aaafcc5 100644 --- a/pkg/remotecmd/kubexec_test.go +++ b/pkg/remotecmd/kubexec_test.go @@ -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 { diff --git a/pkg/remotecmd/types.go b/pkg/remotecmd/types.go index 5be7daed071..d3b3d7b84b7 100644 --- a/pkg/remotecmd/types.go +++ b/pkg/remotecmd/types.go @@ -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.