Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests in cmd/jaeger/internal #5069

Merged
merged 17 commits into from
Jan 19, 2024
Merged
2 changes: 0 additions & 2 deletions cmd/jaeger/internal/.nocover

This file was deleted.

31 changes: 20 additions & 11 deletions cmd/jaeger/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,30 @@ func Command() *cobra.Command {
// are present in the args. If not, we create one with all-in-one configuration.
otelRunE := cmd.RunE
cmd.RunE = func(cmd *cobra.Command, args []string) error {
configFlag := cmd.Flag("config")
if !configFlag.Changed {
log.Print("No '--config' flags detected, using default All-in-One configuration with memory storage.")
log.Print("To customize All-in-One behavior, pass a proper configuration.")
data, err := yamlAllInOne.ReadFile("all-in-one.yaml")
if err != nil {
return fmt.Errorf("cannot read embedded all-in-one configuration: %w", err)
}
configFlag.Value.Set("yaml:" + string(data))
}
return otelRunE(cmd, args)
return checkConfigAndRun(cmd, args, yamlAllInOne.ReadFile, otelRunE)
}

cmd.Short = description
cmd.Long = description

return cmd
}

func checkConfigAndRun(
cmd *cobra.Command,
args []string,
getCfg func(name string) ([]byte, error),
runE func(cmd *cobra.Command, args []string) error,
) error {
configFlag := cmd.Flag("config")
if !configFlag.Changed {
log.Print("No '--config' flags detected, using default All-in-One configuration with memory storage.")
log.Print("To customize All-in-One behavior, pass a proper configuration.")
data, err := getCfg("all-in-one.yaml")
if err != nil {
return fmt.Errorf("cannot read embedded all-in-one configuration: %w", err)
}
configFlag.Value.Set("yaml:" + string(data))
}
return runE(cmd, args)
}
63 changes: 63 additions & 0 deletions cmd/jaeger/internal/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2024 The Jaeger 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 internal

