Skip to content

Commit

Permalink
Fix subscription match nil format. (#7067)
Browse files Browse the repository at this point in the history
* fix subscription match nil format.

Signed-off-by: zhangchao <zchao9100@gmail.com>

* fix reivew

Signed-off-by: zhangchao <zchao9100@gmail.com>

* add integration test

Signed-off-by: zhangchao <zchao9100@gmail.com>

* tiny fix for the test

Signed-off-by: zhangchao <zchao9100@gmail.com>

---------

Signed-off-by: zhangchao <zchao9100@gmail.com>
  • Loading branch information
Taction committed Oct 21, 2023
1 parent a2270ea commit e09bd30
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 9 deletions.
3 changes: 3 additions & 0 deletions pkg/expr/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func (e *Expr) Expr() string {
}

func (e *Expr) String() string {
if e == nil {
return ""
}
return e.expr
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/grpc/universalapi/api_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package universalapi

import (
"context"
"fmt"

"google.golang.org/protobuf/types/known/emptypb"

Expand Down Expand Up @@ -140,14 +139,15 @@ func metadataGetOrDefaultCapabilities(dict map[string][]string, key string) []st

func metadataConvertPubSubSubscriptionRules(rules []*runtimePubsub.Rule) *runtimev1pb.PubsubSubscriptionRules {
out := &runtimev1pb.PubsubSubscriptionRules{
Rules: make([]*runtimev1pb.PubsubSubscriptionRule, 0),
Rules: make([]*runtimev1pb.PubsubSubscriptionRule, len(rules)),
}
for _, r := range rules {
out.Rules = append(out.Rules, &runtimev1pb.PubsubSubscriptionRule{
// TODO avoid using fmt.Sprintf
Match: fmt.Sprintf("%s", r.Match),
Path: r.Path,
})
for i, r := range rules {
out.Rules[i] = &runtimev1pb.PubsubSubscriptionRule{
Path: r.Path,
}
if r.Match != nil {
out.Rules[i].Match = r.Match.String()
}
}
return out
}
4 changes: 4 additions & 0 deletions pkg/runtime/pubsub/subscription.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pubsub

import "fmt"

type Subscription struct {
PubsubName string `json:"pubsubname"`
Topic string `json:"topic"`
Expand All @@ -22,5 +24,7 @@ type Rule struct {
}

type Expr interface {
fmt.Stringer

Eval(variables map[string]interface{}) (interface{}, error)
}
23 changes: 23 additions & 0 deletions pkg/runtime/pubsub/subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,3 +798,26 @@ func TestK8sSubscriptions(t *testing.T) {
assert.Equal(t, "testValue", subs[0].Metadata["testName"])
}
}

func TestGetRuleMatchString(t *testing.T) {
cases := []subscriptionsapiV2alpha1.Rule{
{
Match: `event.type == "myevent.v3"`,
Path: "myroute.v3",
},
{
Match: `event.type == "myevent.v2"`,
Path: "myroute.v2",
},
{
Match: "",
Path: "myroute.v1",
},
}

for _, v := range cases {
rule, err := createRoutingRule(v.Match, v.Path)
assert.NoError(t, err)
assert.Equal(t, v.Match, rule.Match.String())
}
}
33 changes: 32 additions & 1 deletion tests/integration/suite/daprd/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,25 @@ type metadata struct {
}

func (m *metadata) Setup(t *testing.T) []framework.Option {
m.proc = procdaprd.New(t)
subComponentAndConfiguration := `
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: pubsub
spec:
type: pubsub.in-memory
version: v1
---
apiVersion: dapr.io/v1alpha1
kind: Subscription
metadata:
name: sub
spec:
topic: B
route: /B
pubsubname: pubsub
`
m.proc = procdaprd.New(t, procdaprd.WithResourceFiles(subComponentAndConfiguration))
return []framework.Option{
framework.WithProcesses(m.proc),
}
Expand Down Expand Up @@ -95,4 +113,17 @@ func validateResponse(t *testing.T, appID string, appPort int, body io.Reader) {
require.Equal(t, appPort, int(port))
require.Equal(t, "http", appConnectionProperties["protocol"])
require.Equal(t, "127.0.0.1", appConnectionProperties["channelAddress"])

// validate that the metadata contains correct format of subscription.
// The http response struct is private, so we use assert here.
subscriptions, ok := bodyMap["subscriptions"].([]interface{})
require.True(t, ok)
subscription, ok := subscriptions[0].(map[string]interface{})
require.True(t, ok)
rules, ok := subscription["rules"].([]interface{})
require.True(t, ok)
rule, ok := rules[0].(map[string]interface{})
require.True(t, ok)
require.Empty(t, rule["match"])
require.Equal(t, "/B", rule["path"])
}

0 comments on commit e09bd30

Please sign in to comment.