|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using Microsoft.Azure.WebJobs.Script.Config; |
| 5 | +using Microsoft.Azure.WebJobs.Script.WebHost; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using Microsoft.WebJobs.Script.Tests; |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.IO; |
| 11 | +using System.Linq; |
| 12 | +using System.Text; |
| 13 | +using System.Threading.Tasks; |
| 14 | +using WebJobs.Script.Tests; |
| 15 | +using Xunit; |
| 16 | +using static Microsoft.Azure.WebJobs.Script.Tests.SecretsRepositoryTests; |
| 17 | + |
| 18 | +namespace Microsoft.Azure.WebJobs.Script.Tests.Integration.Host |
| 19 | +{ |
| 20 | + public class SecretsRepositoryMigrationTests : IClassFixture<SecretsRepositoryMigrationTests.Fixture> |
| 21 | + { |
| 22 | + private readonly SecretsRepositoryMigrationTests.Fixture _fixture; |
| 23 | + private readonly ScriptSettingsManager _settingsManager; |
| 24 | + |
| 25 | + public SecretsRepositoryMigrationTests(SecretsRepositoryMigrationTests.Fixture fixture) |
| 26 | + { |
| 27 | + _fixture = fixture; |
| 28 | + _settingsManager = ScriptSettingsManager.Instance; |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public async Task SecretMigrate_Successful() |
| 33 | + { |
| 34 | + using (var directory = new TempDirectory()) |
| 35 | + { |
| 36 | + try |
| 37 | + { |
| 38 | + |
| 39 | + await _fixture.TestInitialize(SecretsRepositoryType.FileSystem, directory.Path); |
| 40 | + var loggerProvider = new TestLoggerProvider(); |
| 41 | + |
| 42 | + var fileRepo = _fixture.GetFileSystemSecretsRepository(); |
| 43 | + string hostContent = Guid.NewGuid().ToString(); |
| 44 | + string functionContent = Guid.NewGuid().ToString(); |
| 45 | + await fileRepo.WriteAsync(ScriptSecretsType.Host, "host", hostContent); |
| 46 | + await fileRepo.WriteAsync(ScriptSecretsType.Function, "test1", functionContent); |
| 47 | + |
| 48 | + _settingsManager.SetSetting(EnvironmentSettingNames.AzureWebsiteSlotName, "Production"); |
| 49 | + _settingsManager.SetSetting(EnvironmentSettingNames.AzureWebsiteName, "test-app"); |
| 50 | + |
| 51 | + var blobRepoMigration = _fixture.GetBlobStorageSecretsMigrationRepository(loggerProvider.CreateLogger(ScriptConstants.LogCategoryMigration)); |
| 52 | + |
| 53 | + await blobRepoMigration.ReadAsync(ScriptSecretsType.Host, "host"); |
| 54 | + var logs = loggerProvider.GetAllLogMessages().ToArray(); |
| 55 | + Assert.Contains(logs[logs.Length - 1].FormattedMessage, "Finished successfully."); |
| 56 | + string hostContentFromBlob = await blobRepoMigration.ReadAsync(ScriptSecretsType.Host, ""); |
| 57 | + Assert.Contains(hostContent, hostContentFromBlob); |
| 58 | + string hostContentFromFunction = await blobRepoMigration.ReadAsync(ScriptSecretsType.Function, "test1"); |
| 59 | + Assert.Contains(functionContent, hostContentFromFunction); |
| 60 | + |
| 61 | + var blobRepoMigration2 = _fixture.GetBlobStorageSecretsMigrationRepository(loggerProvider.CreateLogger("")); |
| 62 | + await blobRepoMigration2.ReadAsync(ScriptSecretsType.Host, "host"); |
| 63 | + logs = loggerProvider.GetAllLogMessages().ToArray(); |
| 64 | + Assert.Contains(logs[logs.Length - 1].FormattedMessage, "Sentinel file is detected."); |
| 65 | + } |
| 66 | + finally |
| 67 | + { |
| 68 | + _settingsManager.SetSetting(EnvironmentSettingNames.AzureWebsiteSlotName, null); |
| 69 | + _settingsManager.SetSetting(EnvironmentSettingNames.AzureWebsiteName, null); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public async Task SecretMigrate_Conflict() |
| 76 | + { |
| 77 | + using (var directory = new TempDirectory()) |
| 78 | + { |
| 79 | + try |
| 80 | + { |
| 81 | + await _fixture.TestInitialize(SecretsRepositoryType.FileSystem, directory.Path); |
| 82 | + var loggerProvider = new TestLoggerProvider(); |
| 83 | + |
| 84 | + var fileRepo = _fixture.GetFileSystemSecretsRepository(); |
| 85 | + string hostContent = Guid.NewGuid().ToString(); |
| 86 | + string functionContent = Guid.NewGuid().ToString(); |
| 87 | + await fileRepo.WriteAsync(ScriptSecretsType.Host, "host", hostContent); |
| 88 | + await fileRepo.WriteAsync(ScriptSecretsType.Function, "test1", functionContent); |
| 89 | + |
| 90 | + _settingsManager.SetSetting(EnvironmentSettingNames.AzureWebsiteSlotName, "Production"); |
| 91 | + _settingsManager.SetSetting(EnvironmentSettingNames.AzureWebsiteName, "test-app"); |
| 92 | + |
| 93 | + var blobRepo = _fixture.GetBlobStorageRepository(); |
| 94 | + await blobRepo.WriteAsync(ScriptSecretsType.Host, "host", hostContent); |
| 95 | + await blobRepo.WriteAsync(ScriptSecretsType.Function, "test1", functionContent); |
| 96 | + |
| 97 | + |
| 98 | + var blobRepoMigration = _fixture.GetBlobStorageSecretsMigrationRepository(loggerProvider.CreateLogger(ScriptConstants.LogCategoryMigration)); |
| 99 | + await blobRepoMigration.ReadAsync(ScriptSecretsType.Host, "host"); |
| 100 | + |
| 101 | + var logs = loggerProvider.GetAllLogMessages().ToArray(); |
| 102 | + Assert.Contains("Conflict detected", loggerProvider.GetAllLogMessages().ToList().Last().FormattedMessage); |
| 103 | + } |
| 104 | + finally |
| 105 | + { |
| 106 | + _settingsManager.SetSetting(EnvironmentSettingNames.AzureWebsiteSlotName, null); |
| 107 | + _settingsManager.SetSetting(EnvironmentSettingNames.AzureWebsiteName, null); |
| 108 | + |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public class Fixture : SecretsRepositoryTests.Fixture |
| 114 | + { |
| 115 | + public Fixture() : base() |
| 116 | + { |
| 117 | + } |
| 118 | + |
| 119 | + public ISecretsRepository GetFileSystemSecretsRepository() |
| 120 | + { |
| 121 | + return new FileSystemSecretsRepository(SecretsDirectory); |
| 122 | + } |
| 123 | + |
| 124 | + public ISecretsRepository GetBlobStorageRepository() |
| 125 | + { |
| 126 | + return new BlobStorageSecretsRepository(Path.Combine(SecretsDirectory, "Sentinels"), BlobConnectionString, TestSiteName); |
| 127 | + } |
| 128 | + |
| 129 | + public BlobStorageSecretsMigrationRepository GetBlobStorageSecretsMigrationRepository(ILogger logger) |
| 130 | + { |
| 131 | + var repo = new BlobStorageSecretsMigrationRepository(Path.Combine(SecretsDirectory, "Sentinels"), BlobConnectionString, TestSiteName, logger); |
| 132 | + return repo; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments