Add tests for trace registry

This commit is contained in:
Brian Goff
2018-09-20 16:48:57 -07:00
parent 67c3922863
commit 2fc82818ae
2 changed files with 47 additions and 1 deletions

45
cmd/cencus_test.go Normal file
View File

@@ -0,0 +1,45 @@
package cmd
import (
"testing"
"github.com/cpuguy83/strongerrors"
"go.opencensus.io/trace"
)
func TestGetTracingExporter(t *testing.T) {
defer delete(tracingExporters, "mock")
mockExporterFn := func(_ TracingExporterOptions) (trace.Exporter, error) {
return nil, nil
}
_, err := GetTracingExporter("notexist", TracingExporterOptions{})
if !strongerrors.IsNotFound(err) {
t.Fatalf("expected not found error, got: %v", err)
}
RegisterTracingExporter("mock", mockExporterFn)
if _, err := GetTracingExporter("mock", TracingExporterOptions{}); err != nil {
t.Fatal(err)
}
}
func TestAvailableExporters(t *testing.T) {
defer delete(tracingExporters, "mock")
mockExporterFn := func(_ TracingExporterOptions) (trace.Exporter, error) {
return nil, nil
}
RegisterTracingExporter("mock", mockExporterFn)
for _, e := range AvailableTraceExporters() {
if e == "mock" {
return
}
}
t.Fatal("could not find mock exporter in list of registered exporters")
}

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"os"
"github.com/cpuguy83/strongerrors"
"github.com/pkg/errors"
"github.com/virtual-kubelet/virtual-kubelet/log"
"go.opencensus.io/trace"
@@ -42,7 +43,7 @@ func RegisterTracingExporter(name string, f TracingExporterInitFunc) {
func GetTracingExporter(name string, opts TracingExporterOptions) (trace.Exporter, error) {
f, ok := tracingExporters[name]
if !ok {
return nil, errors.Errorf("tracing exporter %q not found", name)
return nil, strongerrors.NotFound(errors.Errorf("tracing exporter %q not found", name))
}
return f(opts)
}