Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
add test for non-builtin prefix with domain.
Browse files Browse the repository at this point in the history
- also modify createFakeServer to return server and connectio
  • Loading branch information
rghetia committed Sep 18, 2019
1 parent a07dbc2 commit cbdec48
Showing 1 changed file with 46 additions and 48 deletions.
94 changes: 46 additions & 48 deletions metrics_proto_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,9 @@ func TestVariousCasesFromFile(t *testing.T) {
}
for _, file := range files {
tc := readTestCaseFromFiles(t, file)
server, addr, doneFn := createFakeServer(t)
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the agent.
conn := createConn(t, addr)
defer conn.Close()

// Finally create the OpenCensus stats exporter
se := createExporter(t, conn, defaultOpts)
executeTestCase(t, tc, se, server, nil)
Expand All @@ -87,13 +83,9 @@ func TestVariousCasesFromFile(t *testing.T) {
}

func TestMetricsWithPrefix(t *testing.T) {
server, addr, doneFn := createFakeServer(t)
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the server.
conn := createConn(t, addr)
defer conn.Close()

tc := readTestCaseFromFiles(t, "SingleMetric")

prefixes := []string{
Expand Down Expand Up @@ -121,13 +113,40 @@ func TestMetricsWithPrefix(t *testing.T) {
}
}

func TestBuiltInMetricsUsingPrefix(t *testing.T) {
server, addr, doneFn := createFakeServer(t)
func TestMetricsWithPrefixWithDomain(t *testing.T) {
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the server.
conn := createConn(t, addr)
defer conn.Close()
tc := readTestCaseFromFiles(t, "SingleMetric")

prefixes := []string{
"custom.googleapis.com/prometheus/",
"external.googleapis.com/prometheus/",
}
metricName := "ocagent.io"
saveMetricType := strings.Replace(tc.outTSR[0].TimeSeries[0].Metric.Type, "custom.googleapis.com/opencensus/", "", -1)
saveMDRName := strings.Replace(tc.outMDR[0].MetricDescriptor.Name, "custom.googleapis.com/opencensus/", "", -1)
saveMDRDispName := tc.outMDR[0].MetricDescriptor.DisplayName

for _, prefix := range prefixes {
opts := defaultOpts
opts.MetricPrefix = prefix
se := createExporter(t, conn, opts)

mt := strings.Replace(saveMetricType, metricName, (prefix + metricName), -1)
tc.outMDR[0].MetricDescriptor.Name = strings.Replace(saveMDRName, metricName, (prefix + metricName), -1)
tc.outMDR[0].MetricDescriptor.Type = mt
tc.outMDR[0].MetricDescriptor.DisplayName = strings.Replace(saveMDRDispName, "OpenCensus/"+metricName, (prefix + metricName), -1)

tc.outTSR[0].TimeSeries[0].Metric.Type = mt

executeTestCase(t, tc, se, server, nil)
}
}

func TestBuiltInMetricsUsingPrefix(t *testing.T) {
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

tc := readTestCaseFromFiles(t, "SingleMetric")

Expand Down Expand Up @@ -155,13 +174,9 @@ func TestBuiltInMetricsUsingPrefix(t *testing.T) {
}

func TestMetricsWithResourcePerPushCall(t *testing.T) {
server, addr, doneFn := createFakeServer(t)
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the server.
conn := createConn(t, addr)
defer conn.Close()

inResources, outResources := readTestResourcesFiles(t, "Resources")
inLen := len(inResources)
outLen := len(outResources)
Expand All @@ -184,13 +199,9 @@ func TestMetricsWithResourcePerPushCall(t *testing.T) {
}

func TestMetricsWithResourcePerMetric(t *testing.T) {
server, addr, doneFn := createFakeServer(t)
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the server.
conn := createConn(t, addr)
defer conn.Close()

inResources, outResources := readTestResourcesFiles(t, "Resources")
inLen := len(inResources)
outLen := len(outResources)
Expand All @@ -214,13 +225,9 @@ func TestMetricsWithResourcePerMetric(t *testing.T) {
}

func TestMetricsWithResourcePerMetricTakesPrecedence(t *testing.T) {
server, addr, doneFn := createFakeServer(t)
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the server.
conn := createConn(t, addr)
defer conn.Close()

inResources, outResources := readTestResourcesFiles(t, "Resources")
inLen := len(inResources)
outLen := len(outResources)
Expand All @@ -247,13 +254,9 @@ func TestMetricsWithResourcePerMetricTakesPrecedence(t *testing.T) {
}

func TestMetricsWithResourceWithMissingFieldsPerPushCall(t *testing.T) {
server, addr, doneFn := createFakeServer(t)
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the server.
conn := createConn(t, addr)
defer conn.Close()

inResources, outResources := readTestResourcesFiles(t, "ResourcesWithMissingFields")
inLen := len(inResources)
outLen := len(outResources)
Expand All @@ -276,13 +279,9 @@ func TestMetricsWithResourceWithMissingFieldsPerPushCall(t *testing.T) {
}

func TestExportMaxTSPerRequest(t *testing.T) {
server, addr, doneFn := createFakeServer(t)
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the server.
conn := createConn(t, addr)
defer conn.Close()

// Finally create the OpenCensus stats exporter
se := createExporter(t, conn, defaultOpts)

Expand Down Expand Up @@ -319,13 +318,9 @@ func TestExportMaxTSPerRequest(t *testing.T) {
}

func TestExportMaxTSPerRequestAcrossTwoMetrics(t *testing.T) {
server, addr, doneFn := createFakeServer(t)
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the server.
conn := createConn(t, addr)
defer conn.Close()

// Finally create the OpenCensus stats exporter
se := createExporter(t, conn, defaultOpts)

Expand Down Expand Up @@ -528,7 +523,7 @@ type fakeMetricsServer struct {
stackdriverMetricDescriptors []*monitoringpb.CreateMetricDescriptorRequest
}

func createFakeServer(t *testing.T) (*fakeMetricsServer, string, func()) {
func createFakeServerConn(t *testing.T) (*fakeMetricsServer, *grpc.ClientConn, func()) {
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Failed to bind to an available address: %v", err)
Expand All @@ -539,12 +534,15 @@ func createFakeServer(t *testing.T) (*fakeMetricsServer, string, func()) {
go func() {
_ = srv.Serve(ln)
}()
_, serverPortStr, _ := net.SplitHostPort(ln.Addr().String())
conn := createConn(t, "localhost:"+serverPortStr)

stop := func() {
srv.Stop()
_ = ln.Close()
conn.Close()

This comment has been minimized.

Copy link
@songy23

songy23 Sep 18, 2019

Contributor

Nit: I think conn needs to be closed first.

}
_, serverPortStr, _ := net.SplitHostPort(ln.Addr().String())
return server, "localhost:" + serverPortStr, stop
return server, conn, stop
}

func (server *fakeMetricsServer) forEachStackdriverTimeSeries(fn func(sdt *monitoringpb.CreateTimeSeriesRequest)) {
Expand Down

0 comments on commit cbdec48

Please sign in to comment.