Skip to content

Commit

Permalink
Add tests for cmd/jaeger/internal/extension/jaegerstorage (jaegertrac…
Browse files Browse the repository at this point in the history
…ing#5096)

## Which problem is this PR solving?
- jaegertracing#5068 

## Description of the changes
-  Add unit tests for jaegerstorage

## How was this change tested?
- 

## Checklist
- [x] I have read
https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [x] I have signed all commits
- [x] I have added unit tests for the new functionality
- [x] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`
  - for `jaeger-ui`: `yarn lint` and `yarn test`

---------

Signed-off-by: Chirag Ghosh <cghosh828049@gmail.com>
Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
  • Loading branch information
chirag-ghosh and yurishkuro authored Jan 18, 2024
1 parent 9997cce commit 1c0a6ca
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 2 deletions.
2 changes: 0 additions & 2 deletions cmd/jaeger/internal/extension/jaegerstorage/.nocover

This file was deleted.

12 changes: 12 additions & 0 deletions cmd/jaeger/internal/extension/jaegerstorage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package jaegerstorage

import (
"fmt"
"reflect"

memoryCfg "github.com/jaegertracing/jaeger/pkg/memory/config"
)

Expand All @@ -19,3 +22,12 @@ type MemoryStorage struct {
Name string `mapstructure:"name"`
memoryCfg.Configuration
}

func (cfg *Config) Validate() error {
emptyCfg := createDefaultConfig().(*Config)
if reflect.DeepEqual(*cfg, *emptyCfg) {
return fmt.Errorf("%s: no storage type present in config", ID)
} else {
return nil
}
}
162 changes: 162 additions & 0 deletions cmd/jaeger/internal/extension/jaegerstorage/extension_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package jaegerstorage

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/extension"
noopmetric "go.opentelemetry.io/otel/metric/noop"
nooptrace "go.opentelemetry.io/otel/trace/noop"
"go.uber.org/zap"

memoryCfg "github.com/jaegertracing/jaeger/pkg/memory/config"
"github.com/jaegertracing/jaeger/pkg/metrics"
"github.com/jaegertracing/jaeger/storage"
"github.com/jaegertracing/jaeger/storage/dependencystore"
"github.com/jaegertracing/jaeger/storage/spanstore"
)

const memstoreName = "memstore"

type storageHost struct {
t *testing.T
storageExtension component.Component
}

func (host storageHost) GetExtensions() map[component.ID]component.Component {
return map[component.ID]component.Component{
ID: host.storageExtension,
}
}

func (host storageHost) ReportFatalError(err error) {
host.t.Fatal(err)
}

func (storageHost) GetFactory(_ component.Kind, _ component.Type) component.Factory {
return nil
}

func (storageHost) GetExporters() map[component.DataType]map[component.ID]component.Component {
return nil
}

type errorFactory struct {
closeErr error
}

func (e errorFactory) Initialize(metricsFactory metrics.Factory, logger *zap.Logger) error {
panic("not implemented")
}

func (e errorFactory) CreateSpanReader() (spanstore.Reader, error) {
panic("not implemented")
}

func (e errorFactory) CreateSpanWriter() (spanstore.Writer, error) {
panic("not implemented")
}

func (e errorFactory) CreateDependencyReader() (dependencystore.Reader, error) {
panic("not implemented")
}

func (e errorFactory) Close() error {
return e.closeErr
}

func TestStorageExtensionConfigError(t *testing.T) {
config := createDefaultConfig().(*Config)
err := config.Validate()
require.EqualError(t, err, fmt.Sprintf("%s: no storage type present in config", ID))
}

func TestStorageExtensionStartTwiceError(t *testing.T) {
ctx := context.Background()

storageExtension := makeStorageExtension(t, memstoreName)

host := componenttest.NewNopHost()
err := storageExtension.Start(ctx, host)
require.Error(t, err)
require.EqualError(t, err, fmt.Sprintf("duplicate memory storage name %s", memstoreName))
}

func TestStorageFactoryBadHostError(t *testing.T) {
makeStorageExtension(t, memstoreName)

host := componenttest.NewNopHost()
_, err := GetStorageFactory(memstoreName, host)
require.Error(t, err)
require.EqualError(t, err, fmt.Sprintf("cannot find extension '%s' (make sure it's defined earlier in the config)", ID))
}

func TestStorageFactoryBadNameError(t *testing.T) {
storageExtension := makeStorageExtension(t, memstoreName)

host := storageHost{t: t, storageExtension: storageExtension}
const badMemstoreName = "test"

_, err := GetStorageFactory(badMemstoreName, host)
require.Error(t, err)
require.EqualError(t, err, fmt.Sprintf("cannot find storage '%s' declared with '%s' extension", badMemstoreName, ID))
}

func TestStorageFactoryBadShutdownError(t *testing.T) {
storageExtension := storageExt{
factories: make(map[string]storage.Factory),
}
badFactoryError := fmt.Errorf("error factory")
storageExtension.factories[memstoreName] = errorFactory{closeErr: badFactoryError}

err := storageExtension.Shutdown(context.Background())
require.ErrorIs(t, err, badFactoryError)
}

func TestStorageExtension(t *testing.T) {
storageExtension := makeStorageExtension(t, memstoreName)

host := storageHost{t: t, storageExtension: storageExtension}

_, err := GetStorageFactory(memstoreName, host)
require.NoError(t, err)
}

func makeStorageExtension(t *testing.T, memstoreName string) component.Component {
extensionFactory := NewFactory()

ctx := context.Background()
telemetrySettings := component.TelemetrySettings{
Logger: zap.L(),
TracerProvider: nooptrace.NewTracerProvider(),
MeterProvider: noopmetric.NewMeterProvider(),
}
config := &Config{
Memory: map[string]memoryCfg.Configuration{
memstoreName: {MaxTraces: 10000},
},
}
err := config.Validate()
require.NoError(t, err)

storageExtension, err := extensionFactory.CreateExtension(ctx, extension.CreateSettings{
ID: ID,
TelemetrySettings: telemetrySettings,
BuildInfo: component.NewDefaultBuildInfo(),
}, config)
require.NoError(t, err)

host := componenttest.NewNopHost()
err = storageExtension.Start(ctx, host)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, storageExtension.Shutdown(ctx)) })

return storageExtension
}

0 comments on commit 1c0a6ca

Please sign in to comment.