Skip to content

Commit

Permalink
fix: try to make tests less flaky on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
natemcmaster committed Mar 30, 2024
1 parent 17b1c5c commit 178bce2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#nullable enable
using System.Security.Cryptography.X509Certificates;
using LettuceEncrypt.Internal;
using McMaster.Extensions.Xunit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.Internal;
Expand All @@ -16,8 +15,7 @@ namespace LettuceEncrypt.UnitTests;

public class FileSystemCertificateRepoTests
{
[SkippableTheory]
[SkipOnOS(OS.MacOS)] // started failing with macOS 12. I don't really know why. If this matters to you, please send a PR to fix.
[Theory]
[InlineData(null)]
[InlineData("")]
public async Task ItCanSaveCertsWithoutPassword(string? password)
Expand Down
28 changes: 26 additions & 2 deletions test/LettuceEncrypt.UnitTests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,31 @@ public static X509Certificate2 CreateTestCert(string[] domainNames, DateTimeOffs
}

var cert = csr.CreateSelfSigned(DateTimeOffset.Now.AddMinutes(-1), expires.Value);
// https://github.com/dotnet/runtime/issues/29144
return new X509Certificate2(cert.Export(X509ContentType.Pfx), "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
int retries = 5;
while (retries > 0)
{
try
{
// https://github.com/dotnet/runtime/issues/29144
var certWithKey = cert.Export(X509ContentType.Pfx);
return new X509Certificate2(certWithKey, "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
}
catch
{
retries--;
if (retries > 0)
{
// For unclear reasons, on macOS it takes times for certs to be available for re-export.
// Retries appear to work.
Thread.Sleep(50);
continue;
}
else
{
throw;
}
}
}
throw new Exception($"Could not create self signed cert for {domainNames}");
}
}

0 comments on commit 178bce2

Please sign in to comment.