Skip to content

Commit 7ad66f3

Browse files
authored
processor_test.go - add basic unmarshal test (#17)
Add a Go test to validate that the generated Go types can be used to unmarshal a simple pipeline from JSON.
1 parent dce7c05 commit 7ad66f3

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

.github/workflows/generate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828

2929
# Sanity checks the generated files.
3030
- name: build
31-
run: go build ./...
31+
run: go test ./...
3232

3333
- name: check for modified versions
3434
id: is-changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030

3131
# Sanity checks the generated files.
3232
- name: build
33-
run: go build ./...
33+
run: go test ./...
3434

3535
test-generator:
3636
name: test-generator

processor_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package ingestnode
19+
20+
import (
21+
"encoding/json"
22+
"strings"
23+
"testing"
24+
)
25+
26+
const pipeline = `
27+
{
28+
"description": "Parse Common Log Format.",
29+
"processors": [
30+
{
31+
"grok": {
32+
"description": "Extract fields from 'message'",
33+
"field": "message",
34+
"patterns": [
35+
"%{IPORHOST:source.ip} %{USER:user.id} %{USER:user.name} \\[%{HTTPDATE:@timestamp}\\] \"%{WORD:http.request.method} %{DATA:url.original} HTTP/%{NUMBER:http.version}\" %{NUMBER:http.response.status_code:int} (?:-|%{NUMBER:http.response.body.bytes:int}) %{QS:http.request.referrer} %{QS:user_agent}"
36+
]
37+
}
38+
},
39+
{
40+
"date": {
41+
"description": "Format '@timestamp' as 'dd/MMM/yyyy:HH:mm:ss Z'",
42+
"field": "@timestamp",
43+
"formats": [
44+
"dd/MMM/yyyy:HH:mm:ss Z"
45+
]
46+
}
47+
},
48+
{
49+
"geoip": {
50+
"description": "Add 'source.geo' GeoIP data for 'source.ip'",
51+
"field": "source.ip",
52+
"target_field": "source.geo"
53+
}
54+
},
55+
{
56+
"user_agent": {
57+
"if": "ctx.user_agent != null",
58+
"description": "Extract fields from 'user_agent'",
59+
"field": "user_agent"
60+
}
61+
}
62+
],
63+
"on_failure": [
64+
{
65+
"set": {
66+
"field": "event.kind",
67+
"value": "pipeline_error"
68+
}
69+
}
70+
]
71+
}
72+
`
73+
74+
func TestPipelineJSONUnmarshal(t *testing.T) {
75+
dec := json.NewDecoder(strings.NewReader(pipeline))
76+
dec.DisallowUnknownFields()
77+
78+
var p Pipeline
79+
if err := dec.Decode(&p); err != nil {
80+
t.Fatal(err)
81+
}
82+
if len(p.Processors) != 4 {
83+
t.Fatal()
84+
}
85+
if p.Processors[0].Grok == nil {
86+
t.Fatal("expected grok processor")
87+
}
88+
}

0 commit comments

Comments
 (0)