Skip to content

Commit

Permalink
add unit tests for config, factory, and reciever
Browse files Browse the repository at this point in the history
  • Loading branch information
mterhar committed Dec 19, 2024
1 parent 15aab95 commit 31a0daf
Show file tree
Hide file tree
Showing 6 changed files with 400 additions and 50 deletions.
29 changes: 29 additions & 0 deletions receiver/libhoneyreceiver/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package libhoneyreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/libhoneyreceiver"

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
)

func TestCreateDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NotNil(t, cfg, "failed to create default config")
assert.NoError(t, componenttest.CheckConfigStruct(cfg))

libhoneyCfg, ok := cfg.(*Config)
require.True(t, ok, "invalid Config type")

assert.Equal(t, "localhost:8080", libhoneyCfg.HTTP.Endpoint)
assert.Equal(t, []string{"/events", "/event", "/batch"}, libhoneyCfg.HTTP.TracesURLPaths)
assert.Equal(t, "", libhoneyCfg.AuthAPI)
assert.Equal(t, "service.name", libhoneyCfg.FieldMapConfig.Resources.ServiceName)
assert.Equal(t, "library.name", libhoneyCfg.FieldMapConfig.Scopes.LibraryName)
assert.Equal(t, []string{"duration_ms"}, libhoneyCfg.FieldMapConfig.Attributes.DurationFields)
}
47 changes: 47 additions & 0 deletions receiver/libhoneyreceiver/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package libhoneyreceiver

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/libhoneyreceiver/internal/metadata"
)

func TestCreateTracesReceiver(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
set := receivertest.NewNopSettings()
tReceiver, err := factory.CreateTraces(context.Background(), set, cfg, consumertest.NewNop())

assert.NoError(t, err, "receiver creation failed")
assert.NotNil(t, tReceiver, "receiver creation failed")

assert.NoError(t, tReceiver.Start(context.Background(), componenttest.NewNopHost()))
assert.NoError(t, tReceiver.Shutdown(context.Background()))
}

func TestCreateLogsReceiver(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
set := receivertest.NewNopSettings()
lReceiver, err := factory.CreateLogs(context.Background(), set, cfg, consumertest.NewNop())

assert.NoError(t, err, "receiver creation failed")
assert.NotNil(t, lReceiver, "receiver creation failed")

assert.NoError(t, lReceiver.Start(context.Background(), componenttest.NewNopHost()))
assert.NoError(t, lReceiver.Shutdown(context.Background()))
}

func TestType(t *testing.T) {
factory := NewFactory()
assert.Equal(t, metadata.Type, factory.Type())
}
24 changes: 10 additions & 14 deletions receiver/libhoneyreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ go 1.22.0
require (
github.com/gogo/protobuf v1.3.2
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.115.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent v0.115.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent v0.116.0
github.com/stretchr/testify v1.10.0
github.com/vmihailenco/msgpack/v5 v5.4.1
go.opentelemetry.io/collector/component v0.116.0
go.opentelemetry.io/collector/component/componenttest v0.116.0
go.opentelemetry.io/collector/config/confighttp v0.116.0
go.opentelemetry.io/collector/confmap v1.22.0
go.opentelemetry.io/collector/consumer v1.22.0
go.opentelemetry.io/collector/consumer/consumertest v0.116.0
go.opentelemetry.io/collector/receiver/receivertest v0.116.0
github.com/vmihailenco/msgpack/v5 v5.4.1
go.opentelemetry.io/collector/component v0.116.0
go.opentelemetry.io/collector/config/confighttp v0.116.0
go.opentelemetry.io/collector/pdata v1.22.0
go.opentelemetry.io/collector/receiver v0.116.0
go.opentelemetry.io/collector/receiver/receivertest v0.116.0
go.opentelemetry.io/collector/semconv v0.116.0
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9
google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53
)

