Skip to content

Commit

Permalink
Renames and refactoring before private ACR completions work (#16323)
Browse files Browse the repository at this point in the history
Intended to make the next PR easier to read. Mostly renames, plus some
test infrastructure improvements. There should be little or no
production impact.

###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/16323)
  • Loading branch information
StephenWeatherford authored Feb 5, 2025
1 parent a22dc2d commit 5f03b3b
Show file tree
Hide file tree
Showing 52 changed files with 529 additions and 444 deletions.
5 changes: 2 additions & 3 deletions src/Bicep.Cli.IntegrationTests/BuildCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json.Linq;
using RegistryUtils = Bicep.Core.UnitTests.Utils.ContainerRegistryClientFactoryExtensions;

namespace Bicep.Cli.IntegrationTests
{
Expand Down Expand Up @@ -126,7 +125,7 @@ public async Task Build_Valid_SingleFile_WithTemplateSpecReference_ToStdOut_Shou

if (dataSet.HasExternalModules)
{
CachedModules.GetCachedRegistryModules(BicepTestConstants.FileSystem, settings.FeatureOverrides!.CacheRootDirectory!).Should().HaveCountGreaterThan(0)
CachedModules.GetCachedModules(BicepTestConstants.FileSystem, settings.FeatureOverrides!.CacheRootDirectory!).Should().HaveCountGreaterThan(0)
.And.AllSatisfy(m => m.Should().HaveSource());
}

Expand Down Expand Up @@ -191,7 +190,7 @@ public async Task Build_Valid_SingleFile_WithDigestReference_ShouldSucceed()
var registryUri = new Uri("https://" + registry);
var repository = "hello/there";

var client = new MockRegistryBlobClient();
var client = new FakeRegistryBlobClient();

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), registryUri, repository)).Returns(client);
Expand Down
6 changes: 3 additions & 3 deletions src/Bicep.Cli.IntegrationTests/LintCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Bicep.Core.Samples;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.Mock;
using Bicep.Core.UnitTests.Mock.Registry;
using Bicep.Core.UnitTests.Registry;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
Expand Down Expand Up @@ -109,7 +110,7 @@ public async Task Lint_Valid_SingleFile_WithDigestReference_ShouldSucceed()
var registryUri = new Uri("https://" + registry);
var repository = "hello/there";

var client = new MockRegistryBlobClient();
var client = new FakeRegistryBlobClient();

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), registryUri, repository)).Returns(client);
Expand Down Expand Up @@ -178,11 +179,10 @@ public async Task Lint_WithEmptyBicepConfig_ShouldProduceConfigurationError()
string testOutputPath = FileHelper.GetUniqueTestOutputPath(TestContext);
var inputFile = FileHelper.SaveResultFile(TestContext, "main.bicep", DataSets.Empty.Bicep, testOutputPath);
var configurationPath = FileHelper.SaveResultFile(TestContext, "bicepconfig.json", string.Empty, testOutputPath);
var settings = new InvocationSettings() { ModuleMetadataClient = PublicRegistryModuleIndexClientMock.CreateToThrow(new Exception("unit test failed: shouldn't call this")).Object };
var settings = new InvocationSettings() { ModuleMetadataClient = PublicModuleIndexHttpClientMocks.Create([]).Object };

var (output, error, result) = await Bicep(settings, "lint", inputFile);


result.Should().Be(1);
output.Should().BeEmpty();
error.Should().StartWith($"{inputFile}(1,1) : Error BCP271: Failed to parse the contents of the Bicep configuration file \"{configurationPath}\" as valid JSON: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.");
Expand Down
24 changes: 14 additions & 10 deletions src/Bicep.Cli.IntegrationTests/PublishCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using FluentAssertions.Execution;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using static Bicep.Core.UnitTests.Utils.RegistryHelper;
using DataSet = Bicep.Core.Samples.DataSet;

