11# frozen_string_literal: true
22
33require "helper"
4- require 'fluent/test/driver/input'
5- require 'fluent/plugin/in_otlp'
6- require 'excon'
7- require 'timecop'
4+
5+ require "fluent/plugin/in_otlp"
6+ require "fluent/test/driver/input"
87
98class Fluent ::Plugin ::OtlpInputTest < Test ::Unit ::TestCase
109 def config
@@ -21,11 +20,7 @@ def setup
2120 Fluent ::Test . setup
2221 end
2322
24- def teardown
25- Timecop . return
26- end
27-
28- def create_driver ( conf = config )
23+ def create_driver ( conf = config )
2924 Fluent ::Test ::Driver ::Input . new ( Fluent ::Plugin ::OtlpInput ) . configure ( conf )
3025 end
3126
@@ -36,23 +31,103 @@ def test_configure
3631 assert_equal 4318 , d . instance . http_config . port
3732 end
3833
39- def test_receive_json
34+ data ( "metrics" => {
35+ request_path : "/v1/metrics" ,
36+ request_data : TestData ::JSON ::METRICS ,
37+ record_type : "otlp_metrics" ,
38+ record_data : TestData ::JSON ::METRICS
39+ } ,
40+ "traces" => {
41+ request_path : "/v1/traces" ,
42+ request_data : TestData ::JSON ::TRACES ,
43+ record_type : "otlp_traces" ,
44+ record_data : TestData ::JSON ::TRACES
45+ } ,
46+ "logs" => {
47+ request_path : "/v1/logs" ,
48+ request_data : TestData ::JSON ::LOGS ,
49+ record_type : "otlp_logs" ,
50+ record_data : TestData ::JSON ::LOGS
51+ } )
52+ def test_receive_json ( data )
53+ d = create_driver
54+ res = d . run ( expect_records : 1 ) do
55+ post_json ( data [ :request_path ] , data [ :request_data ] )
56+ end
57+
58+ assert_equal ( 200 , res . status )
59+ assert_equal ( "otlp.test" , d . events [ 0 ] [ 0 ] )
60+ assert_equal ( { type : data [ :record_type ] , message : data [ :record_data ] } , d . events [ 0 ] [ 2 ] )
61+ end
62+
63+ def test_receive_compressed_json
4064 d = create_driver
4165 res = d . run ( expect_records : 1 ) do
42- post_json ( "/v1/metrics" , Fluent :: Plugin :: Otlp :: JSON ::METRICS )
66+ post_json ( "/v1/metrics" , compress ( TestData :: JSON ::METRICS ) , { "Content-Encoding" => "gzip" } )
4367 end
4468
4569 assert_equal ( 200 , res . status )
4670 assert_equal ( "otlp.test" , d . events [ 0 ] [ 0 ] )
47- assert_equal ( { type : "otlp_metrics" , message : Fluent ::Plugin ::Otlp ::JSON ::METRICS } , d . events [ 0 ] [ 2 ] )
71+ assert_equal ( { type : "otlp_metrics" , message : TestData ::JSON ::METRICS } , d . events [ 0 ] [ 2 ] )
72+ end
73+
74+ data ( "metrics" => {
75+ request_path : "/v1/metrics" ,
76+ request_data : TestData ::ProtocolBuffers ::METRICS ,
77+ record_type : "otlp_metrics" ,
78+ record_data : TestData ::JSON ::METRICS
79+ } ,
80+ "traces" => {
81+ request_path : "/v1/traces" ,
82+ request_data : TestData ::ProtocolBuffers ::TRACES ,
83+ record_type : "otlp_traces" ,
84+ record_data : TestData ::JSON ::TRACES
85+ } ,
86+ "logs" => {
87+ request_path : "/v1/logs" ,
88+ request_data : TestData ::ProtocolBuffers ::LOGS ,
89+ record_type : "otlp_logs" ,
90+ record_data : TestData ::JSON ::LOGS
91+ } )
92+ def test_receive_protocol_buffers ( data )
93+ d = create_driver
94+ res = d . run ( expect_records : 1 ) do
95+ post_protobuf ( data [ :request_path ] , data [ :request_data ] )
96+ end
97+
98+ assert_equal ( 200 , res . status )
99+ assert_equal ( "otlp.test" , d . events [ 0 ] [ 0 ] )
100+ assert_equal ( { type : data [ :record_type ] , message : data [ :record_data ] } , d . events [ 0 ] [ 2 ] )
101+ end
102+
103+ def test_receive_compressed_protocol_buffers
104+ d = create_driver
105+ res = d . run ( expect_records : 1 ) do
106+ post_json ( "/v1/metrics" , compress ( TestData ::ProtocolBuffers ::METRICS ) , { "Content-Encoding" => "gzip" } )
107+ end
108+
109+ assert_equal ( 200 , res . status )
110+ assert_equal ( "otlp.test" , d . events [ 0 ] [ 0 ] )
111+ assert_equal ( { type : "otlp_metrics" , message : TestData ::JSON ::METRICS } , d . events [ 0 ] [ 2 ] )
112+ end
113+
114+ def compress ( data )
115+ gz = Zlib ::GzipWriter . new ( StringIO . new )
116+ gz << data
117+ gz . close . string
118+ end
119+
120+ def post_json ( path , json , headers = { } )
121+ headers = headers . merge ( { "Content-Type" => "application/json" } )
122+ post ( path , json , headers )
48123 end
49124
50- def post_json ( path , json )
51- headers = { "Content-Type" => "application/json " }
52- post ( path , headers , json )
125+ def post_protobuf ( path , binary , headers = { } )
126+ headers = headers . merge ( { "Content-Type" => "application/x-protobuf " } )
127+ post ( path , binary , headers )
53128 end
54129
55- def post ( path , headers , body )
130+ def post ( path , body , headers = { } )
56131 connection = Excon . new ( "http://127.0.0.1:4318#{ path } " , body : body , headers : headers )
57132 connection . post
58133 end
0 commit comments