fluent-plugin-formatter-protobuf 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'helper'
4
+ require 'fluent/plugin/formatter_protobuf'
5
+
6
+ class ProtobufFormatterTest < Test::Unit::TestCase
7
+ setup do
8
+ Fluent::Test.setup
9
+ end
10
+
11
+ VALID_INCLUDE_PATHS = [File.expand_path(File.join(__dir__, '..', 'proto', 'addressbook_pb.rb'))].freeze
12
+
13
+ sub_test_case 'configure' do
14
+ test 'fail if include_paths is empty' do
15
+ assert_raise(Fluent::ConfigError) do
16
+ create_driver({ message_name: '', include_paths: [] })
17
+ end
18
+ end
19
+
20
+ test 'fail if no protobuf class can be found with message_name' do
21
+ assert_raise(Fluent::ConfigError) do
22
+ create_driver({ message_name: 'Some.Name', include_paths: VALID_INCLUDE_PATHS })
23
+ end
24
+ end
25
+
26
+ test 'passes on valid configuration' do
27
+ assert_nothing_raised do
28
+ create_driver({ message_name: 'tutorial.AddressBook', include_paths: VALID_INCLUDE_PATHS })
29
+ end
30
+ end
31
+ end
32
+
33
+ sub_test_case 'format' do
34
+ test 'encodes into Protobuf binary' do
35
+ formatter = create_formatter({ message_name: 'tutorial.AddressBook', include_paths: VALID_INCLUDE_PATHS })
36
+
37
+ formatted = formatter.format('some-tag', 1234,
38
+ { people: [{ name: 'Masahiro', id: 1337, email: 'repeatedly _at_ gmail.com' }] })
39
+ golden_file = File.binread(File.expand_path(File.join(__dir__, '..', 'proto', 'addressbook.bin')))
40
+ assert_equal(golden_file, formatted)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def create_driver(conf = {})
47
+ Fluent::Test::Driver::Formatter.new(Fluent::Plugin::ProtobufFormatter).configure(conf)
48
+ end
49
+
50
+ def create_formatter(conf)
51
+ create_driver(conf).instance
52
+ end
53
+ end
@@ -0,0 +1,10 @@
1
+ # proto
2
+
3
+ This directory contains relevant Protobuf files for testing.
4
+
5
+ ```
6
+ addressbook - Plain text data, used to pass into `protoc` for generating binary encoded Protobuf golden file `addressbook.bin`
7
+ addressbook.bin - Protobuf encoded golden file
8
+ addressbook.proto - Protobuf definition file
9
+ addressbook_pb.rb - Generated Protobuf Ruby code
10
+ ```
@@ -0,0 +1 @@
1
+ people: [{name:"Masahiro", id: 1337, email: "repeatedly _at_ gmail.com"}]
@@ -0,0 +1,4 @@
1
+
2
+ (
3
+ Masahiro�
4
+ repeatedly _at_ gmail.com
@@ -0,0 +1,31 @@
1
+ // Taken from https://github.com/protocolbuffers/protobuf/blob/32af37aa387f7480669388cad5ebbc22ec75ed8b/examples/addressbook.proto
2
+ syntax = "proto3";
3
+ package tutorial;
4
+
5
+ import "google/protobuf/timestamp.proto";
6
+
7
+ message Person {
8
+ string name = 1;
9
+ int32 id = 2; // Unique ID number for this person.
10
+ string email = 3;
11
+
12
+ enum PhoneType {
13
+ MOBILE = 0;
14
+ HOME = 1;
15
+ WORK = 2;
16
+ }
17
+
18
+ message PhoneNumber {
19
+ string number = 1;
20
+ PhoneType type = 2;
21
+ }
22
+
23
+ repeated PhoneNumber phones = 4;
24
+
25
+ google.protobuf.Timestamp last_updated = 5;
26
+ }
27
+
28
+ // Our address book file is just one of these.
29
+ message AddressBook {
30
+ repeated Person people = 1;
31
+ }
@@ -0,0 +1,36 @@
1
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
2
+ # source: addressbook.proto
3
+
4
+ require 'google/protobuf'
5
+
6
+ require 'google/protobuf/timestamp_pb'
7
+ Google::Protobuf::DescriptorPool.generated_pool.build do
8
+ add_file("addressbook.proto", :syntax => :proto3) do
9
+ add_message "tutorial.Person" do
10
+ optional :name, :string, 1
11
+ optional :id, :int32, 2
12
+ optional :email, :string, 3
13
+ repeated :phones, :message, 4, "tutorial.Person.PhoneNumber"
14
+ optional :last_updated, :message, 5, "google.protobuf.Timestamp"
15
+ end
16
+ add_message "tutorial.Person.PhoneNumber" do
17
+ optional :number, :string, 1
18
+ optional :type, :enum, 2, "tutorial.Person.PhoneType"
19
+ end
20
+ add_enum "tutorial.Person.PhoneType" do
21
+ value :MOBILE, 0
22
+ value :HOME, 1
23
+ value :WORK, 2
24
+ end
25
+ add_message "tutorial.AddressBook" do
26
+ repeated :people, :message, 1, "tutorial.Person"
27
+ end
28
+ end
29
+ end
30
+
31
+ module Tutorial
32
+ Person = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("tutorial.Person").msgclass
33
+ Person::PhoneNumber = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("tutorial.Person.PhoneNumber").msgclass
34
+ Person::PhoneType = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("tutorial.Person.PhoneType").enummodule
35
+ AddressBook = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("tutorial.AddressBook").msgclass
36
+ end