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 support for recorded tests. #18836

Merged
merged 3 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Azure.Iot.ModelsRepository.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100d15ddcb29688295338af4b7686603fe614abd555e09efba8fb88ee09e1f7b1ccaeed2e8f823fa9eef3fdd60217fc012ea67d2479751a0b8c087a4185541b851bd8b16f8d91b840e51b1cb0ba6fe647997e57429265e85ef62d565db50a69ae1647d54d7bd855e4db3d8a91510e5bcbd0edfbbecaa20a7bd9ae74593daa7b11b4")]
[assembly: Azure.Core.AzureResourceProviderNamespace("Azure.Iot.ModelsRepository")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is no resource provider for the SDK, I used the same namespace as the project. I will verify that with SDK team

Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,17 @@ public ResolverClient(Uri repositoryUri, ResolverClientOptions options)
Justification = "<Pending>")]
public virtual async Task<IDictionary<string, string>> ResolveAsync(string dtmi, CancellationToken cancellationToken = default)
{
return await _repositoryHandler.ProcessAsync(dtmi, cancellationToken).ConfigureAwait(false);
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ResolverClient)}.{nameof(Resolve)}");
azabbasi marked this conversation as resolved.
Show resolved Hide resolved
scope.Start();
try
{
return await _repositoryHandler.ProcessAsync(dtmi, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}

/// <summary>
Expand All @@ -119,7 +129,18 @@ public virtual async Task<IDictionary<string, string>> ResolveAsync(string dtmi,
Justification = "<Pending>")]
public virtual IDictionary<string, string> Resolve(string dtmi, CancellationToken cancellationToken = default)
{
return _repositoryHandler.Process(dtmi, cancellationToken);
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ResolverClient)}.{nameof(Resolve)}");
scope.Start();

