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 @@
// 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)

Check warning on line 43 in cmd/jaeger/internal/command.go

View check run for this annotation

Codecov / codecov/patch

cmd/jaeger/internal/command.go#L43

Added line #L43 was not covered by tests
}

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)
}

Check warning on line 65 in cmd/jaeger/internal/command.go

View check run for this annotation

Codecov / codecov/patch

cmd/jaeger/internal/command.go#L64-L65

Added lines #L64 - L65 were not covered by tests
configFlag.Value.Set("yaml:" + string(data))
}
return runE(cmd, args)
}
56 changes: 56 additions & 0 deletions cmd/jaeger/internal/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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/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)

runE := cmd.RunE
assert.NotNil(t, runE)
akagami-harsh marked this conversation as resolved.
Show resolved Hide resolved
}

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

configFlag := cmd.Flag("config")
assert.Equal(t, "yaml:default-config", configFlag.Value.String())
assert.False(t, configFlag.Changed)
akagami-harsh marked this conversation as resolved.
Show resolved Hide resolved
}
37 changes: 37 additions & 0 deletions cmd/jaeger/internal/components_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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)
}
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