Add some negative tests for invoke and publish

This commit is contained in:
Charlie Stanley 2021-04-09 12:37:25 -07:00
parent 7b69e1a6c6
commit ff73dabd02
3 changed files with 48 additions and 2 deletions

View File

@ -39,6 +39,11 @@ dapr invoke --app-id target --method sample --verb GET
Run: func(cmd *cobra.Command, args []string) {
bytePayload := []byte{}
var err error
if invokeDataFile != "" && invokeData != "" {
print.FailureStatusEvent(os.Stdout, "Only one of --data and --data-file allowed in the same invoke command")
os.Exit(1)
}
if invokeDataFile != "" {
bytePayload, err = ioutil.ReadFile(invokeDataFile)
if err != nil {

View File

@ -33,6 +33,11 @@ dapr publish --publish-app-id myapp --pubsub target --topic sample --data '{"key
Run: func(cmd *cobra.Command, args []string) {
bytePayload := []byte{}
var err error
if publishPayloadFile != "" && publishPayload != "" {
print.FailureStatusEvent(os.Stdout, "Only one of --data and --data-file allowed in the same publish command")
os.Exit(1)
}
if publishPayloadFile != "" {
bytePayload, err = ioutil.ReadFile(publishPayloadFile)
if err != nil {

View File

@ -397,6 +397,12 @@ func testList(t *testing.T) {
t.Log(output)
require.NoError(t, err, "dapr list failed")
listtOutputCheck(t, output)
// We can call stop so as not to wait for the app to time out
output, err = spawn.Command(getDaprPath(), "stop", "--app-id", "dapr_e2e_list")
t.Log(output)
require.NoError(t, err, "dapr stop failed")
assert.Contains(t, output, "app stopped successfully: dapr_e2e_list")
}, "run", "--app-id", "dapr_e2e_list", "-H", "3555", "-G", "4555", "--", "bash", "-c", "sleep 10 ; exit 0")
}
@ -457,6 +463,22 @@ func testPublish(t *testing.T) {
assert.Equal(t, map[string]interface{}{"cli": "is_working"}, event.Data)
})
t.Run("publish from non-existant file fails", func(t *testing.T) {
output, err := spawn.Command(daprPath, "publish", "--publish-app-id", "pub_e2e", "--pubsub", "pubsub", "--topic", "sample", "--data-file", "a/file/that/does/not/exist")
t.Log(output)
assert.Contains(t, output, "Error reading payload from 'a/file/that/does/not/exist'. Error: ")
assert.Error(t, err, "a non-existant --data-file should fail with error")
})
t.Run("publish only one of data and data-file", func(t *testing.T) {
output, err := spawn.Command(daprPath, "publish", "--publish-app-id", "pub_e2e", "--pubsub", "pubsub", "--topic", "sample", "--data-file", "../testdata/message.json", "--data", "{\"cli\": \"is_working\"}")
t.Log(output)
assert.Error(t, err, "--data and --data-file should not be allowed together")
assert.Contains(t, output, "Only one of --data and --data-file allowed in the same publish command")
})
output, err := spawn.Command(getDaprPath(), "stop", "--app-id", "pub_e2e")
t.Log(output)
require.NoError(t, err, "dapr stop failed")
@ -491,7 +513,7 @@ func testInvoke(t *testing.T) {
t.Run("data from file", func(t *testing.T) {
output, err := spawn.Command(daprPath, "invoke", "--app-id", "invoke_e2e", "--method", "test", "--data-file", "../testdata/message.json")
t.Log(output)
assert.NoError(t, err, "unable to publish from --data-file")
assert.NoError(t, err, "unable to invoke with --data-file")
assert.Contains(t, output, "App invoked successfully")
assert.Contains(t, output, "{\"dapr\": \"is_great\"}")
})
@ -499,11 +521,25 @@ func testInvoke(t *testing.T) {
t.Run("data from string", func(t *testing.T) {
output, err := spawn.Command(daprPath, "invoke", "--app-id", "invoke_e2e", "--method", "test", "--data", "{\"cli\": \"is_working\"}")
t.Log(output)
assert.NoError(t, err, "unable to publish from --data")
assert.NoError(t, err, "unable to invoke with --data")
assert.Contains(t, output, "{\"cli\": \"is_working\"}")
assert.Contains(t, output, "App invoked successfully")
})
t.Run("data from non-existant file fails", func(t *testing.T) {
output, err := spawn.Command(daprPath, "invoke", "--app-id", "invoke_e2e", "--method", "test", "--data-file", "a/file/that/does/not/exist")
t.Log(output)
assert.Error(t, err, "a non-existant --data-file should fail with error")
assert.Contains(t, output, "Error reading payload from 'a/file/that/does/not/exist'. Error: ")
})
t.Run("invoke only one of data and data-file", func(t *testing.T) {
output, err := spawn.Command(daprPath, "invoke", "--app-id", "invoke_e2e", "--method", "test", "--data-file", "../testdata/message.json", "--data", "{\"cli\": \"is_working\"}")
t.Log(output)
assert.Error(t, err, "--data and --data-file should not be allowed together")
assert.Contains(t, output, "Only one of --data and --data-file allowed in the same invoke command")
})
output, err := spawn.Command(getDaprPath(), "stop", "--app-id", "invoke_e2e")
t.Log(output)
require.NoError(t, err, "dapr stop failed")