Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
orpheuslummis committed Dec 16, 2022
1 parent 7839112 commit 6334854
Show file tree
Hide file tree
Showing 11 changed files with 636 additions and 28 deletions.
71 changes: 71 additions & 0 deletions tests/integration/cli/client_ping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

//go:build integrationcli
// +build integrationcli

package clitest

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"

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

func TestPingCommandToValidHostWithNoNode(t *testing.T) {
logLines := captureStderr(t, func() {
cfg := config.NewWithDefaults()
defraCmd := cli.NewDefraCommand(cfg)
defraCmd.RootCmd.SetArgs([]string{"client", "ping", "--url", "localhost:9876"})
assert.Error(t, defraCmd.Execute(context.Background()))
})
gotExpectedError := false
expectedError1 := "{\"Error\": \"failed to send ping: Get \\\"http://localhost:9876/api/v0/ping\\\": dial tcp [::1]:9876: connect: connection refused\"}"
expectedError2 := "{\"Error\": \"failed to send ping: Get \\\"http://localhost:9876/api/v0/ping\\\": dial tcp 127.0.0.1:9876: connect: connection refused\"}"
for _, line := range logLines {
if strings.Contains(line, expectedError1) || strings.Contains(line, expectedError2) {
gotExpectedError = true
break
}
}
assert.True(t, gotExpectedError)
}

func TestPingCommandToInvalidHost(t *testing.T) {
logLines := captureStderr(t, func() {
cfg := config.NewWithDefaults()
defraCmd := cli.NewDefraCommand(cfg)
defraCmd.RootCmd.SetArgs([]string{"client", "ping", "--url", "'1!2:3!4'"})
assert.Error(t, defraCmd.Execute(context.Background()))
})
expectedError := "{\"Error\": \"failed to load config: failed to validate API config: invalid database URL provided\"}"
assert.Contains(t, parseLogLine(logLines[0]), expectedError)
}

func TestPingCommandToValidLocalhostwithFramework(t *testing.T) {
cfg := config.NewWithDefaults()
testCfg := newRandomTestConfig(t)
cfg.API.Address = testCfg.serialize().APIAddress
cfg.Rootdir = testCfg.serialize().RootDir
defraCmd := cli.NewDefraCommand(cfg)
// defer(releasePortsOfCommand(defraCmd))
logLines := testFrameworkWithBackgroundNodeExec(t, testCfg, func() {
defraCmd.RootCmd.SetArgs([]string{"client", "ping"})
err := defraCmd.Execute(context.Background())
assert.NoError(t, err)
})
expectedOutput := `{"data":{"response":"pong"}}`
assert.Contains(t, parseLogLine(logLines[0]), expectedOutput)
}
24 changes: 24 additions & 0 deletions tests/integration/cli/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

//go:build integrationcli
// +build integrationcli

package clitest

/*
- schema
- add
- blocks
- get
- dump
- peerid
- query
*/
74 changes: 74 additions & 0 deletions tests/integration/cli/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

//go:build integrationcli
// +build integrationcli

package clitest

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/sourcenetwork/defradb/cli"
"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))
}

// 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")
// }

// 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)
// }
41 changes: 13 additions & 28 deletions tests/integration/cli/log_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,18 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package cli

import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/sourcenetwork/defradb/cli"
"github.com/sourcenetwork/defradb/logging"
)

const (
testLogger1 = "testLogger1"
testLogger2 = "testLogger2"
testLogger3 = "testLogger3"
)

var (
log1 = logging.MustNewLogger(testLogger1)
log2 = logging.MustNewLogger(testLogger2)
log3 = logging.MustNewLogger(testLogger3)
)
//go:build integrationcli
// +build integrationcli

package clitest

// const (
// testLogger1 = "testLogger1"
// testLogger2 = "testLogger2"
// testLogger3 = "testLogger3"
// )

/*
func TestCLILogsToStderrGivenNamedLogLevel(t *testing.T) {
ctx := context.Background()
logLines := captureLogLines(
Expand Down Expand Up @@ -109,3 +92,5 @@ func parseLines(r io.Reader) ([]string, error) {
return logLines, nil
}
*/
56 changes: 56 additions & 0 deletions tests/integration/cli/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

//go:build integrationcli
// +build integrationcli

package clitest

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

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

func TestRootCommandEmptyRootDir(t *testing.T) {
defraCmd := cli.NewDefraCommand(config.NewWithDefaults())
tmpdir := t.TempDir()
defraCmd.RootCmd.SetArgs([]string{"--rootdir", tmpdir})
assert.NoError(t, defraCmd.Execute(context.Background()))
assert.Equal(t, config.NewWithDefaults(), defraCmd.Cfg)
}

func TestRootCommandRootDirWithDefaultConfig(t *testing.T) {
defraCmd := cli.NewDefraCommand(config.NewWithDefaults())
tmpdir := t.TempDir()
cfg := config.NewWithDefaults()
cfg.Rootdir = tmpdir
assert.NoError(t, cfg.CreateRootDir())
defraCmd.RootCmd.SetArgs([]string{"--rootdir", tmpdir})
assert.NoError(t, defraCmd.Execute(context.Background()))
assert.Equal(t, config.NewWithDefaults(), defraCmd.Cfg)
}

// loaded config is non-default
func TestRootCommandRootDirWithNonDefaultConfig(t *testing.T) {
tmpdir := t.TempDir()
cfg := config.NewWithDefaults()
cfg.Rootdir = tmpdir
defraCmd := cli.NewDefraCommand(cfg)
defraCmd.Cfg.Datastore.Store = "memory"
assert.NoError(t, defraCmd.Cfg.WriteConfigFile())
defraCmd.RootCmd.SetArgs([]string{"--rootdir", tmpdir})
assert.NoError(t, defraCmd.Execute(context.Background()))
assert.Equal(t, "memory", defraCmd.Cfg.Datastore.Store)
}
14 changes: 14 additions & 0 deletions tests/integration/cli/rpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

//go:build integrationcli
// +build integrationcli

package clitest
62 changes: 62 additions & 0 deletions tests/integration/cli/serverdump_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

//go:build integrationcli
// +build integrationcli

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"}
}
*/
Loading

0 comments on commit 6334854

Please sign in to comment.