Ejemplo n.º 1
0
        public async Task ExternalModuleNotInCacheShouldThrow()
        {
            var dispatcher = StrictMock.Of <IModuleDispatcher>();

            DiagnosticBuilder.ErrorBuilderDelegate?failureBuilder = null;

            const string UnqualifiedModuleRefStr = "example.azurecr.invalid/foo/bar:v3";
            const string ModuleRefStr            = "br:" + UnqualifiedModuleRefStr;

            var configuration   = ConfigurationManager.GetBuiltInConfiguration();
            var moduleReference = OciArtifactModuleReference.TryParse(null, UnqualifiedModuleRefStr, configuration, out _) !;

            moduleReference.Should().NotBeNull();

            dispatcher.Setup(m => m.TryGetModuleReference(ModuleRefStr, It.IsAny <RootConfiguration>(), out failureBuilder)).Returns(moduleReference);
            dispatcher.Setup(m => m.GetModuleRestoreStatus(moduleReference, configuration, out failureBuilder)).Returns(ModuleRestoreStatus.Unknown);

            var resolver = StrictMock.Of <IFileResolver>();

            var handler = new BicepRegistryCacheRequestHandler(dispatcher.Object, resolver.Object, ConfigurationManager);

            var @params = new BicepRegistryCacheParams("/foo/main.bicep", ModuleRefStr);

            (await FluentActions
             .Awaiting(() => handler.Handle(@params, default))
             .Should()
             .ThrowAsync <InvalidOperationException>())
            .WithMessage($"The module '{ModuleRefStr}' has not yet been successfully restored.");
        }
Ejemplo n.º 2
0
        public async Task RestoredValidModuleShouldReturnSuccessfully()
        {
            var dispatcher = StrictMock.Of <IModuleDispatcher>();

            // needed for mocking out parameters
            DiagnosticBuilder.ErrorBuilderDelegate?nullBuilder        = null;
            DiagnosticBuilder.ErrorBuilderDelegate?readFailureBuilder = x => x.ErrorOccurredReadingFile("Mock file read failure.");
            string?fileContents = "mock file contents";

            const string UnqualifiedModuleRefStr = "example.azurecr.invalid/foo/bar:v3";
            const string ModuleRefStr            = "br:" + UnqualifiedModuleRefStr;

            var fileUri       = new Uri("file:///foo/bar/main.bicep");
            var configuration = ConfigurationManager.GetConfiguration(fileUri);

            var moduleReference = OciArtifactModuleReference.TryParse(null, UnqualifiedModuleRefStr, configuration, out _) !;

            moduleReference.Should().NotBeNull();

            dispatcher.Setup(m => m.TryGetModuleReference(ModuleRefStr, It.IsAny <RootConfiguration>(), out nullBuilder)).Returns(moduleReference);
            dispatcher.Setup(m => m.GetModuleRestoreStatus(moduleReference, It.IsAny <RootConfiguration>(), out nullBuilder)).Returns(ModuleRestoreStatus.Succeeded);
            dispatcher.Setup(m => m.TryGetLocalModuleEntryPointUri(null, moduleReference, It.IsAny <RootConfiguration>(), out nullBuilder)).Returns(fileUri);

            var resolver = StrictMock.Of <IFileResolver>();

            resolver.Setup(m => m.TryRead(fileUri, out fileContents, out nullBuilder)).Returns(true);

            var handler = new BicepRegistryCacheRequestHandler(dispatcher.Object, resolver.Object, ConfigurationManager);

            var @params  = new BicepRegistryCacheParams(fileUri.AbsolutePath, ModuleRefStr);
            var response = await handler.Handle(@params, default);

            response.Should().NotBeNull();
            response.Content.Should().Be(fileContents);
        }
