|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Reflection; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Azure; |
| 9 | +using Azure.Storage.Blobs; |
| 10 | +using Azure.Storage.Blobs.Models; |
| 11 | +using Azure.Storage.Queues; |
| 12 | +using Microsoft.Azure.WebJobs.Extensions.Storage.Blobs.Listeners; |
| 13 | +using Microsoft.Azure.WebJobs.Extensions.Storage.Common; |
| 14 | +using Microsoft.Azure.WebJobs.Extensions.Storage.Common.Listeners; |
| 15 | +using Microsoft.Azure.WebJobs.Host; |
| 16 | +using Microsoft.Azure.WebJobs.Host.Executors; |
| 17 | +using Microsoft.Azure.WebJobs.Host.Listeners; |
| 18 | +using Microsoft.Azure.WebJobs.Host.Protocols; |
| 19 | +using Microsoft.Azure.WebJobs.Host.Scale; |
| 20 | +using Microsoft.Azure.WebJobs.Host.Timers; |
| 21 | +using Microsoft.Extensions.Logging; |
| 22 | +using Moq; |
| 23 | +using NUnit.Framework; |
| 24 | +using NUnit.Framework.Internal; |
| 25 | + |
| 26 | +namespace Microsoft.Azure.WebJobs.Extensions.Storage.Blobs.Tests.Listeners |
| 27 | +{ |
| 28 | + public class BlobListenerFactoryTests |
| 29 | + { |
| 30 | + [Test] |
| 31 | + public async Task CreateAsync_RegisterWithSharedBlobListenerAsync_UsesTargetBlobClient() |
| 32 | + { |
| 33 | + // Arrange |
| 34 | + // Storage account and container names |
| 35 | + string accountName1 = "fakeaccount1"; |
| 36 | + string accountName2 = "fakeaccount2"; |
| 37 | + string containerName1 = "fakecontainer1"; |
| 38 | + string containerName2 = "fakecontainer2"; |
| 39 | + |
| 40 | + // Mock BlobContainerClients |
| 41 | + var containerClient1 = new Mock<BlobContainerClient>(new Uri($"https://{accountName1}.blob.core.windows.net/{containerName1}"), null); |
| 42 | + containerClient1.Setup(x => x.Uri).Returns(new Uri($"https://{accountName1}.blob.core.windows.net/{containerName1}")); |
| 43 | + containerClient1.Setup(x => x.Name).Returns(containerName1); |
| 44 | + containerClient1.Setup(x => x.AccountName).Returns(accountName1); |
| 45 | + |
| 46 | + var containerClient2 = new Mock<BlobContainerClient>(new Uri($"https://{accountName1}.blob.core.windows.net/{containerName2}"), null); |
| 47 | + containerClient2.Setup(x => x.Uri).Returns(new Uri($"https://{accountName1}.blob.core.windows.net/{containerName2}")); |
| 48 | + containerClient2.Setup(x => x.Name).Returns(containerName2); |
| 49 | + containerClient2.Setup(x => x.AccountName).Returns(accountName2); |
| 50 | + |
| 51 | + var hostNamesContainerClient = new Mock<BlobContainerClient>(new Uri($"https://{accountName1}.blob.core.windows.net/{HostContainerNames.Hosts}"), null); |
| 52 | + hostNamesContainerClient.Setup(x => x.Uri).Returns(new Uri($"https://{accountName1}.blob.core.windows.net/{HostContainerNames.Hosts}")); |
| 53 | + hostNamesContainerClient.Setup(x => x.Name).Returns(HostContainerNames.Hosts); |
| 54 | + hostNamesContainerClient.Setup(x => x.AccountName).Returns(accountName1); |
| 55 | + |
| 56 | + // Mock BlobServiceClients |
| 57 | + var primaryClient = new Mock<BlobServiceClient>(new Uri($"https://{accountName1}.blob.core.windows.net/"), null); |
| 58 | + primaryClient.Setup(x => x.Uri).Returns(new Uri($"https://{accountName1}.blob.core.windows.net/")); |
| 59 | + primaryClient.Setup(x => x.AccountName).Returns(accountName1); |
| 60 | + primaryClient.Setup(x => x.GetBlobContainerClient(containerName1)).Returns(containerClient1.Object); |
| 61 | + primaryClient.Setup(x => x.GetBlobContainerClient(containerName2)).Returns(containerClient2.Object); |
| 62 | + primaryClient.Setup(x => x.GetBlobContainerClient(HostContainerNames.Hosts)).Returns(hostNamesContainerClient.Object); |
| 63 | + primaryClient.Setup(x => x.GetPropertiesAsync(It.IsAny<CancellationToken>())).ReturnsAsync(Response.FromValue(new BlobServiceProperties(), null)); |
| 64 | + |
| 65 | + var targetClient = new Mock<BlobServiceClient>(new Uri($"https://{accountName2}.blob.core.windows.net/"), null); |
| 66 | + targetClient.Setup(x => x.Uri).Returns(new Uri($"https://{accountName2}.blob.core.windows.net/")); |
| 67 | + targetClient.Setup(x => x.AccountName).Returns(accountName2); |
| 68 | + targetClient.Setup(x => x.GetPropertiesAsync(It.IsAny<CancellationToken>())).ReturnsAsync(Response.FromValue(new BlobServiceProperties(), null)); |
| 69 | + |
| 70 | + // Other dependencies |
| 71 | + var loggerFactory = new LoggerFactory(); |
| 72 | + var logger = loggerFactory.CreateLogger<BlobListener>(); |
| 73 | + var hostIdProvider = new Mock<IHostIdProvider>(); |
| 74 | + var blobsOptions = new BlobsOptions(); |
| 75 | + var exceptionHandler = new Mock<IWebJobsExceptionHandler>(); |
| 76 | + var blobWrittenWatcherSetter = new Mock<IContextSetter<IBlobWrittenWatcher>>(); |
| 77 | + var hostQueueServiceClient = new QueueServiceClient(new Uri($"https://{accountName1}.queue.core.windows.net/")); |
| 78 | + var dataQueueServiceClient = new QueueServiceClient(new Uri($"https://{accountName2}.queue.core.windows.net/")); |
| 79 | + var queueServiceClientProvider = new FakeQueueServiceClientProvider(hostQueueServiceClient); |
| 80 | + var sharedQueueWatcher = new SharedQueueWatcher(); |
| 81 | + var blobTriggerQueueWriterFactory = new BlobTriggerQueueWriterFactory( |
| 82 | + hostIdProvider.Object, |
| 83 | + queueServiceClientProvider, |
| 84 | + sharedQueueWatcher); |
| 85 | + var executor = new Mock<ITriggeredFunctionExecutor>(); |
| 86 | + var input = new Mock<IBlobPathSource>(); |
| 87 | + var singletonManager = new Mock<IHostSingletonManager>(); |
| 88 | + var concurrencyManager = new Mock<ConcurrencyManager>(); |
| 89 | + var drainModeManager = new Mock<IDrainModeManager>(); |
| 90 | + var functionDescriptor = new FunctionDescriptor { Id = "id", ShortName = "shortname" }; |
| 91 | + |
| 92 | + // Setup SharedContextProvider and Test Strategy |
| 93 | + var sharedContextProvider = new Mock<ISharedContextProvider>(); |
| 94 | + var testStrategy = new TestBlobListenerStrategy(); |
| 95 | + var sharedBlobListener = new SharedBlobListener(testStrategy, exceptionHandler.Object); |
| 96 | + sharedContextProvider.Setup(x => x.GetOrCreateInstance(It.IsAny<SharedBlobListenerFactory>())) |
| 97 | + .Returns(sharedBlobListener); |
| 98 | + |
| 99 | + // Setup SharedBlobQueueListener |
| 100 | + var sharedBlobQueueListener = new SharedBlobQueueListener( |
| 101 | + new Mock<IListener>().Object, |
| 102 | + new BlobQueueTriggerExecutor(BlobTriggerSource.LogsAndContainerScan, |
| 103 | + new Mock<IBlobWrittenWatcher>().Object, logger)); |
| 104 | + sharedContextProvider.Setup(s => s.GetOrCreateInstance<SharedBlobQueueListener>(It.IsAny<SharedBlobQueueListenerFactory>())) |
| 105 | + .Returns(sharedBlobQueueListener); |
| 106 | + |
| 107 | + // Create the factory |
| 108 | + var factory = new BlobListenerFactory( |
| 109 | + hostIdProvider.Object, |
| 110 | + blobsOptions, |
| 111 | + exceptionHandler.Object, |
| 112 | + blobWrittenWatcherSetter.Object, |
| 113 | + blobTriggerQueueWriterFactory, |
| 114 | + sharedContextProvider.Object, |
| 115 | + loggerFactory, |
| 116 | + functionDescriptor, |
| 117 | + primaryClient.Object, |
| 118 | + hostQueueServiceClient, |
| 119 | + targetClient.Object, |
| 120 | + dataQueueServiceClient, |
| 121 | + containerClient1.Object, |
| 122 | + input.Object, |
| 123 | + BlobTriggerSource.LogsAndContainerScan, |
| 124 | + executor.Object, |
| 125 | + singletonManager.Object, |
| 126 | + concurrencyManager.Object, |
| 127 | + drainModeManager.Object |
| 128 | + ); |
| 129 | + |
| 130 | + // Act |
| 131 | + await factory.CreateAsync(CancellationToken.None); |
| 132 | + |
| 133 | + // Assert |
| 134 | + // 1. The strategy should have registered the target client and container |
| 135 | + Assert.AreEqual(targetClient.Object, testStrategy.TargetServiceClient, "TargetServiceClient should be the target client."); |
| 136 | + Assert.AreEqual(containerClient1.Object, testStrategy.ContainerClient, "ContainerClient should be the primary container."); |
| 137 | + |
| 138 | + // 2. The BlobTriggerExecutor should use a BlobReceiptManager with the primary client |
| 139 | + var receiptManagerField = typeof(BlobTriggerExecutor).GetField("_receiptManager", BindingFlags.NonPublic | BindingFlags.Instance); |
| 140 | + var receiptManager = receiptManagerField.GetValue(testStrategy.Executor); |
| 141 | + var blobContainerClientField = typeof(BlobReceiptManager).GetField("_blobContainerClient", BindingFlags.NonPublic | BindingFlags.Instance); |
| 142 | + var resultPrimaryClient = blobContainerClientField.GetValue(receiptManager); |
| 143 | + |
| 144 | + // The BlobReceiptManager should use the hostNamesContainerClient (from the primary client) |
| 145 | + Assert.AreEqual(hostNamesContainerClient.Object, resultPrimaryClient, "BlobReceiptManager should use the primary container client."); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments