Skip to content

Commit

Permalink
add a bunch of tests, add DONTCOVER text tag
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Alessio Treglia committed Feb 8, 2019
1 parent d759bef commit a4e3eac
Show file tree
Hide file tree
Showing 27 changed files with 219 additions and 90 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: 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
16 changes: 3 additions & 13 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,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 @@ -1025,7 +1026,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 @@ -1035,15 +1036,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 @@ -1057,43 +1055,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
27 changes: 26 additions & 1 deletion server/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/server"
"github.com/stretchr/testify/require"
)

func TestGenerateCoinKey(t *testing.T) {
t.Parallel()
addr, mnemonic, err := server.GenerateCoinKey()
require.NoError(t, err)

Expand All @@ -21,6 +23,7 @@ func TestGenerateCoinKey(t *testing.T) {
}

func TestGenerateSaveCoinKey(t *testing.T) {
t.Parallel()
dir, cleanup := tempdir(t)
defer cleanup() // clean after itself
// Remove the dir to that GenerateSaveCoinKey creates it automatically
Expand All @@ -42,6 +45,28 @@ func TestGenerateSaveCoinKey(t *testing.T) {
require.Equal(t, addr, info.GetAddress())
}

func TestGenerateSaveCoinKeyOverwriteFlag(t *testing.T) {
t.Parallel()
dir, cleanup := tempdir(t)
defer cleanup() // clean after itself
// Remove the dir to that GenerateSaveCoinKey creates it automatically
os.RemoveAll(dir)

keyname := "justakey"
addr1, _, err := server.GenerateSaveCoinKey(dir, keyname, "012345678", false)
require.NoError(t, err)

// Test overwrite with overwrite=false
_, _, err = server.GenerateSaveCoinKey(dir, keyname, "012345678", false)
require.Error(t, err)

// Test overwrite with overwrite=true
addr2, _, err := server.GenerateSaveCoinKey(dir, keyname, "012345678", true)
require.NoError(t, err)

require.NotEqual(t, addr1, addr2)
}

func tempdir(t *testing.T) (string, func()) {
dir, err := ioutil.TempDir("", t.Name()+"_")
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions server/tm_cmds.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package server

// DONTCOVER

import (
"fmt"

Expand Down
Loading

0 comments on commit a4e3eac

Please sign in to comment.