Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
benvillalobos committed Oct 12, 2022
1 parent cd9724b commit cbd92a0
Showing 1 changed file with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,91 @@ public void ParseContainerProperties_EndToEnd()
Assert.IsTrue(cni.Execute());
newProjectDir.Delete(true);
}

/// <summary>
/// Creates a console app that outputs the environment variable added to the image.
/// </summary>
[TestMethod]
public void Tasks_EndToEnd_With_EnvironmentVariable_Validation()
{
DirectoryInfo newProjectDir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), nameof(Tasks_EndToEnd_With_EnvironmentVariable_Validation)));

if (newProjectDir.Exists)
{
newProjectDir.Delete(recursive: true);
}

newProjectDir.Create();

ProcessStartInfo info = new ProcessStartInfo
{
WorkingDirectory = newProjectDir.FullName,
FileName = "dotnet",
Arguments = "new console -f net7.0",
RedirectStandardOutput = true,
RedirectStandardError = true
};

Process dotnetNew = Process.Start(info);
Assert.IsNotNull(dotnetNew);
dotnetNew.WaitForExit();
Assert.AreEqual(0, dotnetNew.ExitCode, dotnetNew.StandardOutput.ReadToEnd());

File.WriteAllText(Path.Combine(newProjectDir.FullName, "Program.cs"), $"Console.Write(Environment.GetEnvironmentVariable(\"GoodEnvVar\"));");

info.Arguments = "build --configuration release /p:runtimeidentifier=linux-x64";

Process dotnetBuildRelease = Process.Start(info);
Assert.IsNotNull(dotnetBuildRelease);
dotnetBuildRelease.WaitForExit();
dotnetBuildRelease.Kill();
Assert.AreEqual(0, dotnetBuildRelease.ExitCode);

ParseContainerProperties pcp = new ParseContainerProperties();
pcp.FullyQualifiedBaseImageName = "https://mcr.microsoft.com/dotnet/runtime:6.0";
pcp.ContainerRegistry = "docker://";
pcp.ContainerImageName = "dotnet/envvarvalidation";
pcp.ContainerImageTag = "latest";

Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("Value", "Foo");

pcp.ContainerEnvironmentVariables = new[] { new TaskItem("[email protected]", dict), new TaskItem("GoodEnvVar", dict) };

Assert.IsTrue(pcp.Execute());
Assert.AreEqual("https://mcr.microsoft.com", pcp.ParsedContainerRegistry);
Assert.AreEqual("dotnet/runtime", pcp.ParsedContainerImage);
Assert.AreEqual("6.0", pcp.ParsedContainerTag);
Assert.AreEqual(1, pcp.NewContainerEnvironmentVariables.Length);
Assert.AreEqual("Foo", pcp.NewContainerEnvironmentVariables[0].GetMetadata("Value"));

Assert.AreEqual("dotnet/envvarvalidation", pcp.NewContainerImageName);
Assert.AreEqual("latest", pcp.NewContainerTags[0]);

CreateNewImage cni = new CreateNewImage();
cni.BaseRegistry = pcp.ParsedContainerRegistry;
cni.BaseImageName = pcp.ParsedContainerImage;
cni.BaseImageTag = pcp.ParsedContainerTag;
cni.ImageName = pcp.NewContainerImageName;
cni.OutputRegistry = pcp.NewContainerRegistry;
cni.PublishDirectory = Path.Combine(newProjectDir.FullName, "bin", "release", "net7.0", "linux-x64");
cni.WorkingDirectory = "/app";
cni.Entrypoint = new TaskItem[] { new("/app/Tasks_EndToEnd_With_EnvironmentVariable_Validation") };
cni.ImageTags = pcp.NewContainerTags;
cni.ContainerEnvironmentVariables = pcp.NewContainerEnvironmentVariables;

Assert.IsTrue(cni.Execute());

ProcessStartInfo runInfo = new("docker", $"run --rm {pcp.NewContainerImageName}:latest")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
};

Process run = Process.Start(runInfo);
Assert.IsNotNull(run);
run.WaitForExit();
Assert.AreEqual(0, run.ExitCode);
Assert.AreEqual("Foo", run.StandardOutput.ReadToEnd());
}
}

0 comments on commit cbd92a0

Please sign in to comment.