Ejemplo n.º 1
0
        public async Task VerifyAspnetSample(SampleImageData imageData)
        {
            if (imageData.OS == OS.Bionic && imageData.DockerfileSuffix != "ubuntu-x64")
            {
                return;
            }

            await VerifySampleAsync(imageData, SampleImageType.Aspnetapp, async (image, containerName) =>
            {
                try
                {
                    DockerHelper.Run(
                        image: image,
                        name: containerName,
                        detach: true,
                        optionalRunArgs: "-p 80");

                    if (!Config.IsHttpVerificationDisabled)
                    {
                        await ImageScenarioVerifier.VerifyHttpResponseFromContainerAsync(containerName, DockerHelper, OutputHelper);
                    }

                    ValidateEnvironmentVariables(imageData, image, SampleImageType.Aspnetapp);
                }
                finally
                {
                    DockerHelper.DeleteContainer(containerName);
                }
            });
        }
Ejemplo n.º 2
0
        private async Task VerifySampleAsync(
            SampleImageData imageData,
            SampleImageType sampleImageType,
            Func <string, string, Task> verifyImageAsync)
        {
            string image     = imageData.GetImage(sampleImageType, DockerHelper);
            string imageType = Enum.GetName(typeof(SampleImageType), sampleImageType).ToLowerInvariant();

            try
            {
                if (!imageData.IsPublished)
                {
                    string sampleFolder   = Path.Combine(s_samplesPath, imageType);
                    string dockerfilePath = $"{sampleFolder}/Dockerfile.{imageData.DockerfileSuffix}";

                    DockerHelper.Build(image, dockerfilePath, contextDir: sampleFolder, pull: Config.PullImages);
                }

                string containerName = imageData.GetIdentifier($"sample-{imageType}");
                await verifyImageAsync(image, containerName);
            }
            finally
            {
                if (!imageData.IsPublished)
                {
                    DockerHelper.DeleteImage(image);
                }
            }
        }
Ejemplo n.º 3
0
 private void GetNames(SampleImageData imageData, out string imageName, out string containerName)
 {
     // Need to allow pulling of the sample image since these are not built in the same pipeline
     // as the other images; otherwise, these tests will fail due to lack of sample image.
     imageName     = imageData.GetImage(SampleImageType.Aspnetapp, DockerHelper, allowPull: true);
     containerName = imageData.GetIdentifier("monitortest-sample");
 }
Ejemplo n.º 4
0
        public Task VerifyListenMode(MonitorImageData imageData, SampleImageData sampleData)
        {
            return(VerifyScenarioAsync(
                       monitorImageData: imageData,
                       sampleImageData: sampleData,
                       shareTmpVolume: false,
                       listenDiagPortVolume: true,
                       noAuthentication: true,
                       async(monitorName, sampleName) =>
            {
                if (!Config.IsHttpVerificationDisabled)
                {
                    using HttpResponseMessage responseMessage =
                              await ImageScenarioVerifier.GetHttpResponseFromContainerAsync(
                                  monitorName,
                                  DockerHelper,
                                  OutputHelper,
                                  DefaultArtifactsPort,
                                  UrlPath_Processes);

                    JsonElement rootElement = GetContentAsJsonElement(responseMessage);

                    // Verify returns an array with one element (the sample container process)
                    Assert.Equal(JsonValueKind.Array, rootElement.ValueKind);
                    Assert.Equal(1, rootElement.GetArrayLength());
                }
            }));
        }
        public void VerifyDotnetSample(SampleImageData imageData)
        {
            string image         = imageData.GetImage(SampleImageType.Dotnetapp, DockerHelper);
            string containerName = imageData.GetIdentifier("sample-dotnetapp");
            string output        = DockerHelper.Run(image, containerName);

            Assert.StartsWith("Hello from .NET Core!", output);
        }
Ejemplo n.º 6
0
 public async Task VerifyDotnetSample(SampleImageData imageData)
 {
     await VerifySampleAsync(imageData, SampleImageType.Dotnetapp, (image, containerName) =>
     {
         string output = DockerHelper.Run(image, containerName);
         Assert.StartsWith("Hello from .NET Core!", output);
         return(Task.CompletedTask);
     });
 }
