etcd_spawn_cov.go 3.7 KB

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