123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package opencensusexporter
- import (
- "context"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/component"
- "go.opentelemetry.io/collector/component/componenttest"
- "go.opentelemetry.io/collector/config/configgrpc"
- "go.opentelemetry.io/collector/config/configopaque"
- "go.opentelemetry.io/collector/config/configtls"
- "go.opentelemetry.io/collector/exporter/exportertest"
- "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil"
- )
- func TestCreateDefaultConfig(t *testing.T) {
- cfg := createDefaultConfig()
- assert.NotNil(t, cfg, "failed to create default config")
- assert.NoError(t, componenttest.CheckConfigStruct(cfg))
- }
- func TestCreateTracesExporter(t *testing.T) {
- endpoint := testutil.GetAvailableLocalAddress(t)
- tests := []struct {
- name string
- config *Config
- mustFailOnCreate bool
- mustFailOnStart bool
- }{
- {
- name: "NoEndpoint",
- config: &Config{
- GRPCClientSettings: configgrpc.GRPCClientSettings{
- Endpoint: "",
- },
- NumWorkers: 3,
- },
- mustFailOnCreate: true,
- },
- {
- name: "ZeroNumWorkers",
- config: &Config{
- GRPCClientSettings: configgrpc.GRPCClientSettings{
- Endpoint: endpoint,
- TLSSetting: configtls.TLSClientSetting{
- Insecure: false,
- },
- },
- NumWorkers: 0,
- },
- mustFailOnCreate: true,
- },
- {
- name: "UseSecure",
- config: &Config{
- GRPCClientSettings: configgrpc.GRPCClientSettings{
- Endpoint: endpoint,
- TLSSetting: configtls.TLSClientSetting{
- Insecure: false,
- },
- },
- NumWorkers: 3,
- },
- },
- {
- name: "Keepalive",
- config: &Config{
- GRPCClientSettings: configgrpc.GRPCClientSettings{
- Endpoint: endpoint,
- Keepalive: &configgrpc.KeepaliveClientConfig{
- Time: 30 * time.Second,
- Timeout: 25 * time.Second,
- PermitWithoutStream: true,
- },
- },
- NumWorkers: 3,
- },
- },
- {
- name: "Compression",
- config: &Config{
- GRPCClientSettings: configgrpc.GRPCClientSettings{
- Endpoint: endpoint,
- Compression: "gzip",
- },
- NumWorkers: 3,
- },
- },
- {
- name: "Headers",
- config: &Config{
- GRPCClientSettings: configgrpc.GRPCClientSettings{
- Endpoint: endpoint,
- Headers: map[string]configopaque.String{
- "hdr1": "val1",
- "hdr2": "val2",
- },
- },
- NumWorkers: 3,
- },
- },
- {
- name: "CompressionError",
- config: &Config{
- GRPCClientSettings: configgrpc.GRPCClientSettings{
- Endpoint: endpoint,
- Compression: "unknown compression",
- },
- NumWorkers: 3,
- },
- mustFailOnCreate: false,
- mustFailOnStart: true,
- },
- {
- name: "CaCert",
- config: &Config{
- GRPCClientSettings: configgrpc.GRPCClientSettings{
- Endpoint: endpoint,
- TLSSetting: configtls.TLSClientSetting{
- TLSSetting: configtls.TLSSetting{
- CAFile: "testdata/test_cert.pem",
- },
- },
- },
- NumWorkers: 3,
- },
- },
- {
- name: "CertPemFileError",
- config: &Config{
- GRPCClientSettings: configgrpc.GRPCClientSettings{
- Endpoint: endpoint,
- TLSSetting: configtls.TLSClientSetting{
- TLSSetting: configtls.TLSSetting{
- CAFile: "nosuchfile",
- },
- },
- },
- NumWorkers: 3,
- },
- mustFailOnCreate: false,
- mustFailOnStart: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- set := exportertest.NewNopCreateSettings()
- tExporter, tErr := createTracesExporter(context.Background(), set, tt.config)
- checkErrorsAndStartAndShutdown(t, tExporter, tErr, tt.mustFailOnCreate, tt.mustFailOnStart)
- mExporter, mErr := createMetricsExporter(context.Background(), set, tt.config)
- checkErrorsAndStartAndShutdown(t, mExporter, mErr, tt.mustFailOnCreate, tt.mustFailOnStart)
- })
- }
- }
- func checkErrorsAndStartAndShutdown(t *testing.T, exporter component.Component, err error, mustFailOnCreate, mustFailOnStart bool) {
- if mustFailOnCreate {
- assert.NotNil(t, err)
- return
- }
- assert.NoError(t, err)
- assert.NotNil(t, exporter)
- sErr := exporter.Start(context.Background(), componenttest.NewNopHost())
- if mustFailOnStart {
- require.Error(t, sErr)
- return
- }
- require.NoError(t, sErr)
- require.NoError(t, exporter.Shutdown(context.Background()))
- }
|