Skip to content

Commit

Permalink
cli integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
orpheuslummis committed Apr 24, 2023
1 parent 4f2c3cb commit 226ab35
Show file tree
Hide file tree
Showing 15 changed files with 898 additions and 3 deletions.
41 changes: 41 additions & 0 deletions tests/integration/cli/client_blocks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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.

package clitest

import "testing"

func TestClientBlocksEmpty(t *testing.T) {
conf := NewDefraNodeDefaultConfig(t)
stdout, _ := runDefraCommand(t, conf, []string{"client", "blocks"})
assertContainsSubstring(t, stdout, "Usage:")
}

func TestClientBlocksGetEmpty(t *testing.T) {
conf := NewDefraNodeDefaultConfig(t)
stdout, _ := runDefraCommand(t, conf, []string{"client", "blocks", "get"})
assertContainsSubstring(t, stdout, "Usage:")
}

func TestClientBlocksGetInvalidCID(t *testing.T) {
conf := NewDefraNodeDefaultConfig(t)
stopDefra := runDefraNode(t, conf)
stdout, _ := runDefraCommand(t, conf, []string{"client", "blocks", "get", "invalid-cid"})
_ = stopDefra()
assertContainsSubstring(t, stdout, "\"errors\"")
}

func TestClientBlocksGetNonExistentCID(t *testing.T) {
conf := NewDefraNodeDefaultConfig(t)
stopDefra := runDefraNode(t, conf)
stdout, _ := runDefraCommand(t, conf, []string{"client", "blocks", "get", "bafybeieelb43ol5e5jiick2p7k4p577ph72ecwcuowlhbops4hpz24zhz4"})
_ = stopDefra()
assertContainsSubstring(t, stdout, "could not find")
}
34 changes: 34 additions & 0 deletions tests/integration/cli/client_peerid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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.

package clitest

import (
"testing"
)

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

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

defraLogLines := stopDefra()

assertNotContainsSubstring(t, defraLogLines, "ERROR")

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")
}
63 changes: 63 additions & 0 deletions tests/integration/cli/client_ping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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.

package clitest

import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"

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

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

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

nodeLog := stopDefra()

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

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

nodeLog := stopDefra()

for _, line := range nodeLog {
assert.NotContains(t, line, "ERROR")
}
// for some line in stderr to contain the error message
for _, line := range stderr {
if strings.Contains(line, config.ErrFailedToValidateConfig.Error()) {
return
}
}
t.Error("expected error message not found in stderr")
}

func TestPingCommandNoHost(t *testing.T) {
conf := NewDefraNodeDefaultConfig(t)
p, err := findFreePortInRange(49152, 65535)
assert.NoError(t, err)
addr := fmt.Sprintf("localhost:%d", p)
_, stderr := runDefraCommand(t, conf, []string{"client", "ping", "--url", addr})
assertContainsSubstring(t, stderr, "failed to send ping")
}
66 changes: 66 additions & 0 deletions tests/integration/cli/client_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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.

package clitest

import (
"testing"
)

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

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

assertContainsSubstring(t, stdout, "Query")
assertNotContainsSubstring(t, nodeLog, "ERROR")
}

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

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

assertContainsSubstring(t, stdout, "Syntax Error")
}

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

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

assertContainsSubstring(t, stdout, "Cannot query field")
}

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

fname := schemaFileFixture(t, "schema.graphql", `
type User {
id: ID
name: String
}`)
stdout, _ := runDefraCommand(t, conf, []string{"client", "schema", "add", "-f", fname})
assertContainsSubstring(t, stdout, "success")

stdout, _ = runDefraCommand(t, conf, []string{"client", "query", "query { User { nonexistent } }"})

assertContainsSubstring(t, stdout, `Cannot query field \"nonexistent\"`)
}
13 changes: 13 additions & 0 deletions tests/integration/cli/client_rpc_p2p_collection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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.

package clitest

// TBD
22 changes: 22 additions & 0 deletions tests/integration/cli/client_rpc_replicator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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.

package clitest

/* WIP client rpc replicator getall is broken currently
func TestReplicatorGetAllEmpty(t *testing.T) {
conf := NewDefraNodeDefaultConfig(t)
stopDefra := runDefraNode(t, conf)
defer stopDefra()
_, stderr := runDefraCommand(t, conf, []string{"client", "rpc", "replicator", "getall"})
assertContainsSubstring(t, stderr, "No replicator found")
}
*/
95 changes: 95 additions & 0 deletions tests/integration/cli/client_schema_add_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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.

package clitest

import (
"testing"

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

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

fname := schemaFileFixture(t, "schema.graphql", `
type User {
id: ID
name: String
}`)

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

nodeLog := stopDefra()

assert.Contains(t, stdout, `{"data":{"result":"success"}}`)
assertNotContainsSubstring(t, nodeLog, "ERROR")
}

func TestAddSchemaWithDuplicateType(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 Post { id: ID author: String }`)

stdout1, _ := runDefraCommand(t, conf, []string{"client", "schema", "add", "-f", fname1})
stdout2, _ := runDefraCommand(t, conf, []string{"client", "schema", "add", "-f", fname2})

_ = stopDefra()

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"}`)
}
*/
53 changes: 53 additions & 0 deletions tests/integration/cli/client_schema_patch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.

package clitest

import (
"testing"
)

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

fname := schemaFileFixture(t, "schema.graphql", `
type User {
id: ID
name: String
}`)
stdout, _ := runDefraCommand(t, conf, []string{"client", "schema", "add", "-f", fname})
assertContainsSubstring(t, stdout, "success")

stdout, _ = runDefraCommand(t, conf, []string{"client", "schema", "patch", `[{ "op": "add", "path": "/User/Schema/Fields/-", "value": {"Name": "address", "Kind": "String"} }]`})
assertContainsSubstring(t, stdout, "success")

stdout, _ = runDefraCommand(t, conf, []string{"client", "query", `query IntrospectionQuery { __type (name: "User") { fields { name } }}`})
assertContainsSubstring(t, stdout, "address")
}

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

fname := schemaFileFixture(t, "schema.graphql", `
type User {
id: ID
name: String
}
`)
stdout, _ := runDefraCommand(t, conf, []string{"client", "schema", "add", "-f", fname})
assertContainsSubstring(t, stdout, "success")

stdout, _ = runDefraCommand(t, conf, []string{"client", "schema", "patch", `[{ "op": "invalidOp" }]`})
assertContainsSubstring(t, stdout, "Internal Server Error")
}
Loading

0 comments on commit 226ab35

Please sign in to comment.