Esempio n. 1
0
 public async Task ThrowsExceptionForInvalidUrls(string url)
 {
     var downloader = new PackageDownloadHandler(_httpClientFactory, _managedIdentityTokenProvider.Object,
                                                 _bashCmdHandlerMock.Object, _logger, _metricsLogger);
     var runFromPackageContext = new RunFromPackageContext(EnvironmentSettingNames.AzureWebsiteRunFromPackage, url, null, true);
     await Assert.ThrowsAsync <InvalidOperationException>(async() => await downloader.Download(runFromPackageContext));
 }
Esempio n. 2
0
        public async Task DownloadsZipsUsingHttpClient(bool isWarmupRequest)
        {
            FileUtility.Instance = GetFileSystem().Object;
            var       url      = $"http://url/{ZipFileName}";
            const int fileSize = PackageDownloadHandler.AriaDownloadThreshold - 1;

            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.IsAny <HttpRequestMessage>(),
                                                                        ItExpr.IsAny <CancellationToken>()).ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new ReadOnlyMemoryContent(ReadOnlyMemory <byte> .Empty)
            });

            _httpClientFactory = TestHelpers.CreateHttpClientFactory(handlerMock.Object);

            var packageDownloadHandler = new PackageDownloadHandler(_httpClientFactory, _managedIdentityTokenProvider.Object,
                                                                    _bashCmdHandlerMock.Object, _logger, _metricsLogger);

            var runFromPackageContext = new RunFromPackageContext(EnvironmentSettingNames.AzureWebsiteRunFromPackage, url, fileSize, isWarmupRequest);
            var filePath = await packageDownloadHandler.Download(runFromPackageContext);;

            Assert.Equal(ZipFileName, Path.GetFileName(filePath), StringComparer.Ordinal);

            handlerMock.Protected().Verify <Task <HttpResponseMessage> >("SendAsync", Times.Once(),
                                                                         ItExpr.Is <HttpRequestMessage>(r => IsZipDownloadRequest(r, url)),
                                                                         ItExpr.IsAny <CancellationToken>());
        }
Esempio n. 3
0
        public async Task DownloadsLargeZipsUsingAria2c(bool isWarmupRequest)
        {
            var url = $"http://url/{ZipFileName}";

            FileUtility.Instance = GetFileSystem().Object;
            const int fileSize = PackageDownloadHandler.AriaDownloadThreshold + 1;

            var expectedMetricName = isWarmupRequest
                ? MetricEventNames.LinuxContainerSpecializationZipDownloadWarmup
                : MetricEventNames.LinuxContainerSpecializationZipDownload;

            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            if (isWarmupRequest)
            {
                handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                            ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Get) && MatchesTargetUri(s, url)),
                                                                            ItExpr.IsAny <CancellationToken>()).ReturnsAsync(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content    = new ReadOnlyMemoryContent(ReadOnlyMemory <byte> .Empty)
                });

                _httpClientFactory = TestHelpers.CreateHttpClientFactory(handlerMock.Object);
            }
            else
            {
                _bashCmdHandlerMock.Setup(b => b.RunBashCommand(It.Is <string>(s => s.StartsWith(PackageDownloadHandler.Aria2CExecutable)),
                                                                expectedMetricName)).Returns(("", "", 0));
            }

            var runFromPackageContext = new RunFromPackageContext(EnvironmentSettingNames.AzureWebsiteRunFromPackage, url, fileSize, isWarmupRequest);

            var packageDownloadHandler = new PackageDownloadHandler(_httpClientFactory, _managedIdentityTokenProvider.Object,
                                                                    _bashCmdHandlerMock.Object, _logger, _metricsLogger);

            var filePath = await packageDownloadHandler.Download(runFromPackageContext);

            Assert.Equal(ZipFileName, Path.GetFileName(filePath), StringComparer.Ordinal);

            if (isWarmupRequest)
            {
                handlerMock.Protected().Verify <Task <HttpResponseMessage> >("SendAsync", Times.Once(),
                                                                             ItExpr.IsAny <HttpRequestMessage>(),
                                                                             ItExpr.IsAny <CancellationToken>());
            }
            else
            {
                _bashCmdHandlerMock.Verify(b => b.RunBashCommand(It.Is <string>(s => s.StartsWith(PackageDownloadHandler.Aria2CExecutable)),
                                                                 expectedMetricName), Times.Once);
            }
        }
