-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7254 from fanminshi/rework_coverage_e2e
e2e: add code coverage to e2e
- Loading branch information
Showing
7 changed files
with
186 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2017 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build cov | ||
|
||
package e2e | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/coreos/etcd/pkg/expect" | ||
"github.com/coreos/etcd/pkg/fileutil" | ||
"github.com/coreos/etcd/pkg/flags" | ||
) | ||
|
||
func spawnCmd(args []string) (*expect.ExpectProcess, error) { | ||
if args[0] == binPath { | ||
coverPath := os.Getenv("COVERDIR") | ||
if !filepath.IsAbs(coverPath) { | ||
// COVERDIR is relative to etcd root but e2e test has its path set to be relative to the e2e folder. | ||
// adding ".." in front of COVERDIR ensures that e2e saves coverage reports to the correct location. | ||
coverPath = filepath.Join("..", coverPath) | ||
} | ||
if !fileutil.Exist(coverPath) { | ||
return nil, fmt.Errorf("could not find coverage folder") | ||
} | ||
covArgs := []string{ | ||
fmt.Sprintf("-test.coverprofile=e2e.%v.coverprofile", time.Now().UnixNano()), | ||
"-test.outputdir=" + coverPath, | ||
} | ||
ep := expect.NewExpectWithEnv(binDir+"/etcd_test", covArgs, args2env(args[1:])) | ||
// ep sends SIGTERM to etcd_test process on ep.close() | ||
// allowing the process to exit gracefully in order to generate a coverage report. | ||
// note: go runtime ignores SIGINT but not SIGTERM | ||
// if e2e test is run as a background process. | ||
ep.StopSignal = syscall.SIGTERM | ||
return nil, ep | ||
} | ||
return expect.NewExpect(args[0], args[1:]...) | ||
} | ||
|
||
func args2env(args []string) []string { | ||
var covEnvs []string | ||
for i := range args[1:] { | ||
if !strings.HasPrefix(args[i], "--") { | ||
continue | ||
} | ||
flag := strings.Split(args[i], "--")[1] | ||
val := "true" | ||
// split the flag that has "=" | ||
// e.g --auto-tls=true" => flag=auto-tls and val=true | ||
if strings.Contains(args[i], "=") { | ||
split := strings.Split(flag, "=") | ||
flag = split[0] | ||
val = split[1] | ||
} | ||
|
||
if i+1 < len(args) { | ||
if !strings.HasPrefix(args[i+1], "--") { | ||
val = args[i+1] | ||
} | ||
} | ||
covEnvs = append(covEnvs, flags.FlagToEnv("ETCD", flag)+"="+val) | ||
} | ||
return covEnvs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2017 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build !cov | ||
|
||
package e2e | ||
|
||
import "github.com/coreos/etcd/pkg/expect" | ||
|
||
func spawnCmd(args []string) (*expect.ExpectProcess, error) { | ||
return expect.NewExpect(args[0], args[1:]...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,9 @@ var ( | |
"snapshot", | ||
"v", | ||
"vv", | ||
// for coverage testing | ||
"test.coverprofile", | ||
"test.outputdir", | ||
} | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2017 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"strings" | ||
"syscall" | ||
"testing" | ||
) | ||
|
||
func TestMain(t *testing.T) { | ||
// don't launch etcd server when invoked via go test | ||
if strings.HasSuffix(os.Args[0], "etcd.test") { | ||
return | ||
} | ||
|
||
notifier := make(chan os.Signal, 1) | ||
signal.Notify(notifier, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL) | ||
go main() | ||
<-notifier | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters