-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved tests for a logging exporter and jaeger exporter #377
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,28 +25,20 @@ import ( | |
"github.com/open-telemetry/opentelemetry-collector/consumer/consumerdata" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
const testHTTPAddress = "http://a.test.dom:123/at/some/path" | ||
const testHTTPAddress = "http://a.test.dom:123/at/some/path" | ||
|
||
type args struct { | ||
config configmodels.Exporter | ||
httpAddress string | ||
headers map[string]string | ||
timeout time.Duration | ||
} | ||
type args struct { | ||
config configmodels.Exporter | ||
httpAddress string | ||
headers map[string]string | ||
timeout time.Duration | ||
} | ||
|
||
func TestNew(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
name string | ||
args args | ||
}{ | ||
{ | ||
name: "empty_exporterName", | ||
args: args{ | ||
config: nil, | ||
httpAddress: testHTTPAddress, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "createExporter", | ||
args: args{ | ||
|
@@ -57,21 +49,39 @@ func TestNew(t *testing.T) { | |
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := New(tt.args.config, tt.args.httpAddress, tt.args.headers, tt.args.timeout) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
|
||
if got == nil { | ||
return | ||
} | ||
assert.NoError(t, err, "nil config") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The third parameter of NoError looks incorrect to me. If I remember correctly it is the message to print when assertion fails. This assertion is not about config, why does it print "nil config"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, it is a wrong usage. Fixed |
||
assert.NotNil(t, got) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to use |
||
|
||
// This is expected to fail. | ||
err = got.ConsumeTraceData(context.Background(), consumerdata.TraceData{}) | ||
assert.Error(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewFails(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strictly speaking there was no need to split TestNewFails as separate function to be able to use |
||
tests := []struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since there is only one test here the value of having an array of test definitions is unclear here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assumed that other cases could be added here in the future. But you're right, for now there is only one. So I fixed this test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use the principle of YAGNI here. :-) |
||
name string | ||
args args | ||
}{ | ||
{ | ||
name: "empty_exporterName", | ||
args: args{ | ||
config: nil, | ||
httpAddress: testHTTPAddress, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := New(tt.args.config, tt.args.httpAddress, tt.args.headers, tt.args.timeout) | ||
assert.EqualError(t, err, "nil config") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assumes all test cases in |
||
assert.Nil(t, got) | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,66 +66,81 @@ func TestCreateInstanceViaFactory(t *testing.T) { | |
|
||
func TestFactory_CreateTraceExporter(t *testing.T) { | ||
tests := []struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, |
||
name string | ||
config *Config | ||
wantErr bool | ||
name string | ||
config *Config | ||
}{ | ||
{ | ||
name: "empty_url", | ||
name: "create_instance", | ||
config: &Config{ | ||
ExporterSettings: configmodels.ExporterSettings{ | ||
TypeVal: typeStr, | ||
NameVal: typeStr, | ||
}, | ||
URL: "http://some.other.location/api/traces", | ||
Headers: map[string]string{ | ||
"added-entry": "added value", | ||
"dot.test": "test", | ||
}, | ||
Timeout: 2 * time.Second, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
f := &Factory{} | ||
te, err := f.CreateTraceExporter(zap.NewNop(), tt.config) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, te) | ||
}) | ||
} | ||
} | ||
|
||
func TestFactory_CreateTraceExporterFails(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
config *Config | ||
errorMessage string | ||
}{ | ||
{ | ||
name: "invalid_url", | ||
name: "empty_url", | ||
config: &Config{ | ||
ExporterSettings: configmodels.ExporterSettings{ | ||
TypeVal: typeStr, | ||
NameVal: typeStr, | ||
}, | ||
URL: "localhost:123", | ||
}, | ||
wantErr: true, | ||
errorMessage: "\"jaeger_thrift_http\" config requires a valid \"url\": parse : empty url", | ||
}, | ||
{ | ||
name: "negative_duration", | ||
name: "invalid_url", | ||
config: &Config{ | ||
ExporterSettings: configmodels.ExporterSettings{ | ||
TypeVal: typeStr, | ||
NameVal: typeStr, | ||
}, | ||
Timeout: -2 * time.Second, | ||
URL: ".localhost:123", | ||
}, | ||
wantErr: true, | ||
errorMessage: "\"jaeger_thrift_http\" config requires a valid \"url\": parse .localhost:123: invalid URI for request", | ||
}, | ||
{ | ||
name: "create_instance", | ||
name: "negative_duration", | ||
config: &Config{ | ||
ExporterSettings: configmodels.ExporterSettings{ | ||
TypeVal: typeStr, | ||
NameVal: typeStr, | ||
}, | ||
URL: "http://some.other.location/api/traces", | ||
Headers: map[string]string{ | ||
"added-entry": "added value", | ||
"dot.test": "test", | ||
}, | ||
Timeout: 2 * time.Second, | ||
URL: "localhost:123", | ||
Timeout: -2 * time.Second, | ||
}, | ||
errorMessage: "\"jaeger_thrift_http\" config requires a positive value for \"timeout\"", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
f := &Factory{} | ||
_, err := f.CreateTraceExporter(zap.NewNop(), tt.config) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Factory.CreateTraceExporter() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
te, err := f.CreateTraceExporter(zap.NewNop(), tt.config) | ||
assert.EqualError(t, err, tt.errorMessage) | ||
assert.Nil(t, te) | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,28 +28,24 @@ import ( | |
|
||
func TestLoggingTraceExporterNoErrors(t *testing.T) { | ||
lte, err := NewTraceExporter(&configmodels.ExporterSettings{}, zap.NewNop()) | ||
if err != nil { | ||
t.Fatalf("Wanted nil got %v", err) | ||
} | ||
assert.NotNil(t, lte) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to use |
||
assert.NoError(t, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
td := consumerdata.TraceData{ | ||
Spans: make([]*tracepb.Span, 7), | ||
} | ||
if err := lte.ConsumeTraceData(context.Background(), td); err != nil { | ||
t.Fatalf("Wanted nil got %v", err) | ||
} | ||
assert.NoError(t, lte.ConsumeTraceData(context.Background(), td)) | ||
assert.NoError(t, lte.Shutdown()) | ||
} | ||
|
||
func TestLoggingMetricsExporterNoErrors(t *testing.T) { | ||
lme, err := NewMetricsExporter(&configmodels.ExporterSettings{}, zap.NewNop()) | ||
if err != nil { | ||
t.Fatalf("Wanted nil got %v", err) | ||
} | ||
assert.NotNil(t, lme) | ||
assert.NoError(t, err) | ||
|
||
md := consumerdata.MetricsData{ | ||
Metrics: make([]*metricspb.Metric, 7), | ||
} | ||
if err := lme.ConsumeMetricsData(context.Background(), md); err != nil { | ||
t.Fatalf("Wanted nil got %v", err) | ||
} | ||
assert.NoError(t, lme.ConsumeMetricsData(context.Background(), md)) | ||
assert.NoError(t, lme.Shutdown()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one also is a single test case, let's remove the
tests
. It can be added later if there are multiple similar tests.