forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for cmd/jaeger/internal/extension/jaegerstorage (jaegertrac…
…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
1 parent
9997cce
commit 1c0a6ca
Showing
3 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
cmd/jaeger/internal/extension/jaegerstorage/extension_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |