1414package fileexporter
1515
1616import (
17+ "bytes"
1718 "context"
19+ "io/ioutil"
20+ "os"
1821 "testing"
1922
2023 "github.com/gogo/protobuf/jsonpb"
@@ -31,8 +34,7 @@ import (
3134)
3235
3336func TestFileTracesExporter (t * testing.T ) {
34- mf := & testutil.LimitedWriter {}
35- fe := & fileExporter {file : mf }
37+ fe := & fileExporter {path : tempFileName (t )}
3638 require .NotNil (t , fe )
3739
3840 td := testdata .GenerateTracesTwoSpansSameResource ()
@@ -42,7 +44,9 @@ func TestFileTracesExporter(t *testing.T) {
4244
4345 var unmarshaler = & jsonpb.Unmarshaler {}
4446 got := & collectortrace.ExportTraceServiceRequest {}
45- assert .NoError (t , unmarshaler .Unmarshal (mf , got ))
47+ buf , err := ioutil .ReadFile (fe .path )
48+ assert .NoError (t , err )
49+ assert .NoError (t , unmarshaler .Unmarshal (bytes .NewReader (buf ), got ))
4650 assert .EqualValues (t , internal .TracesToOtlp (td .InternalRep ()), got )
4751}
4852
@@ -54,14 +58,13 @@ func TestFileTracesExporterError(t *testing.T) {
5458 require .NotNil (t , fe )
5559
5660 td := testdata .GenerateTracesTwoSpansSameResource ()
57- assert . NoError ( t , fe . Start ( context . Background (), componenttest . NewNopHost ()))
61+ // Cannot call Start since we inject directly the WriterCloser.
5862 assert .Error (t , fe .ConsumeTraces (context .Background (), td ))
5963 assert .NoError (t , fe .Shutdown (context .Background ()))
6064}
6165
6266func TestFileMetricsExporter (t * testing.T ) {
63- mf := & testutil.LimitedWriter {}
64- fe := & fileExporter {file : mf }
67+ fe := & fileExporter {path : tempFileName (t )}
6568 require .NotNil (t , fe )
6669
6770 md := testdata .GenerateMetricsTwoMetrics ()
@@ -71,7 +74,9 @@ func TestFileMetricsExporter(t *testing.T) {
7174
7275 var unmarshaler = & jsonpb.Unmarshaler {}
7376 got := & collectormetrics.ExportMetricsServiceRequest {}
74- assert .NoError (t , unmarshaler .Unmarshal (mf , got ))
77+ buf , err := ioutil .ReadFile (fe .path )
78+ assert .NoError (t , err )
79+ assert .NoError (t , unmarshaler .Unmarshal (bytes .NewReader (buf ), got ))
7580 assert .EqualValues (t , internal .MetricsToOtlp (md .InternalRep ()), got )
7681}
7782
@@ -83,14 +88,13 @@ func TestFileMetricsExporterError(t *testing.T) {
8388 require .NotNil (t , fe )
8489
8590 md := testdata .GenerateMetricsTwoMetrics ()
86- assert . NoError ( t , fe . Start ( context . Background (), componenttest . NewNopHost ()))
91+ // Cannot call Start since we inject directly the WriterCloser.
8792 assert .Error (t , fe .ConsumeMetrics (context .Background (), md ))
8893 assert .NoError (t , fe .Shutdown (context .Background ()))
8994}
9095
9196func TestFileLogsExporter (t * testing.T ) {
92- mf := & testutil.LimitedWriter {}
93- fe := & fileExporter {file : mf }
97+ fe := & fileExporter {path : tempFileName (t )}
9498 require .NotNil (t , fe )
9599
96100 otlp := testdata .GenerateLogsTwoLogRecordsSameResource ()
@@ -100,7 +104,9 @@ func TestFileLogsExporter(t *testing.T) {
100104
101105 var unmarshaler = & jsonpb.Unmarshaler {}
102106 got := & collectorlogs.ExportLogsServiceRequest {}
103- assert .NoError (t , unmarshaler .Unmarshal (mf , got ))
107+ buf , err := ioutil .ReadFile (fe .path )
108+ assert .NoError (t , err )
109+ assert .NoError (t , unmarshaler .Unmarshal (bytes .NewReader (buf ), got ))
104110 assert .EqualValues (t , internal .LogsToOtlp (otlp .InternalRep ()), got )
105111}
106112
@@ -112,7 +118,17 @@ func TestFileLogsExporterErrors(t *testing.T) {
112118 require .NotNil (t , fe )
113119
114120 otlp := testdata .GenerateLogsTwoLogRecordsSameResource ()
115- assert . NoError ( t , fe . Start ( context . Background (), componenttest . NewNopHost ()))
121+ // Cannot call Start since we inject directly the WriterCloser.
116122 assert .Error (t , fe .ConsumeLogs (context .Background (), otlp ))
117123 assert .NoError (t , fe .Shutdown (context .Background ()))
118124}
125+
126+ // tempFileName provides a temporary file name for testing.
127+ func tempFileName (t * testing.T ) string {
128+ tmpfile , err := ioutil .TempFile ("" , "*.json" )
129+ require .NoError (t , err )
130+ require .NoError (t , tmpfile .Close ())
131+ socket := tmpfile .Name ()
132+ require .NoError (t , os .Remove (socket ))
133+ return socket
134+ }
0 commit comments