From 0292a3b4d99a7786f7cb3fbba29046257a042635 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Tue, 19 Jun 2018 15:29:54 -0700 Subject: [PATCH] Merge PR #1291: Delete unused tests * Tweak retry logic on waitForHeight * Add HTTP retry logic to LCD tests * Simplify waitForHeight changes * Update changelog * Add 'make test_cli_retry', 'make test_unit_retry' * Run test_cli_retry in CI * Delete unused tests --- .circleci/config.yml | 2 +- CHANGELOG.md | 1 + Makefile | 6 + tests/tests.go | 321 ------------------------------------------- tests/util.go | 13 +- 5 files changed, 20 insertions(+), 323 deletions(-) delete mode 100644 tests/tests.go diff --git a/.circleci/config.yml b/.circleci/config.yml index 06f5bb03b..a873c8190 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -99,7 +99,7 @@ jobs: name: Test cli command: | export PATH="$GOBIN:$PATH" - make test_cli + make test_cli_retry test_cover: <<: *defaults diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a8615977..b7e56af33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ BREAKING CHANGES FIXES * \#1259 - fix bug where certain tests that could have a nil pointer in defer +* Retry on HTTP request failure in CLI tests, add option to retry tests in Makefile ## 0.19.0 diff --git a/Makefile b/Makefile index f42044669..be593ef35 100644 --- a/Makefile +++ b/Makefile @@ -92,9 +92,15 @@ test: test_unit test_cli: @go test -count 1 -p 1 `go list github.com/cosmos/cosmos-sdk/cmd/gaia/cli_test` +test_cli_retry: + for i in 1 2 3; do make test_cli && break || sleep 2; done + test_unit: @go test $(PACKAGES_NOCLITEST) +test_unit_retry: + for i in 1 2 3; do make test_unit && break || sleep 2; done + test_race: @go test -race $(PACKAGES_NOCLITEST) diff --git a/tests/tests.go b/tests/tests.go deleted file mode 100644 index b4435659b..000000000 --- a/tests/tests.go +++ /dev/null @@ -1,321 +0,0 @@ -package tests - -import ( - "bufio" - "bytes" - "fmt" - "os" - "os/exec" - "path" - "path/filepath" - "strings" - "testing" - "time" - - "github.com/cosmos/cosmos-sdk/server" - "github.com/stretchr/testify/require" -) - -// Tests assume the `basecoind` and `basecli` binaries -// have been built and are located in `./build` - -// TODO remove test dirs if tests are successful - -//nolint -var ( - basecoind = "build/basecoind" - basecli = "build/basecli" - - basecoindDir = "./tmp-basecoind-tests" - basecliDir = "./tmp-basecli-tests" - - ACCOUNTS = []string{"alice", "bob", "charlie", "igor"} - alice = ACCOUNTS[0] - bob = ACCOUNTS[1] - charlie = ACCOUNTS[2] - igor = ACCOUNTS[3] -) - -func gopath() string { - return filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "cosmos", "cosmos-sdk") -} - -func whereIsBasecoind() string { - return filepath.Join(gopath(), basecoind) -} - -func whereIsBasecli() string { - return filepath.Join(gopath(), basecli) -} - -// Init Basecoin Test -func TestInitBasecoin(t *testing.T, home string) string { - var err error - - password := "some-random-password" - - initBasecoind := exec.Command(whereIsBasecoind(), "init", "--home", home) - cmdWriter, err := initBasecoind.StdinPipe() - require.Nil(t, err) - - buf := new(bytes.Buffer) - initBasecoind.Stdout = buf - - if err = initBasecoind.Start(); err != nil { - t.Error(err) - } - - _, err = cmdWriter.Write([]byte(password)) - require.Nil(t, err) - cmdWriter.Close() - - if err = initBasecoind.Wait(); err != nil { - t.Error(err) - } - - // get seed from initialization - theOutput := strings.Split(buf.String(), "\n") - var seedLine int - for _seedLine, o := range theOutput { - if strings.HasPrefix(string(o), "Secret phrase") { - seedLine = _seedLine + 1 - break - } - } - - seed := string(theOutput[seedLine]) - - // enable indexing - err = appendToFile(path.Join(home, "config", "config.toml"), "\n\n[tx_indexing]\nindex_all_tags = true\n") - require.Nil(t, err) - - return seed -} - -func appendToFile(path string, text string) error { - f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0600) - if err != nil { - return err - } - - defer f.Close() - - if _, err = f.WriteString(text); err != nil { - return err - } - - return nil -} - -func makeKeys() error { - for _, acc := range ACCOUNTS { - makeKeys := exec.Command(whereIsBasecli(), "keys", "add", acc, "--home", basecliDir) - cmdWriter, err := makeKeys.StdinPipe() - if err != nil { - return err - } - - makeKeys.Stdout = os.Stdout - if err := makeKeys.Start(); err != nil { - return err - } - cmdWriter.Write([]byte("1234567890")) - if err != nil { - return err - } - cmdWriter.Close() - - if err := makeKeys.Wait(); err != nil { - return err - } - } - - return nil -} - -func _TestSendCoins(t *testing.T) { - if err := StartServer(); err != nil { - t.Error(err) - } - - // send some coins - // [zr] where dafuq do I get a FROM (oh, use --name) - - sendTo := fmt.Sprintf("--to=%s", bob) - sendFrom := fmt.Sprintf("--from=%s", alice) - - cmdOut, err := exec.Command(whereIsBasecli(), "send", sendTo, "--amount=1000mycoin", sendFrom, "--seq=0").Output() - if err != nil { - t.Error(err) - } - - fmt.Printf("sent: %s", string(cmdOut)) - -} - -// expects TestInitBaseCoin to have been run -func StartServer() error { - // straight outta https://nathanleclaire.com/blog/2014/12/29/shelled-out-commands-in-golang/ - cmdName := whereIsBasecoind() - cmdArgs := []string{"start", "--home", basecoindDir} - - cmd := exec.Command(cmdName, cmdArgs...) - cmdReader, err := cmd.StdoutPipe() - if err != nil { - return err - } - - scanner := bufio.NewScanner(cmdReader) - go func() { - for scanner.Scan() { - fmt.Printf("running [basecoind start] %s\n", scanner.Text()) - } - }() - - err = cmd.Start() - if err != nil { - return err - } - - err = cmd.Wait() - if err != nil { - return err - } - - time.Sleep(5 * time.Second) - - return nil - - // TODO return cmd.Process so that we can later do something like: - // cmd.Process.Kill() - // see: https://stackoverflow.com/questions/11886531/terminating-a-process-started-with-os-exec-in-golang -} - -// Init Basecoin Test -func InitServerForTest(t *testing.T) { - Clean() - - var err error - - password := "some-random-password" - usePassword := exec.Command("echo", password) - - initBasecoind := exec.Command(whereIsBasecoind(), "init", "--home", basecoindDir) - - initBasecoind.Stdin, err = usePassword.StdoutPipe() - require.Nil(t, err) - - initBasecoind.Stdout = os.Stdout - - err = initBasecoind.Start() - require.Nil(t, err) - err = usePassword.Run() - require.Nil(t, err) - err = initBasecoind.Wait() - require.Nil(t, err) - - err = makeKeys() - require.Nil(t, err) -} - -// expects TestInitBaseCoin to have been run -func StartNodeServerForTest(t *testing.T, home string) *exec.Cmd { - cmdName := whereIsBasecoind() - cmdArgs := []string{"start", "--home", home} - cmd := exec.Command(cmdName, cmdArgs...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err := cmd.Start() - require.Nil(t, err) - - // FIXME: if there is a nondeterministic node start failure, - // we should probably make this read the logs to wait for RPC - time.Sleep(time.Second * 2) - - return cmd -} - -// expects TestInitBaseCoin to have been run -func StartLCDServerForTest(t *testing.T, home, chainID string) (cmd *exec.Cmd, port string) { - cmdName := whereIsBasecli() - var err error - _, port, err = server.FreeTCPAddr() - require.NoError(t, err) - cmdArgs := []string{ - "rest-server", - "--home", - home, - "--bind", - fmt.Sprintf("localhost:%s", port), - "--chain-id", - chainID, - } - cmd = exec.Command(cmdName, cmdArgs...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err = cmd.Start() - require.Nil(t, err) - time.Sleep(time.Second * 2) // TODO: LOL - return cmd, port -} - -// clean the directories -func Clean() { - // ignore errors b/c the dirs may not yet exist - err := os.Remove(basecoindDir) - panic(err) - err = os.Remove(basecliDir) - panic(err) -} - -/* - - chainID = "staking_test" - testDir = "./tmp_tests" -) - -func runTests() { - - if err := os.Mkdir(testDir, 0666); err != nil { - panic(err) - } - defer os.Remove(testDir) - - // make some keys - - //if err := makeKeys(); err != nil { - // panic(err) - //} - - if err := initServer(); err != nil { - fmt.Printf("Err: %v", err) - panic(err) - } - -} - -func initServer() error { - serveDir := filepath.Join(testDir, "server") - //serverLog := filepath.Join(testDir, "gaia-node.log") - - // get RICH - keyOut, err := exec.Command(GAIA, CLIENT_EXE, "keys", "get", "alice").Output() - if err != nil { - fmt.Println("one") - return err - } - key := strings.Split(string(keyOut), "\t") - fmt.Printf("wit:%s", key[2]) - - outByte, err := exec.Command(GAIA, SERVER_EXE, "init", "--static", fmt.Sprintf("--chain-id=%s", chainID), fmt.Sprintf("--home=%s", serveDir), key[2]).Output() - if err != nil { - fmt.Println("teo") - fmt.Printf("Error: %v", err) - - return err - } - fmt.Sprintf("OUT: %s", string(outByte)) - return nil -} - -*/ diff --git a/tests/util.go b/tests/util.go index 387ccb769..9e2c0d044 100644 --- a/tests/util.go +++ b/tests/util.go @@ -64,6 +64,17 @@ func WaitForHeight(height int64, port string) { waitForHeight(height, url) } +// Whether or not an HTTP status code was "successful" +func StatusOK(statusCode int) bool { + switch statusCode { + case http.StatusOK: + case http.StatusCreated: + case http.StatusNoContent: + return true + } + return false +} + func waitForHeight(height int64, url string) { for { // get url, try a few times @@ -71,7 +82,7 @@ func waitForHeight(height int64, url string) { var err error for i := 0; i < 5; i++ { res, err = http.Get(url) - if err == nil { + if err == nil && StatusOK(res.StatusCode) { break } time.Sleep(time.Millisecond * 200)