namespace Bicep.Cli.IntegrationTests
Expand Down Expand Up @@ -155,14 +156,14 @@ public async Task Publish_AllValidDataSets_ShouldSucceed(string testName, DataSe
var registryUri = new Uri($"https://{registryStr}");
var repository = $"test/{dataSet.Name}".ToLowerInvariant();

var clientFactory = dataSet.CreateMockRegistryClients((registryStr, repository));
var clientFactory = dataSet.CreateMockRegistryClients(new RepoDescriptor(registryStr, repository, ["tag"]));
var templateSpecRepositoryFactory = dataSet.CreateMockTemplateSpecRepositoryFactory(TestContext);
await dataSet.PublishModulesToRegistryAsync(clientFactory);
var bicepFilePath = Path.Combine(outputDirectory, DataSet.TestFileMain);
var compiledFilePath = Path.Combine(outputDirectory, DataSet.TestFileMainCompiled);

// mock client factory caches the clients
var testClient = (MockRegistryBlobClient)clientFactory.CreateAuthenticatedBlobClient(BicepTestConstants.BuiltInConfiguration, registryUri, repository);
var testClient = (FakeRegistryBlobClient)clientFactory.CreateAuthenticatedBlobClient(BicepTestConstants.BuiltInConfiguration, registryUri, repository);

var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), clientFactory, templateSpecRepositoryFactory);

Expand Down Expand Up @@ -255,13 +256,13 @@ public async Task Publish_ValidArmTemplateFile_AllValidDataSets_ShouldSucceed(Da
var registryUri = new Uri($"https://{registryStr}");
var repository = $"test/{dataSet.Name}".ToLowerInvariant();

var clientFactory = dataSet.CreateMockRegistryClients((registryStr, repository));
var clientFactory = dataSet.CreateMockRegistryClients(new RepoDescriptor(registryStr, repository, ["tag"]));
var templateSpecRepositoryFactory = dataSet.CreateMockTemplateSpecRepositoryFactory(TestContext);
await dataSet.PublishModulesToRegistryAsync(clientFactory);
var compiledFilePath = Path.Combine(outputDirectory, DataSet.TestFileMainCompiled);

// mock client factory caches the clients
var testClient = (MockRegistryBlobClient)clientFactory.CreateAuthenticatedBlobClient(BicepTestConstants.BuiltInConfiguration, registryUri, repository);
var testClient = (FakeRegistryBlobClient)clientFactory.CreateAuthenticatedBlobClient(BicepTestConstants.BuiltInConfiguration, registryUri, repository);

var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), clientFactory, templateSpecRepositoryFactory);

Expand Down Expand Up @@ -309,13 +310,13 @@ public async Task Publish_ValidArmTemplateFile_WithSource_ShouldFail()
var registryUri = new Uri($"https://{registryStr}");
var repository = $"test/{dataSet.Name}".ToLowerInvariant();

var clientFactory = dataSet.CreateMockRegistryClients((registryStr, repository));
var clientFactory = dataSet.CreateMockRegistryClients(new RepoDescriptor(registryStr, repository, ["tag"]));
var templateSpecRepositoryFactory = dataSet.CreateMockTemplateSpecRepositoryFactory(TestContext);
await dataSet.PublishModulesToRegistryAsync(clientFactory);
var compiledFilePath = Path.Combine(outputDirectory, DataSet.TestFileMainCompiled);

// mock client factory caches the clients
var testClient = (MockRegistryBlobClient)clientFactory.CreateAuthenticatedBlobClient(BicepTestConstants.BuiltInConfiguration, registryUri, repository);
var testClient = (FakeRegistryBlobClient)clientFactory.CreateAuthenticatedBlobClient(BicepTestConstants.BuiltInConfiguration, registryUri, repository);

var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), clientFactory, templateSpecRepositoryFactory);

Expand Down Expand Up @@ -450,11 +451,14 @@ public async Task Publish_BicepModule_WithDescriptionAndDocUri_ShouldPlaceDescri
var registryUri = new Uri($"https://{registryStr}");
var repository = $"test/{moduleName}".ToLowerInvariant();

var (clientFactory, blobClients) = RegistryHelper.CreateMockRegistryClients((registryStr, repository));
var clientFactory = RegistryHelper.CreateMockRegistryClient(new RepoDescriptor(registryStr, repository, ["v1"]));
var blobClient = (FakeRegistryBlobClient)clientFactory.CreateAuthenticatedBlobClient(BicepTestConstants.BuiltInConfiguration, registryUri, repository);

var blobClient = blobClients[(registryUri, repository)];

await RegistryHelper.PublishModuleToRegistryAsync(clientFactory, BicepTestConstants.FileSystem, "modulename", $"br:example.com/test/{moduleName}:v1", bicepModuleContents, publishSource: false, documentationUri);
await RegistryHelper.PublishModuleToRegistryAsync(
new ServiceBuilder(),
clientFactory,
BicepTestConstants.FileSystem,
new($"br:example.com/test/{moduleName}:v1", bicepModuleContents, WithSource: false, documentationUri));

