Skip to content

Commit

Permalink
fixed some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
faboweb authored and ebuchman committed Mar 17, 2018
1 parent b446905 commit 579bd56
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 70 deletions.
2 changes: 1 addition & 1 deletion client/lcd/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
keys.db
tmp-base*
60 changes: 29 additions & 31 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func TestKeys(t *testing.T) {
kill, port := junkInit(t)
kill, port, _ := junkInit(t)
defer kill()

// empty keys
Expand Down Expand Up @@ -96,19 +96,19 @@ func TestKeys(t *testing.T) {
}

// TODO/XXX: We should be spawning what we need in process, not shelling out
func junkInit(t *testing.T) (kill func(), port string) {
tests.TestInitBasecoin(t)
func junkInit(t *testing.T) (kill func(), port string, seed string) {
seed = tests.TestInitBasecoin(t)
cmdStart := tests.StartNodeServerForTest(t)
cmdLCD, port := tests.StartLCDServerForTest(t)
kill = func() {
cmdLCD.Process.Kill()
cmdStart.Process.Kill()
}
return kill, port
return kill, port, seed
}

func TestVersion(t *testing.T) {
kill, port := junkInit(t)
kill, port, _ := junkInit(t)
defer kill()

// node info
Expand All @@ -122,7 +122,7 @@ func TestVersion(t *testing.T) {
}

func TestNodeStatus(t *testing.T) {
kill, port := junkInit(t)
kill, port, _ := junkInit(t)
defer kill()

// node info
Expand All @@ -143,69 +143,67 @@ func TestNodeStatus(t *testing.T) {
}

func TestBlock(t *testing.T) {
kill, port := junkInit(t)
kill, port, _ := junkInit(t)
defer kill()

// res, body := request(t, port, "GET", "/blocks/latest", nil)
// require.Equal(t, http.StatusOK, res.StatusCode, body)
var resultBlock ctypes.ResultBlock

res, body := request(t, port, "GET", "/blocks/latest", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

// var m ctypes.ResultBlock
// decoder := json.NewDecoder(res.Body)
// err := decoder.Decode(&m)
// require.Nil(t, err, "Couldn't parse block")
err := json.Unmarshal([]byte(body), &resultBlock)
require.Nil(t, err, "Couldn't parse block")

// assert.NotEqual(t, ctypes.ResultBlock{}, m)
assert.NotEqual(t, ctypes.ResultBlock{}, resultBlock)

// --

res, body := request(t, port, "GET", "/blocks/1", nil)
res, body = request(t, port, "GET", "/blocks/1", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var resultBlock ctypes.ResultBlock
err := json.Unmarshal([]byte(body), &resultBlock)
err = json.Unmarshal([]byte(body), &resultBlock)
require.Nil(t, err, "Couldn't parse block")

assert.NotEqual(t, ctypes.ResultBlock{}, resultBlock)

// --

res, body = request(t, port, "GET", "/blocks/2", nil)
res, body = request(t, port, "GET", "/blocks/1000000000", nil)
require.Equal(t, http.StatusNotFound, res.StatusCode, body)
}

func TestValidators(t *testing.T) {
kill, port := junkInit(t)
kill, port, _ := junkInit(t)
defer kill()

// res, body := request(t, port, "GET", "/validatorsets/latest", nil)
// require.Equal(t, http.StatusOK, res.StatusCode, body)
var resultVals ctypes.ResultValidators

res, body := request(t, port, "GET", "/validatorsets/latest", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

// var m ctypes.ResultValidators
// decoder := json.NewDecoder(res.Body)
// err := decoder.Decode(&m)
// require.Nil(t, err, "Couldn't parse validatorset")
err := json.Unmarshal([]byte(body), &resultVals)
require.Nil(t, err, "Couldn't parse validatorset")

// assert.NotEqual(t, ctypes.ResultValidators{}, m)
assert.NotEqual(t, ctypes.ResultValidators{}, resultVals)

// --

res, body := request(t, port, "GET", "/validatorsets/1", nil)
res, body = request(t, port, "GET", "/validatorsets/1", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var resultVals ctypes.ResultValidators
err := json.Unmarshal([]byte(body), &resultVals)
err = json.Unmarshal([]byte(body), &resultVals)
require.Nil(t, err, "Couldn't parse validatorset")

assert.NotEqual(t, ctypes.ResultValidators{}, resultVals)

// --

res, body = request(t, port, "GET", "/validatorsets/2", nil)
res, body = request(t, port, "GET", "/validatorsets/1000000000", nil)
require.Equal(t, http.StatusNotFound, res.StatusCode)
}

func TestCoinSend(t *testing.T) {
kill, port := junkInit(t)
kill, port, _ := junkInit(t)
defer kill()

// TODO make that account has coins
Expand Down
12 changes: 6 additions & 6 deletions client/rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func BlockRequestHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/block/{height}'."))
return
}
// chainHeight, err := GetChainHeight()
// if height > chainHeight {
// w.WriteHeader(404)
// w.Write([]byte("ERROR: Requested block height is bigger then the chain length."))
// return
// }
chainHeight, err := GetChainHeight()
if height > chainHeight {
w.WriteHeader(404)
w.Write([]byte("ERROR: Requested block height is bigger then the chain length."))
return
}
output, err := getBlock(&height)
if err != nil {
w.WriteHeader(500)
Expand Down
4 changes: 2 additions & 2 deletions client/rpc/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func initClientCommand() *cobra.Command {
func RegisterRoutes(r *mux.Router) {
r.HandleFunc("/node_info", NodeInfoRequestHandler).Methods("GET")
r.HandleFunc("/syncing", NodeSyncingRequestHandler).Methods("GET")
// r.HandleFunc("/blocks/latest", LatestBlockRequestHandler).Methods("GET")
r.HandleFunc("/blocks/latest", LatestBlockRequestHandler).Methods("GET")
r.HandleFunc("/blocks/{height}", BlockRequestHandler).Methods("GET")
// r.HandleFunc("/validatorsets/latest", LatestValidatorsetRequestHandler).Methods("GET")
r.HandleFunc("/validatorsets/latest", LatestValidatorsetRequestHandler).Methods("GET")
r.HandleFunc("/validatorsets/{height}", ValidatorsetRequestHandler).Methods("GET")
}
12 changes: 6 additions & 6 deletions client/rpc/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ func ValidatorsetRequestHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/validatorsets/{height}'."))
return
}
// chainHeight, err := GetChainHeight()
// if height > chainHeight {
// w.WriteHeader(404)
// w.Write([]byte("ERROR: Requested block height is bigger then the chain length."))
// return
// }
chainHeight, err := GetChainHeight()
if height > chainHeight {
w.WriteHeader(404)
w.Write([]byte("ERROR: Requested block height is bigger then the chain length."))
return
}
output, err := getValidators(&height)
if err != nil {
w.WriteHeader(500)
Expand Down
45 changes: 22 additions & 23 deletions tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,53 +49,49 @@ func whereIsBasecli() string {
}

// Init Basecoin Test
func TestInitBasecoin(t *testing.T) {
func TestInitBasecoin(t *testing.T) string {
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()
if err != nil {
t.Error(err)
}
cmdWriter, err := initBasecoind.StdinPipe()
require.Nil(t, err)

buf := new(bytes.Buffer)
initBasecoind.Stdout = buf

if err := initBasecoind.Start(); err != nil {
t.Error(err)
}
if err := usePassword.Run(); err != nil {
t.Error(err)
}
if err := initBasecoind.Wait(); err != nil {
t.Error(err)
}

if err := makeKeys(); err != nil {
_, err = cmdWriter.Write([]byte(password))
require.Nil(t, err)
cmdWriter.Close()

if err := initBasecoind.Wait(); err != nil {
t.Error(err)
}

fmt.Println("-----------------")
// get seed from initialization
theOutput := strings.Split(buf.String(), "\n")
for i, o := range theOutput {
fmt.Println(i, o)
var seedLine int
for seedLine, o := range theOutput {
if strings.HasPrefix(string(o), "Secret phrase") {
seedLine++
break
}
}
fmt.Println("-----------------")

return string(theOutput[seedLine])
}

func makeKeys() error {
var err error
for _, acc := range ACCOUNTS {
pass := exec.Command("echo", "1234567890")
makeKeys := exec.Command(whereIsBasecli(), "keys", "add", acc, "--home", basecliDir)

makeKeys.Stdin, err = pass.StdoutPipe()
cmdWriter, err := makeKeys.StdinPipe()
if err != nil {
return err
}
Expand All @@ -104,9 +100,11 @@ func makeKeys() error {
if err := makeKeys.Start(); err != nil {
return err
}
if err := pass.Run(); err != nil {
cmdWriter.Write([]byte("1234567890"))
if err != nil {
return err
}
cmdWriter.Close()

if err := makeKeys.Wait(); err != nil {
return err
Expand Down Expand Up @@ -176,6 +174,7 @@ func StartServer() error {

// Init Basecoin Test
func InitServerForTest(t *testing.T) {
// TODO cleanup doesn't work -> keys are still there in each iteration
Clean()

var err error
Expand Down
2 changes: 1 addition & 1 deletion x/auth/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, parser sdk.Pa
}

// the query will return empty if there is no data for this account
if len(res) == 0 {
if res == nil {
w.WriteHeader(http.StatusNoContent)
return
}
Expand Down

0 comments on commit 579bd56

Please sign in to comment.