Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
orpheuslummis committed Mar 23, 2023
1 parent 5bfb8e3 commit 9be4ae4
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 774 deletions.
14 changes: 9 additions & 5 deletions tests/integration/cli/client_peerid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ import (
)

func TestPeerID(t *testing.T) {
stopDefra := runDefraNode(t)
conf := NewDefraNodeDefaultConfig(t)
stopDefra := runDefraNode(t, conf)

stdout, stderr, loglines := runDefraCommand(t, []string{"client", "peerid"})
stdout, _ := runDefraCommand(t, conf, []string{"client", "peerid"})

defraLogLines := stopDefra()

for _, line := range defraLogLines {
assert.NotContains(t, line, "ERROR")
}

t.Log("stdout:", stdout)
t.Log("stderr:", stderr)
t.Log("loglines:", loglines)
assertContainsSubstring(t, stdout, "peerID")
}

func TestPeerIDWithNoHost(t *testing.T) {
conf := NewDefraNodeDefaultConfig(t)
_, stderr := runDefraCommand(t, conf, []string{"client", "peerid"})
assertContainsSubstring(t, stderr, "failed to request peer ID")
}
23 changes: 9 additions & 14 deletions tests/integration/cli/client_ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
)

func TestPingSimple(t *testing.T) {
stopDefra := runDefraNode(t)
conf := NewDefraNodeDefaultConfig(t)
stopDefra := runDefraNode(t, conf)

stdout, _, _ := runDefraCommand(t, []string{"client", "ping"})
stdout, _ := runDefraCommand(t, conf, []string{"client", "ping"})

nodeLog := stopDefra()

Expand All @@ -33,9 +34,9 @@ func TestPingSimple(t *testing.T) {
}

func TestPingCommandToInvalidHost(t *testing.T) {
stopDefra := runDefraNode(t)

_, stderr, _ := runDefraCommand(t, []string{"client", "ping", "--url", "'1!2:3!4'"})
conf := NewDefraNodeDefaultConfig(t)
stopDefra := runDefraNode(t, conf)
_, stderr := runDefraCommand(t, conf, []string{"client", "ping", "--url", "'1!2:3!4'"})

nodeLog := stopDefra()

Expand All @@ -52,13 +53,7 @@ func TestPingCommandToInvalidHost(t *testing.T) {
}

func TestPingCommandNoHost(t *testing.T) {
_, _, loglines := runDefraCommand(t, []string{"client", "ping", "--url", "localhost:9876"})

// for some line in stderr to contain the error message
for _, line := range loglines {
if strings.Contains(line, "connection refused") {
return
}
}
t.Error("expected error message not found in stderr")
conf := NewDefraNodeDefaultConfig(t)
_, stderr := runDefraCommand(t, conf, []string{"client", "ping", "--url", "localhost:56788"}) //WIP
assertContainsSubstring(t, stderr, "failed to send ping")
}
63 changes: 8 additions & 55 deletions tests/integration/cli/client_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,15 @@ import (
"testing"
)

// good request
func TestRequestSimple(t *testing.T) {
// var err error
conf := NewDefraNodeDefaultConfig(t)
stopDefra := runDefraNode(t, conf)

// have defradb running
stdout, _ := runDefraCommand(t, conf, []string{"client", "query",
"query IntrospectionQuery {__schema {queryType { name }}}",
})
nodeLog := stopDefra()

// do request
// requestCmd := exec.Command("defradb", "client", "query", "query { ... }")
// requestCmd.Env = append(os.Environ(), "DEFRADB_API_URL=http://localhost:8080")
// requestCmd.Stdout = os.Stdout
// requestCmd.Stderr = os.Stderr
// err := requestCmd.Run()
// if err != nil {
// t.Fatal(err)
// }

// cfg := newTestConfig(t)
// ctx := context.Background()
// defraCmd := cli.NewDefraCommand(cfg)
// defraCmd.RootCmd.SetArgs([]string{"client", "query", "query { ... }"})
// defraCmd.SetArgs([]string{"query { ... }"})
// err = rCmd.Execute()

// defraCmd := cli.NewDefraCommand(cfg)

// err = executeWithArgs(t, defraCmd, []string{"client", "query", "query { ... }"})
// assert.NoError(t, err)
// it should error because defra is not running
// we want defra to be running wiht some given parameters
assertContainsSubstring(t, stdout, "Query")
assertNotContainsSubstring(t, nodeLog, "ERROR")
}

// bad request

// basic test
// func TestOK(t *testing.T) {
// stop := startDefra(t)

// cfg := config.DefaultConfig()
// cmd := newDefraCmd(t, cfg, []string{"client", "ping"})
// out, err := captureStderr(t, func() error {
// return cmd.Execute(context.Background())
// })
// assert.NoError(t, err)
// for _, line := range out {
// t.Log("LINE", line)
// }
// defraOut := stop()
// for _, line := range defraOut {
// t.Log("DEFRAOUT LINE", line)
// }
// t.Fail()
// }

// on the server side we have a log entry that is
// 2023-03-13T13:07:28.473-0400, INFO, defra.http, Request, {"Method": "GET", "Path": "/api/v0/ping", "Status": 200, "LengthBytes": 28, "ElapsedTime": "11.792µs"}

// on the client side we have
// 2023-03-13T13:07:28.472-0400, INFO, defra.cli, Sending ping...
// 2023-03-13T13:07:28.473-0400, INFO, defra.cli, pong
47 changes: 25 additions & 22 deletions tests/integration/cli/client_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,48 @@
package clitest

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

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

// defradb client schema

/*
Example: add from file:
defradb client schema add -f schema.graphql
Example: add from stdin:
cat schema.graphql | defradb client schema add -
*/

func TestAddSchemaFromFile(t *testing.T) {
conf, stopDefra := runDefraNode(t)
const fname = "schema.graphql"
tmpdir := t.TempDir()
conf := NewDefraNodeDefaultConfig(t)
stopDefra := runDefraNode(t, conf)

err := ioutil.WriteFile(fname, []byte(`
fname := filepath.Join(tmpdir, "schema.graphql")
err := os.WriteFile(fname, []byte(`
type User {
id: ID
name: String
}
`), 0644)
assert.NoError(t, err)
defer func() {
err := os.Remove(fname)
assert.NoError(t, err)
}()

stdout, _, _ := runDefraCommand(t, []string{"client", "schema", "add", "-f", "schema.graphql"})
stdout, _ := runDefraCommand(t, conf, []string{"client", "schema", "add", "-f", fname})

nodeLog := stopDefra()

assert.Contains(t, stdout, `{"data":{"result":"success"}}`)
for _, line := range nodeLog {
assert.NotContains(t, line, "ERROR")
}
assertNotContainsSubstring(t, nodeLog, "ERROR")
}

// func TestAddSchemaFromPipe(t *testing.T) {
// conf := NewDefraNodeDefaultConfig(t)
// stopDefra := runDefraNode(t, conf)

// stdout, _, _ := runDefraCommandWithPipe(t, conf,
// []string{"client", "schema", "add", "-"},
// "type User {id: ID}",
// )

// nodeLog := stopDefra()

// assert.Contains(t, stdout, `{"data":{"result":"success"}}`)
// for _, line := range nodeLog {
// assert.NotContains(t, line, "ERROR")
// }
// }
44 changes: 3 additions & 41 deletions tests/integration/cli/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,12 @@

package clitest

/*
import (
"bufio"
"os"
"testing"
"github.com/sourcenetwork/defradb/cli"
)

// Execution error, {"Error": "badger store does not exist at /Users/o/.defradb/data. Try with an existing directory"}
func TestServerDump(t *testing.T) {
lines := capture(t, func() {
cmd := cli.MakeCommandTree()
tmpdir := t.TempDir()
cmd.SetArgs([]string{"init", tmpdir})
cmd.Execute()
cmd.SetArgs([]string{"--rootdir", tmpdir, "server-dump"})
cmd.Execute()
// tmpdir := t.TempDir()
// initCmd := cli.MakeInitCommand()
// initCmd.SetArgs([]string{tmpdir})
// initCmd.Execute()
// serverDumpCmd := cli.MakeServerDumpCmd()
// serverDumpCmd.Execute()
})
t.Log(lines)
t.Fail()
// Dumping DB state
// keys are like
// /db/blocks/
// /db/data/
// /db/system/
// /peers/peers/
// /providers/
}
func TestServerDumpMemoryErrs(t *testing.T) {
// we expect the following error:
// Execution error, {"Error": "server-side dump is only supported for the Badger datastore"}
conf := NewDefraNodeDefaultConfig(t)
_, stderr := runDefraCommand(t, conf, []string{"server-dump", "--store", "memory"})
assertContainsSubstring(t, stderr, "server-side dump is only supported for the Badger datastore")
}
*/
79 changes: 33 additions & 46 deletions tests/integration/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,42 @@

package clitest

/*
import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"

"github.com/sourcenetwork/defradb/config"
)

// Executing init command creates valid config file.
func TestCLIInitCommand(t *testing.T) {
tmpdir := t.TempDir()
logLines := captureStderr(t, func() {
defraCmd := cli.NewDefraCommand(config.NewWithDefaults())
defraCmd.RootCmd.SetArgs([]string{"init", "--rootdir", tmpdir})
assert.NoError(t, defraCmd.Execute(context.Background()))
})
t.Log(logLines)
assert.Contains(t, parseLogLine(logLines[0]), "Initialized configuration file at "+tmpdir+"/"+config.DefaultConfigFileName)
cfg := config.NewWithDefaults()
cfg.Rootdir = tmpdir
assert.NoError(t, cfg.Load(true))
conf := NewDefraNodeDefaultConfig(t)
_, stderr := runDefraCommand(t, conf, []string{"init", conf.rootDir})
cfgfilePath := filepath.Join(conf.rootDir, config.DefaultConfigFileName)
assertContainsSubstring(t, stderr, "Created config file at "+cfgfilePath)
if !assert.FileExists(t, cfgfilePath) {
t.Fatal("Config file not created")
}
}

// Executing init command twice leads to error.
// func TestCLIInitCommandTwiceErrors(t *testing.T) {
// tmpdir := t.TempDir()
// logLines := captureStderr(t, func() {
// rootCmd := cli.NewDefraCommand(config.NewWithDefaults()).RootCmd
// rootCmd.SetArgs([]string{"init", tmpdir})
// assert.NoError(t, rootCmd.Execute())
// })
// assert.Contains(t, parseLogLine(logLines[0]), "Initialized configuration file at "+tmpdir+"/"+config.DefaultConfigFileName)
// logLines = captureStderr(t, func() {
// rootCmd := cli.NewDefraCommand(config.NewWithDefaults()).RootCmd
// rootCmd.SetArgs([]string{"init", tmpdir})
// assert.NoError(t, rootCmd.Execute()) // TBD: should this be an error?
// })
// assert.Contains(t, parseLogLine(logLines[0]), "Configuration file already exists at "+tmpdir+"/"+config.DefaultConfigFileName+". Consider using --reinitialize")
// }
func TestCLIInitCommandTwiceErrors(t *testing.T) {
conf := NewDefraNodeDefaultConfig(t)
cfgfilePath := filepath.Join(conf.rootDir, config.DefaultConfigFileName)
_, stderr := runDefraCommand(t, conf, []string{"init", conf.rootDir})
assertContainsSubstring(t, stderr, "Created config file at "+cfgfilePath)
_, stderr = runDefraCommand(t, conf, []string{"init", conf.rootDir})
assertContainsSubstring(t, stderr, "Configuration file already exists at "+cfgfilePath)
}

// Executing init command twice, but second time reinitializing.
// func TestInitCommandTwiceReinitalize(t *testing.T) {
// tmpdir := t.TempDir()
// defraCommand := cli.NewDefraCommand(config.NewWithDefaults())
// logLines := captureStderr(t, func() {
// defraCommand.RootCmd.SetArgs([]string{"init", "--rootdir", tmpdir})
// assert.NoError(t, defraCommand.Execute(context.Background()))
// })
// t.Log(logLines)
// assert.Contains(t, parseLogLine(logLines[0]), "Initialized configuration file at "+tmpdir+"/"+config.DefaultConfigFileName)
// // logLines = captureStderr(t, func() {
// // rootCmd.SetArgs([]string{"init", tmpdir, "--reinitialize"})
// // assert.NoError(t, rootCmd.Execute())
// // })
// // assert.Contains(t, parseLogLine(logLines[0]), "Reinitialized configuration file at "+tmpdir+"/"+config.DefaultConfigFileName)
// }
*/
func TestInitCommandTwiceReinitalize(t *testing.T) {
conf := NewDefraNodeDefaultConfig(t)
cfgfilePath := filepath.Join(conf.rootDir, config.DefaultConfigFileName)
_, stderr := runDefraCommand(t, conf, []string{"init", conf.rootDir})
assertContainsSubstring(t, stderr, "Created config file at "+cfgfilePath)
_, stderr = runDefraCommand(t, conf, []string{"init", conf.rootDir, "--reinitialize"})
assertContainsSubstring(t, stderr, "Deleted config file at "+cfgfilePath)
assertContainsSubstring(t, stderr, "Created config file at "+cfgfilePath)
}
Loading

0 comments on commit 9be4ae4

Please sign in to comment.