Esempio n. 4
0
        public async Task DownloadsFileUsingManagedIdentity()
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Head)),
                                                                        ItExpr.IsAny <CancellationToken>()).ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.Unauthorized,
                Content    = new ReadOnlyMemoryContent(ReadOnlyMemory <byte> .Empty)
            });


            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Get) && HasBearerToken(s) && MatchesTargetUri(s, UriWithNoSasToken)),
                                                                        ItExpr.IsAny <CancellationToken>()).ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new ReadOnlyMemoryContent(ReadOnlyMemory <byte> .Empty)
            });

            _httpClientFactory = TestHelpers.CreateHttpClientFactory(handlerMock.Object);

            _managedIdentityTokenProvider.Setup(p => p.GetManagedIdentityToken(UriWithNoSasToken)).Returns(Task.FromResult(BearerToken));

            var downloader = new PackageDownloadHandler(_httpClientFactory, _managedIdentityTokenProvider.Object,
                                                        _bashCmdHandlerMock.Object, _logger, _metricsLogger);

            var runFromPackageContext = new RunFromPackageContext(EnvironmentSettingNames.AzureWebsiteRunFromPackage, UriWithNoSasToken, 0, false);
            await downloader.Download(runFromPackageContext);

            handlerMock.Protected().Verify <Task <HttpResponseMessage> >("SendAsync", Times.Once(),
                                                                         ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Head)),
                                                                         ItExpr.IsAny <CancellationToken>());

            handlerMock.Protected().Verify <Task <HttpResponseMessage> >("SendAsync", Times.Once(),
                                                                         ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Get) && HasBearerToken(s) && MatchesTargetUri(s, UriWithNoSasToken)),
                                                                         ItExpr.IsAny <CancellationToken>());

            _managedIdentityTokenProvider.Verify(p => p.GetManagedIdentityToken(UriWithNoSasToken), Times.Once);
        }
Esempio n. 5
0
        public async Task DownloadsPackage(bool isWarmupRequest, string url, long?fileSize, bool expectedUsesAriaDownload, bool expectedDownloadUsingManagedIdentityToken)
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                        ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Head)),
                                                                        ItExpr.IsAny <CancellationToken>()).ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.Unauthorized,
                Content    = new ReadOnlyMemoryContent(ReadOnlyMemory <byte> .Empty)
            });

            if (expectedDownloadUsingManagedIdentityToken)
            {
                handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                            ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Get) && HasBearerToken(s) && MatchesTargetUri(s, url)),
                                                                            ItExpr.IsAny <CancellationToken>()).ReturnsAsync(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content    = new ReadOnlyMemoryContent(ReadOnlyMemory <byte> .Empty)
                });

                _managedIdentityTokenProvider.Setup(p => p.GetManagedIdentityToken(url)).Returns(Task.FromResult(BearerToken));
            }
            else
            {
                if (expectedUsesAriaDownload)
                {
                    _bashCmdHandlerMock.Setup(b =>
                                              b.RunBashCommand(
                                                  It.Is <string>(s =>
                                                                 s.StartsWith(PackageDownloadHandler.Aria2CExecutable) && s.Contains(url)),
                                                  MetricEventNames.LinuxContainerSpecializationZipDownload)).Returns(("", "", 0));
                }
                else
                {
                    handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                                ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Get) && !HasBearerToken(s) && MatchesTargetUri(s, url)),
                                                                                ItExpr.IsAny <CancellationToken>()).ReturnsAsync(new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.OK,
                        Content    = new ReadOnlyMemoryContent(ReadOnlyMemory <byte> .Empty)
                    });
                }
            }

            _httpClientFactory = TestHelpers.CreateHttpClientFactory(handlerMock.Object);

            var downloader = new PackageDownloadHandler(_httpClientFactory, _managedIdentityTokenProvider.Object,
                                                        _bashCmdHandlerMock.Object, _logger, _metricsLogger);

            var runFromPackageContext = new RunFromPackageContext(EnvironmentSettingNames.AzureWebsiteRunFromPackage, url, fileSize, isWarmupRequest);
            await downloader.Download(runFromPackageContext);

            if (expectedDownloadUsingManagedIdentityToken)
            {
                handlerMock.Protected().Verify <Task <HttpResponseMessage> >("SendAsync", Times.Once(),
                                                                             ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Head)),
                                                                             ItExpr.IsAny <CancellationToken>());

                handlerMock.Protected().Verify <Task <HttpResponseMessage> >("SendAsync", Times.Once(),
                                                                             ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Get) && HasBearerToken(s) && MatchesTargetUri(s, url)),
                                                                             ItExpr.IsAny <CancellationToken>());

                _managedIdentityTokenProvider.Verify(p => p.GetManagedIdentityToken(url), Times.Once);
            }
            else
            {
                handlerMock.Protected().Verify <Task <HttpResponseMessage> >("SendAsync", Times.Never(),
                                                                             ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Head)),
                                                                             ItExpr.IsAny <CancellationToken>());

                if (expectedUsesAriaDownload)
                {
                    _bashCmdHandlerMock.Verify(b =>
                                               b.RunBashCommand(
                                                   It.Is <string>(s =>
                                                                  s.StartsWith(PackageDownloadHandler.Aria2CExecutable) && s.Contains(url)),
                                                   MetricEventNames.LinuxContainerSpecializationZipDownload), Times.Once);
                }
                else
                {
                    handlerMock.Protected().Verify <Task <HttpResponseMessage> >("SendAsync", Times.Never(),
                                                                                 ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Get) && HasBearerToken(s) && MatchesTargetUri(s, url)),
                                                                                 ItExpr.IsAny <CancellationToken>());

                    _bashCmdHandlerMock.Verify(b =>
                                               b.RunBashCommand(It.IsAny <string>(), It.IsAny <string>()), Times.Never);
                }

                _managedIdentityTokenProvider.Verify(p => p.GetManagedIdentityToken(It.IsAny <string>()), Times.Never);
            }
        }