import (
"errors"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCommand(t *testing.T) {
cmd := Command()

assert.NotNil(t, cmd)
assert.Equal(t, "jaeger", cmd.Use)
assert.Equal(t, description, cmd.Long)

require.NotNil(t, cmd.RunE)

cmd.ParseFlags([]string{"--config", "bad-file-name"})
err := cmd.Execute()
require.ErrorContains(t, err, "bad-file-name")
}

func TestCheckConfigAndRun_DefaultConfig(t *testing.T) {
cmd := &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
cmd.Flags().String("config", "", "path to config file")
getCfg := func(name string) ([]byte, error) {
return []byte("default-config"), nil
}
runE := func(cmd *cobra.Command, args []string) error {
return nil
}

err := checkConfigAndRun(cmd, nil, getCfg, runE)
require.NoError(t, err)
akagami-harsh marked this conversation as resolved.
Show resolved Hide resolved

errGetCfg := errors.New("error")
getCfgErr := func(name string) ([]byte, error) {
return nil, errGetCfg
}
err = checkConfigAndRun(cmd, nil, getCfgErr, runE)
require.ErrorIs(t, err, errGetCfg)
}
35 changes: 29 additions & 6 deletions cmd/jaeger/internal/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jaegerreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkareceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zipkinreceiver"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/connector/forwardconnector"
"go.opentelemetry.io/collector/exporter"
Expand All @@ -30,11 +31,29 @@ import (
"github.com/jaegertracing/jaeger/cmd/jaeger/internal/extension/jaegerstorage"
)

func components() (otelcol.Factories, error) {
type builders struct {
extension func(factories ...extension.Factory) (map[component.Type]extension.Factory, error)
receiver func(factories ...receiver.Factory) (map[component.Type]receiver.Factory, error)
exporter func(factories ...exporter.Factory) (map[component.Type]exporter.Factory, error)
processor func(factories ...processor.Factory) (map[component.Type]processor.Factory, error)
connector func(factories ...connector.Factory) (map[component.Type]connector.Factory, error)
}

func defaultBuilders() builders {
return builders{
extension: extension.MakeFactoryMap,
receiver: receiver.MakeFactoryMap,
exporter: exporter.MakeFactoryMap,
processor: processor.MakeFactoryMap,
connector: connector.MakeFactoryMap,
}
}

func (b builders) build() (otelcol.Factories, error) {
var err error
factories := otelcol.Factories{}

factories.Extensions, err = extension.MakeFactoryMap(
factories.Extensions, err = b.extension(
// standard
ballastextension.NewFactory(),
zpagesextension.NewFactory(),
Expand All @@ -47,7 +66,7 @@ func components() (otelcol.Factories, error) {
return otelcol.Factories{}, err
}

factories.Receivers, err = receiver.MakeFactoryMap(
factories.Receivers, err = b.receiver(
// standard
otlpreceiver.NewFactory(),
// add-ons
Expand All @@ -59,7 +78,7 @@ func components() (otelcol.Factories, error) {
return otelcol.Factories{}, err
}

factories.Exporters, err = exporter.MakeFactoryMap(
factories.Exporters, err = b.exporter(
// standard
loggingexporter.NewFactory(),
otlpexporter.NewFactory(),
Expand All @@ -73,7 +92,7 @@ func components() (otelcol.Factories, error) {
return otelcol.Factories{}, err
}

factories.Processors, err = processor.MakeFactoryMap(
factories.Processors, err = b.processor(
// standard
batchprocessor.NewFactory(),
memorylimiterprocessor.NewFactory(),
Expand All @@ -84,7 +103,7 @@ func components() (otelcol.Factories, error) {
return otelcol.Factories{}, err
}

factories.Connectors, err = connector.MakeFactoryMap(
factories.Connectors, err = b.connector(
// standard
forwardconnector.NewFactory(),
// add-ons
Expand All @@ -96,3 +115,7 @@ func components() (otelcol.Factories, error) {

return factories, nil
}

func components() (otelcol.Factories, error) {
return defaultBuilders().build()
}
84 changes: 84 additions & 0 deletions cmd/jaeger/internal/components_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2024 The Jaeger 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 internal

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/extension"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/receiver"
)

func TestComponents(t *testing.T) {
factories, err := components()

require.NoError(t, err)

assert.NotNil(t, factories.Extensions)
assert.NotNil(t, factories.Receivers)
assert.NotNil(t, factories.Exporters)
assert.NotNil(t, factories.Processors)
assert.NotNil(t, factories.Connectors)

_, jaegerReceiverFactoryExists := factories.Receivers["jaeger"]
assert.True(t, jaegerReceiverFactoryExists)
}

func TestGetOtelcolFactories(t *testing.T) {
errMock := errors.New("mock error")
{
b := defaultBuilders()
b.extension = mockFactoryMap[extension.Factory](errMock)
_, err := b.build()
require.ErrorIs(t, err, errMock)
}
{
b := defaultBuilders()
b.receiver = mockFactoryMap[receiver.Factory](errMock)
_, err := b.build()
require.ErrorIs(t, err, errMock)
}
{
b := defaultBuilders()
b.exporter = mockFactoryMap[exporter.Factory](errMock)
_, err := b.build()
require.ErrorIs(t, err, errMock)
}
{
b := defaultBuilders()
b.processor = mockFactoryMap[processor.Factory](errMock)
_, err := b.build()
require.ErrorIs(t, err, errMock)
}
{
b := defaultBuilders()
b.connector = mockFactoryMap[connector.Factory](errMock)
_, err := b.build()
require.ErrorIs(t, err, errMock)
}
}

func mockFactoryMap[FACTORY component.Factory](err error) func(factories ...FACTORY) (map[component.Type]FACTORY, error) {
return func(factories ...FACTORY) (map[component.Type]FACTORY, error) {
return nil, err
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use template to remove duplication

25 changes: 25 additions & 0 deletions cmd/jaeger/internal/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024 The Jaeger 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 internal

import (
"testing"

"github.com/jaegertracing/jaeger/pkg/testutils"
)

func TestMain(m *testing.M) {
testutils.VerifyGoLeaks(m)
}
Loading