etcd_spawn_cov.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "github.com/coreos/etcd/pkg/expect"
  24. "github.com/coreos/etcd/pkg/fileutil"
  25. "github.com/coreos/etcd/pkg/flags"
  26. )
  27. func spawnCmd(args []string) (*expect.ExpectProcess, error) {
  28. if args[0] == binPath {
  29. coverPath := os.Getenv("COVERDIR")
  30. if !filepath.IsAbs(coverPath) {
  31. // COVERDIR is relative to etcd root but e2e test has its path set to be relative to the e2e folder.
  32. // adding ".." in front of COVERDIR ensures that e2e saves coverage reports to the correct location.
  33. coverPath = filepath.Join("..", coverPath)
  34. }
  35. if !fileutil.Exist(coverPath) {
  36. return nil, fmt.Errorf("could not find coverage folder")
  37. }
  38. covArgs := []string{
  39. fmt.Sprintf("-test.coverprofile=e2e.%v.coverprofile", time.Now().UnixNano()),
  40. "-test.outputdir=" + coverPath,
  41. }
  42. ep, err := expect.NewExpectWithEnv(binDir+"/etcd_test", covArgs, args2env(args[1:]))
  43. if err != nil {
  44. return nil, err
  45. }
  46. // ep sends SIGTERM to etcd_test process on ep.close()
  47. // allowing the process to exit gracefully in order to generate a coverage report.
  48. // note: go runtime ignores SIGINT but not SIGTERM
  49. // if e2e test is run as a background process.
  50. ep.StopSignal = syscall.SIGTERM
  51. return ep, nil
  52. }
  53. return expect.NewExpect(args[0], args[1:]...)
  54. }
  55. func args2env(args []string) []string {
  56. var covEnvs []string
  57. for i := range args[1:] {
  58. if !strings.HasPrefix(args[i], "--") {
  59. continue
  60. }
  61. flag := strings.Split(args[i], "--")[1]
  62. val := "true"
  63. // split the flag that has "="
  64. // e.g --auto-tls=true" => flag=auto-tls and val=true
  65. if strings.Contains(args[i], "=") {
  66. split := strings.Split(flag, "=")
  67. flag = split[0]
  68. val = split[1]
  69. }
  70. if i+1 < len(args) {
  71. if !strings.HasPrefix(args[i+1], "--") {
  72. val = args[i+1]
  73. }
  74. }
  75. covEnvs = append(covEnvs, flags.FlagToEnv("ETCD", flag)+"="+val)
  76. }
  77. return covEnvs
  78. }