Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Hickey <[email protected]>
  • Loading branch information
hickeyma committed Aug 30, 2022
1 parent 19ce481 commit 7e8a66e
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
2 changes: 1 addition & 1 deletion exporter/instanaexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestConfig_Validate(t *testing.T) {
func TestConfigValidate(t *testing.T) {
t.Run("Empty configuration", func(t *testing.T) {
c := &Config{}
err := c.Validate()
Expand Down
96 changes: 96 additions & 0 deletions exporter/instanaexporter/exporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2022, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package instanaexporter

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/instanaexporter/internal/testutils"
)

func TestPushConvertedDefaultTraces(t *testing.T) {
traceServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusAccepted)
}))
defer traceServer.Close()

cfg := Config{
AgentKey: "key11",
HTTPClientSettings: confighttp.HTTPClientSettings{Endpoint: traceServer.URL},
Endpoint: traceServer.URL,
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "valid")),
}

instanaExporter, err := newInstanaExporter(&cfg,
componenttest.NewNopExporterCreateSettings())
assert.NoError(t, err)

err = instanaExporter.pushConvertedTraces(context.Background(), testutils.TestTraces.Clone())
assert.NoError(t, err)
}

func TestPushConvertedSimpleTraces(t *testing.T) {
got := make(chan string, 1)
traceServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
assert.Equal(t, "key11", req.Header.Get("x-instana-key"))
got <- req.Header.Get("Content-Type")
rw.WriteHeader(http.StatusAccepted)
}))
defer traceServer.Close()

cfg := Config{
AgentKey: "key11",
HTTPClientSettings: confighttp.HTTPClientSettings{Endpoint: traceServer.URL},
Endpoint: traceServer.URL,
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "valid")),
}

instanaExporter, err := newInstanaExporter(&cfg,
componenttest.NewNopExporterCreateSettings())
assert.NoError(t, err)

err = instanaExporter.pushConvertedTraces(context.Background(), simpleTraces())
assert.NoError(t, err)
}

func simpleTraces() ptrace.Traces {
return genTraces(pcommon.NewTraceID([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4}), nil)
}

func genTraces(traceID pcommon.TraceID, attrs map[string]interface{}) ptrace.Traces {
traces := ptrace.NewTraces()
rspans := traces.ResourceSpans().AppendEmpty()
span := rspans.ScopeSpans().AppendEmpty().Spans().AppendEmpty()
span.SetTraceID(traceID)
span.SetSpanID(pcommon.NewSpanID([8]byte{0, 0, 0, 0, 1, 2, 3, 4}))
if attrs == nil {
return traces
}
pcommon.NewMapFromRaw(attrs).Range(func(k string, v pcommon.Value) bool {
rspans.Resource().Attributes().Insert(k, v)
return true
})
return traces
}
43 changes: 43 additions & 0 deletions exporter/instanaexporter/internal/testutils/test_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2022, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package testutils

import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
)

var (
testAttributes = map[string]string{"instana.agent": "agent1"}
// TestTraces traces for tests.
TestTraces = newTracesWithAttributeMap(testAttributes)
)

func fillAttributeMap(attrs pcommon.Map, mp map[string]string) {
attrs.Clear()
attrs.EnsureCapacity(len(mp))
for k, v := range mp {
attrs.Insert(k, pcommon.NewValueString(v))
}
}

func newTracesWithAttributeMap(mp map[string]string) ptrace.Traces {
traces := ptrace.NewTraces()
resourceSpans := traces.ResourceSpans()
rs := resourceSpans.AppendEmpty()
fillAttributeMap(rs.Resource().Attributes(), mp)
rs.ScopeSpans().AppendEmpty().Spans().AppendEmpty()
return traces
}

0 comments on commit 7e8a66e

Please sign in to comment.