var manifest = blobClient.Manifests.Single().Value.ToObjectFromJson<OciManifest>(new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });

Expand Down
10 changes: 6 additions & 4 deletions src/Bicep.Cli.IntegrationTests/PublishProviderCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
using Bicep.Core.TypeSystem.Types;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Registry;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using static Bicep.Core.UnitTests.Utils.RegistryHelper;

namespace Bicep.Cli.IntegrationTests;

Expand All @@ -31,8 +33,8 @@ public async Task Publish_extension_should_succeed()
var repository = $"test/extension";
var version = "0.0.1";

var (clientFactory, blobClientMocks) = RegistryHelper.CreateMockRegistryClients((registryStr, repository));
var mockBlobClient = blobClientMocks[(registryUri, repository)];
var clientFactory = RegistryHelper.CreateMockRegistryClient(new RepoDescriptor(registryStr, repository, ["tag"]));
var fakeBlobClient = (FakeRegistryBlobClient)clientFactory.CreateAuthenticatedBlobClient(BicepTestConstants.BuiltInConfiguration, registryUri, repository);

var indexPath = Path.Combine(outputDirectory, "index.json");
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), clientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
Expand All @@ -48,7 +50,7 @@ public async Task Publish_extension_should_succeed()
result.Stderr.Should().Match("WARNING: The 'publish-extension' CLI command group is an experimental feature.*");

// verify the extension was published
mockBlobClient.Should().HaveExtension(version, out var tgzStream);
fakeBlobClient.Should().HaveExtension(version, out var tgzStream);

var typeLoader = OciTypeLoader.FromStream(tgzStream);
var azTypeLoader = new AzResourceTypeLoader(typeLoader);
Expand All @@ -72,7 +74,7 @@ public async Task Publish_extension_should_succeed()
result2.Should().Succeed().And.NotHaveStdout();

// verify the extension was published
mockBlobClient.Should().HaveExtension(version, out var tgzStream2);
fakeBlobClient.Should().HaveExtension(version, out var tgzStream2);

var typeLoader2 = OciTypeLoader.FromStream(tgzStream2);
var azTypeLoader2 = new AzResourceTypeLoader(typeLoader2);
Expand Down
10 changes: 5 additions & 5 deletions src/Bicep.Cli.IntegrationTests/RestoreCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public async Task Restore_ShouldSucceed(string testName, DataSet dataSet, bool p
if (dataSet.HasExternalModules)
{
// ensure something got restored
CachedModules.GetCachedRegistryModules(BicepTestConstants.FileSystem, settings.FeatureOverrides.CacheRootDirectory!).Should().HaveCountGreaterThan(0)
CachedModules.GetCachedModules(BicepTestConstants.FileSystem, settings.FeatureOverrides.CacheRootDirectory!).Should().HaveCountGreaterThan(0)
.And.AllSatisfy(m => m.Should().HaveSource(publishSource));
}
}
Expand All @@ -91,7 +91,7 @@ public async Task Restore_should_succeed_for_bicepparam_file_with_registry_refer
result.Should().Succeed().And.NotHaveStdout().And.NotHaveStderr();

// ensure something got restored
CachedModules.GetCachedRegistryModules(BicepTestConstants.FileSystem, settings.FeatureOverrides!.CacheRootDirectory!).Should().HaveCountGreaterThan(0)
CachedModules.GetCachedModules(BicepTestConstants.FileSystem, settings.FeatureOverrides!.CacheRootDirectory!).Should().HaveCountGreaterThan(0)
.And.AllSatisfy(m => m.Should().NotHaveSource());
}

Expand Down Expand Up @@ -140,7 +140,7 @@ public async Task Restore_ShouldSucceedWithAnonymousClient(string testName, Data
if (dataSet.HasExternalModules)
{
// ensure something got restored
CachedModules.GetCachedRegistryModules(BicepTestConstants.FileSystem, settings.FeatureOverrides.CacheRootDirectory!).Should().HaveCountGreaterThan(0)
CachedModules.GetCachedModules(BicepTestConstants.FileSystem, settings.FeatureOverrides.CacheRootDirectory!).Should().HaveCountGreaterThan(0)
.And.AllSatisfy(m => m.Should().HaveSource(publishSource));
}
}
Expand Down Expand Up @@ -307,7 +307,7 @@ public async Task Restore_With_Force_Should_Overwrite_Existing_Cache(bool publis
var registryUri = new Uri("https://" + registry);
var repository = "hello/there";

var client = new MockRegistryBlobClient();
var client = new FakeRegistryBlobClient();

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), registryUri, repository)).Returns(client);
Expand Down Expand Up @@ -428,7 +428,7 @@ public async Task Restore_ByDigest_ShouldSucceed(bool publishSource)
var registryUri = new Uri("https://" + registry);
var repository = "hello/there";

