github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/framework/e2e/etcd_spawn_cov.go (about)

     1  // Copyright 2017 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build cov
    16  // +build cov
    17  
    18  package e2e
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/lfch/etcd-io/client/pkg/v3/fileutil"
    27  	"github.com/lfch/etcd-io/pkg/v3/expect"
    28  	"github.com/lfch/etcd-io/tests/v3/framework/integration"
    29  	"go.uber.org/zap"
    30  )
    31  
    32  const noOutputLineCount = 2 // cov-enabled binaries emit PASS and coverage count lines
    33  
    34  var (
    35  	coverDir = integration.MustAbsPath(os.Getenv("COVERDIR"))
    36  )
    37  
    38  func SpawnCmdWithLogger(lg *zap.Logger, args []string, envVars map[string]string, name string) (*expect.ExpectProcess, error) {
    39  	cmd := args[0]
    40  	env := mergeEnvVariables(envVars)
    41  	switch {
    42  	case strings.HasSuffix(cmd, "/etcd"):
    43  		cmd = cmd + "_test"
    44  	case strings.HasSuffix(cmd, "/etcdctl"):
    45  		cmd = cmd + "_test"
    46  	case strings.HasSuffix(cmd, "/etcdutl"):
    47  		cmd = cmd + "_test"
    48  	case strings.HasSuffix(cmd, "/etcdctl3"):
    49  		cmd = CtlBinPath + "_test"
    50  		env = append(env, "ETCDCTL_API=3")
    51  	}
    52  
    53  	wd, err := os.Getwd()
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	covArgs, err := getCovArgs()
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	// when withFlagByEnv() is used in testCtl(), env variables for ctl is set to os.env.
    63  	// they must be included in ctl_cov_env.
    64  
    65  	allArgs := append(args[1:], covArgs...)
    66  	lg.Info("spawning process in cov test",
    67  		zap.Strings("args", args),
    68  		zap.String("working-dir", wd),
    69  		zap.String("name", name),
    70  		zap.Strings("environment-variables", env))
    71  	return expect.NewExpectWithEnv(cmd, allArgs, env, name)
    72  }
    73  
    74  func getCovArgs() ([]string, error) {
    75  	if !fileutil.Exist(coverDir) {
    76  		return nil, fmt.Errorf("could not find coverage folder: %s", coverDir)
    77  	}
    78  	covArgs := []string{
    79  		fmt.Sprintf("-test.coverprofile=e2e.%v.coverprofile", time.Now().UnixNano()),
    80  		"-test.outputdir=" + coverDir,
    81  	}
    82  	return covArgs, nil
    83  }