From 2f2072fd4e92bf52c7cf01affd66c669b730de81 Mon Sep 17 00:00:00 2001 From: maskpp Date: Tue, 14 Mar 2023 16:47:26 +0800 Subject: [PATCH] fix(docker cmd): fix bug in docker cmd (#364) --- bridge/cmd/app/app_test.go | 4 +- common/cmd/cmd.go | 39 ++++++++--------- common/cmd/cmd_app.go | 49 +++++++++++----------- common/cmd/cmd_test.go | 2 +- common/docker/docker_app.go | 10 ++--- common/docker/docker_db.go | 5 +-- common/docker/docker_geth.go | 5 +-- coordinator/cmd/app/app_test.go | 4 +- database/cmd/app/app_test.go | 4 +- roller/cmd/app/app_test.go | 4 +- tests/integration-test/common.go | 14 +++---- tests/integration-test/integration_test.go | 10 ++--- 12 files changed, 73 insertions(+), 77 deletions(-) diff --git a/bridge/cmd/app/app_test.go b/bridge/cmd/app/app_test.go index b2adaf2bf..64701eba2 100644 --- a/bridge/cmd/app/app_test.go +++ b/bridge/cmd/app/app_test.go @@ -10,10 +10,10 @@ import ( ) func TestRunBridge(t *testing.T) { - bridge := cmd.NewCmd(t, "bridge-test", "--version") + bridge := cmd.NewCmd("bridge-test", "--version") defer bridge.WaitExit() // wait result - bridge.ExpectWithTimeout(true, time.Second*3, fmt.Sprintf("bridge version %s", version.Version)) + bridge.ExpectWithTimeout(t, true, time.Second*3, fmt.Sprintf("bridge version %s", version.Version)) bridge.RunApp(nil) } diff --git a/common/cmd/cmd.go b/common/cmd/cmd.go index e67b4db4e..4cc754977 100644 --- a/common/cmd/cmd.go +++ b/common/cmd/cmd.go @@ -1,11 +1,11 @@ package cmd import ( + "fmt" "os" "os/exec" "strings" "sync" - "testing" cmap "github.com/orcaman/concurrent-map" ) @@ -23,8 +23,6 @@ type checkFunc func(buf string) // Cmd struct type Cmd struct { - *testing.T - name string args []string @@ -38,9 +36,8 @@ type Cmd struct { } // NewCmd create Cmd instance. -func NewCmd(t *testing.T, name string, args ...string) *Cmd { +func NewCmd(name string, args ...string) *Cmd { return &Cmd{ - T: t, checkFuncs: cmap.New(), name: name, args: args, @@ -48,40 +45,40 @@ func NewCmd(t *testing.T, name string, args ...string) *Cmd { } // RegistFunc register check func -func (t *Cmd) RegistFunc(key string, check checkFunc) { - t.checkFuncs.Set(key, check) +func (c *Cmd) RegistFunc(key string, check checkFunc) { + c.checkFuncs.Set(key, check) } // UnRegistFunc unregister check func -func (t *Cmd) UnRegistFunc(key string) { - t.checkFuncs.Pop(key) +func (c *Cmd) UnRegistFunc(key string) { + c.checkFuncs.Pop(key) } -func (t *Cmd) runCmd() { - cmd := exec.Command(t.args[0], t.args[1:]...) //nolint:gosec - cmd.Stdout = t - cmd.Stderr = t +func (c *Cmd) runCmd() { + cmd := exec.Command(c.args[0], c.args[1:]...) //nolint:gosec + cmd.Stdout = c + cmd.Stderr = c _ = cmd.Run() } // RunCmd parallel running when parallel is true. -func (t *Cmd) RunCmd(parallel bool) { - t.Log("cmd: ", t.args) +func (c *Cmd) RunCmd(parallel bool) { + fmt.Println("cmd: ", c.args) if parallel { - go t.runCmd() + go c.runCmd() } else { - t.runCmd() + c.runCmd() } } -func (t *Cmd) Write(data []byte) (int, error) { +func (c *Cmd) Write(data []byte) (int, error) { out := string(data) if verbose { - t.Logf("%s: %v", t.name, out) + fmt.Printf("%s: %v", c.name, out) } else if strings.Contains(out, "error") || strings.Contains(out, "warning") { - t.Logf("%s: %v", t.name, out) + fmt.Printf("%s: %v", c.name, out) } - go t.checkFuncs.IterCb(func(_ string, value interface{}) { + go c.checkFuncs.IterCb(func(_ string, value interface{}) { check := value.(checkFunc) check(out) }) diff --git a/common/cmd/cmd_app.go b/common/cmd/cmd_app.go index 031fcaf61..cc02e2561 100644 --- a/common/cmd/cmd_app.go +++ b/common/cmd/cmd_app.go @@ -5,6 +5,7 @@ import ( "os" "os/exec" "strings" + "testing" "time" "github.com/docker/docker/pkg/reexec" @@ -13,13 +14,13 @@ import ( // RunApp exec's the current binary using name as argv[0] which will trigger the // reexec init function for that name (e.g. "geth-test" in cmd/geth/run_test.go) -func (t *Cmd) RunApp(waitResult func() bool) { - t.Log("cmd: ", append([]string{t.name}, t.args...)) +func (c *Cmd) RunApp(waitResult func() bool) { + fmt.Println("cmd: ", append([]string{c.name}, c.args...)) cmd := &exec.Cmd{ Path: reexec.Self(), - Args: append([]string{t.name}, t.args...), - Stderr: t, - Stdout: t, + Args: append([]string{c.name}, c.args...), + Stderr: c, + Stdout: c, } if waitResult != nil { go func() { @@ -30,39 +31,39 @@ func (t *Cmd) RunApp(waitResult func() bool) { _ = cmd.Run() } - t.mu.Lock() - t.cmd = cmd - t.mu.Unlock() + c.mu.Lock() + c.cmd = cmd + c.mu.Unlock() } // WaitExit wait util process exit. -func (t *Cmd) WaitExit() { +func (c *Cmd) WaitExit() { // Wait all the check funcs are finished or test status is failed. - for !(t.Failed() || t.checkFuncs.IsEmpty()) { + for !(c.Err != nil || c.checkFuncs.IsEmpty()) { <-time.After(time.Millisecond * 500) } // Send interrupt signal. - t.mu.Lock() - _ = t.cmd.Process.Signal(os.Interrupt) - _, _ = t.cmd.Process.Wait() - t.mu.Unlock() + c.mu.Lock() + _ = c.cmd.Process.Signal(os.Interrupt) + _, _ = c.cmd.Process.Wait() + c.mu.Unlock() } // Interrupt send interrupt signal. -func (t *Cmd) Interrupt() { - t.mu.Lock() - t.Err = t.cmd.Process.Signal(os.Interrupt) - t.mu.Unlock() +func (c *Cmd) Interrupt() { + c.mu.Lock() + c.Err = c.cmd.Process.Signal(os.Interrupt) + c.mu.Unlock() } // WaitResult return true when get the keyword during timeout. -func (t *Cmd) WaitResult(timeout time.Duration, keyword string) bool { +func (c *Cmd) WaitResult(t *testing.T, timeout time.Duration, keyword string) bool { if keyword == "" { return false } okCh := make(chan struct{}, 1) - t.RegistFunc(keyword, func(buf string) { + c.RegistFunc(keyword, func(buf string) { if strings.Contains(buf, keyword) { select { case okCh <- struct{}{}: @@ -71,7 +72,7 @@ func (t *Cmd) WaitResult(timeout time.Duration, keyword string) bool { } } }) - defer t.UnRegistFunc(keyword) + defer c.UnRegistFunc(keyword) select { case <-okCh: return true @@ -82,12 +83,12 @@ func (t *Cmd) WaitResult(timeout time.Duration, keyword string) bool { } // ExpectWithTimeout wait result during timeout time. -func (t *Cmd) ExpectWithTimeout(parallel bool, timeout time.Duration, keyword string) { +func (c *Cmd) ExpectWithTimeout(t *testing.T, parallel bool, timeout time.Duration, keyword string) { if keyword == "" { return } okCh := make(chan struct{}, 1) - t.RegistFunc(keyword, func(buf string) { + c.RegistFunc(keyword, func(buf string) { if strings.Contains(buf, keyword) { select { case okCh <- struct{}{}: @@ -98,7 +99,7 @@ func (t *Cmd) ExpectWithTimeout(parallel bool, timeout time.Duration, keyword st }) waitResult := func() { - defer t.UnRegistFunc(keyword) + defer c.UnRegistFunc(keyword) select { case <-okCh: return diff --git a/common/cmd/cmd_test.go b/common/cmd/cmd_test.go index bc24e0e52..fcb6f168e 100644 --- a/common/cmd/cmd_test.go +++ b/common/cmd/cmd_test.go @@ -12,7 +12,7 @@ import ( ) func TestCmd(t *testing.T) { - app := cmd.NewCmd(t, "curTime", "date", "+%Y-%m-%d") + app := cmd.NewCmd("curTime", "date", "+%Y-%m-%d") tm := time.Now() curTime := fmt.Sprintf("%d-%02d-%02d", tm.Year(), tm.Month(), tm.Day()) diff --git a/common/docker/docker_app.go b/common/docker/docker_app.go index 834c43421..98d0931fc 100644 --- a/common/docker/docker_app.go +++ b/common/docker/docker_app.go @@ -72,11 +72,11 @@ func (b *App) runDBImage(t *testing.T) { // RunDBApp runs DB app with command func (b *App) RunDBApp(t *testing.T, option, keyword string) { args := []string{option, "--config", b.dbFile} - app := cmd.NewCmd(t, "db_cli-test", args...) + app := cmd.NewCmd("db_cli-test", args...) defer app.WaitExit() // Wait expect result. - app.ExpectWithTimeout(true, time.Second*3, keyword) + app.ExpectWithTimeout(t, true, time.Second*3, keyword) app.RunApp(nil) } @@ -180,7 +180,7 @@ func (b *App) mockDBConfig() error { func newTestL1Docker(t *testing.T) ImgInstance { id, _ := rand.Int(rand.Reader, big.NewInt(2000)) - imgL1geth := NewImgGeth(t, "scroll_l1geth", "", "", 0, l1StartPort+int(id.Int64())) + imgL1geth := NewImgGeth("scroll_l1geth", "", "", 0, l1StartPort+int(id.Int64())) assert.NoError(t, imgL1geth.Start()) // try 3 times to get chainID until is ok. @@ -199,7 +199,7 @@ func newTestL1Docker(t *testing.T) ImgInstance { func newTestL2Docker(t *testing.T) ImgInstance { id, _ := rand.Int(rand.Reader, big.NewInt(2000)) - imgL2geth := NewImgGeth(t, "scroll_l2geth", "", "", 0, l2StartPort+int(id.Int64())) + imgL2geth := NewImgGeth("scroll_l2geth", "", "", 0, l2StartPort+int(id.Int64())) assert.NoError(t, imgL2geth.Start()) // try 3 times to get chainID until is ok. @@ -218,7 +218,7 @@ func newTestL2Docker(t *testing.T) ImgInstance { func newTestDBDocker(t *testing.T, driverName string) ImgInstance { id, _ := rand.Int(rand.Reader, big.NewInt(2000)) - imgDB := NewImgDB(t, driverName, "123456", "test_db", dbStartPort+int(id.Int64())) + imgDB := NewImgDB(driverName, "123456", "test_db", dbStartPort+int(id.Int64())) assert.NoError(t, imgDB.Start()) // try 5 times until the db is ready. diff --git a/common/docker/docker_db.go b/common/docker/docker_db.go index b6cf388ec..54bb521de 100644 --- a/common/docker/docker_db.go +++ b/common/docker/docker_db.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "strings" - "testing" "time" "github.com/docker/docker/api/types" @@ -28,7 +27,7 @@ type ImgDB struct { } // NewImgDB return postgres db img instance. -func NewImgDB(t *testing.T, image, password, dbName string, port int) ImgInstance { +func NewImgDB(image, password, dbName string, port int) ImgInstance { img := &ImgDB{ image: image, name: fmt.Sprintf("%s-%s_%d", image, dbName, port), @@ -36,7 +35,7 @@ func NewImgDB(t *testing.T, image, password, dbName string, port int) ImgInstanc dbName: dbName, port: port, } - img.cmd = cmd.NewCmd(t, img.name, img.prepare()...) + img.cmd = cmd.NewCmd(img.name, img.prepare()...) return img } diff --git a/common/docker/docker_geth.go b/common/docker/docker_geth.go index 45b8f39ca..0e563ccb2 100644 --- a/common/docker/docker_geth.go +++ b/common/docker/docker_geth.go @@ -5,7 +5,6 @@ import ( "fmt" "strconv" "strings" - "testing" "time" "github.com/docker/docker/api/types" @@ -30,7 +29,7 @@ type ImgGeth struct { } // NewImgGeth return geth img instance. -func NewImgGeth(t *testing.T, image, volume, ipc string, hPort, wPort int) ImgInstance { +func NewImgGeth(image, volume, ipc string, hPort, wPort int) ImgInstance { img := &ImgGeth{ image: image, name: fmt.Sprintf("%s-%d", image, time.Now().Nanosecond()), @@ -39,7 +38,7 @@ func NewImgGeth(t *testing.T, image, volume, ipc string, hPort, wPort int) ImgIn httpPort: hPort, wsPort: wPort, } - img.cmd = cmd.NewCmd(t, img.name, img.prepare()...) + img.cmd = cmd.NewCmd(img.name, img.prepare()...) return img } diff --git a/coordinator/cmd/app/app_test.go b/coordinator/cmd/app/app_test.go index f711ab0c3..146fbc863 100644 --- a/coordinator/cmd/app/app_test.go +++ b/coordinator/cmd/app/app_test.go @@ -10,10 +10,10 @@ import ( ) func TestRunCoordinator(t *testing.T) { - coordinator := cmd.NewCmd(t, "coordinator-test", "--version") + coordinator := cmd.NewCmd("coordinator-test", "--version") defer coordinator.WaitExit() // wait result - coordinator.ExpectWithTimeout(true, time.Second*3, fmt.Sprintf("coordinator version %s", version.Version)) + coordinator.ExpectWithTimeout(t, true, time.Second*3, fmt.Sprintf("coordinator version %s", version.Version)) coordinator.RunApp(nil) } diff --git a/database/cmd/app/app_test.go b/database/cmd/app/app_test.go index 5d7d8f861..1be758250 100644 --- a/database/cmd/app/app_test.go +++ b/database/cmd/app/app_test.go @@ -10,10 +10,10 @@ import ( ) func TestRunDatabase(t *testing.T) { - dbcli := cmd.NewCmd(t, "db_cli-test", "--version") + dbcli := cmd.NewCmd("db_cli-test", "--version") defer dbcli.WaitExit() // wait result - dbcli.ExpectWithTimeout(true, time.Second*3, fmt.Sprintf("db_cli version %s", version.Version)) + dbcli.ExpectWithTimeout(t, true, time.Second*3, fmt.Sprintf("db_cli version %s", version.Version)) dbcli.RunApp(nil) } diff --git a/roller/cmd/app/app_test.go b/roller/cmd/app/app_test.go index e4987f867..da7842f92 100644 --- a/roller/cmd/app/app_test.go +++ b/roller/cmd/app/app_test.go @@ -10,10 +10,10 @@ import ( ) func TestRunRoller(t *testing.T) { - roller := cmd.NewCmd(t, "roller-test", "--version") + roller := cmd.NewCmd("roller-test", "--version") defer roller.WaitExit() // wait result - roller.ExpectWithTimeout(true, time.Second*3, fmt.Sprintf("roller version %s", version.Version)) + roller.ExpectWithTimeout(t, true, time.Second*3, fmt.Sprintf("roller version %s", version.Version)) roller.RunApp(nil) } diff --git a/tests/integration-test/common.go b/tests/integration-test/common.go index 3e11c5e80..2e23982ba 100644 --- a/tests/integration-test/common.go +++ b/tests/integration-test/common.go @@ -75,36 +75,36 @@ func free(t *testing.T) { } type appAPI interface { - WaitResult(timeout time.Duration, keyword string) bool + WaitResult(t *testing.T, timeout time.Duration, keyword string) bool RunApp(waitResult func() bool) WaitExit() - ExpectWithTimeout(parallel bool, timeout time.Duration, keyword string) + ExpectWithTimeout(t *testing.T, parallel bool, timeout time.Duration, keyword string) } func runBridgeApp(t *testing.T, args ...string) appAPI { args = append(args, "--log.debug", "--config", bridgeFile) - return cmd.NewCmd(t, "bridge-test", args...) + return cmd.NewCmd("bridge-test", args...) } func runCoordinatorApp(t *testing.T, args ...string) appAPI { args = append(args, "--log.debug", "--config", coordinatorFile, "--ws", "--ws.port", strconv.Itoa(int(wsPort))) // start process - return cmd.NewCmd(t, "coordinator-test", args...) + return cmd.NewCmd("coordinator-test", args...) } func runDBCliApp(t *testing.T, option, keyword string) { args := []string{option, "--config", dbFile} - app := cmd.NewCmd(t, "db_cli-test", args...) + app := cmd.NewCmd("db_cli-test", args...) defer app.WaitExit() // Wait expect result. - app.ExpectWithTimeout(true, time.Second*3, keyword) + app.ExpectWithTimeout(t, true, time.Second*3, keyword) app.RunApp(nil) } func runRollerApp(t *testing.T, args ...string) appAPI { args = append(args, "--log.debug", "--config", rollerFile) - return cmd.NewCmd(t, "roller-test", args...) + return cmd.NewCmd("roller-test", args...) } func runSender(t *testing.T, endpoint string) *sender.Sender { diff --git a/tests/integration-test/integration_test.go b/tests/integration-test/integration_test.go index fababbd6a..5e3fba65c 100644 --- a/tests/integration-test/integration_test.go +++ b/tests/integration-test/integration_test.go @@ -42,16 +42,16 @@ func testStartProcess(t *testing.T) { // Start bridge process. bridgeCmd := runBridgeApp(t) - bridgeCmd.RunApp(func() bool { return bridgeCmd.WaitResult(time.Second*20, "Start bridge successfully") }) + bridgeCmd.RunApp(func() bool { return bridgeCmd.WaitResult(t, time.Second*20, "Start bridge successfully") }) // Start coordinator process. coordinatorCmd := runCoordinatorApp(t, "--ws", "--ws.port", "8391") - coordinatorCmd.RunApp(func() bool { return coordinatorCmd.WaitResult(time.Second*20, "Start coordinator successfully") }) + coordinatorCmd.RunApp(func() bool { return coordinatorCmd.WaitResult(t, time.Second*20, "Start coordinator successfully") }) // Start roller process. rollerCmd := runRollerApp(t) - rollerCmd.ExpectWithTimeout(true, time.Second*60, "register to coordinator successfully!") - rollerCmd.RunApp(func() bool { return rollerCmd.WaitResult(time.Second*40, "roller start successfully") }) + rollerCmd.ExpectWithTimeout(t, true, time.Second*60, "register to coordinator successfully!") + rollerCmd.RunApp(func() bool { return rollerCmd.WaitResult(t, time.Second*40, "roller start successfully") }) rollerCmd.WaitExit() bridgeCmd.WaitExit() @@ -67,7 +67,7 @@ func testMonitorMetrics(t *testing.T) { port, _ := rand.Int(rand.Reader, big.NewInt(2000)) svrPort := strconv.FormatInt(port.Int64()+50000, 10) bridgeCmd := runBridgeApp(t, "--metrics", "--metrics.addr", "localhost", "--metrics.port", svrPort) - bridgeCmd.RunApp(func() bool { return bridgeCmd.WaitResult(time.Second*20, "Start bridge successfully") }) + bridgeCmd.RunApp(func() bool { return bridgeCmd.WaitResult(t, time.Second*20, "Start bridge successfully") }) // Get monitor metrics. resp, err := http.Get("http://localhost:" + svrPort)