public void TestManagedDependencySuccessfulModuleDownloadAfterTwoTries()
        {
            try
            {
                // Test case setup
                var requirementsDirectoryName     = "BasicRequirements";
                var functionFolderPath            = Path.Combine(_dependencyManagementDirectory, requirementsDirectoryName, "FunctionDirectory");
                var functionAppRoot               = Path.Combine(_dependencyManagementDirectory, requirementsDirectoryName);
                var managedDependenciesFolderPath = InitializeManagedDependenciesDirectory(functionAppRoot);

                var functionLoadRequest = GetFuncLoadRequest(functionFolderPath, true);

                // Configure MockModuleProvider to not throw in the RunSaveModuleCommand call after 2 tries.
                var mockModuleProvider = new MockModuleProvider {
                    ShouldNotThrowAfterCount = 2
                };

                // Create DependencyManager and process the requirements.psd1 file at the function app root.
                using (var dependencyManager = new DependencyManager(functionLoadRequest.Metadata.Directory, mockModuleProvider))
                {
                    dependencyManager.Initialize(_testLogger);

                    // Try to install the function app dependencies.
                    var dependencyError = dependencyManager.InstallFunctionAppDependencies(PowerShell.Create(), PowerShell.Create, _testLogger);

                    var relevantLogs = _testLogger.FullLog.Where(
                        message => message.StartsWith("Error: Failed to install module") ||
                        message.StartsWith("Trace: Installing function app required modules") ||
                        message.StartsWith("Trace: Module name")).ToList();

                    // Here we will get four logs:
                    // - one that say that we are installing the dependencies
                    // - two that say that we failed to download the module
                    // - one for a successful module download
                    Assert.Equal(4, relevantLogs.Count);

                    Assert.Contains("Installing function app required modules", relevantLogs[0]);

                    // The subsequent two logs should contain the following: "Fail to install module"
                    for (int index = 1; index < relevantLogs.Count - 1; index++)
                    {
                        Assert.Contains("Failed to install module", relevantLogs[index]);
                        var currentAttempt = DependencySnapshotInstaller.GetCurrentAttemptMessage(index);
                        Assert.Contains(currentAttempt, relevantLogs[index]);
                    }

                    // Successful module download log after two retries.
                    // In the overwritten RunSaveModuleCommand method, we saved in DownloadedModuleInfo the module name and version.
                    // This same information is logged after running save-module, so validate that they match.
                    Assert.Contains(mockModuleProvider.DownloadedModuleInfo, relevantLogs[3]);

                    // Lastly, DependencyError should be null since the module was downloaded successfully after two tries.
                    Assert.Null(dependencyError);
                }
            }
            finally
            {
                TestCaseCleanup();
            }
        }
Exemple #2
0
        public void TestManagedDependencyRetryLogicMaxNumberOfTries()
        {
            try
            {
                // Test case setup
                var requirementsDirectoryName     = "BasicRequirements";
                var functionFolderPath            = Path.Combine(_dependencyManagementDirectory, requirementsDirectoryName, "FunctionDirectory");
                var functionAppRoot               = Path.Combine(_dependencyManagementDirectory, requirementsDirectoryName);
                var managedDependenciesFolderPath = InitializeManagedDependenciesDirectory(functionAppRoot);

                var functionLoadRequest = GetFuncLoadRequest(functionFolderPath, true);

                // Create DependencyManager and process the requirements.psd1 file at the function app root.
                using (var dependencyManager = new DependencyManager(functionLoadRequest.Metadata.Directory, new MockModuleProvider()))
                {
                    dependencyManager.Initialize(_testLogger);

                    // Try to install the function app dependencies.
                    var dependencyError = dependencyManager.InstallFunctionAppDependencies(PowerShell.Create(), PowerShell.Create, _testLogger);

                    var relevantLogs = _testLogger.FullLog.Where(
                        message => message.StartsWith("Error: Fail to install module") ||
                        message.StartsWith("Trace: Installing FunctionApp dependent modules") ||
                        message.StartsWith("Warning: Failed to install dependencies")).ToList();

                    // Here we will get five logs: one that says that we are installing the
                    // dependencies, three for failing to install the module,
                    // and one warning notifying of removing the dependencies folder.
                    Assert.Equal(5, relevantLogs.Count);

                    // The first log should say "Installing FunctionApp dependent modules."
                    Assert.Contains(PowerShellWorkerStrings.InstallingFunctionAppDependentModules, relevantLogs[0]);

                    // The subsequent logs should contain the following:
                    for (int index = 1; index < 4; index++)
                    {
                        Assert.Contains("Fail to install module", relevantLogs[index]);
                        var currentAttempt = DependencySnapshotInstaller.GetCurrentAttemptMessage(index);
                        Assert.Contains(currentAttempt, relevantLogs[index]);
                    }

                    Assert.Matches("Warning: Failed to install dependencies into '(.+?)', removing the folder", relevantLogs[4]);

                    // Lastly, DependencyError should get set after unsuccessfully retrying 3 times.
                    Assert.NotNull(dependencyError);
                    Assert.Contains("Fail to install FunctionApp dependencies. Error:", dependencyError.Message);
                }
            }
            finally
            {
                TestCaseCleanup();
            }
        }