Skip to content

Commit

Permalink
Merge pull request #96 from darcys22/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
darcys22 authored Oct 23, 2020
2 parents 9651f9f + 7d85f82 commit 642be6a
Show file tree
Hide file tree
Showing 28 changed files with 489 additions and 240 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ clean:
rm -rf release/
rm -rf cert/

lint:
GO111MODULE=on go run utils/ci.go lint

travis:
GO111MODULE=on go run utils/ci.go install
GO111MODULE=on go run utils/ci.go test -coverage $$TEST_PACKAGES
Expand Down
6 changes: 4 additions & 2 deletions godbledger/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ func MakeConfig(cli *cli.Context) (error, *LedgerConfig) {
return err, nil
}
logrus.SetLevel(level)
if len(cli.String("config")) > 0 {
config.ConfigFile = cli.String("config")
setConfig(cli, config)
if config.DataDirectory != DefaultDataDir() {
config.ConfigFile = config.DataDirectory + "/config.toml"
config.DatabaseLocation = config.DataDirectory + "/ledgerdata/ledger.db"
}
err = InitConfig(config)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion godbledger/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func setConfig(ctx *cli.Context, cfg *LedgerConfig) {
cfg.ConfigFile = ctx.String(ConfigFileFlag.Name)
}
if ctx.IsSet(DataDirFlag.Name) {
cfg.ConfigFile = ctx.String(DataDirFlag.Name)
cfg.DataDirectory = ctx.String(DataDirFlag.Name)
}
if ctx.IsSet(RPCHost.Name) {
cfg.Host = ctx.String(RPCHost.Name)
Expand Down
65 changes: 65 additions & 0 deletions godbledger/core/transactions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package core

import (
"math/big"
"testing"
"time"

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

func init() {
}

func TestTransaction(t *testing.T) {
user, err := NewUser("Tester")
assert.NoError(t, err)

txn, err := NewTransaction(user)
assert.NoError(t, err)

cash, err := NewAccount("1", "cash")
assert.NoError(t, err)
income, err := NewAccount("2", "income")
assert.NoError(t, err)
aud, err := NewCurrency("AUD", 2)
assert.NoError(t, err)

amountDR := big.NewInt(10)

spl1, err := NewSplit(time.Now(), []byte("Cash Income"), []*Account{cash}, aud, amountDR)
assert.NoError(t, err)

err = txn.AppendSplit(spl1)
assert.NoError(t, err)

amountCR := big.NewInt(-10)
spl2, err := NewSplit(time.Now(), []byte("Cash Income"), []*Account{income}, aud, amountCR)
assert.NoError(t, err)

err = txn.AppendSplit(spl2)
assert.NoError(t, err)

total, txnBalances := txn.Balance()
assert.True(t, txnBalances)
assert.Equal(t, total.Cmp(big.NewInt(0)), 0)

assert.Equal(t, txn.Splits[0].Amount, amountDR)
assert.Equal(t, txn.Splits[0].Accounts[0].Name, "cash")
assert.Equal(t, txn.Splits[1].Amount, amountCR)
assert.Equal(t, txn.Splits[1].Accounts[0].Name, "income")

//func ReverseTransaction(originalTxn *Transaction, usr *User) (*Transaction, error) {
reversedTxn, err := ReverseTransaction(txn, user)
assert.NoError(t, err)

total, txnBalances = reversedTxn.Balance()
assert.True(t, txnBalances)
assert.Equal(t, total.Cmp(big.NewInt(0)), 0)

assert.Equal(t, reversedTxn.Splits[0].Amount, amountCR)
assert.Equal(t, reversedTxn.Splits[0].Accounts[0].Name, "cash")
assert.Equal(t, reversedTxn.Splits[1].Amount, amountDR)
assert.Equal(t, reversedTxn.Splits[1].Accounts[0].Name, "income")

}
1 change: 1 addition & 0 deletions godbledger/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func New(ctx *cli.Context, cfg *cmd.LedgerConfig) (*Ledger, error) {
}
log.WithField("path", dbPath).Debug("Checking db path")
if ctx.Bool(cmd.ClearDB.Name) {
log.Info("Clearing SQLite3 DB")
if err := sqlite3db.ClearDB(dbPath); err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion godbledger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ func main() {
}
app.Version = version.Version
app.Commands = []*cli.Command{
// See config.go
// See cmd/config.go
cmd.DumpConfigCommand,
cmd.InitConfigCommand,
}

app.Flags = []cli.Flag{
// See cmd/flags.go
cmd.VerbosityFlag,
cmd.DataDirFlag,
cmd.ClearDB,
Expand Down
1 change: 0 additions & 1 deletion godbledger/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"syscall"

"github.com/sirupsen/logrus"
//"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/darcys22/godbledger/godbledger/core"
Expand Down
90 changes: 90 additions & 0 deletions godbledger/node/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package node

import (
"context"
"crypto/rand"
"flag"
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/darcys22/godbledger/godbledger/cmd"
"github.com/darcys22/godbledger/godbledger/ledger"
"github.com/darcys22/godbledger/shared"

"github.com/sirupsen/logrus"
logTest "github.com/sirupsen/logrus/hooks/test"
"github.com/urfave/cli/v2"
)

const (
maxPollingWaitTime = 1 * time.Second
)

func init() {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetOutput(ioutil.Discard)
}

// Test that godbledger node can close.
func TestNodeClose_OK(t *testing.T) {
hook := logTest.NewGlobal()

app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.String("config", "", "doc")

ctx := cli.NewContext(&app, set, nil)

node, err := New(ctx)
assert.NoError(t, err)

node.Close()

shared.LogsContain(t.Fatalf, hook, "Stopping ledger node", true)
}

// TestClearDB tests clearing the database
func TestClearDB(t *testing.T) {
hook := logTest.NewGlobal()

randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
assert.NoError(t, err, "Could not generate random number for file path")
tmp := filepath.Join(os.TempDir(), fmt.Sprintf("datadirtest%d", randPath))
assert.NoError(t, os.RemoveAll(tmp))

app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.Bool(cmd.ClearDB.Name, true, "")

ctx := cli.NewContext(&app, set, nil)
assert.NoError(t, err)
err, cfg := cmd.MakeConfig(ctx)
assert.NoError(t, err)
cfg.DatabaseType = "memorydb"
cfg.DataDirectory = tmp

node, err := New(ctx)
assert.NoError(t, err)

ledger, err := ledger.New(ctx, cfg)
assert.NoError(t, err)

node.Register(ledger)
go node.Start()
d := time.Now().Add(maxPollingWaitTime)
contextWithDeadline, cancel := context.WithDeadline(context.Background(), d)
defer cancel()
<-contextWithDeadline.Done()
shared.LogsContain(t.Fatalf, hook, "Clearing SQLite3 DB", true)
//case <-contextWithDeadline.Done():

node.Close()
assert.NoError(t, os.RemoveAll(tmp))
}
91 changes: 91 additions & 0 deletions godbledger/rpc/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package rpc

import (
"context"
"errors"
"flag"
"io/ioutil"
"testing"

"github.com/darcys22/godbledger/godbledger/cmd"
"github.com/darcys22/godbledger/godbledger/ledger"
"github.com/darcys22/godbledger/shared"

"github.com/sirupsen/logrus"
logTest "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
)

func init() {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetOutput(ioutil.Discard)
}

func TestLifecycle_OK(t *testing.T) {
hook := logTest.NewGlobal()

set := flag.NewFlagSet("test", 0)
set.String("config", "", "doc")
ctx := cli.NewContext(nil, set, nil)
err, cfg := cmd.MakeConfig(ctx)
assert.NoError(t, err)

cfg.DatabaseType = "memorydb"
cfg.Host = "127.0.0.1"
cfg.RPCPort = "7348"
cfg.CACert = "bob.crt"
cfg.Cert = "alice.crt"
cfg.Key = "alice.key"

ledger, err := ledger.New(ctx, cfg)
assert.NoError(t, err)

rpcService := NewRPCService(context.Background(), &Config{
Host: cfg.Host,
Port: cfg.RPCPort,
CACertFlag: cfg.CACert,
CertFlag: cfg.Cert,
KeyFlag: cfg.Key,
}, ledger)

rpcService.Start()

shared.LogsContain(t.Fatalf, hook, "GRPC Listening on port", true)
assert.NoError(t, rpcService.Stop())
}

func TestStatus_CredentialError(t *testing.T) {
credentialErr := errors.New("credentialError")
s := &Service{credentialError: credentialErr}

assert.Contains(t, s.credentialError.Error(), s.Status().Error())
}

func TestRPC_InsecureEndpoint(t *testing.T) {
hook := logTest.NewGlobal()

set := flag.NewFlagSet("test", 0)
set.String("config", "", "doc")
ctx := cli.NewContext(nil, set, nil)
err, cfg := cmd.MakeConfig(ctx)
assert.NoError(t, err)

cfg.DatabaseType = "memorydb"
cfg.Host = "127.0.0.1"
cfg.RPCPort = "7777"

ledger, err := ledger.New(ctx, cfg)
assert.NoError(t, err)

rpcService := NewRPCService(context.Background(), &Config{
Host: cfg.Host,
Port: cfg.RPCPort,
}, ledger)

rpcService.Start()

shared.LogsContain(t.Fatalf, hook, "GRPC Listening on port", true)
shared.LogsContain(t.Fatalf, hook, "You are using an insecure gRPC server", true)
assert.NoError(t, rpcService.Stop())
}
2 changes: 1 addition & 1 deletion godbledger/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

const (
VersionMajor = 0 // Major version component of the current release
VersionMinor = 4 // Minor version component of the current release
VersionMinor = 5 // Minor version component of the current release
VersionPatch = 0 // Patch version component of the current release
VersionMeta = "alpha" // Version metadata to append to the version string
)
Expand Down
Loading

0 comments on commit 642be6a

Please sign in to comment.