Ejemplo n.º 7
0
        public void VerifyComplexAppSample()
        {
            string appTag            = SampleImageData.GetImageName("complexapp-local-app");
            string testTag           = SampleImageData.GetImageName("complexapp-local-test");
            string sampleFolder      = Path.Combine(s_samplesPath, "complexapp");
            string dockerfilePath    = $"{sampleFolder}/Dockerfile";
            string testContainerName = ImageData.GenerateContainerName("sample-complex-test");
            string tempDir           = null;

            try
            {
                // Test that the app works
                DockerHelper.Build(appTag, dockerfilePath, contextDir: sampleFolder, pull: Config.PullImages);
                string containerName = ImageData.GenerateContainerName("sample-complex");
                string output        = DockerHelper.Run(appTag, containerName);
                Assert.StartsWith("string: The quick brown fox jumps over the lazy dog", output);

                if (!DockerHelper.IsLinuxContainerModeEnabled &&
                    DockerHelper.DockerArchitecture.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
                {
                    // Skipping run app tests due to a .NET issue: https://github.com/dotnet/runtime/issues/2082
                    return;
                }

                // Run the app's tests
                DockerHelper.Build(testTag, dockerfilePath, target: "test", contextDir: sampleFolder);
                DockerHelper.Run(testTag, testContainerName, skipAutoCleanup: true);

                // Copy the test log from the container to the host
                tempDir = Directory.CreateDirectory(
                    Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())).FullName;
                DockerHelper.Copy($"{testContainerName}:/source/tests/TestResults", tempDir);
                string testLogFile = new DirectoryInfo($"{tempDir}/TestResults").GetFiles("*.trx").First().FullName;

                // Open the test log file and verify the tests passed
                XDocument doc     = XDocument.Load(testLogFile);
                XElement  summary = doc.Root.Element(XName.Get("ResultSummary", doc.Root.Name.NamespaceName));
                Assert.Equal("Completed", summary.Attribute("outcome").Value);
                XElement counters = summary.Element(XName.Get("Counters", doc.Root.Name.NamespaceName));
                Assert.Equal("2", counters.Attribute("total").Value);
                Assert.Equal("2", counters.Attribute("passed").Value);
            }
            finally
            {
                if (tempDir != null)
                {
                    Directory.Delete(tempDir, true);
                }

                DockerHelper.DeleteContainer(testContainerName);
                DockerHelper.DeleteImage(testTag);
                DockerHelper.DeleteImage(appTag);
            }
        }
Ejemplo n.º 8
0
        public async Task VerifyDotnetSample(SampleImageData imageData)
        {
            await VerifySampleAsync(imageData, SampleImageType.Dotnetapp, (image, containerName) =>
            {
                string output = DockerHelper.Run(image, containerName);
                Assert.True(output.Contains("42") || output.StartsWith("Hello"));

                ValidateEnvironmentVariables(imageData, image, SampleImageType.Dotnetapp);

                return(Task.CompletedTask);
            });
        }
