Skip to content

Commit

Permalink
Add Quiet option for DockerRun
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Aug 20, 2024
1 parent 7b2ba9c commit f58d83b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 41 deletions.
60 changes: 33 additions & 27 deletions CSharpInteractive.HostApi/DockerRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,57 +35,60 @@ public partial record DockerRun(
IEnumerable<string> Args,
// Specifies the set of environment variables that apply to this process and its child processes.
IEnumerable<(string name, string value)> Vars,
// Additional docker options
// Additional docker options.
IEnumerable<string> Options,
// Expose a port or a range of ports
// Expose a port or a range of ports.
IEnumerable<string> ExposedPorts,
// Publish a container's port(s) to the host
// Publish a container's port(s) to the host.
IEnumerable<string> PublishedPorts,
// Adds bind mounts or volumes using the --mount flag
// Adds bind mounts or volumes using the --mount flag.
IEnumerable<string> Mounts,
// Bind mount a volume
// Bind mount a volume.
IEnumerable<(string from, string to)> Volumes,
// Adds entries to container hosts file.
IEnumerable<(string host, string ip)> Hosts,
// Overrides the tool executable path.
string ExecutablePath = "",
// Specifies the working directory for the tool to be started.
string WorkingDirectory = "",
// Number of CPUs
// Number of CPUs.
int? CPUs = default,
// Overwrite the default ENTRYPOINT of the image
// Overwrite the default ENTRYPOINT of the image.
string EntryPoint = "",
// Container host name
// Container host name.
string HostName = "",
// Kernel memory limit
// Kernel memory limit.
int? KernelMemory = default,
// Memory limit
// Memory limit.
int? Memory = default,
// Assign a name to the container
// Assign a name to the container.
string? Name = default,
// Connect a container to a network
// Connect a container to a network.
string Network = "",
// Set platform if server is multi-platform capable
// Set platform if server is multi-platform capable.
string Platform = "",
// Give extended privileges to this container
// Give extended privileges to this container.
bool? Privileged = default,
// Pull image before running (&quot;always&quot;|&quot;missing&quot;|&quot;never&quot;)
// Pull image before running (&quot;always&quot;|&quot;missing&quot;|&quot;never&quot;).
DockerPullType? Pull = default,
// Mount the container's root filesystem as read only
// Suppress the pull output.
bool Quiet = false,
// Mount the container's root filesystem as read only.
bool? ReadOnly = default,
// Automatically remove the container when it exits
// Automatically remove the container when it exits.
bool? AutoRemove = default,
// Username or UID (format: &lt;name|uid&gt;[:&lt;group|gid&gt;])
// Username or UID (format: &lt;name|uid&gt;[:&lt;group|gid&gt;]).
string User = "",
// Working directory inside the container
// Working directory inside the container.
string ContainerWorkingDirectory = "",
// A file with environment variables inside the container
// A file with environment variables inside the container.
string EnvFile = "",
// Specifies a short name for this operation.
string ShortName = "",
// Keep STDIN open even if not attached
// Keep STDIN open even if not attached.
bool Interactive = false,
// Allocate a pseudo-TTY
bool Tty = false
)
// Allocate a pseudo-TTY.
bool Tty = false,
// Specifies a short name for this operation.
string ShortName = "")
{
/// <summary>
/// Create a new instance of the command.
Expand All @@ -109,6 +112,7 @@ public DockerRun(ICommandLine commandLine, string image)
[],
[],
[],
[],
[])
{ }

Expand All @@ -134,7 +138,8 @@ public IStartInfo GetStartInfo(IHost host)
("--tty", Tty),
("--privileged", Privileged),
("--read-only", ReadOnly),
("--rm", AutoRemove))
("--rm", AutoRemove),
("--quiet", Quiet))
.AddArgs("--expose", ExposedPorts)
.AddArgs("--publish", PublishedPorts)
.AddArgs("--mount", Mounts)
Expand All @@ -151,6 +156,7 @@ public IStartInfo GetStartInfo(IHost host)
("--user", User),
("--workdir", ContainerWorkingDirectory),
("--env-file", EnvFile))
.AddArgs(Hosts.Select(i => $"--add-host={i.host}={i.ip}"))
.AddArgs(Args.ToArray())
.AddValues("-e", "=", startInfo.Vars.ToArray());

Expand Down
30 changes: 16 additions & 14 deletions Samples/MySampleLib/Build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,26 @@ void CancellationOnFirstFailedTest(BuildMessage message)
}

// Parallel tests
var tempDir = Directory.CreateTempSubdirectory();
try
var results = await Task.WhenAll(
RunTestsAsync("7.0", "bookworm-slim", "alpine"),
RunTestsAsync("8.0", "bookworm-slim", "alpine", "noble"));
results.SelectMany(i => i).EnsureSuccess();

async Task<IEnumerable<IBuildResult>> RunTestsAsync(string framework, params string[] platforms)
{
new DotNetPublish()
.WithConfiguration(configuration).WithNoLogo(true).WithNoBuild(true)
.WithFramework("net8.0").AddProps(("PublishDir", tempDir.FullName)).Build().EnsureSuccess();
var publish = new DotNetPublish().WithWorkingDirectory("MySampleLib.Tests")
.WithFramework($"net{framework}").WithConfiguration(configuration).WithNoBuild(true);
await publish.BuildAsync(cancellationToken: cts.Token).EnsureSuccess();
var publishPath = Path.Combine(publish.WorkingDirectory, "bin", configuration, $"net{framework}", "publish");

var test = new VSTest().WithTestFileNames("*.Tests.dll");

var tasks = from tagSuffix in new[] {"bookworm-slim", "alpine", "noble"}
let image = $"mcr.microsoft.com/dotnet/sdk:8.0-{tagSuffix}"
let dockerRun = new DockerRun(image).WithCommandLine(test).WithAutoRemove(true)
.WithVolumes((tempDir.FullName, "/app")).WithContainerWorkingDirectory("/app")
select dockerRun.BuildAsync(CancellationOnFirstFailedTest, cts.Token);

await Task.WhenAll(tasks).EnsureSuccess();
var testInDocker = new DockerRun().WithCommandLine(test).WithAutoRemove(true).WithQuiet(true)
.WithVolumes((Path.GetFullPath(publishPath), "/app")).WithContainerWorkingDirectory("/app");
var tasks = from platform in platforms
let image = $"mcr.microsoft.com/dotnet/sdk:{framework}-{platform}"
select testInDocker.WithImage(image).BuildAsync(CancellationOnFirstFailedTest, cts.Token);
return await Task.WhenAll(tasks);
}
finally { tempDir.Delete(); }

#pragma warning disable CS9113 // Parameter is unread.
class MyTool(INuGet nuGet);
Expand Down

0 comments on commit f58d83b

Please sign in to comment.