try
{
return _repositoryHandler.Process(dtmi, cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}

/// <summary>
Expand All @@ -135,7 +156,18 @@ public virtual IDictionary<string, string> Resolve(string dtmi, CancellationToke
[SuppressMessage("Usage", "AZC0015:Unexpected client method return type.", Justification = "<Pending>")]
public virtual async Task<IDictionary<string, string>> ResolveAsync(IEnumerable<string> dtmis, CancellationToken cancellationToken = default)
{
return await _repositoryHandler.ProcessAsync(dtmis, cancellationToken).ConfigureAwait(false);
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ResolverClient)}.{nameof(Resolve)}");
scope.Start();

try
{
return await _repositoryHandler.ProcessAsync(dtmis, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}

/// <summary>
Expand All @@ -151,7 +183,18 @@ public virtual async Task<IDictionary<string, string>> ResolveAsync(IEnumerable<
[SuppressMessage("Usage", "AZC0015:Unexpected client method return type.", Justification = "<Pending>")]
public virtual IDictionary<string, string> Resolve(IEnumerable<string> dtmis, CancellationToken cancellationToken = default)
{
return _repositoryHandler.Process(dtmis, cancellationToken);
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(ResolverClient)}.{nameof(Resolve)}");
scope.Start();

try
{
return _repositoryHandler.Process(dtmis, cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Azure.Iot.ModelsRepository.Tests
{
public class ClientTests
public class ClientTests : ModelRepoTestBase
{
[Test]
public void CtorOverloads()
Expand All @@ -28,7 +28,7 @@ public void CtorOverloads()
Assert.AreEqual(remoteUri, new ResolverClient(remoteUriStr).RepositoryUri);
Assert.AreEqual(remoteUri, new ResolverClient(remoteUriStr, options).RepositoryUri);

string localUriStr = TestHelpers.TestLocalModelRepository;
string localUriStr = TestLocalModelRepository;
Uri localUri = new Uri(localUriStr);

Assert.AreEqual(localUri, new ResolverClient(localUri).RepositoryUri);
Expand Down Expand Up @@ -57,8 +57,10 @@ public void ClientIsValidDtmi(string dtmi, bool expected)
public void ClientOptions()
{
DependencyResolutionOption defaultResolutionOption = DependencyResolutionOption.Enabled;

ResolverClientOptions customOptions =
new ResolverClientOptions(resolutionOption: DependencyResolutionOption.TryFromExpanded);

int maxRetries = 10;
customOptions.Retry.MaxRetries = maxRetries;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Azure.Iot.ModelsRepository.Tests
{
public class DtmiConversionTests
public class DtmiConversionTests : ModelRepoTestBase
{
[TestCase("dtmi:com:Example:Model;1", "dtmi/com/example/model-1.json")]
[TestCase("dtmi:com:example:Model;1", "dtmi/com/example/model-1.json")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace Azure.Iot.ModelsRepository.Tests
{
public class ModelQueryTests
public class ModelQueryTests : ModelRepoTestBase
{
private readonly string _modelTemplate = @"{{
{0}
Expand Down Expand Up @@ -165,7 +164,7 @@ public void GetModelDependencies(string id, string extends, string contents, str
[Test]
public void ListToDict()
{
string testRepoPath = TestHelpers.TestLocalModelRepository;
string testRepoPath = TestLocalModelRepository;
string expandedContent = File.ReadAllText(
$"{testRepoPath}/dtmi/com/example/temperaturecontroller-1.expanded.json", Encoding.UTF8);
ModelQuery query = new ModelQuery(expandedContent);
Expand All @@ -183,7 +182,7 @@ public void ListToDict()
foreach (string id in expectedIds)
{
Assert.True(transformResult.ContainsKey(id));
Assert.True(TestHelpers.ParseRootDtmiFromJson(transformResult[id]).Equals(id, System.StringComparison.Ordinal));
Assert.True(ParseRootDtmiFromJson(transformResult[id]).Equals(id, System.StringComparison.Ordinal));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Net;
using Azure.Core.TestFramework;
using NUnit.Framework;

namespace Azure.Iot.ModelsRepository.Tests
{
public class ModelRepoRecordedTestBase : RecordedTestBase<ModelRepoTestEnvironment>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is only one set of tests that need recording and they are in ResolverIntegrationTests

azabbasi marked this conversation as resolved.
Show resolved Hide resolved
{
protected const string TestModeEnvVariable = "AZURE_TEST_MODE";

protected static RecordedTestMode TestMode => (RecordedTestMode)Enum.Parse(
typeof(RecordedTestMode),
Environment.GetEnvironmentVariable(TestModeEnvVariable));

public ModelRepoRecordedTestBase(bool isAsync) : base(isAsync, TestMode)
{
}

[SetUp]
public virtual void SetupE2eTestBase()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
}

protected ResolverClient GetClient(ModelRepoTestBase.ClientType clientType, ResolverClientOptions options = null)
{
if (options == null)
{
options = new ResolverClientOptions();
}

return
clientType == ModelRepoTestBase.ClientType.Local
? InstrumentClient(
new ResolverClient(
new Uri(ModelRepoTestBase.TestLocalModelRepository),
InstrumentClientOptions(options)))
: InstrumentClient(
new ResolverClient(
new Uri(ModelRepoTestBase.TestRemoteModelRepository),
InstrumentClientOptions(options)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@

namespace Azure.Iot.ModelsRepository.Tests
{
public class TestHelpers
/// <summary>
/// This class will initialize all the settings and create and instance of the ResolverClient client.
/// </summary>
public abstract class ModelRepoTestBase
{
private static readonly string s_fallbackTestRemoteRepo = "https://devicemodels.azure.com/";

public enum ClientType
public ModelRepoTestBase()
{
Local,
Remote
}

public static string ParseRootDtmiFromJson(string json)
Expand All @@ -33,30 +32,18 @@ public static string ParseRootDtmiFromJson(string json)
return dtmi;
}

public static ResolverClient GetTestClient(ClientType clientType, ResolverClientOptions clientOptions = null)
{
if (clientOptions == null)
{
clientOptions = new ResolverClientOptions();
}

if (clientType == ClientType.Local)
{
return new ResolverClient(TestLocalModelRepository, clientOptions);
}

if (clientType == ClientType.Remote)
{
return new ResolverClient(TestRemoteModelRepository, clientOptions);
}

throw new ArgumentException();
}
public static readonly string FallbackTestRemoteRepo = ModelRepositoryConstants.DefaultModelsRepository;

public static string TestDirectoryPath => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

public static string TestLocalModelRepository => Path.Combine(TestDirectoryPath, "TestModelRepo");

public static string TestRemoteModelRepository => Environment.GetEnvironmentVariable("PNP_TEST_REMOTE_REPO") ?? s_fallbackTestRemoteRepo;
public static string TestRemoteModelRepository => Environment.GetEnvironmentVariable("PNP_TEST_REMOTE_REPO") ?? FallbackTestRemoteRepo;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is no resources to be deployed, I would like to change this value to be straight up the FallbackTestRemoteRepo, unless anyone sees value in having a repo stored in an environment variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FallbackTestRemoteRepo is actually the default repo so we can even get rid of that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The historical purpose of the env var was to support running remote tests on non prod model repos, for example testing against a forked repo or a test endpoint https://devicemodeltest.azureedge.net/


public enum ClientType
{
Local,
Remote
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core.TestFramework;

namespace Azure.Iot.ModelsRepository.Tests
{
public class ModelRepoTestEnvironment : TestEnvironment
{
}
}
Loading