var client = new MockRegistryBlobClient();
var client = new FakeRegistryBlobClient();

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), registryUri, repository)).Returns(client);
Expand Down
12 changes: 6 additions & 6 deletions src/Bicep.Cli.IntegrationTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ namespace Bicep.Cli.IntegrationTests
{
public abstract class TestBase : Bicep.Core.UnitTests.TestBase
{
private static BicepCompiler CreateCompiler(IContainerRegistryClientFactory clientFactory, ITemplateSpecRepositoryFactory templateSpecRepositoryFactory, IPublicRegistryModuleIndexClient? moduleMetadataClient)
private static BicepCompiler CreateCompiler(IContainerRegistryClientFactory clientFactory, ITemplateSpecRepositoryFactory templateSpecRepositoryFactory, IPublicModuleIndexClient? moduleMetadataClient)
=> ServiceBuilder.Create(
services =>
{
services
.AddSingleton(clientFactory)
.AddSingleton(templateSpecRepositoryFactory)
.AddSingleton<IPublicRegistryModuleMetadataProvider, PublicRegistryModuleMetadataProvider>();
.AddSingleton<IPublicModuleMetadataProvider, PublicModuleMetadataProvider>();

IServiceCollectionExtensions.AddMockHttpClientIfNotNull(services, moduleMetadataClient);
}
Expand All @@ -49,21 +49,21 @@ protected record InvocationSettings
public IContainerRegistryClientFactory ClientFactory { get; init; }
public ITemplateSpecRepositoryFactory TemplateSpecRepositoryFactory { get; init; }
public IEnvironment? Environment { get; init; }
public IPublicRegistryModuleIndexClient ModuleMetadataClient { get; init; }
public IPublicModuleIndexClient ModuleMetadataClient { get; init; }

public InvocationSettings(
FeatureProviderOverrides? FeatureOverrides = null,
IContainerRegistryClientFactory? ClientFactory = null,
ITemplateSpecRepositoryFactory? TemplateSpecRepositoryFactory = null,
IEnvironment? Environment = null,
IPublicRegistryModuleIndexClient? ModuleMetadataClient = null)
IPublicModuleIndexClient? ModuleMetadataClient = null)
{
this.FeatureOverrides = FeatureOverrides;
this.ClientFactory = ClientFactory ?? Repository.Create<IContainerRegistryClientFactory>().Object;
this.TemplateSpecRepositoryFactory = TemplateSpecRepositoryFactory ?? Repository.Create<ITemplateSpecRepositoryFactory>().Object;
this.Environment = Environment;

this.ModuleMetadataClient = ModuleMetadataClient ?? StrictMock.Of<IPublicRegistryModuleIndexClient>().Object;
this.ModuleMetadataClient = ModuleMetadataClient ?? StrictMock.Of<IPublicModuleIndexClient>().Object;
}
}

Expand Down Expand Up @@ -109,7 +109,7 @@ protected static void AssertNoErrors(string error)
}
}

protected static async Task<IEnumerable<string>> GetAllDiagnostics(string bicepFilePath, IContainerRegistryClientFactory clientFactory, ITemplateSpecRepositoryFactory templateSpecRepositoryFactory, IPublicRegistryModuleIndexClient? moduleMetadataClient = null)
protected static async Task<IEnumerable<string>> GetAllDiagnostics(string bicepFilePath, IContainerRegistryClientFactory clientFactory, ITemplateSpecRepositoryFactory templateSpecRepositoryFactory, IPublicModuleIndexClient? moduleMetadataClient = null)
{
var compilation = await CreateCompiler(clientFactory, templateSpecRepositoryFactory, moduleMetadataClient).CreateCompilation(PathHelper.FilePathToFileUrl(bicepFilePath));

Expand Down
Loading

0 comments on commit 5f03b3b

Please sign in to comment.