Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenWeatherford committed Jan 24, 2025
1 parent 8ae4324 commit 5ea8c28
Show file tree
Hide file tree
Showing 113 changed files with 3,937 additions and 1,773 deletions.
4 changes: 2 additions & 2 deletions src/Bicep.Cli.IntegrationTests/BuildCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,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 @@ -194,7 +194,7 @@ public async Task Build_Valid_SingleFile_WithDigestReference_ShouldSucceed()
var client = new MockRegistryBlobClient();

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), registryUri, repository)).Returns(client);
clientFactory.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<CloudConfiguration>(), registryUri, repository)).Returns(client);

var templateSpecRepositoryFactory = BicepTestConstants.TemplateSpecRepositoryFactory;

Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Cli.IntegrationTests/BuildParamsCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public async Task Build_bicepparam_should_fail_with_error_diagnostics_for_regist

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), new Uri("https://mockregistry.io"), "parameters/basic"))
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<CloudConfiguration>(), new Uri("https://mockregistry.io"), "parameters/basic"))
.Returns(client.Object);

var templateSpecRepositoryFactory = StrictMock.Of<ITemplateSpecRepositoryFactory>();
Expand Down
9 changes: 5 additions & 4 deletions src/Bicep.Cli.IntegrationTests/LintCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
using Bicep.Cli.UnitTests;
using Bicep.Core.Configuration;
using Bicep.Core.Registry;
using Bicep.Core.Registry.PublicRegistry;
using Bicep.Core.Registry.Catalog;
using Bicep.Core.Samples;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.Mock;
using Bicep.Core.UnitTests.Mock.Registry;
using Bicep.Core.UnitTests.Mock.Registry.Catalog;
using Bicep.Core.UnitTests.Registry;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
Expand Down Expand Up @@ -112,7 +114,7 @@ public async Task Lint_Valid_SingleFile_WithDigestReference_ShouldSucceed()
var client = new MockRegistryBlobClient();

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), registryUri, repository)).Returns(client);
clientFactory.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<CloudConfiguration>(), registryUri, repository)).Returns(client);

var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), clientFactory.Object, BicepTestConstants.TemplateSpecRepositoryFactory);

Expand Down Expand Up @@ -178,11 +180,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.RegistryTestHelper;
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 = (MockRegistryBlobClient)clientFactory.CreateAuthenticatedBlobClient(BicepTestConstants.BuiltInConfiguration.Cloud, 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 = (MockRegistryBlobClient)clientFactory.CreateAuthenticatedBlobClient(BicepTestConstants.BuiltInConfiguration.Cloud, 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 = (MockRegistryBlobClient)clientFactory.CreateAuthenticatedBlobClient(BicepTestConstants.BuiltInConfiguration.Cloud, registryUri, repository);

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

Expand Down Expand Up @@ -343,7 +344,7 @@ public async Task Publish_RequestFailedException_ShouldFail()

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), new Uri("https://fake"), "fake"))
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<CloudConfiguration>(), new Uri("https://fake"), "fake"))
.Returns(client.Object);

var templateSpecRepositoryFactory = StrictMock.Of<ITemplateSpecRepositoryFactory>();
Expand Down Expand Up @@ -375,7 +376,7 @@ public async Task Publish_AggregateExceptionWithInnerRequestFailedExceptions_Sho

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), new Uri("https://fake"), "fake"))
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<CloudConfiguration>(), new Uri("https://fake"), "fake"))
.Returns(client.Object);

var templateSpecRepositoryFactory = StrictMock.Of<ITemplateSpecRepositoryFactory>();
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, blobClients, _) = RegistryTestHelper.CreateMockRegistryClients(new RepoDescriptor(registryStr, repository, ["v1"]));

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

