Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 178bce2

Browse files
committed
fix: try to make tests less flaky on macos
1 parent 17b1c5c commit 178bce2

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

test/LettuceEncrypt.UnitTests/FileSystemCertificateRepoTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#nullable enable
55
using System.Security.Cryptography.X509Certificates;
66
using LettuceEncrypt.Internal;
7-
using McMaster.Extensions.Xunit;
87
using Microsoft.Extensions.DependencyInjection;
98
using Microsoft.Extensions.Hosting;
109
using Microsoft.Extensions.Hosting.Internal;
@@ -16,8 +15,7 @@ namespace LettuceEncrypt.UnitTests;
1615

1716
public class FileSystemCertificateRepoTests
1817
{
19-
[SkippableTheory]
20-
[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.
18+
[Theory]
2119
[InlineData(null)]
2220
[InlineData("")]
2321
public async Task ItCanSaveCertsWithoutPassword(string? password)

test/LettuceEncrypt.UnitTests/TestUtils.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,31 @@ public static X509Certificate2 CreateTestCert(string[] domainNames, DateTimeOffs
3535
}
3636

3737
var cert = csr.CreateSelfSigned(DateTimeOffset.Now.AddMinutes(-1), expires.Value);
38-
// https://github.com/dotnet/runtime/issues/29144
39-
return new X509Certificate2(cert.Export(X509ContentType.Pfx), "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
38+
int retries = 5;
39+
while (retries > 0)
40+
{
41+
try
42+
{
43+
// https://github.com/dotnet/runtime/issues/29144
44+
var certWithKey = cert.Export(X509ContentType.Pfx);
45+
return new X509Certificate2(certWithKey, "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
46+
}
47+
catch
48+
{
49+
retries--;
50+
if (retries > 0)
51+
{
52+
// For unclear reasons, on macOS it takes times for certs to be available for re-export.
53+
// Retries appear to work.
54+
Thread.Sleep(50);
55+
continue;
56+
}
57+
else
58+
{
59+
throw;
60+
}
61+
}
62+
}
63+
throw new Exception($"Could not create self signed cert for {domainNames}");
4064
}
4165
}

0 commit comments

Comments
 (0)