|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using System.IO; |
| 4 | +using System.Net; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Xunit; |
| 8 | +using Xunit.Abstractions; |
| 9 | + |
| 10 | +namespace Microsoft.DotNet.Docker.Tests; |
| 11 | + |
| 12 | +public class YarpBasicScenario : ITestScenario |
| 13 | +{ |
| 14 | + private readonly DockerHelper _dockerHelper; |
| 15 | + |
| 16 | + private readonly ITestOutputHelper _outputHelper; |
| 17 | + |
| 18 | + private readonly ProductImageData _imageData; |
| 19 | + |
| 20 | + private readonly string _imageTag; |
| 21 | + |
| 22 | + private readonly int _webPort; |
| 23 | + |
| 24 | + public YarpBasicScenario( |
| 25 | + int webPort, |
| 26 | + ProductImageData imageData, |
| 27 | + DockerHelper dockerHelper, |
| 28 | + ITestOutputHelper outputHelper) |
| 29 | + { |
| 30 | + _dockerHelper = dockerHelper; |
| 31 | + _imageData = imageData; |
| 32 | + _outputHelper = outputHelper; |
| 33 | + _webPort = webPort; |
| 34 | + |
| 35 | + _imageTag = _imageData.GetImage(DotNetImageRepo.Yarp, _dockerHelper); |
| 36 | + } |
| 37 | + |
| 38 | + public async Task ExecuteAsync() |
| 39 | + { |
| 40 | + string containerName = _imageData.GetIdentifier(nameof(YarpBasicScenario)); |
| 41 | + using TempFileContext configFile = FileHelper.UseTempFile(); |
| 42 | + string sampleContainer = $"{containerName}_aspnetapp"; |
| 43 | + |
| 44 | + try |
| 45 | + { |
| 46 | + // Deploy the aspnet sample app |
| 47 | + _dockerHelper.Run( |
| 48 | + image: "mcr.microsoft.com/dotnet/samples:aspnetapp", |
| 49 | + name: sampleContainer, |
| 50 | + detach: true, |
| 51 | + optionalRunArgs: "", |
| 52 | + skipAutoCleanup: true); |
| 53 | + |
| 54 | + File.WriteAllText(configFile.Path, ConfigFileContent); |
| 55 | + |
| 56 | + _dockerHelper.Run( |
| 57 | + image: _imageTag, |
| 58 | + name: containerName, |
| 59 | + detach: true, |
| 60 | + optionalRunArgs: $"-p {_webPort} -v {configFile.Path}:/etc/yarp.config --link {sampleContainer}:aspnetapp1", |
| 61 | + skipAutoCleanup: true); |
| 62 | + |
| 63 | + // base uri should return 404 |
| 64 | + HttpResponseMessage notFoundResponse = await WebScenario.GetHttpResponseFromContainerAsync( |
| 65 | + containerName, |
| 66 | + _dockerHelper, |
| 67 | + _outputHelper, |
| 68 | + _webPort, |
| 69 | + pathAndQuery: "/"); |
| 70 | + |
| 71 | + Assert.Equal(HttpStatusCode.NotFound, notFoundResponse.StatusCode); |
| 72 | + |
| 73 | + // /aspnetapp should return a valid response |
| 74 | + HttpResponseMessage okResponse = await WebScenario.GetHttpResponseFromContainerAsync( |
| 75 | + containerName, |
| 76 | + _dockerHelper, |
| 77 | + _outputHelper, |
| 78 | + _webPort, |
| 79 | + pathAndQuery: "/aspnetapp"); |
| 80 | + |
| 81 | + Assert.Equal(HttpStatusCode.OK, okResponse.StatusCode); |
| 82 | + } |
| 83 | + finally |
| 84 | + { |
| 85 | + _dockerHelper.DeleteContainer(containerName); |
| 86 | + _dockerHelper.DeleteContainer(sampleContainer); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private const string ConfigFileContent = """ |
| 91 | + { |
| 92 | + "Logging": { |
| 93 | + "LogLevel": { |
| 94 | + "Default": "Information", |
| 95 | + "Microsoft": "Information", |
| 96 | + "Microsoft.Hosting.Lifetime": "Information" |
| 97 | + } |
| 98 | + }, |
| 99 | + "AllowedHosts": "*", |
| 100 | + "ReverseProxy": { |
| 101 | + "Routes": { |
| 102 | + "route1": { |
| 103 | + "ClusterId": "cluster1", |
| 104 | + "Match": { |
| 105 | + "Path": "/aspnetapp/{**catch-all}" |
| 106 | + }, |
| 107 | + "Transforms": [ |
| 108 | + { "PathRemovePrefix": "/aspnetapp" } |
| 109 | + ] |
| 110 | + } |
| 111 | + }, |
| 112 | + "Clusters": { |
| 113 | + "cluster1": { |
| 114 | + "Destinations": { |
| 115 | + "destination1": { |
| 116 | + "Address": "http://aspnetapp1:8080" |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + """; |
| 124 | +} |
0 commit comments