Skip to content

Commit

Permalink
apply feedback and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
orpheuslummis committed Apr 25, 2023
1 parent 6855dd0 commit 0983d4f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 56 deletions.
25 changes: 25 additions & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
package cli

import (
"fmt"
"math/rand"
"net"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"

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

// Verify that the top-level commands are registered, and if particular ones have subcommands.
Expand Down Expand Up @@ -57,3 +61,24 @@ func walkCommandTree(t *testing.T, cmd *cobra.Command, f func(*cobra.Command)) {
walkCommandTree(t, c, f)
}
}

// findFreePortInRange returns a free port in the range [minPort, maxPort].
// The range of ports that are unfrequently used is [49152, 65535].
func findFreePortInRange(minPort, maxPort int) (int, error) {
if minPort < 1 || maxPort > 65535 || minPort > maxPort {
return 0, errors.New("invalid port range")
}

const maxAttempts = 100
for i := 0; i < maxAttempts; i++ {
port := rand.Intn(maxPort-minPort+1) + minPort
addr := fmt.Sprintf("127.0.0.1:%d", port)
listener, err := net.Listen("tcp", addr)
if err == nil {
_ = listener.Close()
return port, nil
}
}

return 0, errors.New("unable to find a free port")
}
25 changes: 21 additions & 4 deletions cli/peerid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
Expand All @@ -27,10 +28,26 @@ import (
// setTestingAddresses overrides the config addresses to be the ones reserved for testing.
// Used to ensure the tests don't fail due to address clashes with the running server (with default config).
func setTestingAddresses(cfg *config.Config) {
cfg.API.Address = "localhost:9182"
cfg.Net.P2PAddress = "/ip4/0.0.0.0/tcp/9172"
cfg.Net.TCPAddress = "/ip4/0.0.0.0/tcp/9162"
cfg.Net.RPCAddress = "0.0.0.0:9162"
portAPI, err := findFreePortInRange(49152, 65535)
if err != nil {
panic(err)
}
portTCP, err := findFreePortInRange(49152, 65535)
if err != nil {
panic(err)
}
portP2P, err := findFreePortInRange(49152, 65535)
if err != nil {
panic(err)
}
portRPC, err := findFreePortInRange(49152, 65535)
if err != nil {
panic(err)
}
cfg.API.Address = fmt.Sprintf("localhost:%d", portAPI)
cfg.Net.P2PAddress = fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", portP2P)
cfg.Net.TCPAddress = fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", portTCP)
cfg.Net.RPCAddress = fmt.Sprintf("0.0.0.0:%d", portRPC)
}

func TestGetPeerIDCmd(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ See https://docs.source.network/BSL.txt for more information.
// Loads the rootDir containing the configuration file, otherwise warn about it and load a default configuration.
// This allows some subcommands (`init`, `start`) to override the PreRun to create a rootDir by default.
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
if err := cfg.LoadRootDirFromFlagOrDefault(); err != nil {
return err
}
if cfg.ConfigFileExists() {
if err := cfg.LoadWithRootdir(true); err != nil {
return errors.Wrap("failed to load config", err)
Expand Down
5 changes: 4 additions & 1 deletion cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ func MakeStartCommand(cfg *config.Config) *cobra.Command {
Long: "Start a new instance of DefraDB node.",
// Load the root config if it exists, otherwise create it.
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
if err := cfg.LoadRootDirFromFlagOrDefault(); err != nil {
return err
}
if cfg.ConfigFileExists() {
if err := cfg.LoadWithRootdir(true); err != nil {
return config.NewErrLoadingConfig(err)
}
log.FeedbackInfo(cmd.Context(), fmt.Sprintf("Configuration loaded from DefraDB directory %v", cfg.Rootdir))
} else {
if err := cfg.LoadWithRootdir(false); err != nil {
return config.NewErrLoadingConfig(err)
Expand All @@ -60,7 +64,6 @@ func MakeStartCommand(cfg *config.Config) *cobra.Command {
if err := cfg.WriteConfigFile(); err != nil {
return err
}
log.FeedbackInfo(cmd.Context(), fmt.Sprintf("Configuration loaded from DefraDB directory %v", cfg.Rootdir))
} else {
if err := cfg.CreateRootDirAndConfigFile(); err != nil {
return err
Expand Down
5 changes: 2 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (cfg *Config) LoadWithRootdir(withRootdir bool) error {
return err
}

if err := cfg.loadRootDirFromFlagOrDefault(); err != nil {
if err := cfg.LoadRootDirFromFlagOrDefault(); err != nil {
return err
}

Expand Down Expand Up @@ -156,9 +156,8 @@ func (cfg *Config) LoadWithRootdir(withRootdir bool) error {
return nil
}

func (cfg *Config) loadRootDirFromFlagOrDefault() error {
func (cfg *Config) LoadRootDirFromFlagOrDefault() error {
if cfg.Rootdir == "" {
// Check CLI flag
rootdir := cfg.v.GetString(RootdirKey)
if rootdir != "" {
return cfg.setRootdir(rootdir)
Expand Down
21 changes: 17 additions & 4 deletions tests/integration/cli/client_rpc_replicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,26 @@

package clitest

/* WIP client rpc replicator getall is broken currently
import (
"fmt"
"testing"
)

func TestReplicatorGetAllEmpty(t *testing.T) {
conf := NewDefraNodeDefaultConfig(t)
conf := NewDefraNodeDefaultConfig(t)
portTCP, err := findFreePortInRange(49152, 65535)
if err != nil {
t.Fatal(err)
}
conf.GRPCAddr = fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", portTCP)
if err != nil {
t.Fatal(err)
}

stopDefra := runDefraNode(t, conf)
defer stopDefra()

_, stderr := runDefraCommand(t, conf, []string{"client", "rpc", "replicator", "getall"})
tcpAddr := fmt.Sprintf("localhost:%d", portTCP)
_, stderr := runDefraCommand(t, conf, []string{"client", "--addr", tcpAddr, "rpc", "replicator", "getall"})
assertContainsSubstring(t, stderr, "No replicator found")
}
*/
44 changes: 0 additions & 44 deletions tests/integration/cli/client_schema_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,47 +49,3 @@ func TestAddSchemaWithDuplicateType(t *testing.T) {
assertContainsSubstring(t, stdout1, `{"data":{"result":"success"}}`)
assertContainsSubstring(t, stdout2, `schema type already exists. Name: Post`)
}

/* disabled because current implementation doesn't support this currently
func TestAddSchemaWithMultipleFiles(t *testing.T) {
conf := NewDefraNodeDefaultConfig(t)
stopDefra := runDefraNode(t, conf)
fname1 := schemaFileFixture(t, "schema1.graphql", `type Post { id: ID title: String }`)
fname2 := schemaFileFixture(t, "schema2.graphql", `type User { id: ID name: String }`)
stdout, _ := runDefraCommand(t, conf, []string{"client", "schema", "add", "-f", fname1, "-f", fname2})
nodeLog := stopDefra()
assertContainsSubstring(t, stdout, `{"data":{"result":"success"}}`)
assertNotContainsSubstring(t, nodeLog, "ERROR")
stdout, _ = runDefraCommand(t, conf, []string{"client", "query",
`query IntrospectionQuery { __schema { types { name } } }`})
assertContainsSubstring(t, stdout, `{"name":"Post"}`)
assertContainsSubstring(t, stdout, `{"name":"User"}`)
}
*/

/* disabled because current implementation doesn't support this currently
func TestAddSchemaWithMultipleFilesWithIntertwinedSchemas(t *testing.T) {
conf := NewDefraNodeDefaultConfig(t)
stopDefra := runDefraNode(t, conf)
fname1 := schemaFileFixture(t, "schema1.graphql", `type Post { id: ID title: String }`)
fname2 := schemaFileFixture(t, "schema2.graphql", `type User { id: ID posts: [Post] }`)
stdout, _ := runDefraCommand(t, conf, []string{"client", "schema", "add", "-f", fname1, "-f", fname2})
nodeLog := stopDefra()
assertContainsSubstring(t, stdout, `{"data":{"result":"success"}}`)
assertNotContainsSubstring(t, nodeLog, "ERROR")
stdout, _ = runDefraCommand(t, conf, []string{"client", "query",
`query IntrospectionQuery { __schema { types { name } } }`})
assertContainsSubstring(t, stdout, `{"name":"Post"}`)
assertContainsSubstring(t, stdout, `{"name":"User"}`)
}
*/

0 comments on commit 0983d4f

Please sign in to comment.