Skip to content

Commit

Permalink
Removed Moq dependency from Shims.OpenTracing.Tests (#5126)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngruson authored Dec 5, 2023
1 parent 5ca1124 commit f11801d
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" PrivateAssets="All">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// </copyright>

using System.Diagnostics;
using Moq;
using OpenTelemetry.Trace;
using Xunit;

Expand Down Expand Up @@ -56,7 +55,7 @@ public void Activate_SpanMustBeShim()
{
var shim = new ScopeManagerShim();

Assert.Throws<InvalidCastException>(() => shim.Activate(new Mock<global::OpenTracing.ISpan>().Object, true));
Assert.Throws<InvalidCastException>(() => shim.Activate(new TestSpan(), true));
}

[Fact]
Expand Down
23 changes: 23 additions & 0 deletions test/OpenTelemetry.Shims.OpenTracing.Tests/TestFormatTextMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// <copyright file="TestFormatTextMap.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry 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.
// </copyright>

using OpenTracing.Propagation;

namespace OpenTelemetry.Shims.OpenTracing.Tests;

internal class TestFormatTextMap : IFormat<ITextMap>
{
}
110 changes: 110 additions & 0 deletions test/OpenTelemetry.Shims.OpenTracing.Tests/TestSpan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// <copyright file="TestSpan.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry 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.
// </copyright>

using OpenTracing;
using OpenTracing.Tag;

namespace OpenTelemetry.Shims.OpenTracing.Tests;

internal class TestSpan : ISpan
{
public ISpanContext Context => throw new NotImplementedException();

public void Finish()
{
throw new NotImplementedException();
}

public void Finish(DateTimeOffset finishTimestamp)
{
throw new NotImplementedException();
}

public string GetBaggageItem(string key)
{
throw new NotImplementedException();
}

public ISpan Log(IEnumerable<KeyValuePair<string, object>> fields)
{
throw new NotImplementedException();
}

public ISpan Log(DateTimeOffset timestamp, IEnumerable<KeyValuePair<string, object>> fields)
{
throw new NotImplementedException();
}

public ISpan Log(string @event)
{
throw new NotImplementedException();
}

public ISpan Log(DateTimeOffset timestamp, string @event)
{
throw new NotImplementedException();
}

public ISpan SetBaggageItem(string key, string value)
{
throw new NotImplementedException();
}

public ISpan SetOperationName(string operationName)
{
throw new NotImplementedException();
}

public ISpan SetTag(string key, string value)
{
throw new NotImplementedException();
}

public ISpan SetTag(string key, bool value)
{
throw new NotImplementedException();
}

public ISpan SetTag(string key, int value)
{
throw new NotImplementedException();
}

public ISpan SetTag(string key, double value)
{
throw new NotImplementedException();
}

public ISpan SetTag(BooleanTag tag, bool value)
{
throw new NotImplementedException();
}

public ISpan SetTag(IntOrStringTag tag, string value)
{
throw new NotImplementedException();
}

public ISpan SetTag(IntTag tag, int value)
{
throw new NotImplementedException();
}

public ISpan SetTag(StringTag tag, string value)
{
throw new NotImplementedException();
}
}
31 changes: 31 additions & 0 deletions test/OpenTelemetry.Shims.OpenTracing.Tests/TestSpanContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <copyright file="TestSpanContext.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry 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.
// </copyright>

using OpenTracing;

namespace OpenTelemetry.Shims.OpenTracing.Tests;

internal class TestSpanContext : ISpanContext
{
public string TraceId => throw new NotImplementedException();

public string SpanId => throw new NotImplementedException();

public IEnumerable<KeyValuePair<string, string>> GetBaggageItems()
{
throw new NotImplementedException();
}
}
47 changes: 47 additions & 0 deletions test/OpenTelemetry.Shims.OpenTracing.Tests/TestTextMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// <copyright file="TestTextMap.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry 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.
// </copyright>

using System.Collections;
using OpenTracing.Propagation;

namespace OpenTelemetry.Shims.OpenTracing.Tests;

internal class TestTextMap : ITextMap
{
public bool GetEnumeratorCalled { get; private set; }

public bool SetCalled { get; private set; }

#pragma warning disable IDE0028 // Simplify collection initialization
public Dictionary<string, string> Items { get; } = new();
#pragma warning restore IDE0028 // Simplify collection initialization

public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
this.GetEnumeratorCalled = true;
return this.Items.GetEnumerator();
}

public void Set(string key, string value)
{
this.SetCalled = true;
}

IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
53 changes: 23 additions & 30 deletions test/OpenTelemetry.Shims.OpenTracing.Tests/TracerShimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

using System.Collections;
using System.Diagnostics;
using Moq;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;
using OpenTracing;
Expand All @@ -31,10 +30,10 @@ public class TracerShimTests
public void CtorArgumentValidation()
{
// null tracer provider and text format
Assert.Throws<ArgumentNullException>(() => new TracerShim((TracerProvider)null, null));
Assert.Throws<ArgumentNullException>(() => new TracerShim(null, null));

// null tracer provider
Assert.Throws<ArgumentNullException>(() => new TracerShim((TracerProvider)null, new TraceContextPropagator()));
Assert.Throws<ArgumentNullException>(() => new TracerShim(null, new TraceContextPropagator()));
}

[Fact]
Expand All @@ -61,13 +60,13 @@ public void Inject_ArgumentValidation()
var shim = new TracerShim(TracerProvider.Default, new TraceContextPropagator());

var spanContextShim = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None));
var mockFormat = new Mock<IFormat<ITextMap>>();
var mockCarrier = new Mock<ITextMap>();
var testFormat = new TestFormatTextMap();
var testCarrier = new TestTextMap();