Ejemplo n.º 3
0
        public void TryGetModuleReference_ValidAlias_ReplacesReferenceValue(string aliasName, string referenceValue, string fullyQualifiedReferenceValue, RootConfiguration configuration)
        {
            var reference = OciArtifactModuleReference.TryParse(aliasName, referenceValue, configuration, out var errorBuilder);

            reference.Should().NotBeNull();
            reference !.FullyQualifiedReference.Should().Be(fullyQualifiedReferenceValue);
        }
        private static OciArtifactModuleReference Parse(string package)
        {
            var parsed = OciArtifactModuleReference.TryParse(null, package, BicepTestConstants.BuiltInConfigurationWithAnalyzersDisabled, out var failureBuilder);

            failureBuilder !.Should().BeNull();
            parsed.Should().NotBeNull();
            return(parsed !);
        }
        public void TryParse_InvalidAliasName_ReturnsNullAndSetsErrorDiagnostic(string aliasName)
        {
            var reference = OciArtifactModuleReference.TryParse(aliasName, "", BicepTestConstants.BuiltInConfiguration, out var errorBuilder);

            reference.Should().BeNull();
            errorBuilder !.Should().HaveCode("BCP211");
            errorBuilder !.Should().HaveMessage($"The module alias name \"{aliasName}\" is invalid. Valid characters are alphanumeric, \"_\", or \"-\".");
        }
        public void TryParse_InvalidAlias_ReturnsNullAndSetsError(string aliasName, string referenceValue, RootConfiguration configuration, string expectedCode, string expectedMessage)
        {
            var reference = OciArtifactModuleReference.TryParse(aliasName, referenceValue, configuration, out var errorBuilder);

            reference.Should().BeNull();
            ((object?)errorBuilder).Should().NotBeNull();
            errorBuilder !.Should().HaveCode(expectedCode);
            errorBuilder !.Should().HaveMessage(expectedMessage);
        }
        public void InvalidReferencesShouldProduceExpectedError(string value, string expectedCode, string expectedError)
        {
            OciArtifactModuleReference.TryParse(null, value, BicepTestConstants.BuiltInConfigurationWithAnalyzersDisabled, out var failureBuilder).Should().BeNull();
            failureBuilder !.Should().NotBeNull();

            using (new AssertionScope())
            {
                failureBuilder !.Should().HaveCode(expectedCode);
                failureBuilder !.Should().HaveMessage(expectedError);
            }
        }
        public void TryParse_AliasNotInConfiguration_ReturnsNullAndSetsError(string aliasName, string referenceValue, string?configurationPath, string expectedCode, string expectedMessage)
        {
            var configuration = BicepTestConstants.CreateMockConfiguration(configurationPath: configurationPath);

            var reference = OciArtifactModuleReference.TryParse(aliasName, referenceValue, configuration, out var errorBuilder);

            reference.Should().BeNull();
            ((object?)errorBuilder).Should().NotBeNull();
            errorBuilder !.Should().HaveCode(expectedCode);
            errorBuilder !.Should().HaveMessage(expectedMessage);
        }
Ejemplo n.º 9
0
        public async Task FailureToReadEntryPointShouldThrow()
        {
            var dispatcher = StrictMock.Of <IModuleDispatcher>();

            // needed for mocking out parameters
            DiagnosticBuilder.ErrorBuilderDelegate?nullBuilder        = null;
            DiagnosticBuilder.ErrorBuilderDelegate?readFailureBuilder = x => x.ErrorOccurredReadingFile("Mock file read failure.");
            string?fileContents = null;

            const string UnqualifiedModuleRefStr = "example.azurecr.invalid/foo/bar:v3";
            const string ModuleRefStr            = "br:" + UnqualifiedModuleRefStr;

            var configuration   = ConfigurationManager.GetBuiltInConfiguration();
            var moduleReference = OciArtifactModuleReference.TryParse(null, UnqualifiedModuleRefStr, configuration, out _) !;

            moduleReference.Should().NotBeNull();

            var fileUri = new Uri("file:///main.bicep");

            dispatcher.Setup(m => m.TryGetModuleReference(ModuleRefStr, It.IsAny <RootConfiguration>(), out nullBuilder)).Returns(moduleReference);
            dispatcher.Setup(m => m.GetModuleRestoreStatus(moduleReference, configuration, out nullBuilder)).Returns(ModuleRestoreStatus.Succeeded);
            dispatcher.Setup(m => m.TryGetLocalModuleEntryPointUri(null, moduleReference, configuration, out nullBuilder)).Returns(fileUri);

            var resolver = StrictMock.Of <IFileResolver>();

            resolver.Setup(m => m.TryRead(fileUri, out fileContents, out readFailureBuilder)).Returns(false);

            var handler = new BicepRegistryCacheRequestHandler(dispatcher.Object, resolver.Object, ConfigurationManager);

            var @params = new BicepRegistryCacheParams(fileUri.AbsolutePath, ModuleRefStr);

            (await FluentActions
             .Awaiting(() => handler.Handle(@params, default))
             .Should()
             .ThrowAsync <InvalidOperationException>())
            .WithMessage($"Unable to read file 'file:///main.bicep'. An error occurred reading file. Mock file read failure.");
        }