diff --git a/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/.dockerignore b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/.dockerignore new file mode 100644 index 0000000000..3729ff0cd1 --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/.dockerignore @@ -0,0 +1,25 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/Controllers/WeatherForecastController.cs b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000000..46a4155472 --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/Controllers/WeatherForecastController.cs @@ -0,0 +1,40 @@ +// Copyright 2020 New Relic, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +namespace ContainerizedAspNetCoreApp.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git a/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/Dockerfile b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/Dockerfile new file mode 100644 index 0000000000..208b6ba0c4 --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/Dockerfile @@ -0,0 +1,25 @@ +#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. + +FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base +ARG RESOURCE_REAPER_SESSION_ID="00000000-0000-0000-0000-000000000000" +WORKDIR /app +EXPOSE 80 + +FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build +ARG RESOURCE_REAPER_SESSION_ID="00000000-0000-0000-0000-000000000000" +WORKDIR /src +COPY ["ContainerApplications/SmokeTestApp/SmokeTestApp.csproj", "SmokeTestApp/"] +RUN dotnet restore "SmokeTestApp/SmokeTestApp.csproj" +COPY . . +WORKDIR "/src/SmokeTestApp" +RUN dotnet build "SmokeTestApp.csproj" -c Release -o /app/build + +FROM build AS publish +ARG RESOURCE_REAPER_SESSION_ID="00000000-0000-0000-0000-000000000000" +RUN dotnet publish "SmokeTestApp.csproj" -c Release -o /app/publish /p:UseAppHost=false + +FROM base AS final +ARG RESOURCE_REAPER_SESSION_ID="00000000-0000-0000-0000-000000000000" +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "SmokeTestApp.dll"] diff --git a/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/Program.cs b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/Program.cs new file mode 100644 index 0000000000..42c8aca9fe --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/Program.cs @@ -0,0 +1,31 @@ +// Copyright 2020 New Relic, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace ContainerizedAspNetCoreApp +{ + public class Program + { + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + // Add services to the container. + + builder.Services.AddControllers(); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + + app.UseAuthorization(); + + + app.MapControllers(); + + app.Run(); + } + } +} diff --git a/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/Properties/launchSettings.json b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/Properties/launchSettings.json new file mode 100644 index 0000000000..78788e5606 --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/Properties/launchSettings.json @@ -0,0 +1,37 @@ +{ + "profiles": { + "ContainerizedAspNetCoreApp": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "http://localhost:5075" + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Docker": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/weatherforecast", + "publishAllPorts": true + } + }, + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:7298", + "sslPort": 0 + } + } +} \ No newline at end of file diff --git a/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/SmokeTestApp.csproj b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/SmokeTestApp.csproj new file mode 100644 index 0000000000..f56a664377 --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/SmokeTestApp.csproj @@ -0,0 +1,13 @@ + + + + net7.0 + Linux + . + + + + + + + diff --git a/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/WeatherForecast.cs b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/WeatherForecast.cs new file mode 100644 index 0000000000..f230d27f3d --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/WeatherForecast.cs @@ -0,0 +1,18 @@ +// Copyright 2020 New Relic, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +using System; + +namespace ContainerizedAspNetCoreApp +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git a/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/appsettings.Development.json b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/appsettings.Development.json new file mode 100644 index 0000000000..0c208ae918 --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/appsettings.json b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/appsettings.json new file mode 100644 index 0000000000..10f68b8c8b --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerApplications/SmokeTestApp/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/tests/Agent/IntegrationTests/ContainerIntegrationTests.sln b/tests/Agent/IntegrationTests/ContainerIntegrationTests.sln new file mode 100644 index 0000000000..e8976807d8 --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerIntegrationTests.sln @@ -0,0 +1,54 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.33516.290 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContainerIntegrationTests", "ContainerIntegrationTests\ContainerIntegrationTests.csproj", "{3B33858F-A8CF-44FE-B1A6-9308254C0019}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntegrationTestHelpers", "IntegrationTestHelpers\IntegrationTestHelpers.csproj", "{1A7589F4-614C-47BA-8163-E42A6863BC13}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntegrationTestHelpers.Test", "IntegrationTestHelpers.Test\IntegrationTestHelpers.Test.csproj", "{992B2E91-6439-4AB8-9C4C-E8F7FC68AE4D}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "Shared\Shared.csproj", "{0EECB18A-4350-4D17-AB0D-F7647B4E3B58}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmokeTestApp", "ContainerApplications\SmokeTestApp\SmokeTestApp.csproj", "{FBA07795-8066-4641-88E5-05DD272D333A}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ContainerApplications", "ContainerApplications", "{84D70574-4AC7-4EA7-AE52-832C3531E082}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3B33858F-A8CF-44FE-B1A6-9308254C0019}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3B33858F-A8CF-44FE-B1A6-9308254C0019}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3B33858F-A8CF-44FE-B1A6-9308254C0019}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3B33858F-A8CF-44FE-B1A6-9308254C0019}.Release|Any CPU.Build.0 = Release|Any CPU + {1A7589F4-614C-47BA-8163-E42A6863BC13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1A7589F4-614C-47BA-8163-E42A6863BC13}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1A7589F4-614C-47BA-8163-E42A6863BC13}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1A7589F4-614C-47BA-8163-E42A6863BC13}.Release|Any CPU.Build.0 = Release|Any CPU + {992B2E91-6439-4AB8-9C4C-E8F7FC68AE4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {992B2E91-6439-4AB8-9C4C-E8F7FC68AE4D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {992B2E91-6439-4AB8-9C4C-E8F7FC68AE4D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {992B2E91-6439-4AB8-9C4C-E8F7FC68AE4D}.Release|Any CPU.Build.0 = Release|Any CPU + {0EECB18A-4350-4D17-AB0D-F7647B4E3B58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0EECB18A-4350-4D17-AB0D-F7647B4E3B58}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0EECB18A-4350-4D17-AB0D-F7647B4E3B58}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0EECB18A-4350-4D17-AB0D-F7647B4E3B58}.Release|Any CPU.Build.0 = Release|Any CPU + {FBA07795-8066-4641-88E5-05DD272D333A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FBA07795-8066-4641-88E5-05DD272D333A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FBA07795-8066-4641-88E5-05DD272D333A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FBA07795-8066-4641-88E5-05DD272D333A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {FBA07795-8066-4641-88E5-05DD272D333A} = {84D70574-4AC7-4EA7-AE52-832C3531E082} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {BB230433-D05D-4A1F-951B-CC14F47BBF42} + EndGlobalSection +EndGlobal diff --git a/tests/Agent/IntegrationTests/ContainerIntegrationTests/Class1.cs b/tests/Agent/IntegrationTests/ContainerIntegrationTests/Class1.cs new file mode 100644 index 0000000000..df156ae9dc --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerIntegrationTests/Class1.cs @@ -0,0 +1,10 @@ +// Copyright 2020 New Relic, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +namespace ContainerIntegrationTests +{ + public class Class1 + { + + } +} diff --git a/tests/Agent/IntegrationTests/ContainerIntegrationTests/ContainerFixtures/ContainerApplication.cs b/tests/Agent/IntegrationTests/ContainerIntegrationTests/ContainerFixtures/ContainerApplication.cs new file mode 100644 index 0000000000..666b3283cb --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerIntegrationTests/ContainerFixtures/ContainerApplication.cs @@ -0,0 +1,52 @@ +// Copyright 2020 New Relic, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +using System.Collections.Generic; +using System.Diagnostics; +using System.Threading.Tasks; +using DotNet.Testcontainers.Builders; +using DotNet.Testcontainers.Containers; +using NewRelic.Agent.IntegrationTestHelpers.RemoteServiceFixtures; + +namespace NewRelic.Agent.ContainerIntegrationTests.ContainerFixtures +{ + public class ContainerApplication : RemoteService + { + public ContainerApplication(string applicationDirectoryName, string executableName) : + base(applicationDirectoryName, executableName, applicationType:ApplicationType.Container, createsPidFile:false, isCoreApp:true, publishApp:true) + { + } + + protected override void PublishWithDotnetExe(string framework) + { + // In a container application we do not need to perform a dotnet publish. Instead we need to copy the application + // so that it can be built into a docker container. + CopyApplicationDirectoryToRemote(); + } + + public override void Start(string commandLineArguments, Dictionary environmentVariables, bool captureStandardOutput = false, bool doProfile = true) + { + var futureImage = new ImageFromDockerfileBuilder() + .WithDockerfileDirectory(DestinationApplicationDirectoryPath) + .WithDockerfile("Dockerfile") + .WithBuildArgument("RESOURCE_REAPER_SESSION_ID", ResourceReaper.DefaultSessionId.ToString("D")) + .WithCleanUp(true) + .Build(); + + Task.Run(async () => await futureImage.CreateAsync().ConfigureAwait(false)).GetAwaiter().GetResult(); + + var containerBuilder = new ContainerBuilder() + .WithBindMount(DestinationNewRelicHomeDirectoryPath, "/newrelichome") + .WithCleanUp(true) + .WithImage(futureImage.FullName) + .WithPortBinding(Port, 80) + .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(80)); + + } + + protected override void WaitForAppServerToStartListening(Process process, bool captureStandardOutput) + { + base.WaitForAppServerToStartListening(process, captureStandardOutput); + } + } +} diff --git a/tests/Agent/IntegrationTests/ContainerIntegrationTests/ContainerFixtures/ContainerFixture.cs b/tests/Agent/IntegrationTests/ContainerIntegrationTests/ContainerFixtures/ContainerFixture.cs new file mode 100644 index 0000000000..f50f9bb5e9 --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerIntegrationTests/ContainerFixtures/ContainerFixture.cs @@ -0,0 +1,14 @@ +// Copyright 2020 New Relic, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +using NewRelic.Agent.IntegrationTestHelpers.RemoteServiceFixtures; + +namespace NewRelic.Agent.ContainerIntegrationTests.ContainerFixtures +{ + public class ContainerFixture : RemoteApplicationFixture + { + public ContainerFixture(ContainerApplication remoteApplication) : base(remoteApplication) + { + } + } +} diff --git a/tests/Agent/IntegrationTests/ContainerIntegrationTests/ContainerIntegrationTests.csproj b/tests/Agent/IntegrationTests/ContainerIntegrationTests/ContainerIntegrationTests.csproj new file mode 100644 index 0000000000..92642bb97a --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerIntegrationTests/ContainerIntegrationTests.csproj @@ -0,0 +1,28 @@ + + + + NewRelic.Agent.ContainerIntegrationTests + NewRelic.Agent.ContainerIntegrationTests + net6.0 + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/tests/Agent/IntegrationTests/ContainerIntegrationTests/Test.targets b/tests/Agent/IntegrationTests/ContainerIntegrationTests/Test.targets new file mode 100644 index 0000000000..609e08465e --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerIntegrationTests/Test.targets @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/Agent/IntegrationTests/ContainerIntegrationTests/xunit.runner.json b/tests/Agent/IntegrationTests/ContainerIntegrationTests/xunit.runner.json new file mode 100644 index 0000000000..86c7ea05b1 --- /dev/null +++ b/tests/Agent/IntegrationTests/ContainerIntegrationTests/xunit.runner.json @@ -0,0 +1,3 @@ +{ + "$schema": "https://xunit.net/schema/current/xunit.runner.schema.json" +} diff --git a/tests/Agent/IntegrationTests/IntegrationTestHelpers/RemoteServiceFixtures/RemoteApplication.cs b/tests/Agent/IntegrationTests/IntegrationTestHelpers/RemoteServiceFixtures/RemoteApplication.cs index 8d2c06487a..0774cfc125 100644 --- a/tests/Agent/IntegrationTests/IntegrationTestHelpers/RemoteServiceFixtures/RemoteApplication.cs +++ b/tests/Agent/IntegrationTests/IntegrationTestHelpers/RemoteServiceFixtures/RemoteApplication.cs @@ -17,7 +17,8 @@ public enum ApplicationType { Bounded, Unbounded, - Shared + Shared, + Container } public abstract class RemoteApplication : IDisposable @@ -247,6 +248,9 @@ protected RemoteApplication(ApplicationType applicationType, bool isCoreApp = fa case ApplicationType.Shared: applicationsFolder = "SharedApplications"; break; + case ApplicationType.Container: + applicationsFolder = "ContainerApplications"; + break; default: applicationsFolder = "Applications"; break; diff --git a/tests/Agent/IntegrationTests/IntegrationTestHelpers/RemoteServiceFixtures/RemoteService.cs b/tests/Agent/IntegrationTests/IntegrationTestHelpers/RemoteServiceFixtures/RemoteService.cs index 516168fa98..eeb11f0189 100644 --- a/tests/Agent/IntegrationTests/IntegrationTestHelpers/RemoteServiceFixtures/RemoteService.cs +++ b/tests/Agent/IntegrationTests/IntegrationTestHelpers/RemoteServiceFixtures/RemoteService.cs @@ -106,7 +106,7 @@ public override void CopyToRemote() ModifyNewRelicConfig(); } - private void PublishWithDotnetExe(string framework) + protected virtual void PublishWithDotnetExe(string framework) { var projectFile = Path.Combine(SourceApplicationsDirectoryPath, ApplicationDirectoryName, ApplicationDirectoryName + ".csproj");