Skip to content

Commit

Permalink
fix(#476): Do not run SonarCloud analysis for pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Jun 18, 2022
1 parent d18fc52 commit b6e5056
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 32 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ jobs:
test-results
publish:
if: ${{ contains(fromJson('["develop", "master"]'), github.ref_name) }}

needs: build

environment: ${{ contains(fromJson('["develop", "master"]'), github.ref_name) && 'production' || 'development' }}
environment: production

runs-on: ubuntu-20.04

Expand Down
14 changes: 8 additions & 6 deletions src/Testcontainers/Builders/DockerCredentialProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ public static string Get(string credentialProviderName, string hostname)

try
{
if (!dockerCredentialProcess.Start())
if (dockerCredentialProcess.Start())
{
return null;
dockerCredentialProcess.StandardInput.WriteLine(hostname);
dockerCredentialProcess.StandardInput.Close();
return dockerCredentialProcess.StandardOutput.ReadToEnd().Trim();
}
else
{
throw new InvalidOperationException("Docker not found.");
}

dockerCredentialProcess.StandardInput.WriteLine(hostname);
dockerCredentialProcess.StandardInput.Close();
return dockerCredentialProcess.StandardOutput.ReadToEnd().Trim();
}
catch (Exception)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Testcontainers/Clients/DockerContainerOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public async Task<Stream> GetArchiveFromContainerAsync(string id, string path, C

public async Task AttachAsync(string id, IOutputConsumer outputConsumer, CancellationToken ct = default)
{
if (!outputConsumer.Enabled)
{
return;
}

this.logger.AttachToDockerContainer(id, outputConsumer.GetType());

var attachParameters = new ContainerAttachParameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace DotNet.Testcontainers.Configurations
/// </summary>
public interface IOutputConsumer : IDisposable
{
/// <summary>
/// Gets a value indicating whether the <see cref="IOutputConsumer" /> is enabled or not.
/// </summary>
bool Enabled { get; }

/// <summary>
/// Gets the stream that receives stdout.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ internal sealed class RedirectStdoutAndStderrToNull : IOutputConsumer
/// </summary>
public RedirectStdoutAndStderrToNull()
{
this.Enabled = false;
this.Stdout = Stream.Null;
this.Stderr = Stream.Null;
}

/// <inheritdoc />
public bool Enabled { get; }

/// <inheritdoc />
public Stream Stdout { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ public RedirectStdoutAndStderrToStream()
/// <param name="stderr">The stderr stream.</param>
public RedirectStdoutAndStderrToStream(Stream stdout, Stream stderr)
{
this.stdout = new StreamWriter(stdout) { AutoFlush = true };
this.stderr = new StreamWriter(stderr) { AutoFlush = true };
this.Enabled = stdout.CanWrite || stderr.CanWrite;
this.stdout = new StreamWriter(stdout);
this.stdout.AutoFlush = true;
this.stderr = new StreamWriter(stderr);
this.stderr.AutoFlush = true;
}

/// <inheritdoc />
public bool Enabled { get; }

/// <inheritdoc />
public Stream Stdout
=> this.stdout.BaseStream;
Expand Down
10 changes: 6 additions & 4 deletions src/Testcontainers/Containers/ResourceReaper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ public async ValueTask DisposeAsync()

this.disposed = true;

this.maintainConnectionCts.Cancel();

try
{
this.maintainConnectionCts.Cancel();

// Close connection before disposing ResourceReaper.
await this.maintainConnectionTask
.ConfigureAwait(false);
Expand All @@ -209,8 +209,10 @@ await this.maintainConnectionTask
{
// Ignore
}

this.maintainConnectionCts.Dispose();
finally
{
this.maintainConnectionCts.Dispose();
}

if (this.resourceReaperContainer != null)
{
Expand Down
7 changes: 3 additions & 4 deletions src/Testcontainers/Containers/TestcontainersContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,10 @@ private async Task<ContainerListResponse> Create(CancellationToken ct = default)

private async Task<ContainerListResponse> Start(string id, CancellationToken ct = default)
{
var attachTask = this.client.AttachAsync(id, this.configuration.OutputConsumer, ct);

var startTask = this.client.StartAsync(id, ct);
await this.client.AttachAsync(id, this.configuration.OutputConsumer, ct)
.ConfigureAwait(false);

await Task.WhenAll(attachTask, startTask)
await this.client.StartAsync(id, ct)
.ConfigureAwait(false);

this.container = await this.client.GetContainer(id, ct)
Expand Down
19 changes: 14 additions & 5 deletions tests/Testcontainers.Tests/SkipOnLinuxEngineAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@ private static bool GetIsLinuxEngineEnabled()
dockerProcessStartInfo.RedirectStandardOutput = true;
dockerProcessStartInfo.UseShellExecute = false;

using (var dockerProcess = Process.Start(dockerProcessStartInfo))
var dockerProcess = new Process();
dockerProcess.StartInfo = dockerProcessStartInfo;

try
{
if (dockerProcess == null)
if (dockerProcess.Start())
{
var stdout = dockerProcess.StandardOutput.ReadToEnd();
return stdout.Contains("linux", StringComparison.OrdinalIgnoreCase);
}
else
{
throw new InvalidOperationException("Docker not found.");
}

var stdout = dockerProcess.StandardOutput.ReadToEnd();
return stdout.Contains("linux", StringComparison.OrdinalIgnoreCase);
}
finally
{
dockerProcess.Dispose();
}
}
}
Expand Down
35 changes: 25 additions & 10 deletions tests/Testcontainers.Tests/Unit/Docker.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
namespace DotNet.Testcontainers.Tests.Unit
{
using System;
using System.Diagnostics;

public static class Docker
{
public static bool Exists(DockerResource dockerResource, string name)
{
DataReceivedEventHandler ignoreReceivedData = (_, _) => { };

var dockerProcessStartInfo = new ProcessStartInfo();
dockerProcessStartInfo.FileName = "docker";
dockerProcessStartInfo.Arguments = $"inspect --type={dockerResource.Type} {name}";
dockerProcessStartInfo.RedirectStandardOutput = true;
dockerProcessStartInfo.RedirectStandardError = true;
dockerProcessStartInfo.RedirectStandardOutput = true;
dockerProcessStartInfo.UseShellExecute = false;

using (var dockerProcess = Process.Start(dockerProcessStartInfo))
var dockerProcess = new Process();
dockerProcess.StartInfo = dockerProcessStartInfo;
dockerProcess.ErrorDataReceived += ignoreReceivedData;
dockerProcess.OutputDataReceived += ignoreReceivedData;

try
{
if (dockerProcess == null)
if (dockerProcess.Start())
{
return false;
dockerProcess.BeginErrorReadLine();
dockerProcess.BeginOutputReadLine();
dockerProcess.WaitForExit();
return 0.Equals(dockerProcess.ExitCode);
}

_ = dockerProcess.StandardOutput.ReadToEnd();
_ = dockerProcess.StandardError.ReadToEnd();

dockerProcess.WaitForExit();
return 0.Equals(dockerProcess.ExitCode);
else
{
throw new InvalidOperationException("Docker not found.");
}
}
finally
{
dockerProcess.ErrorDataReceived -= ignoreReceivedData;
dockerProcess.OutputDataReceived -= ignoreReceivedData;
dockerProcess.Dispose();
}
}
}
Expand Down

0 comments on commit b6e5056

Please sign in to comment.