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// +build cov 16 17package e2e 18 19import ( 20 "fmt" 21 "os" 22 "path/filepath" 23 "strings" 24 "syscall" 25 "time" 26 27 "go.etcd.io/etcd/pkg/expect" 28 "go.etcd.io/etcd/pkg/fileutil" 29 "go.etcd.io/etcd/pkg/flags" 30) 31 32const noOutputLineCount = 2 // cov-enabled binaries emit PASS and coverage count lines 33 34func spawnCmd(args []string) (*expect.ExpectProcess, error) { 35 if args[0] == binPath { 36 return spawnEtcd(args) 37 } 38 if args[0] == ctlBinPath || args[0] == ctlBinPath+"3" { 39 // avoid test flag conflicts in coverage enabled etcdctl by putting flags in ETCDCTL_ARGS 40 env := []string{ 41 // was \xff, but that's used for testing boundary conditions; 0xe7cd should be safe 42 "ETCDCTL_ARGS=" + strings.Join(args, "\xe7\xcd"), 43 } 44 if args[0] == ctlBinPath+"3" { 45 env = append(env, "ETCDCTL_API=3") 46 } 47 48 covArgs, err := getCovArgs() 49 if err != nil { 50 return nil, err 51 } 52 // when withFlagByEnv() is used in testCtl(), env variables for ctl is set to os.env. 53 // they must be included in ctl_cov_env. 54 env = append(env, os.Environ()...) 55 ep, err := expect.NewExpectWithEnv(binDir+"/etcdctl_test", covArgs, env) 56 if err != nil { 57 return nil, err 58 } 59 ep.StopSignal = syscall.SIGTERM 60 return ep, nil 61 } 62 63 return expect.NewExpect(args[0], args[1:]...) 64} 65 66func spawnEtcd(args []string) (*expect.ExpectProcess, error) { 67 covArgs, err := getCovArgs() 68 if err != nil { 69 return nil, err 70 } 71 72 var env []string 73 if args[1] == "grpc-proxy" { 74 // avoid test flag conflicts in coverage enabled etcd by putting flags in ETCDCOV_ARGS 75 env = append(os.Environ(), "ETCDCOV_ARGS="+strings.Join(args, "\xe7\xcd")) 76 } else { 77 env = args2env(args[1:]) 78 } 79 80 ep, err := expect.NewExpectWithEnv(binDir+"/etcd_test", covArgs, env) 81 if err != nil { 82 return nil, err 83 } 84 // ep sends SIGTERM to etcd_test process on ep.close() 85 // allowing the process to exit gracefully in order to generate a coverage report. 86 // note: go runtime ignores SIGINT but not SIGTERM 87 // if e2e test is run as a background process. 88 ep.StopSignal = syscall.SIGTERM 89 return ep, nil 90} 91 92func getCovArgs() ([]string, error) { 93 coverPath := os.Getenv("COVERDIR") 94 if !filepath.IsAbs(coverPath) { 95 // COVERDIR is relative to etcd root but e2e test has its path set to be relative to the e2e folder. 96 // adding ".." in front of COVERDIR ensures that e2e saves coverage reports to the correct location. 97 coverPath = filepath.Join("../..", coverPath) 98 } 99 if !fileutil.Exist(coverPath) { 100 return nil, fmt.Errorf("could not find coverage folder") 101 } 102 covArgs := []string{ 103 fmt.Sprintf("-test.coverprofile=e2e.%v.coverprofile", time.Now().UnixNano()), 104 "-test.outputdir=" + coverPath, 105 } 106 return covArgs, nil 107} 108 109func args2env(args []string) []string { 110 var covEnvs []string 111 for i := range args { 112 if !strings.HasPrefix(args[i], "--") { 113 continue 114 } 115 flag := strings.Split(args[i], "--")[1] 116 val := "true" 117 // split the flag that has "=" 118 // e.g --auto-tls=true" => flag=auto-tls and val=true 119 if strings.Contains(args[i], "=") { 120 split := strings.Split(flag, "=") 121 flag = split[0] 122 val = split[1] 123 } 124 125 if i+1 < len(args) { 126 if !strings.HasPrefix(args[i+1], "--") { 127 val = args[i+1] 128 } 129 } 130 covEnvs = append(covEnvs, flags.FlagToEnv("ETCD", flag)+"="+val) 131 } 132 return covEnvs 133} 134