Skip to content

Commit 7ba062f

Browse files
Override the expected converted Pub/Sub payload (#69)
There are multiple legacy Pub/Sub payload schemas. For example, see: - legacy_pubsub-legacy-output.json - pubsub_text-legacy-output.json When converting Cloud Events to legacy events there is no information in the CE that enables us to infer which schema to convert to. Unfortunately, this tool assumes the converted output will always match a corresponding unconverted payload, which isn't necessarily possible. This commit adds a mechanism to override the exepcted output when converting between event types and creates an override for converting legacy_pubsub test case
1 parent be47fab commit 7ba062f

File tree

7 files changed

+129
-17
lines changed

7 files changed

+129
-17
lines changed

events/events.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,17 @@ func InputData(name string, t EventType) []byte {
7979
}
8080

8181
// OutputData returns the contents of the output event for a particular event name and type.
82-
func OutputData(name string, t EventType) []byte {
82+
func OutputData(name string, t EventType, isConversion bool) []byte {
8383
switch t {
8484
case LegacyEvent:
85+
if isConversion && Events[name].ConvertedOutput.LegacyEvent != nil {
86+
return Events[name].ConvertedOutput.LegacyEvent
87+
}
8588
return Events[name].Output.LegacyEvent
8689
case CloudEvent:
90+
if isConversion && Events[name].ConvertedOutput.CloudEvent != nil {
91+
return Events[name].ConvertedOutput.CloudEvent
92+
}
8793
return Events[name].Output.CloudEvent
8894
}
8995
return nil

events/events_data.go

Lines changed: 57 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

events/generate/data/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ Generally, try to stay consistent across tests for the following fields (arbitra
2828
- Timestamp: "2020-09-29T11:32:00.000Z"
2929
- Project ID: my-project-id
3030

31+
The `output-converted.json` suffix can be used to override the expected output
32+
when converting between event types. For example, consider the following files:
33+
34+
- `newevent-legacy-output.json`
35+
- `newevent-legacy-output-converted.json`
36+
37+
The `newevent-legacy-output.json` file will be used to validate legacy events,
38+
and `newevent-legacy-output-converted.json` will be used to validate converting
39+
between cloud events and legacy events.
40+
3141
Once you have the input and output data, generate the test cases to embed them
3242
in the binary. Run the following:
3343

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"context": {
3+
"eventId": "aaaaaa-1111-bbbb-2222-cccccccccccc",
4+
"timestamp": "2020-09-29T11:32:00.000Z",
5+
"eventType":"google.pubsub.topic.publish",
6+
"resource": {
7+
"service":"pubsub.googleapis.com",
8+
"name":"projects/sample-project/topics/gcf-test",
9+
"type":"type.googleapis.com/google.pubsub.v1.PubsubMessage"
10+
}
11+
},
12+
"data": {
13+
"@type": "type.googleapis.com/google.pubsub.v1.PubsubMessage",
14+
"attributes": {
15+
"attribute1": "value1"
16+
},
17+
"data": "VGhpcyBpcyBhIHNhbXBsZSBtZXNzYWdl"
18+
}
19+
}

events/generate/events_generate.go

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var (
1919
const (
2020
input = "input"
2121
output = "output"
22+
converted = "converted"
2223
legacyType = "legacy"
2324
cloudeventType = "cloudevent"
2425
dataDir = "generate/data"
@@ -33,8 +34,9 @@ type EventData struct {
3334
}
3435
3536
type Event struct {
36-
Input EventData
37-
Output EventData
37+
Input EventData
38+
Output EventData
39+
ConvertedOutput EventData
3840
}
3941
4042
var Events = map[string]Event{[[ range $k, $v := . ]]
@@ -47,25 +49,37 @@ var Events = map[string]Event{[[ range $k, $v := . ]]
4749
LegacyEvent: [[ $v.LegacyOutput ]],[[ end ]][[ if $v.CloudEventOutput ]]
4850
CloudEvent: [[ $v.CloudEventOutput ]],[[ end ]]
4951
},
52+
ConvertedOutput: EventData{[[ if $v.ConvertedLegacyOutput ]]
53+
LegacyEvent: [[ $v.ConvertedLegacyOutput ]],[[ end ]][[ if $v.ConvertedCloudEventOutput ]]
54+
CloudEvent: [[ $v.ConvertedCloudEventOutput ]],[[ end ]]
55+
},
5056
},
5157
[[ end ]]}
5258
`
5359
)
5460

5561
type eventData struct {
56-
LegacyInput string
57-
LegacyOutput string
58-
CloudEventInput string
59-
CloudEventOutput string
62+
LegacyInput string
63+
LegacyOutput string
64+
CloudEventInput string
65+
CloudEventOutput string
66+
ConvertedCloudEventOutput string
67+
ConvertedLegacyOutput string
6068
}
6169

62-
func breakdownFileName(path string) (string, string) {
70+
func breakdownFileName(path string) (string, string, bool) {
71+
6372
// Must be a JSON file.
6473
if !strings.HasSuffix(path, ".json") {
65-
return "", ""
74+
return "", "", false
6675
}
6776
fileName := strings.TrimSuffix(path, ".json")
6877

78+
isConverted := strings.HasSuffix(fileName, converted)
79+
if isConverted {
80+
fileName = strings.TrimSuffix(fileName, "-"+converted)
81+
}
82+
6983
var et, ft string
7084
if strings.HasSuffix(fileName, input) {
7185
ft = input
@@ -83,7 +97,7 @@ func breakdownFileName(path string) (string, string) {
8397
fileName = strings.TrimSuffix(fileName, "-"+cloudeventType)
8498
}
8599

86-
return fileName, et + ft
100+
return fileName, et + ft, isConverted
87101
}
88102

89103
func main() {
@@ -110,7 +124,7 @@ func main() {
110124
return nil
111125
}
112126

113-
name, t := breakdownFileName(info.Name())
127+
name, t, c := breakdownFileName(info.Name())
114128
if name == "" {
115129
return nil
116130
}
@@ -125,11 +139,19 @@ func main() {
125139
case legacyType + input:
126140
ed.LegacyInput = d
127141
case legacyType + output:
128-
ed.LegacyOutput = d
142+
if c {
143+
ed.ConvertedLegacyOutput = d
144+
} else {
145+
ed.LegacyOutput = d
146+
}
129147
case cloudeventType + input:
130148
ed.CloudEventInput = d
131149
case cloudeventType + output:
132-
ed.CloudEventOutput = d
150+
if c {
151+
ed.ConvertedCloudEventOutput = d
152+
} else {
153+
ed.CloudEventOutput = d
154+
}
133155
}
134156

135157
return nil

events/validators.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func PrintValidationInfos(vis []*ValidationInfo) (string, error) {
6868

6969
// ValidateEvent validates that a particular function output matches the expected contents.
7070
func ValidateEvent(name string, it EventType, ot EventType, got []byte) *ValidationInfo {
71-
want := OutputData(name, ot)
71+
want := OutputData(name, ot, it != ot)
7272

7373
// If validating CloudEvent to CloudEvent (no event conversions),
7474
// the output data should be exactly the same as the input data.

events/validators_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
func TestValidateLegacyEvent(t *testing.T) {
2323
testName := "firebase-auth"
24-
data := OutputData(testName, LegacyEvent)
24+
data := OutputData(testName, LegacyEvent, false)
2525
if data == nil {
2626
t.Fatalf("no legacy event data")
2727
}

0 commit comments

Comments
 (0)