require (
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
go.opentelemetry.io/collector/consumer/xconsumer v0.116.0 // indirect
go.opentelemetry.io/collector/receiver/xreceiver v0.116.0 // indirect
)
Expand All @@ -47,12 +47,10 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent v0.116.0
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.11.1 // indirect
go.opentelemetry.io/collector/client v1.22.0 // indirect
go.opentelemetry.io/collector/component v0.116.0
go.opentelemetry.io/collector/component/componentstatus v0.116.0
go.opentelemetry.io/collector/config/configauth v0.116.0 // indirect
go.opentelemetry.io/collector/config/configcompression v1.22.0 // indirect
Expand All @@ -63,21 +61,19 @@ require (
go.opentelemetry.io/collector/consumer/consumererror v0.116.0 // indirect
go.opentelemetry.io/collector/extension v0.116.0 // indirect
go.opentelemetry.io/collector/extension/auth v0.116.0 // indirect
go.opentelemetry.io/collector/pdata v1.22.0 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.116.0 // indirect
go.opentelemetry.io/collector/pipeline v0.116.0 // indirect
go.opentelemetry.io/collector/receiver v0.116.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/otel/metric v1.32.0 // indirect
go.opentelemetry.io/otel/sdk v1.32.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.32.0 // indirect
go.opentelemetry.io/otel/trace v1.32.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
google.golang.org/grpc v1.68.1 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/grpc v1.69.0 // indirect
google.golang.org/protobuf v1.35.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
10 changes: 6 additions & 4 deletions receiver/libhoneyreceiver/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 36 additions & 32 deletions receiver/libhoneyreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,38 +96,7 @@ func (r *libhoneyReceiver) startHTTPServer(ctx context.Context, host component.H

if r.cfg.AuthAPI != "" {
httpMux.HandleFunc("/1/auth", func(resp http.ResponseWriter, req *http.Request) {
authURL := fmt.Sprintf("%s/1/auth", r.cfg.AuthAPI)
authReq, err := http.NewRequest(http.MethodGet, authURL, nil)
if err != nil {
errJSON, _ := json.Marshal(`{"error": "failed to create AuthInfo request"}`)
writeResponse(resp, "json", http.StatusBadRequest, errJSON)
return
}
authReq.Header.Set("x-honeycomb-team", req.Header.Get("x-honeycomb-team"))
var authClient http.Client
authResp, err := authClient.Do(authReq)
if err != nil {
errJSON, _ := json.Marshal(fmt.Sprintf(`"error": "failed to send request to auth api endpoint", "message", "%s"}`, err.Error()))
writeResponse(resp, "json", http.StatusBadRequest, errJSON)
return
}
defer authResp.Body.Close()

switch {
case authResp.StatusCode == http.StatusUnauthorized:
errJSON, _ := json.Marshal(`"error": "received 401 response for AuthInfo request from Honeycomb API - check your API key"}`)
writeResponse(resp, "json", http.StatusBadRequest, errJSON)
return
case authResp.StatusCode > 299:
errJSON, _ := json.Marshal(fmt.Sprintf(`"error": "bad response code from API", "status_code", %d}`, authResp.StatusCode))
writeResponse(resp, "json", http.StatusBadRequest, errJSON)
return
}
authRawBody, _ := io.ReadAll(authResp.Body)
_, err = resp.Write(authRawBody)
if err != nil {
r.settings.Logger.Info("couldn't write http response")
}
r.handleAuth(resp, req)
})
}

Expand Down Expand Up @@ -181,6 +150,41 @@ func (r *libhoneyReceiver) registerLogConsumer(tc consumer.Logs) {
r.nextLogs = tc
}

func (r *libhoneyReceiver) handleAuth(resp http.ResponseWriter, req *http.Request) {
authURL := fmt.Sprintf("%s/1/auth", r.cfg.AuthAPI)
authReq, err := http.NewRequest(http.MethodGet, authURL, nil)
if err != nil {
errJSON, _ := json.Marshal(`{"error": "failed to create AuthInfo request"}`)
writeResponse(resp, "json", http.StatusBadRequest, errJSON)
return
}
authReq.Header.Set("x-honeycomb-team", req.Header.Get("x-honeycomb-team"))
var authClient http.Client
authResp, err := authClient.Do(authReq)
if err != nil {
errJSON, _ := json.Marshal(fmt.Sprintf(`"error": "failed to send request to auth api endpoint", "message", "%s"}`, err.Error()))
writeResponse(resp, "json", http.StatusBadRequest, errJSON)
return
}
defer authResp.Body.Close()

switch {
case authResp.StatusCode == http.StatusUnauthorized:
errJSON, _ := json.Marshal(`"error": "received 401 response for AuthInfo request from Honeycomb API - check your API key"}`)
writeResponse(resp, "json", http.StatusBadRequest, errJSON)
return
case authResp.StatusCode > 299:
errJSON, _ := json.Marshal(fmt.Sprintf(`"error": "bad response code from API", "status_code", %d}`, authResp.StatusCode))
writeResponse(resp, "json", http.StatusBadRequest, errJSON)
return
}
authRawBody, _ := io.ReadAll(authResp.Body)
_, err = resp.Write(authRawBody)
if err != nil {
r.settings.Logger.Info("couldn't write http response")
}
}

func (r *libhoneyReceiver) handleEvent(resp http.ResponseWriter, req *http.Request) {
enc, ok := readContentType(resp, req)
if !ok {
Expand Down
Loading

0 comments on commit 31a0daf

Please sign in to comment.