public void ResolveAssembly_WithIndirectPrivateDependency_LogsIfResolutionFails()
        {
            TestLogger testLogger = new TestLogger("Test");

            var resolver = new FunctionAssemblyLoader("c:\\");

            var metadata1Directory = @"c:\testroot\test1";
            var metadata1          = new FunctionMetadata {
                Name = "Test1", ScriptFile = $@"{metadata1Directory}\test.tst"
            };
            var metadata2 = new FunctionMetadata {
                Name = "Test2", ScriptFile = @"c:\testroot\test2\test.tst"
            };

            var mockResolver = new Mock <IFunctionMetadataResolver>();

            mockResolver.Setup(m => m.ResolveAssembly("MyTestAssembly.dll"))
            .Returns <Assembly>(null);

            resolver.CreateOrUpdateContext(metadata1, this.GetType().Assembly, new FunctionMetadataResolver(metadata1.ScriptFile, new Collection <ScriptBindingProvider>(), testLogger), testLogger);
            resolver.CreateOrUpdateContext(metadata2, this.GetType().Assembly, mockResolver.Object, testLogger);

            Assembly result = resolver.ResolveAssembly(AppDomain.CurrentDomain, new System.ResolveEventArgs("MyTestAssembly.dll",
                                                                                                            new TestAssembly(new AssemblyName("MyDirectReference"), @"file:///c:/testroot/test2/bin/MyDirectReference.dll")));

            Assert.Null(result);
            Assert.Equal(1, testLogger.GetLogMessages().Count);
            Assert.Contains("MyTestAssembly.dll", testLogger.GetLogMessages()[0].FormattedMessage);
        }