Assert.Throws<ArgumentNullException>(() => shim.Inject(null, mockFormat.Object, mockCarrier.Object));
Assert.Throws<InvalidCastException>(() => shim.Inject(new Mock<ISpanContext>().Object, mockFormat.Object, mockCarrier.Object));
Assert.Throws<ArgumentNullException>(() => shim.Inject(spanContextShim, null, mockCarrier.Object));
Assert.Throws<ArgumentNullException>(() => shim.Inject(spanContextShim, mockFormat.Object, null));
Assert.Throws<ArgumentNullException>(() => shim.Inject(null, testFormat, testCarrier));
Assert.Throws<InvalidCastException>(() => shim.Inject(new TestSpanContext(), testFormat, testCarrier));
Assert.Throws<ArgumentNullException>(() => shim.Inject(spanContextShim, null, testCarrier));
Assert.Throws<ArgumentNullException>(() => shim.Inject(spanContextShim, testFormat, null));
}

[Fact]
Expand All @@ -78,56 +77,50 @@ public void Inject_UnknownFormatIgnored()
var spanContextShim = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded));

// Only two specific types of ITextMap are supported, and neither is a Mock.
var mockCarrier = new Mock<ITextMap>();
shim.Inject(spanContextShim, new Mock<IFormat<ITextMap>>().Object, mockCarrier.Object);
var testCarrier = new TestTextMap();
shim.Inject(spanContextShim, new TestFormatTextMap(), testCarrier);

// Verify that the carrier mock was never called.
mockCarrier.Verify(x => x.Set(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
// Verify that the test carrier was never called.
Assert.False(testCarrier.SetCalled);
}

[Fact]
public void Extract_ArgumentValidation()
{
var shim = new TracerShim(TracerProvider.Default, new TraceContextPropagator());

Assert.Throws<ArgumentNullException>(() => shim.Extract(null, new Mock<ITextMap>().Object));
Assert.Throws<ArgumentNullException>(() => shim.Extract(new Mock<IFormat<ITextMap>>().Object, null));
Assert.Throws<ArgumentNullException>(() => shim.Extract(null, new TestTextMap()));
Assert.Throws<ArgumentNullException>(() => shim.Extract(new TestFormatTextMap(), null));
}

[Fact]
public void Extract_UnknownFormatIgnored()
{
var shim = new TracerShim(TracerProvider.Default, new TraceContextPropagator());

var spanContextShim = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None));
_ = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None));

// Only two specific types of ITextMap are supported, and neither is a Mock.
var mockCarrier = new Mock<ITextMap>();
var context = shim.Extract(new Mock<IFormat<ITextMap>>().Object, mockCarrier.Object);
var testCarrier = new TestTextMap();
_ = shim.Extract(new TestFormatTextMap(), testCarrier);

// Verify that the carrier mock was never called.
mockCarrier.Verify(x => x.GetEnumerator(), Times.Never);
// Verify that the test carrier was never called.
Assert.False(testCarrier.SetCalled);
}

[Fact]
public void Extract_InvalidTraceParent()
{
var shim = new TracerShim(TracerProvider.Default, new TraceContextPropagator());

var mockCarrier = new Mock<ITextMap>();
var testCarrier = new TestTextMap();

// The ProxyTracer uses OpenTelemetry.Context.Propagation.TraceContextPropagator, so we need to satisfy the traceparent key at the least
var carrierMap = new Dictionary<string, string>
{
// This is an invalid traceparent value
{ "traceparent", "unused" },
};
testCarrier.Items["traceparent"] = "unused";

mockCarrier.Setup(x => x.GetEnumerator()).Returns(carrierMap.GetEnumerator());
var spanContextShim = shim.Extract(BuiltinFormats.TextMap, mockCarrier.Object) as SpanContextShim;
var spanContextShim = shim.Extract(BuiltinFormats.TextMap, testCarrier) as SpanContextShim;

// Verify that the carrier was called
mockCarrier.Verify(x => x.GetEnumerator(), Times.Once);
Assert.True(testCarrier.GetEnumeratorCalled);

Assert.Null(spanContextShim);
}
Expand Down

0 comments on commit f11801d

Please sign in to comment.