Esempio n. 6
0
        public async Task FetchesManagedIdentityToken(string url, bool isPublicUrl, bool expectedFetchesManagedIdentityToken)
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            // urls with sas tokens are always publicly accessible
            if (url == UriWithSasToken)
            {
                isPublicUrl = true;
            }

            var statusCode = isPublicUrl ? HttpStatusCode.OK : HttpStatusCode.Unauthorized;

            handlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync",
                                                 ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Head)),
                                                 ItExpr.IsAny <CancellationToken>()).ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = statusCode, Content = new ReadOnlyMemoryContent(ReadOnlyMemory <byte> .Empty)
            });

            if (expectedFetchesManagedIdentityToken)
            {
                handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                            ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Get) && HasBearerToken(s) && MatchesTargetUri(s, url)),
                                                                            ItExpr.IsAny <CancellationToken>()).ReturnsAsync(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content    = new ReadOnlyMemoryContent(ReadOnlyMemory <byte> .Empty)
                });
            }
            else
            {
                handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                            ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Get) && !HasBearerToken(s) && MatchesTargetUri(s, url)),
                                                                            ItExpr.IsAny <CancellationToken>()).ReturnsAsync(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content    = new ReadOnlyMemoryContent(ReadOnlyMemory <byte> .Empty)
                });
            }

            _httpClientFactory = TestHelpers.CreateHttpClientFactory(handlerMock.Object);

            if (expectedFetchesManagedIdentityToken)
            {
                _managedIdentityTokenProvider.Setup(p => p.GetManagedIdentityToken(url))
                .Returns(Task.FromResult(BearerToken));
            }

            var downloader = new PackageDownloadHandler(_httpClientFactory, _managedIdentityTokenProvider.Object,
                                                        _bashCmdHandlerMock.Object, _logger, _metricsLogger);

            var runFromPackageContext = new RunFromPackageContext(EnvironmentSettingNames.AzureWebsiteRunFromPackage, url, 0, false);
            await downloader.Download(runFromPackageContext);

            if (expectedFetchesManagedIdentityToken)
            {
                _managedIdentityTokenProvider.Verify(p => p.GetManagedIdentityToken(url), Times.Once);
                handlerMock.Protected().Verify <Task <HttpResponseMessage> >("SendAsync", Times.Once(),
                                                                             ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Get) && HasBearerToken(s) && MatchesTargetUri(s, url)),
                                                                             ItExpr.IsAny <CancellationToken>());
            }
            else
            {
                _managedIdentityTokenProvider.Verify(p => p.GetManagedIdentityToken(It.IsAny <string>()), Times.Never);
                handlerMock.Protected().Verify <Task <HttpResponseMessage> >("SendAsync", Times.Once(),
                                                                             ItExpr.Is <HttpRequestMessage>(s => MatchesVerb(s, HttpMethod.Get) && !HasBearerToken(s) && MatchesTargetUri(s, url)),
                                                                             ItExpr.IsAny <CancellationToken>());
            }
        }