await RegistryHelper.PublishModuleToRegistryAsync(clientFactory, BicepTestConstants.FileSystem, "modulename", $"br:example.com/test/{moduleName}:v1", bicepModuleContents, publishSource: false, documentationUri);
await RegistryTestHelper.PublishModuleToRegistryAsync(
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using static Bicep.Core.UnitTests.Utils.RegistryTestHelper;

namespace Bicep.Cli.IntegrationTests;

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

var (clientFactory, blobClientMocks) = RegistryHelper.CreateMockRegistryClients((registryStr, repository));
var (clientFactory, blobClientMocks, _) = RegistryTestHelper.CreateMockRegistryClients(new RepoDescriptor(registryStr, repository, ["tag"]));
var mockBlobClient = blobClientMocks[(registryUri, repository)];

var indexPath = Path.Combine(outputDirectory, "index.json");
Expand Down
22 changes: 11 additions & 11 deletions src/Bicep.Cli.IntegrationTests/RestoreCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,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 @@ -90,7 +90,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 All @@ -117,13 +117,13 @@ public async Task Restore_ShouldSucceedWithAnonymousClient(string testName, Data
// this will force fallback to the anonymous client
var clientFactoryForRestore = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactoryForRestore
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), It.IsAny<Uri>(), It.IsAny<string>()))
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<CloudConfiguration>(), It.IsAny<Uri>(), It.IsAny<string>()))
.Returns(clientWithCredentialUnavailable.Object);

// anonymous client creation will redirect to the working client factory containing mock published modules
clientFactoryForRestore
.Setup(m => m.CreateAnonymousBlobClient(It.IsAny<RootConfiguration>(), It.IsAny<Uri>(), It.IsAny<string>()))
.Returns<RootConfiguration, Uri, string>(clientFactory.CreateAnonymousBlobClient);
.Setup(m => m.CreateAnonymousBlobClient(It.IsAny<CloudConfiguration>(), It.IsAny<Uri>(), It.IsAny<string>()))
.Returns<CloudConfiguration, Uri, string>(clientFactory.CreateAnonymousBlobClient);

var settings = new InvocationSettings(new(TestContext, RegistryEnabled: dataSet.HasExternalModules), clientFactoryForRestore.Object, templateSpecRepositoryFactory);
TestContext.WriteLine($"Cache root = {settings.FeatureOverrides!.CacheRootDirectory}");
Expand All @@ -139,7 +139,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 @@ -309,7 +309,7 @@ public async Task Restore_With_Force_Should_Overwrite_Existing_Cache(bool publis
var client = new MockRegistryBlobClient();

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), registryUri, repository)).Returns(client);
clientFactory.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<CloudConfiguration>(), registryUri, repository)).Returns(client);

var templateSpecRepositoryFactory = BicepTestConstants.TemplateSpecRepositoryFactory;

Expand Down Expand Up @@ -430,7 +430,7 @@ public async Task Restore_ByDigest_ShouldSucceed(bool publishSource)
var client = new MockRegistryBlobClient();

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), registryUri, repository)).Returns(client);
clientFactory.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<CloudConfiguration>(), registryUri, repository)).Returns(client);

var templateSpecRepositoryFactory = BicepTestConstants.TemplateSpecRepositoryFactory;

Expand Down Expand Up @@ -517,7 +517,7 @@ public async Task Restore_AggregateExceptionWithInnerRequestFailedExceptions_Sho

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), new Uri("https://fake"), "fake"))
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<CloudConfiguration>(), new Uri("https://fake"), "fake"))
.Returns(client.Object);

var templateSpecRepositoryFactory = StrictMock.Of<ITemplateSpecRepositoryFactory>();
Expand Down Expand Up @@ -549,7 +549,7 @@ public async Task Restore_RequestFailedException_ShouldFail()

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), new Uri("https://fake"), "fake"))
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<CloudConfiguration>(), new Uri("https://fake"), "fake"))
.Returns(client.Object);

var templateSpecRepositoryFactory = StrictMock.Of<ITemplateSpecRepositoryFactory>();
Expand Down Expand Up @@ -578,7 +578,7 @@ public async Task Restore_bicepparam_should_fail_with_error_diagnostics_for_regi

var clientFactory = StrictMock.Of<IContainerRegistryClientFactory>();
clientFactory
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<RootConfiguration>(), new Uri("https://mockregistry.io"), "parameters/basic"))
.Setup(m => m.CreateAuthenticatedBlobClient(It.IsAny<CloudConfiguration>(), new Uri("https://mockregistry.io"), "parameters/basic"))
.Returns(client.Object);

var templateSpecRepositoryFactory = StrictMock.Of<ITemplateSpecRepositoryFactory>();
Expand Down
Loading

0 comments on commit 5ea8c28

Please sign in to comment.