Skip to content

Commit

Permalink
tests: Share SpawnCmdWithLogger between coverage and no coverage scen…
Browse files Browse the repository at this point in the history
…arios

Signed-off-by: Marek Siarkowicz <[email protected]>
  • Loading branch information
serathius committed Oct 17, 2022
1 parent 5c3191b commit 4fa4bc3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 49 deletions.
23 changes: 22 additions & 1 deletion tests/framework/e2e/etcd_spawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package e2e

import (
"os"
"strings"

"go.uber.org/zap"
Expand All @@ -23,7 +24,8 @@ import (
)

var (
initBinPath func(string) binPath
initBinPath func(string) binPath
additionalArgs func() ([]string, error)
)

func SpawnCmd(args []string, envVars map[string]string) (*expect.ExpectProcess, error) {
Expand All @@ -33,3 +35,22 @@ func SpawnCmd(args []string, envVars map[string]string) (*expect.ExpectProcess,
func SpawnNamedCmd(processName string, args []string, envVars map[string]string) (*expect.ExpectProcess, error) {
return SpawnCmdWithLogger(zap.NewNop(), args, envVars, processName)
}

func SpawnCmdWithLogger(lg *zap.Logger, args []string, envVars map[string]string, name string) (*expect.ExpectProcess, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}

newArgs, err := additionalArgs()
if err != nil {
return nil, err
}
env := mergeEnvVariables(envVars)
lg.Info("spawning process",
zap.Strings("args", args),
zap.String("working-dir", wd),
zap.String("name", name),
zap.Strings("environment-variables", env))
return expect.NewExpectWithEnv(args[0], append(args[1:], newArgs...), env, name)
}
30 changes: 2 additions & 28 deletions tests/framework/e2e/etcd_spawn_cov.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import (
"time"

"go.etcd.io/etcd/client/pkg/v3/fileutil"
"go.etcd.io/etcd/pkg/v3/expect"
"go.etcd.io/etcd/tests/v3/framework/integration"
"go.uber.org/zap"
)

const noOutputLineCount = 2 // cov-enabled binaries emit PASS and coverage count lines
Expand All @@ -36,6 +34,7 @@ var (

func init() {
initBinPath = initBinPathCov
additionalArgs = additionalArgsCov
}

func initBinPathCov(binDir string) binPath {
Expand All @@ -47,32 +46,7 @@ func initBinPathCov(binDir string) binPath {
}
}

func SpawnCmdWithLogger(lg *zap.Logger, args []string, envVars map[string]string, name string) (*expect.ExpectProcess, error) {
cmd := args[0]
env := mergeEnvVariables(envVars)

wd, err := os.Getwd()
if err != nil {
return nil, err
}

covArgs, err := getCovArgs()
if err != nil {
return nil, err
}
// when withFlagByEnv() is used in testCtl(), env variables for ctl is set to os.env.
// they must be included in ctl_cov_env.

allArgs := append(args[1:], covArgs...)
lg.Info("spawning process in cov test",
zap.Strings("args", args),
zap.String("working-dir", wd),
zap.String("name", name),
zap.Strings("environment-variables", env))
return expect.NewExpectWithEnv(cmd, allArgs, env, name)
}

func getCovArgs() ([]string, error) {
func additionalArgsCov() ([]string, error) {
if !fileutil.Exist(coverDir) {
return nil, fmt.Errorf("could not find coverage folder: %s", coverDir)
}
Expand Down
23 changes: 3 additions & 20 deletions tests/framework/e2e/etcd_spawn_nocov.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@

package e2e

import (
"os"

"go.uber.org/zap"

"go.etcd.io/etcd/pkg/v3/expect"
)

const noOutputLineCount = 0 // regular binaries emit no extra lines

func init() {
initBinPath = initBinPathNoCov
additionalArgs = additionalArgsNoCov
}

func initBinPathNoCov(binDir string) binPath {
Expand All @@ -40,16 +33,6 @@ func initBinPathNoCov(binDir string) binPath {
}
}

func SpawnCmdWithLogger(lg *zap.Logger, args []string, envVars map[string]string, name string) (*expect.ExpectProcess, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
env := mergeEnvVariables(envVars)
lg.Info("spawning process",
zap.Strings("args", args),
zap.String("working-dir", wd),
zap.String("name", name),
zap.Strings("environment-variables", env))
return expect.NewExpectWithEnv(args[0], args[1:], env, name)
func additionalArgsNoCov() ([]string, error) {
return []string{}, nil
}

0 comments on commit 4fa4bc3

Please sign in to comment.