Skip to content

Commit 86a4a43

Browse files
committed
Fix unit tests.
Signed-off-by: sudipto baral <[email protected]>
1 parent bf1c373 commit 86a4a43

File tree

4 files changed

+105
-4
lines changed

4 files changed

+105
-4
lines changed

otelcol/collector.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,6 @@ func (col *Collector) DryRun(ctx context.Context) error {
286286
ExportersFactories: factories.Exporters,
287287
ConnectorsConfigs: cfg.Connectors,
288288
ConnectorsFactories: factories.Connectors,
289-
ExtensionsConfigs: cfg.Extensions,
290-
ExtensionsFactories: factories.Extensions,
291289
}, service.Config{
292290
Pipelines: cfg.Service.Pipelines,
293291
})

otelcol/unmarshal_dry_run_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func TestDryRunWithExpandedValues(t *testing.T) {
7171
mockMap: map[string]string{
7272
"number": "123",
7373
},
74+
expectErr: true,
7475
},
7576
{
7677
name: "string that looks like a bool",

service/service.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import (
1414
config "go.opentelemetry.io/contrib/otelconf/v0.3.0"
1515
"go.opentelemetry.io/otel/log"
1616
"go.opentelemetry.io/otel/metric"
17+
noopmetric "go.opentelemetry.io/otel/metric/noop"
1718
sdkresource "go.opentelemetry.io/otel/sdk/resource"
19+
nooptrace "go.opentelemetry.io/otel/trace/noop"
1820
"go.uber.org/multierr"
1921
"go.uber.org/zap"
2022

@@ -538,7 +540,14 @@ func ptr[T any](v T) *T {
538540

539541
// ValidateGraph verifies the graph by calling the internal graph.Build.
540542
func ValidateGraph(ctx context.Context, set Settings, cfg Config) error {
543+
tel := component.TelemetrySettings{
544+
Logger: zap.NewNop(),
545+
TracerProvider: nooptrace.NewTracerProvider(),
546+
MeterProvider: noopmetric.NewMeterProvider(),
547+
Resource: pcommon.NewResource(),
548+
}
541549
_, err := graph.Build(ctx, graph.Settings{
550+
Telemetry: tel,
542551
BuildInfo: set.BuildInfo,
543552
ReceiverBuilder: builders.NewReceiver(set.ReceiversConfigs, set.ReceiversFactories),
544553
ProcessorBuilder: builders.NewProcessor(set.ProcessorsConfigs, set.ProcessorsFactories),

service/service_test.go

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,13 +745,66 @@ func newPtr[T int | string](str T) *T {
745745
func TestValidateGraph(t *testing.T) {
746746
testCases := map[string]struct {
747747
connectorCfg map[component.ID]component.Config
748+
receiverCfg map[component.ID]component.Config
749+
exporterCfg map[component.ID]component.Config
748750
pipelinesCfg pipelines.Config
749751
expectedError string
750752
}{
753+
"Valid connector usage": {
754+
connectorCfg: map[component.ID]component.Config{
755+
component.NewIDWithName(nopType, "connector1"): &struct{}{},
756+
},
757+
receiverCfg: map[component.ID]component.Config{
758+
component.NewID(nopType): &struct{}{},
759+
},
760+
exporterCfg: map[component.ID]component.Config{
761+
component.NewID(nopType): &struct{}{},
762+
},
763+
pipelinesCfg: pipelines.Config{
764+
pipeline.NewIDWithName(pipeline.SignalLogs, "in"): {
765+
Receivers: []component.ID{component.NewID(nopType)},
766+
Processors: []component.ID{},
767+
Exporters: []component.ID{component.NewIDWithName(nopType, "connector1")},
768+
},
769+
pipeline.NewIDWithName(pipeline.SignalLogs, "out"): {
770+
Receivers: []component.ID{component.NewIDWithName(nopType, "connector1")},
771+
Processors: []component.ID{},
772+
Exporters: []component.ID{component.NewID(nopType)},
773+
},
774+
},
775+
expectedError: "",
776+
},
777+
"Valid without Connector": {
778+
receiverCfg: map[component.ID]component.Config{
779+
component.NewID(nopType): &struct{}{},
780+
},
781+
exporterCfg: map[component.ID]component.Config{
782+
component.NewID(nopType): &struct{}{},
783+
},
784+
pipelinesCfg: pipelines.Config{
785+
pipeline.NewIDWithName(pipeline.SignalLogs, "in"): {
786+
Receivers: []component.ID{component.NewID(nopType)},
787+
Processors: []component.ID{},
788+
Exporters: []component.ID{component.NewID(nopType)},
789+
},
790+
pipeline.NewIDWithName(pipeline.SignalLogs, "out"): {
791+
Receivers: []component.ID{component.NewID(nopType)},
792+
Processors: []component.ID{},
793+
Exporters: []component.ID{component.NewID(nopType)},
794+
},
795+
},
796+
expectedError: "",
797+
},
751798
"Connector used as exporter but not as receiver": {
752799
connectorCfg: map[component.ID]component.Config{
753800
component.NewIDWithName(nopType, "connector1"): &struct{}{},
754801
},
802+
receiverCfg: map[component.ID]component.Config{
803+
component.NewID(nopType): &struct{}{},
804+
},
805+
exporterCfg: map[component.ID]component.Config{
806+
component.NewID(nopType): &struct{}{},
807+
},
755808
pipelinesCfg: pipelines.Config{
756809
pipeline.NewIDWithName(pipeline.SignalLogs, "in1"): {
757810
Receivers: []component.ID{component.NewID(nopType)},
@@ -775,6 +828,12 @@ func TestValidateGraph(t *testing.T) {
775828
connectorCfg: map[component.ID]component.Config{
776829
component.NewIDWithName(nopType, "connector1"): &struct{}{},
777830
},
831+
receiverCfg: map[component.ID]component.Config{
832+
component.NewID(nopType): &struct{}{},
833+
},
834+
exporterCfg: map[component.ID]component.Config{
835+
component.NewID(nopType): &struct{}{},
836+
},
778837
pipelinesCfg: pipelines.Config{
779838
pipeline.NewIDWithName(pipeline.SignalLogs, "in1"): {
780839
Receivers: []component.ID{component.NewID(nopType)},
@@ -794,23 +853,57 @@ func TestValidateGraph(t *testing.T) {
794853
},
795854
expectedError: `failed to build pipelines: connector "nop/connector1" used as receiver in [logs/in2] pipeline but not used in any supported exporter pipeline`,
796855
},
856+
"Connector creates direct cycle between pipelines": {
857+
connectorCfg: map[component.ID]component.Config{
858+
component.NewIDWithName(nopType, "forward"): &struct{}{},
859+
},
860+
receiverCfg: map[component.ID]component.Config{
861+
component.NewID(nopType): &struct{}{},
862+
},
863+
exporterCfg: map[component.ID]component.Config{
864+
component.NewID(nopType): &struct{}{},
865+
},
866+
pipelinesCfg: pipelines.Config{
867+
pipeline.NewIDWithName(pipeline.SignalTraces, "in"): {
868+
Receivers: []component.ID{component.NewIDWithName(nopType, "forward")},
869+
Processors: []component.ID{},
870+
Exporters: []component.ID{component.NewIDWithName(nopType, "forward")},
871+
},
872+
pipeline.NewIDWithName(pipeline.SignalTraces, "out"): {
873+
Receivers: []component.ID{component.NewIDWithName(nopType, "forward")},
874+
Processors: []component.ID{},
875+
Exporters: []component.ID{component.NewIDWithName(nopType, "forward")},
876+
},
877+
},
878+
expectedError: `failed to build pipelines: cycle detected: connector "nop/forward" (traces to traces) -> connector "nop/forward" (traces to traces)`,
879+
},
797880
}
798881

799882
_, connectorsFactories := builders.NewNopConnectorConfigsAndFactories()
883+
_, receiversFactories := builders.NewNopReceiverConfigsAndFactories()
884+
_, exportersFactories := builders.NewNopExporterConfigsAndFactories()
800885

801886
for name, tc := range testCases {
802887
t.Run(name, func(t *testing.T) {
803888
settings := Settings{
804889
ConnectorsConfigs: tc.connectorCfg,
805890
ConnectorsFactories: connectorsFactories,
891+
ReceiversConfigs: tc.receiverCfg,
892+
ReceiversFactories: receiversFactories,
893+
ExportersConfigs: tc.exporterCfg,
894+
ExportersFactories: exportersFactories,
806895
}
807896
cfg := Config{
808897
Pipelines: tc.pipelinesCfg,
809898
}
810899

811900
err := ValidateGraph(context.Background(), settings, cfg)
812-
require.Error(t, err)
813-
assert.Equal(t, tc.expectedError, err.Error())
901+
if tc.expectedError == "" {
902+
require.NoError(t, err)
903+
} else {
904+
require.Error(t, err)
905+
assert.Equal(t, tc.expectedError, err.Error())
906+
}
814907
})
815908
}
816909
}

0 commit comments

Comments
 (0)