Ejemplo n.º 9
0
        private void ValidateEnvironmentVariables(SampleImageData imageData, string image, SampleImageType imageType)
        {
            List <EnvironmentVariableInfo> variables = new List <EnvironmentVariableInfo>();

            variables.AddRange(ProductImageTests.GetCommonEnvironmentVariables());

            if (imageType == SampleImageType.Aspnetapp)
            {
                variables.Add(new EnvironmentVariableInfo("ASPNETCORE_URLS", "http://+:80"));
            }

            EnvironmentVariableInfo.Validate(
                variables,
                image,
                imageData,
                DockerHelper);
        }
        public async Task VerifyAspnetSample(SampleImageData imageData)
        {
            string image         = imageData.GetImage(SampleImageType.Aspnetapp, DockerHelper);
            string containerName = imageData.GetIdentifier("sample-aspnetapp");

            try
            {
                DockerHelper.Run(
                    image: image,
                    name: containerName,
                    detach: true,
                    optionalRunArgs: "-p 80");

                if (!Config.IsHttpVerificationDisabled)
                {
                    await ImageScenarioVerifier.VerifyHttpResponseFromContainerAsync(containerName, DockerHelper, OutputHelper);
                }
            }
            finally
            {
                DockerHelper.DeleteContainer(containerName);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Runs a single instance of each of the dotnet-monitor and samples images.
        /// </summary>
        /// <param name="monitorImageData">The image data of the dotnet-monitor image.</param>
        /// <param name="shareTmpVolume">Set to true to mount the /tmp directory in both containers.</param>
        /// <param name="listenDiagPortVolume">
        /// Set to true to have the monitor container listen with a diagnostic port listener
        /// for diagnostic connections from the samples container.
        /// </param>
        /// <param name="noAuthentication">Set to true to disable dotnet-monitor authenication.</param>
        /// <param name="verifyContainerAsync">Callback to test some aspect of the containers.</param>
        /// <param name="monitorRunArgsCallback">Allows for modifying the "docker run" args of the dotnet-monitor container.</param>
        /// <param name="sampleRunArgsCallback">Allows for modifying the "docker run" args of the samples container.</param>
        private async Task VerifyScenarioAsync(
            MonitorImageData monitorImageData,
            SampleImageData sampleImageData,
            bool shareTmpVolume,
            bool listenDiagPortVolume,
            bool noAuthentication,
            Func <string, string, Task> verifyContainerAsync,
            Action <DockerRunArgsBuilder> monitorRunArgsCallback = null,
            Action <DockerRunArgsBuilder> sampleRunArgsCallback  = null)
        {
            GetNames(monitorImageData, out string monitorImageName, out string monitorContainerName);
            GetNames(sampleImageData, out string sampleImageName, out string sampleContainerName);

            DockerRunArgsBuilder monitorArgsBuilder = DockerRunArgsBuilder.Create()
                                                      .MonitorUrl(DefaultArtifactsPort);

            DockerRunArgsBuilder sampleArgsBuilder = DockerRunArgsBuilder.Create()
                                                     .ExposePort(DefaultHttpPort);

            string diagPortVolumeName = null;
            string tmpVolumeName      = null;

            try
            {
                // Create a volume for the two containers to share the /tmp directory.
                if (shareTmpVolume)
                {
                    tmpVolumeName = DockerHelper.CreateVolume(UniqueName("tmpvol"));

                    monitorArgsBuilder.VolumeMount(tmpVolumeName, Directory_Tmp);

                    sampleArgsBuilder.VolumeMount(tmpVolumeName, Directory_Tmp);
                }

                // Create a volume so that the dotnet-monitor container can provide a
                // diagnostic listening port to the samples container so that the samples
                // process can connect to the dotnet-monitor process.
                if (listenDiagPortVolume)
                {
                    diagPortVolumeName = DockerHelper.CreateVolume(UniqueName("diagportvol"));

                    monitorArgsBuilder.VolumeMount(diagPortVolumeName, Directory_Diag);
                    monitorArgsBuilder.MonitorListen(File_DiagPort);

                    sampleArgsBuilder.VolumeMount(diagPortVolumeName, Directory_Diag);
                    sampleArgsBuilder.RuntimeSuspend(File_DiagPort);
                }

                // Allow modification of the "docker run" args of the monitor container
                if (null != monitorRunArgsCallback)
                {
                    monitorRunArgsCallback(monitorArgsBuilder);
                }

                // Allow modification of the "docker run" args of the samples container
                if (null != sampleRunArgsCallback)
                {
                    sampleRunArgsCallback(sampleArgsBuilder);
                }

                // Run the sample container
                DockerHelper.Run(
                    image: sampleImageName,
                    name: sampleContainerName,
                    detach: true,
                    optionalRunArgs: sampleArgsBuilder.Build());

                // Run the dotnet-monitor container
                DockerHelper.Run(
                    image: monitorImageName,
                    name: monitorContainerName,
                    command: GetMonitorAdditionalArgs(noAuthentication),
                    detach: true,
                    optionalRunArgs: monitorArgsBuilder.Build());

                await verifyContainerAsync(
                    monitorContainerName,
                    sampleContainerName);
            }
            finally
            {
                DockerHelper.DeleteContainer(monitorContainerName);

                DockerHelper.DeleteContainer(sampleContainerName);

                if (!string.IsNullOrEmpty(diagPortVolumeName))
                {
                    DockerHelper.DeleteVolume(diagPortVolumeName);
                }

                if (!string.IsNullOrEmpty(tmpVolumeName))
                {
                    DockerHelper.DeleteVolume(tmpVolumeName);
                }
            }
        }