Przeglądaj źródła

*: add and expose StopSignal field in ExpectProcess

add and expose StopSignal to ExpectProcess allows user
to define what signal to send on ExpectProcess.close()

coverage testing code sets StopSignal to SIGTERM allowing
the test binary to shutdown gracefully so that it can generate
a coverage report.
fanmin shi 8 lat temu
rodzic
commit
07129a6370
2 zmienionych plików z 18 dodań i 5 usunięć
  1. 9 3
      e2e/etcd_spawn_cov.go.go
  2. 9 2
      pkg/expect/expect.go

+ 9 - 3
e2e/etcd_spawn_cov.go.go

@@ -19,11 +19,11 @@ package e2e
 import (
 import (
 	"fmt"
 	"fmt"
 	"os"
 	"os"
+	"path/filepath"
 	"strings"
 	"strings"
+	"syscall"
 	"time"
 	"time"
 
 
-	"path/filepath"
-
 	"github.com/coreos/etcd/pkg/expect"
 	"github.com/coreos/etcd/pkg/expect"
 	"github.com/coreos/etcd/pkg/fileutil"
 	"github.com/coreos/etcd/pkg/fileutil"
 	"github.com/coreos/etcd/pkg/flags"
 	"github.com/coreos/etcd/pkg/flags"
@@ -44,7 +44,13 @@ func spawnCmd(args []string) (*expect.ExpectProcess, error) {
 			fmt.Sprintf("-test.coverprofile=e2e.%v.coverprofile", time.Now().UnixNano()),
 			fmt.Sprintf("-test.coverprofile=e2e.%v.coverprofile", time.Now().UnixNano()),
 			"-test.outputdir=" + coverPath,
 			"-test.outputdir=" + coverPath,
 		}
 		}
-		return expect.NewExpectWithEnv(binDir+"/etcd_test", covArgs, args2env(args[1:]))
+		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:]...)
 	return expect.NewExpect(args[0], args[1:]...)
 }
 }

+ 9 - 2
pkg/expect/expect.go

@@ -23,6 +23,7 @@ import (
 	"os/exec"
 	"os/exec"
 	"strings"
 	"strings"
 	"sync"
 	"sync"
+	"syscall"
 
 
 	"github.com/kr/pty"
 	"github.com/kr/pty"
 )
 )
@@ -38,6 +39,9 @@ type ExpectProcess struct {
 	lines []string
 	lines []string
 	count int // increment whenever new line gets added
 	count int // increment whenever new line gets added
 	err   error
 	err   error
+
+	// StopSignal is the signal Stop sends to the process; defaults to SIGKILL.
+	StopSignal os.Signal
 }
 }
 
 
 var printDebugLines = os.Getenv("EXPECT_DEBUG") != ""
 var printDebugLines = os.Getenv("EXPECT_DEBUG") != ""
@@ -52,7 +56,10 @@ func NewExpect(name string, arg ...string) (ep *ExpectProcess, err error) {
 func NewExpectWithEnv(name string, args []string, env []string) (ep *ExpectProcess, err error) {
 func NewExpectWithEnv(name string, args []string, env []string) (ep *ExpectProcess, err error) {
 	cmd := exec.Command(name, args...)
 	cmd := exec.Command(name, args...)
 	cmd.Env = env
 	cmd.Env = env
-	ep = &ExpectProcess{cmd: cmd}
+	ep = &ExpectProcess{
+		cmd:        cmd,
+		StopSignal: syscall.SIGKILL,
+	}
 	ep.cond = sync.NewCond(&ep.mu)
 	ep.cond = sync.NewCond(&ep.mu)
 	ep.cmd.Stderr = ep.cmd.Stdout
 	ep.cmd.Stderr = ep.cmd.Stdout
 	ep.cmd.Stdin = nil
 	ep.cmd.Stdin = nil
@@ -140,7 +147,7 @@ func (ep *ExpectProcess) close(kill bool) error {
 		return ep.err
 		return ep.err
 	}
 	}
 	if kill {
 	if kill {
-		ep.Signal(os.Interrupt)
+		ep.Signal(ep.StopSignal)
 	}
 	}
 
 
 	err := ep.cmd.Wait()
 	err := ep.cmd.Wait()