Skip to content

Commit

Permalink
Merge pull request etcd-io#12274 from ptabor/20200907-fix-cov-e2e-tests
Browse files Browse the repository at this point in the history
tests/e2e,etcdctl,etcdmain: Fix go test --tags cov -v ./tests/e2e
  • Loading branch information
jpbetz authored Sep 9, 2020
2 parents 10fa961 + 7c880e5 commit e81cae7
Show file tree
Hide file tree
Showing 20 changed files with 182 additions and 283 deletions.
7 changes: 5 additions & 2 deletions etcdctl/ctlv2/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/urfave/cli"
)

func Start() {
func Start() error {
app := cli.NewApp()
app.Name = "etcdctl"
app.Version = version.Version
Expand Down Expand Up @@ -72,8 +72,11 @@ func Start() {
command.NewRoleCommands(),
command.NewAuthCommands(),
}
return app.Run(os.Args)
}

err := runCtlV2(app)
func MustStart() {
err := Start()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
28 changes: 0 additions & 28 deletions etcdctl/ctlv2/ctl_cov.go

This file was deleted.

27 changes: 0 additions & 27 deletions etcdctl/ctlv2/ctl_nocov.go

This file was deleted.

13 changes: 13 additions & 0 deletions etcdctl/ctlv3/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ func init() {
)
}

func Start() error {
rootCmd.SetUsageFunc(usageFunc)
// Make help just show the usage
rootCmd.SetHelpTemplate(`{{.UsageString}}`)
return rootCmd.Execute()
}

func MustStart() {
if err := Start(); err != nil {
command.ExitWithError(command.ExitError, err)
}
}

func init() {
cobra.EnablePrefixMatching = true
}
34 changes: 0 additions & 34 deletions etcdctl/ctlv3/ctl_cov.go

This file was deleted.

28 changes: 0 additions & 28 deletions etcdctl/ctlv3/ctl_nocov.go

This file was deleted.

30 changes: 27 additions & 3 deletions etcdctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,44 @@ const (
apiEnv = "ETCDCTL_API"
)

/**
mainWithError is fully analogous to main, but instead of signaling errors
by os.Exit, it exposes the error explicitly, such that test-logic can intercept
control to e.g. dump coverage data (even for test-for-failure scenarios).
*/
func mainWithError() error {
apiv := os.Getenv(apiEnv)

// unset apiEnv to avoid side-effect for future env and flag parsing.
os.Unsetenv(apiEnv)

if len(apiv) == 0 || apiv == "3" {
return ctlv3.Start()
}

if apiv == "2" {
return ctlv2.Start()
}

fmt.Fprintf(os.Stderr, "unsupported API version: %s\n", apiv)
return fmt.Errorf("unsupported API version: %s", apiv)
}

func main() {
apiv := os.Getenv(apiEnv)

// unset apiEnv to avoid side-effect for future env and flag parsing.
os.Unsetenv(apiEnv)
if len(apiv) == 0 || apiv == "3" {
ctlv3.Start()
ctlv3.MustStart()
return
}

if apiv == "2" {
ctlv2.Start()
ctlv2.MustStart()
return
}

fmt.Fprintln(os.Stderr, "unsupported API version", apiv)
fmt.Fprintf(os.Stderr, "unsupported API version: %v\n", apiv)
os.Exit(1)
}
41 changes: 39 additions & 2 deletions etcdctl/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,52 @@
package main

import (
"log"
"os"
"strings"
"testing"
)

func TestMain(t *testing.T) {
func SplitTestArgs(args []string) (testArgs, appArgs []string) {
for i, arg := range os.Args {
switch {
case strings.HasPrefix(arg, "-test."):
testArgs = append(testArgs, arg)
case i == 0:
appArgs = append(appArgs, arg)
testArgs = append(testArgs, arg)
default:
appArgs = append(appArgs, arg)
}
}
return
}

// Empty test to avoid no-tests warning.
func TestEmpty(t *testing.T) {}

/**
* The purpose of this "test" is to run etcdctl with code-coverage
* collection turned on. The technique is documented here:
*
* https://www.cyphar.com/blog/post/20170412-golang-integration-coverage
*/
func TestMain(m *testing.M) {
// don't launch etcdctl when invoked via go test
if strings.HasSuffix(os.Args[0], "etcdctl.test") {
return
}
main()

testArgs, appArgs := SplitTestArgs(os.Args)

os.Args = appArgs

err := mainWithError()
if err != nil {
log.Fatalf("etcdctl failed with: %v", err)
}

// This will generate coverage files:
os.Args = testArgs
m.Run()
}
5 changes: 3 additions & 2 deletions etcdmain/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ var (
dirEmpty = dirType("empty")
)

func startEtcdOrProxyV2() {
func startEtcdOrProxyV2(args []string) {
grpc.EnableTracing = false

cfg := newConfig()
defaultInitialCluster := cfg.ec.InitialCluster

err := cfg.parse(os.Args[1:])
err := cfg.parse(args[1:])
lg := cfg.ec.GetLogger()
if lg == nil {
var zapError error
Expand All @@ -66,6 +66,7 @@ func startEtcdOrProxyV2() {
os.Exit(1)
}
}
lg.Info("Running: ", zap.Strings("args", args))
if err != nil {
lg.Warn("failed to verify flags", zap.Error(err))
switch err {
Expand Down
3 changes: 3 additions & 0 deletions etcdmain/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ func startGateway(cmd *cobra.Command, args []string) {
os.Exit(1)
}

// We use os.Args to show all the arguments (not only passed-through Cobra).
lg.Info("Running: ", zap.Strings("args", os.Args))

srvs := discoverEndpoints(lg, gatewayDNSCluster, gatewayCA, gatewayInsecureDiscovery, gatewayDNSClusterServiceName)
if len(srvs.Endpoints) == 0 {
// no endpoints discovered, fall back to provided endpoints
Expand Down
14 changes: 4 additions & 10 deletions etcdmain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,16 @@ package etcdmain
import (
"fmt"
"os"
"strings"

"github.com/coreos/go-systemd/v22/daemon"
"go.uber.org/zap"
)

func Main() {
func Main(args []string) {
checkSupportArch()

if len(os.Args) > 1 {
cmd := os.Args[1]
if covArgs := os.Getenv("ETCDCOV_ARGS"); len(covArgs) > 0 {
args := strings.Split(os.Getenv("ETCDCOV_ARGS"), "\xe7\xcd")[1:]
rootCmd.SetArgs(args)
cmd = "grpc-proxy"
}
if len(args) > 1 {
cmd := args[1]
switch cmd {
case "gateway", "grpc-proxy":
if err := rootCmd.Execute(); err != nil {
Expand All @@ -43,7 +37,7 @@ func Main() {
}
}

startEtcdOrProxyV2()
startEtcdOrProxyV2(args)
}

func notifySystemd(lg *zap.Logger) {
Expand Down
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
//
package main

import "go.etcd.io/etcd/v3/etcdmain"
import (
"os"

"go.etcd.io/etcd/v3/etcdmain"
)

func main() {
etcdmain.Main()
etcdmain.Main(os.Args)
}
Loading

0 comments on commit e81cae7

Please sign in to comment.