mirror of https://github.com/dapr/cli.git
Add publish --from-file option and e2e tests
This commit is contained in:
parent
65122e4ce2
commit
3dbaed0d3d
|
@ -7,6 +7,7 @@ package cmd
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/dapr/cli/pkg/print"
|
||||
|
@ -15,10 +16,11 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
publishAppID string
|
||||
pubsubName string
|
||||
publishTopic string
|
||||
publishPayload string
|
||||
publishAppID string
|
||||
pubsubName string
|
||||
publishTopic string
|
||||
publishPayload string
|
||||
publishPayloadFile string
|
||||
)
|
||||
|
||||
var PublishCmd = &cobra.Command{
|
||||
|
@ -29,8 +31,20 @@ var PublishCmd = &cobra.Command{
|
|||
dapr publish --publish-app-id myapp --pubsub target --topic sample --data '{"key":"value"}'
|
||||
`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
bytePayload := []byte{}
|
||||
var err error
|
||||
if publishPayloadFile != "" {
|
||||
bytePayload, err = ioutil.ReadFile(publishPayloadFile)
|
||||
if err != nil {
|
||||
print.FailureStatusEvent(os.Stdout, "Error reading payload from '%s'. Error: %s", publishPayloadFile, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else if publishPayload != "" {
|
||||
bytePayload = []byte(publishPayload)
|
||||
}
|
||||
|
||||
client := standalone.NewClient()
|
||||
err := client.Publish(publishAppID, pubsubName, publishTopic, publishPayload)
|
||||
err = client.Publish(publishAppID, pubsubName, publishTopic, bytePayload)
|
||||
if err != nil {
|
||||
print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error publishing topic %s: %s", publishTopic, err))
|
||||
os.Exit(1)
|
||||
|
@ -45,6 +59,7 @@ func init() {
|
|||
PublishCmd.Flags().StringVarP(&pubsubName, "pubsub", "p", "", "The name of the pub/sub component")
|
||||
PublishCmd.Flags().StringVarP(&publishTopic, "topic", "t", "", "The topic to be published to")
|
||||
PublishCmd.Flags().StringVarP(&publishPayload, "data", "d", "", "The JSON serialized data string (optional)")
|
||||
PublishCmd.Flags().StringVarP(&publishPayloadFile, "data-file", "f", "", "A file containing the JSON serialized data (optional)")
|
||||
PublishCmd.Flags().BoolP("help", "h", false, "Print this help message")
|
||||
PublishCmd.MarkFlagRequired("publish-app-id")
|
||||
PublishCmd.MarkFlagRequired("topic")
|
||||
|
|
1
go.mod
1
go.mod
|
@ -7,6 +7,7 @@ require (
|
|||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
|
||||
github.com/briandowns/spinner v1.6.1
|
||||
github.com/dapr/dapr v1.0.0-rc.1.0.20201217002310-310e670d987b
|
||||
github.com/dapr/go-sdk v1.0.0
|
||||
github.com/docker/docker v17.12.0-ce-rc1.0.20200618181300-9dc6525e6118+incompatible
|
||||
github.com/fatih/color v1.7.0
|
||||
github.com/go-ole/go-ole v1.2.4 // indirect
|
||||
|
|
14
go.sum
14
go.sum
|
@ -237,6 +237,7 @@ github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp
|
|||
github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
|
||||
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
|
||||
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
|
||||
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
|
||||
|
@ -272,6 +273,7 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc
|
|||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
||||
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4=
|
||||
github.com/cyphar/filepath-securejoin v0.2.2 h1:jCwT2GTP+PY5nBz3c/YL5PAIbusElVrPujOBSCj8xRg=
|
||||
github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2G9VLXONKD9G7QGMM+4=
|
||||
|
@ -282,6 +284,8 @@ github.com/dapr/dapr v0.4.1-0.20200228055659-71892bc0111e/go.mod h1:c60DJ9TdSdpb
|
|||
github.com/dapr/dapr v1.0.0-rc.1.0.20201217002310-310e670d987b h1:RKmKCmzFf78U+P9AowyLhqg03uWR+sZ4aFVcySOsHSg=
|
||||
github.com/dapr/dapr v1.0.0-rc.1.0.20201217002310-310e670d987b/go.mod h1:RheO+S/0TSVzji/NHlD9TgpC1FgxXfadWVkDqibBAP4=
|
||||
github.com/dapr/go-sdk v0.0.0-20200121181907-48249cda2fad/go.mod h1:yeOIFBz6+BigHpk4ASJbgQDVjQ8+00oCWrFyOAFdob8=
|
||||
github.com/dapr/go-sdk v1.0.0 h1:3nOmFzsPaHzzgINQQD9AFofVX6LV8QYJtpogaBfwkUg=
|
||||
github.com/dapr/go-sdk v1.0.0/go.mod h1:zyhsocIKv4pqQ2VtvWvf2CK1UhP7Z2OAOXgEpVxMgIs=
|
||||
github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
|
@ -347,6 +351,7 @@ github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4s
|
|||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/evanphx/json-patch v0.0.0-20190203023257-5858425f7550/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
|
||||
github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
|
||||
|
@ -740,6 +745,7 @@ github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
|
|||
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/kubernetes-client/go v0.0.0-20190625181339-cd8e39e789c7/go.mod h1:ks4KCmmxdXksTSu2dlnUanEOqNd/dsoyS6/7bay2RQ8=
|
||||
github.com/kubernetes-client/go v0.0.0-20190928040339-c757968c4c36/go.mod h1:ks4KCmmxdXksTSu2dlnUanEOqNd/dsoyS6/7bay2RQ8=
|
||||
github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g=
|
||||
|
@ -863,6 +869,7 @@ github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OS
|
|||
github.com/nats-io/stan.go v0.5.0/go.mod h1:dYqB+vMN3C2F9pT1FRQpg9eHbjPj6mP0yYuyBNuXHZE=
|
||||
github.com/nats-io/stan.go v0.6.0/go.mod h1:eIcD5bi3pqbHT/xIIvXMwvzXYElgouBvaVRftaE+eac=
|
||||
github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||
github.com/nightlyone/lockfile v0.0.0-20180618180623-0ad87eef1443 h1:+2OJrU8cmOstEoh0uQvYemRGVH1O6xtO2oANUWHFnP0=
|
||||
github.com/nightlyone/lockfile v0.0.0-20180618180623-0ad87eef1443/go.mod h1:JbxfV1Iifij2yhRjXai0oFrbpxszXHRx1E5RuM26o4Y=
|
||||
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
|
||||
|
@ -1295,6 +1302,8 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R
|
|||
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b h1:uwuIcX0g4Yl1NC5XAz37xsr2lTtcqevgzYNVt49waME=
|
||||
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb h1:eBmm0M9fYhWpKZLjQUUKka/LtIxf46G4fxeEz5KJr9U=
|
||||
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20190402181905-9f3314589c9a/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
|
@ -1472,6 +1481,8 @@ google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6D
|
|||
google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
|
||||
google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a h1:pOwg4OoaRYScjmR4LlLgdtnyoHYTSAVhhqe5uPdpII8=
|
||||
google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
|
||||
google.golang.org/genproto v0.0.0-20201204160425-06b3db808446 h1:65ppmIPdaZE+BO34gntwqexoTYr30IRNGmS0OGOHu3A=
|
||||
google.golang.org/genproto v0.0.0-20201204160425-06b3db808446/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
|
||||
google.golang.org/grpc v0.0.0-20160317175043-d3ddb4469d5a/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
|
||||
google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
|
||||
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
|
||||
|
@ -1497,6 +1508,8 @@ google.golang.org/grpc v1.31.1/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM
|
|||
google.golang.org/grpc v1.32.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
|
||||
google.golang.org/grpc v1.33.1 h1:DGeFlSan2f+WEtCERJ4J9GJWk15TxUi8QGagfI87Xyc=
|
||||
google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0=
|
||||
google.golang.org/grpc v1.34.0 h1:raiipEjMOIC/TO2AvyTxP25XFdLxNIBwzDh3FM3XztI=
|
||||
google.golang.org/grpc v1.34.0/go.mod h1:WotjhfgOW/POjDeRt8vscBtXq+2VjORFy659qA51WJ8=
|
||||
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
||||
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
||||
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
||||
|
@ -1516,6 +1529,7 @@ gopkg.in/check.v1 v1.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
|
|||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
|
||||
gopkg.in/couchbase/gocb.v1 v1.6.4/go.mod h1:Ri5Qok4ZKiwmPr75YxZ0uELQy45XJgUSzeUnK806gTY=
|
||||
gopkg.in/couchbase/gocbcore.v7 v7.1.15/go.mod h1:48d2Be0MxRtsyuvn+mWzqmoGUG9uA00ghopzOs148/E=
|
||||
|
|
|
@ -17,7 +17,7 @@ type Client interface {
|
|||
// Invoke is a command to invoke a remote or local dapr instance
|
||||
Invoke(appID, method, data, verb string) (string, error)
|
||||
// Publish is used to publish event to a topic in a pubsub for an app ID.
|
||||
Publish(publishAppID, pubsubName, topic, payload string) error
|
||||
Publish(publishAppID, pubsubName, topic string, payload []byte) error
|
||||
}
|
||||
|
||||
type Standalone struct {
|
||||
|
|
|
@ -15,7 +15,7 @@ import (
|
|||
)
|
||||
|
||||
// Publish publishes payload to topic in pubsub referenced by pubsubName.
|
||||
func (s *Standalone) Publish(publishAppID, pubsubName, topic, payload string) error {
|
||||
func (s *Standalone) Publish(publishAppID, pubsubName, topic string, payload []byte) error {
|
||||
if publishAppID == "" {
|
||||
return errors.New("publishAppID is missing")
|
||||
}
|
||||
|
@ -38,15 +38,9 @@ func (s *Standalone) Publish(publishAppID, pubsubName, topic, payload string) er
|
|||
return err
|
||||
}
|
||||
|
||||
b := []byte{}
|
||||
|
||||
if payload != "" {
|
||||
b = []byte(payload)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("http://localhost:%s/v%s/publish/%s/%s", fmt.Sprintf("%v", daprHTTPPort), api.RuntimeAPIVersion, pubsubName, topic)
|
||||
// nolint: gosec
|
||||
r, err := http.Post(url, "application/json", bytes.NewBuffer(b))
|
||||
r, err := http.Post(url, "application/json", bytes.NewBuffer(payload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ func TestPublish(t *testing.T) {
|
|||
name string
|
||||
publishAppID string
|
||||
pubsubName string
|
||||
payload string
|
||||
payload []byte
|
||||
topic string
|
||||
lo ListOutput
|
||||
listErr error
|
||||
|
@ -29,7 +29,7 @@ func TestPublish(t *testing.T) {
|
|||
{
|
||||
name: "test empty topic",
|
||||
publishAppID: "",
|
||||
payload: "test",
|
||||
payload: []byte("test"),
|
||||
pubsubName: "test",
|
||||
errString: "publishAppID is missing",
|
||||
errorExpected: true,
|
||||
|
@ -37,7 +37,7 @@ func TestPublish(t *testing.T) {
|
|||
{
|
||||
name: "test empty topic",
|
||||
publishAppID: "test",
|
||||
payload: "test",
|
||||
payload: []byte("test"),
|
||||
pubsubName: "test",
|
||||
errString: "topic is missing",
|
||||
errorExpected: true,
|
||||
|
@ -45,7 +45,7 @@ func TestPublish(t *testing.T) {
|
|||
{
|
||||
name: "test empty pubsubName",
|
||||
publishAppID: "test",
|
||||
payload: "test",
|
||||
payload: []byte("test"),
|
||||
topic: "test",
|
||||
errString: "pubsubName is missing",
|
||||
errorExpected: true,
|
||||
|
@ -53,7 +53,7 @@ func TestPublish(t *testing.T) {
|
|||
{
|
||||
name: "test list error",
|
||||
publishAppID: "test",
|
||||
payload: "test",
|
||||
payload: []byte("test"),
|
||||
topic: "test",
|
||||
pubsubName: "test",
|
||||
listErr: assert.AnError,
|
||||
|
@ -63,7 +63,7 @@ func TestPublish(t *testing.T) {
|
|||
{
|
||||
name: "test empty appID in list output",
|
||||
publishAppID: "test",
|
||||
payload: "test",
|
||||
payload: []byte("test"),
|
||||
topic: "test",
|
||||
pubsubName: "test",
|
||||
lo: ListOutput{
|
||||
|
@ -78,7 +78,7 @@ func TestPublish(t *testing.T) {
|
|||
publishAppID: "myAppID",
|
||||
pubsubName: "testPubsubName",
|
||||
topic: "testTopic",
|
||||
payload: "test payload",
|
||||
payload: []byte("test payload"),
|
||||
lo: ListOutput{
|
||||
AppID: "not my myAppID",
|
||||
},
|
||||
|
@ -90,7 +90,7 @@ func TestPublish(t *testing.T) {
|
|||
publishAppID: "myAppID",
|
||||
pubsubName: "testPubsubName",
|
||||
topic: "testTopic",
|
||||
payload: "test payload",
|
||||
payload: []byte("test payload"),
|
||||
expectedPath: "/v1.0/publish/testPubsubName/testTopic",
|
||||
postResponse: "test payload",
|
||||
lo: ListOutput{
|
||||
|
|
|
@ -24,6 +24,8 @@ import (
|
|||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/dapr/cli/tests/e2e/spawn"
|
||||
"github.com/dapr/go-sdk/service/common"
|
||||
daprHttp "github.com/dapr/go-sdk/service/http"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
)
|
||||
|
@ -45,6 +47,7 @@ func TestStandaloneInstall(t *testing.T) {
|
|||
{"test install", testInstall},
|
||||
{"test run", testRun},
|
||||
{"test stop", testStop},
|
||||
{"test publish", testPublish},
|
||||
{"test uninstall", testUninstall},
|
||||
}
|
||||
|
||||
|
@ -304,11 +307,11 @@ func testRun(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func testStop(t *testing.T) {
|
||||
func executeAgainstRunningDapr(t *testing.T, f func(), daprArgs ...string) {
|
||||
daprPath := getDaprPath()
|
||||
|
||||
cmd := exec.Command(daprPath, "run", "--app-id", "dapr_e2e_stop", "--", "bash", "-c", "sleep 60 ; exit 1")
|
||||
reader, _ := cmd.StdoutPipe()
|
||||
cmd := exec.Command(daprPath, daprArgs...)
|
||||
reader, _ := cmd.StdoutPipe()
|
||||
scanner := bufio.NewScanner(reader)
|
||||
|
||||
cmd.Start()
|
||||
|
@ -317,18 +320,79 @@ func testStop(t *testing.T) {
|
|||
for scanner.Scan() {
|
||||
outputChunk := scanner.Text()
|
||||
t.Log(outputChunk)
|
||||
if strings.Contains(outputChunk, "You're up and running! Both Dapr and your app logs will appear here.") {
|
||||
output, err := spawn.Command(daprPath, "stop", "--app-id", "dapr_e2e_stop")
|
||||
t.Log(output)
|
||||
require.NoError(t, err, "dapr stop failed")
|
||||
assert.Contains(t, output, "app stopped successfully: dapr_e2e_stop")
|
||||
if strings.Contains(outputChunk, "You're up and running!") {
|
||||
f()
|
||||
}
|
||||
daprOutput += outputChunk
|
||||
}
|
||||
|
||||
err := cmd.Wait()
|
||||
require.NoError(t, err, "dapr didn't exit cleanly")
|
||||
assert.Contains(t, daprOutput, "Exited App successfully", "Stop command should have been called before the app had a chance to exit")
|
||||
assert.NotContains(t, daprOutput, "The App process exited with error code: exit status", "Stop command should have been called before the app had a chance to exit")
|
||||
assert.Contains(t, daprOutput, "Exited Dapr successfully")
|
||||
}
|
||||
|
||||
func testStop(t *testing.T) {
|
||||
executeAgainstRunningDapr(t, func() {
|
||||
output, err := spawn.Command(getDaprPath(), "stop", "--app-id", "dapr_e2e_stop")
|
||||
t.Log(output)
|
||||
require.NoError(t, err, "dapr stop failed")
|
||||
assert.Contains(t, output, "app stopped successfully: dapr_e2e_stop")
|
||||
|
||||
}, "run", "--app-id", "dapr_e2e_stop", "--", "bash", "-c", "sleep 60 ; exit 1")
|
||||
}
|
||||
|
||||
func testPublish(t *testing.T) {
|
||||
var sub = &common.Subscription{
|
||||
PubsubName: "pubsub",
|
||||
Topic: "sample",
|
||||
Route: "/orders",
|
||||
}
|
||||
|
||||
s := daprHttp.NewService(":9988")
|
||||
|
||||
events := make(chan *common.TopicEvent)
|
||||
|
||||
err := s.AddTopicEventHandler(sub, func(ctx context.Context, e *common.TopicEvent) (retry bool, err error) {
|
||||
events <- e
|
||||
return false, nil
|
||||
})
|
||||
|
||||
assert.NoError(t, err, "unable to AddTopicEventHandler")
|
||||
|
||||
defer s.Stop()
|
||||
go func() {
|
||||
err = s.Start()
|
||||
|
||||
assert.NoError(t, err, "unable to listen on :9988")
|
||||
}()
|
||||
|
||||
daprPath := getDaprPath()
|
||||
executeAgainstRunningDapr(t, func() {
|
||||
t.Run("publish from 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")
|
||||
t.Log(output)
|
||||
assert.NoError(t, err, "unable to publish from --data-file")
|
||||
assert.Contains(t, output, "Event published successfully")
|
||||
|
||||
event := <-events
|
||||
assert.Equal(t, map[string]interface{}{"dapr": "is_great"}, event.Data)
|
||||
})
|
||||
|
||||
t.Run("publish from string", func(t *testing.T) {
|
||||
output, err := spawn.Command(daprPath, "publish", "--publish-app-id", "pub_e2e", "--pubsub", "pubsub", "--topic", "sample", "--data", "{\"cli\": \"is_working\"}")
|
||||
t.Log(output)
|
||||
assert.NoError(t, err, "unable to publish from --data")
|
||||
assert.Contains(t, output, "Event published successfully")
|
||||
|
||||
event := <-events
|
||||
assert.Equal(t, map[string]interface{}{"cli": "is_working"}, event.Data)
|
||||
})
|
||||
|
||||
output, err := spawn.Command(getDaprPath(), "stop", "--app-id", "pub_e2e")
|
||||
t.Log(output)
|
||||
require.NoError(t, err, "dapr stop failed")
|
||||
assert.Contains(t, output, "app stopped successfully: pub_e2e")
|
||||
}, "run", "--app-id", "pub_e2e", "--app-port", "9988")
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
{"dapr": "is_great"}
|
Loading…
Reference in New Issue