Skip to content

Commit

Permalink
Merge PR #3553: Code cleanup, take #1
Browse files Browse the repository at this point in the history
* add a bunch of tests, add DONTCOVER text tag

- Also fix flaky test (closes: #3559). Don't test values
  returned by queries since there's no way to query a
  specific height via REST.

* GetTempDir -> NewTestCaseDir
  • Loading branch information
alessio authored and cwgoes committed Feb 8, 2019
1 parent 2c9a5bc commit e7e3c32
Show file tree
Hide file tree
Showing 32 changed files with 225 additions and 102 deletions.
8 changes: 8 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ jobs:
for prof in $(ls /tmp/workspace/profiles/); do
tail -n +2 /tmp/workspace/profiles/"$prof" >> coverage.txt
done
- run:
name: filter out DONTCOVER
command: |
excludelist="$(find ./ -type f -name '*.go' | xargs grep -l 'DONTCOVER' | xargs realpath --relative-to=$GOPATH/src)"
for filename in ${excludelist}; do
echo "Excluding ${filename} ..."
sed -i "\%${filename}:%d" coverage.txt
done
- run:
name: upload
command: bash <(curl -s https://codecov.io/bash) -f coverage.txt
Expand Down
3 changes: 2 additions & 1 deletion client/context/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (

"strings"

"github.com/cosmos/cosmos-sdk/store/rootmulti"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/merkle"
cmn "github.com/tendermint/tendermint/libs/common"
tmliteErr "github.com/tendermint/tendermint/lite/errors"
tmliteProxy "github.com/tendermint/tendermint/lite/proxy"
rpcclient "github.com/tendermint/tendermint/rpc/client"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/store/rootmulti"
)

// GetNode returns an RPC client. If the context's client is not defined, an
Expand Down
3 changes: 1 addition & 2 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ func Test_runAddCmdBasic(t *testing.T) {
assert.EqualError(t, err, "EOF")

// Prepare a keybase
kbHome, kbCleanUp, err := tests.GetTempDir("Test_runDeleteCmd")
assert.NoError(t, err)
kbHome, kbCleanUp := tests.NewTestCaseDir(t)
assert.NotNil(t, kbHome)
defer kbCleanUp()
viper.Set(cli.HomeFlag, kbHome)
Expand Down
3 changes: 1 addition & 2 deletions client/keys/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ func Test_runDeleteCmd(t *testing.T) {
fakeKeyName2 := "runDeleteCmd_Key2"

// Now add a temporary keybase
kbHome, cleanUp, err := tests.GetTempDir("Test_runDeleteCmd")
assert.NoError(t, err)
kbHome, cleanUp := tests.NewTestCaseDir(t)
defer cleanUp()
viper.Set(cli.HomeFlag, kbHome)

Expand Down
6 changes: 2 additions & 4 deletions client/keys/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ func Test_runListCmd(t *testing.T) {
cmdBasic := listKeysCmd()

// Prepare some keybases
kbHome1, cleanUp1, err := tests.GetTempDir("Test_runListCmd")
kbHome1, cleanUp1 := tests.NewTestCaseDir(t)
defer cleanUp1()
assert.NoError(t, err)
// Do nothing, leave home1 empty

kbHome2, cleanUp2, err := tests.GetTempDir("Test_runListCmd")
kbHome2, cleanUp2 := tests.NewTestCaseDir(t)
defer cleanUp2()
viper.Set(cli.HomeFlag, kbHome2)
assert.NoError(t, err)

kb, err := NewKeyBaseFromHomeFlag()
assert.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion client/keys/mnemonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"fmt"

bip39 "github.com/bartekn/go-bip39"
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
)

const (
Expand Down
3 changes: 1 addition & 2 deletions client/keys/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ func Test_runShowCmd(t *testing.T) {

// Prepare a key base
// Now add a temporary keybase
kbHome, cleanUp, err := tests.GetTempDir("Test_runShowCmd")
assert.NoError(t, err)
kbHome, cleanUp := tests.NewTestCaseDir(t)
defer cleanUp()
viper.Set(cli.HomeFlag, kbHome)

Expand Down
3 changes: 1 addition & 2 deletions client/keys/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func Test_runUpdateCmd(t *testing.T) {

// Prepare a key base
// Now add a temporary keybase
kbHome, cleanUp1, err := tests.GetTempDir("Test_runShowCmd")
assert.NoError(t, err)
kbHome, cleanUp1 := tests.NewTestCaseDir(t)
defer cleanUp1()
viper.Set(cli.HomeFlag, kbHome)

Expand Down
16 changes: 3 additions & 13 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,8 @@ func TestBonding(t *testing.T) {
require.NoError(t, err)
addr, _ := CreateAddr(t, name1, pw, kb)

cleanup, valPubKeys, operAddrs, port := InitializeTestLCD(t, 2, []sdk.AccAddress{addr}, true)
cleanup, valPubKeys, operAddrs, port := InitializeTestLCD(t, 2, []sdk.AccAddress{addr}, false)
tests.WaitForHeight(1, port)
defer cleanup()

require.Equal(t, 2, len(valPubKeys))
Expand Down Expand Up @@ -1066,7 +1067,7 @@ func TestDistributionFlow(t *testing.T) {
kb, err := keys.NewKeyBaseFromDir(InitClientHome(t, ""))
require.NoError(t, err)
addr, seed := CreateAddr(t, name1, pw, kb)
cleanup, _, valAddrs, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr}, false)
cleanup, _, valAddrs, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr}, true)
defer cleanup()

valAddr := valAddrs[0]
Expand All @@ -1076,15 +1077,12 @@ func TestDistributionFlow(t *testing.T) {
res, body := Request(t, port, "GET", fmt.Sprintf("/distribution/outstanding_rewards"), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
require.NoError(t, cdc.UnmarshalJSON([]byte(body), &rewards))
require.Equal(t, sdk.DecCoins(nil), rewards)

var valDistInfo distrrest.ValidatorDistInfo
res, body = Request(t, port, "GET", "/distribution/validators/"+valAddr.String(), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
require.NoError(t, cdc.UnmarshalJSON([]byte(body), &valDistInfo))
require.Equal(t, valDistInfo.OperatorAddress.String(), sdk.AccAddress(valAddr).String())
require.Equal(t, valDistInfo.ValidatorCommission, sdk.DecCoins(nil))
require.Equal(t, valDistInfo.SelfBondRewards, sdk.DecCoins(nil))

// Delegate some coins
delTokens := staking.TokensFromTendermintPower(60)
Expand All @@ -1098,43 +1096,35 @@ func TestDistributionFlow(t *testing.T) {
require.Equal(t, uint32(0), resultTx.Code)

// Query outstanding rewards changed
oustandingRewards := mustParseDecCoins("9.80stake")
res, body = Request(t, port, "GET", fmt.Sprintf("/distribution/outstanding_rewards"), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
require.NoError(t, cdc.UnmarshalJSON([]byte(body), &rewards))
require.Equal(t, oustandingRewards, rewards)

// Query validator distribution info
res, body = Request(t, port, "GET", "/distribution/validators/"+valAddr.String(), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

valRewards := mustParseDecCoins("6.125stake")
require.NoError(t, cdc.UnmarshalJSON([]byte(body), &valDistInfo))
require.Equal(t, valRewards, valDistInfo.SelfBondRewards)

// Query validator's rewards
res, body = Request(t, port, "GET", fmt.Sprintf("/distribution/validators/%s/rewards", valAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
require.NoError(t, cdc.UnmarshalJSON([]byte(body), &rewards))
require.Equal(t, valRewards, rewards)

// Query self-delegation
res, body = Request(t, port, "GET", fmt.Sprintf("/distribution/delegators/%s/rewards/%s", operAddr, valAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
require.NoError(t, cdc.UnmarshalJSON([]byte(body), &rewards))
require.Equal(t, valRewards, rewards)

// Query delegation
res, body = Request(t, port, "GET", fmt.Sprintf("/distribution/delegators/%s/rewards/%s", addr, valAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
require.NoError(t, cdc.UnmarshalJSON([]byte(body), &rewards))
require.Equal(t, mustParseDecCoins("3.675stake"), rewards)

// Query delegator's rewards total
res, body = Request(t, port, "GET", fmt.Sprintf("/distribution/delegators/%s/rewards", operAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
require.NoError(t, cdc.UnmarshalJSON([]byte(body), &rewards))
require.Equal(t, valRewards, rewards)

// Query delegator's withdrawal address
var withdrawAddr string
Expand Down
5 changes: 1 addition & 4 deletions cmd/gaia/app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,7 @@ func TestGaiaImportExport(t *testing.T) {
fmt.Printf("Exporting genesis...\n")

appState, _, err := app.ExportAppStateAndValidators(false, []string{})
if err != nil {
panic(err)
}

require.NoError(t, err)
fmt.Printf("Importing genesis...\n")

newDir, _ := ioutil.TempDir("", "goleveldb-gaia-sim-2")
Expand Down
2 changes: 2 additions & 0 deletions cmd/gaia/init/collect.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package init

// DONTCOVER

import (
"encoding/json"
"path/filepath"
Expand Down
2 changes: 2 additions & 0 deletions cmd/gaia/init/gentx.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package init

// DONTCOVER

import (
"bytes"
"fmt"
Expand Down
1 change: 0 additions & 1 deletion cmd/gaia/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func TestEmptyState(t *testing.T) {
w.Close()
os.Stdout = old
out := <-outC
require.Contains(t, out, "WARNING: State is not initialized")
require.Contains(t, out, "genesis_time")
require.Contains(t, out, "chain_id")
require.Contains(t, out, "consensus_params")
Expand Down
2 changes: 2 additions & 0 deletions cmd/gaia/init/testnet.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package init

// DONTCOVER

import (
"encoding/json"
"fmt"
Expand Down
49 changes: 49 additions & 0 deletions cmd/gaia/init/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package init

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/tests"

"github.com/stretchr/testify/require"
)

func TestExportGenesisFileWithTime(t *testing.T) {
t.Parallel()
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup()

fname := filepath.Join(dir, "genesis.json")
require.NoError(t, ExportGenesisFileWithTime(fname, "test", nil, json.RawMessage(""), time.Now()))
}

func TestLoadGenesisDoc(t *testing.T) {
t.Parallel()
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup()

fname := filepath.Join(dir, "genesis.json")
require.NoError(t, ExportGenesisFileWithTime(fname, "test", nil, json.RawMessage(""), time.Now()))

_, err := LoadGenesisDoc(codec.Cdc, fname)
require.NoError(t, err)

// Non-existing file
_, err = LoadGenesisDoc(codec.Cdc, "non-existing-file")
require.Error(t, err)

malformedFilename := filepath.Join(dir, "malformed")
malformedFile, err := os.Create(malformedFilename)
require.NoError(t, err)
fmt.Fprint(malformedFile, "invalidjson")
malformedFile.Close()
// Non-existing file
_, err = LoadGenesisDoc(codec.Cdc, malformedFilename)
require.Error(t, err)
}
5 changes: 3 additions & 2 deletions crypto/keys/lazy_keybase.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package keys

import (
"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
"github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto"
dbm "github.com/tendermint/tendermint/libs/db"

"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
"github.com/cosmos/cosmos-sdk/types"
)

var _ Keybase = lazyKeybase{}
Expand Down
3 changes: 2 additions & 1 deletion crypto/ledger_secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"os"

"github.com/btcsuite/btcd/btcec"
"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
"github.com/pkg/errors"
tmbtcec "github.com/tendermint/btcd/btcec"
tmcrypto "github.com/tendermint/tendermint/crypto"
tmsecp256k1 "github.com/tendermint/tendermint/crypto/secp256k1"

"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
)

var (
Expand Down
33 changes: 33 additions & 0 deletions server/constructors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package server

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/tests"
)

func Test_openDB(t *testing.T) {
t.Parallel()
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup()
_, err := openDB(dir)
require.NoError(t, err)
}

func Test_openTraceWriter(t *testing.T) {
t.Parallel()
dir, cleanup := tests.NewTestCaseDir(t)
defer cleanup()
fname := filepath.Join(dir, "logfile")
w, err := openTraceWriter(fname)
require.NoError(t, err)
require.NotNil(t, w)

// test no-op
w, err = openTraceWriter("")
require.NoError(t, err)
require.Nil(t, w)
}
17 changes: 11 additions & 6 deletions server/export.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package server

// DONTCOVER

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"io/ioutil"
"path"

"github.com/tendermint/tendermint/libs/cli"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -26,25 +30,26 @@ func ExportCmd(ctx *Context, cdc *codec.Codec, appExporter AppExporter) *cobra.C
Use: "export",
Short: "Export state to JSON",
RunE: func(cmd *cobra.Command, args []string) error {
home := viper.GetString("home")
config := ctx.Config
config.SetRoot(viper.GetString(cli.HomeFlag))

traceWriterFile := viper.GetString(flagTraceStore)
emptyState, err := isEmptyState(home)
emptyState, err := isEmptyState(config.RootDir)
if err != nil {
return err
}

if emptyState || appExporter == nil {
fmt.Println("WARNING: State is not initialized. Returning genesis file.")
genesisFile := path.Join(home, "config", "genesis.json")
genesis, err := ioutil.ReadFile(genesisFile)
fmt.Fprintln(os.Stderr, "WARNING: State is not initialized. Returning genesis file.")
genesis, err := ioutil.ReadFile(config.GenesisFile())
if err != nil {
return err
}
fmt.Println(string(genesis))
return nil
}

db, err := openDB(home)
db, err := openDB(config.RootDir)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit e7e3c32

Please sign in to comment.