Exemplo n.º 2
0
        public async Task Test_LogAutorestGeneratedJson_Without_AutorestGeneratedJson()
        {
            string functionAppDirectory = GetTemporaryDirectory();

            try
            {
                Utility.LogAutorestGeneratedJsonIfExists(functionAppDirectory, _testLogger);
                var allLogs = _testLogger.GetLogMessages();
                Assert.Empty(allLogs);
            }
            finally
            {
                await FileUtility.DeleteDirectoryAsync(functionAppDirectory, true);
            }
        }
        public async Task GetExtensionsStartupTypes_ValidExtensionBundle_FiltersBuiltinExtensionsAsync()
        {
            var references = new[]
            {
                new ExtensionReference {
                    Name = "Http", TypeName = typeof(HttpWebJobsStartup).AssemblyQualifiedName
                },
                new ExtensionReference {
                    Name = "Timers", TypeName = typeof(ExtensionsWebJobsStartup).AssemblyQualifiedName
                },
                new ExtensionReference {
                    Name = "Storage", TypeName = typeof(AzureStorageWebJobsStartup).AssemblyQualifiedName
                },
            };

            var extensions = new JObject
            {
                { "extensions", JArray.FromObject(references) }
            };

            var mockExtensionBundleManager = new Mock <IExtensionBundleManager>();

            mockExtensionBundleManager.Setup(e => e.IsExtensionBundleConfigured()).Returns(true);

            using (var directory = new TempDirectory())
            {
                var binPath = Path.Combine(directory.Path, "bin");
                mockExtensionBundleManager.Setup(e => e.GetExtensionBundlePath()).ReturnsAsync(directory.Path);
                Directory.CreateDirectory(binPath);

                void CopyToBin(string path)
                {
                    File.Copy(path, Path.Combine(binPath, Path.GetFileName(path)));
                }

                CopyToBin(typeof(HttpWebJobsStartup).Assembly.Location);
                CopyToBin(typeof(ExtensionsWebJobsStartup).Assembly.Location);
                CopyToBin(typeof(AzureStorageWebJobsStartup).Assembly.Location);

                File.WriteAllText(Path.Combine(binPath, "extensions.json"), extensions.ToString());

                var testLogger = new TestLogger("test");
                var discoverer = new ScriptStartupTypeLocator(directory.Path, testLogger, mockExtensionBundleManager.Object);

                // Act
                var types = await discoverer.GetExtensionsStartupTypesAsync();

                var traces = testLogger.GetLogMessages();

                // Assert
                Assert.Single(types);
                Assert.Equal(typeof(AzureStorageWebJobsStartup).FullName, types.Single().FullName);
                Assert.True(traces.Any(m => string.Equals(m.FormattedMessage, $"The extension startup type '{references[0].TypeName}' belongs to a builtin extension")));
                Assert.True(traces.Any(m => string.Equals(m.FormattedMessage, $"The extension startup type '{references[1].TypeName}' belongs to a builtin extension")));
            }
        }
        public async Task GetExtensionsStartupTypes_UnableToDownloadExtensionBundle_ReturnsNull()
        {
            var mockExtensionBundleManager = new Mock <IExtensionBundleManager>();

            mockExtensionBundleManager.Setup(e => e.IsExtensionBundleConfigured()).Returns(true);
            mockExtensionBundleManager.Setup(e => e.GetExtensionBundlePath()).ReturnsAsync(string.Empty);
            var testLogger = new TestLogger("test");
            var discoverer = new ScriptStartupTypeLocator(string.Empty, testLogger, mockExtensionBundleManager.Object);

            // Act
            var types = await discoverer.GetExtensionsStartupTypesAsync();

            var traces = testLogger.GetLogMessages();

            // Assert
            Assert.True(traces.Any(m => string.Equals(m.FormattedMessage, $"Unable to find or download extension bundle")));
            Assert.Null(types);
        }
        public void GetFunctionMetadataAsync_WorkerIndexing_HostFallback()
        {
            // Arrange
            _logger.ClearLogMessages();

            var function = GetTestRawFunctionMetadata(useDefaultMetadataIndexing: true);
            IEnumerable <RawFunctionMetadata> rawFunctionMetadataCollection = new List <RawFunctionMetadata>()
            {
                function
            };
            var functionMetadataCollection = new List <FunctionMetadata>();

            functionMetadataCollection.Add(GetTestFunctionMetadata());

            var workerConfigs = TestHelpers.GetTestWorkerConfigs().ToImmutableArray();

            workerConfigs.ToList().ForEach(config => config.Description.WorkerIndexing = "true");
            var scriptjobhostoptions = new ScriptJobHostOptions();

            scriptjobhostoptions.RootScriptPath = Path.Combine(Environment.CurrentDirectory, @"..", "..", "..", "..", "..", "sample", "node");

            var environment = SystemEnvironment.Instance;

            environment.SetEnvironmentVariable(EnvironmentSettingNames.FunctionWorkerRuntime, "node");
            environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebJobsFeatureFlags, "EnableWorkerIndexing");

            _mockRpcFunctionInvocationDispatcher.Setup(m => m.InitializeAsync(functionMetadataCollection, default)).Returns(Task.FromResult(0));
            _mockRpcFunctionInvocationDispatcher.Setup(m => m.GetWorkerMetadata()).Returns(Task.FromResult(rawFunctionMetadataCollection));
            _mockRpcFunctionInvocationDispatcher.Setup(m => m.FinishInitialization(functionMetadataCollection, default)).Returns(Task.FromResult(0));
            _mockFunctionMetadataProvider.Setup(m => m.GetFunctionMetadataAsync(workerConfigs, environment, false)).Returns(Task.FromResult(functionMetadataCollection.ToImmutableArray()));

            _aggregateFunctionMetadataProvider = new AggregateFunctionMetadataProvider(_logger, _mockRpcFunctionInvocationDispatcher.Object, _mockFunctionMetadataProvider.Object, new OptionsWrapper <ScriptJobHostOptions>(scriptjobhostoptions));

            // Act
            var functions = _aggregateFunctionMetadataProvider.GetFunctionMetadataAsync(workerConfigs, environment, false).GetAwaiter().GetResult();

            // Assert
            var traces           = _logger.GetLogMessages();
            var functionLoadLogs = traces.Where(m => string.Equals(m.FormattedMessage, "Fallback to host indexing as worker denied indexing"));

            Assert.True(functionLoadLogs.Any());
        }