Skip to content

Commit

Permalink
Make tests stable
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigusu-Allehu committed Apr 1, 2024
1 parent c27cbfc commit 666889d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,20 @@ public async Task DotnetRestore_withTLSCertificateValidationDisabled_DoesnotThro
{
// Arrange
using var pathContext = _msbuildFixture.CreateSimpleTestPathContext();
TestDirectory packageSourceDirectory = TestDirectory.Create();
var packageA100 = new SimpleTestPackageContext("A", "1.0.0");
await SimpleTestPackageUtility.CreateFolderFeedV3Async(
pathContext.PackageSource,
packageSourceDirectory,
PackageSaveMode.Defaultv3,
packageA100);
var projectA = XPlatTestUtils.CreateProject("ProjectA", pathContext, packageA100, "net472");
var workingDirectory = Path.Combine(pathContext.SolutionRoot, projectA.ProjectName);
SelfSignedCertificateMockServer tcpListenerServer = new SelfSignedCertificateMockServer(pathContext.PackageSource);
SelfSignedCertificateMockServer tcpListenerServer = new SelfSignedCertificateMockServer(packageSourceDirectory);
var serverTask = tcpListenerServer.StartServerAsync();
var configFile = @$"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""source1"" value=""{tcpListenerServer.URI}v3/index.json"" disableTLSCertificateValidation=""true""/>
</packageSources>
</configuration>
";
pathContext.Settings.AddSource("https-feed", $"{tcpListenerServer.URI}v3/index.json", "disableTLSCertificateValidation", "true");

// Act & Assert
File.WriteAllText(Path.Combine(workingDirectory, "NuGet.Config"), configFile);
_msbuildFixture.RunDotnetExpectSuccess(workingDirectory, $"restore {projectA.ProjectName}.csproj --configfile ./NuGet.config");
_msbuildFixture.RunDotnetExpectSuccess(workingDirectory, $"restore {projectA.ProjectName}.csproj --configfile {pathContext.Settings.ConfigPath}");
tcpListenerServer.StopServer();
}

Expand All @@ -54,27 +48,20 @@ public async Task DotnetRestore_withTLSCertificateValidationEnabled_ThrowExcepti
{
// Arrange
using var pathContext = _msbuildFixture.CreateSimpleTestPathContext();
TestDirectory packageSourceDirectory = TestDirectory.Create();
var packageB100 = new SimpleTestPackageContext("myPackg", "1.0.0");
await SimpleTestPackageUtility.CreateFolderFeedV3Async(
pathContext.PackageSource,
packageSourceDirectory,
PackageSaveMode.Defaultv3,
packageB100);
var projectB = XPlatTestUtils.CreateProject("ProjectB", pathContext, packageB100, "net472");
var workingDirectory = Path.Combine(pathContext.SolutionRoot, projectB.ProjectName);
SelfSignedCertificateMockServer tcpListenerServer = new SelfSignedCertificateMockServer(pathContext.PackageSource);
SelfSignedCertificateMockServer tcpListenerServer = new SelfSignedCertificateMockServer(packageSourceDirectory);
var serverTask = tcpListenerServer.StartServerAsync();
var configFile = @$"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""source1"" value=""{tcpListenerServer.URI}v3/index.json""/>
</packageSources>
</configuration>
";
File.WriteAllText(Path.Combine(workingDirectory, "NuGet.Config"), configFile);
pathContext.Settings.AddSource("https-feed", $"{tcpListenerServer.URI}v3/index.json");

// Act & Assert
var _result = _msbuildFixture.RunDotnetExpectFailure(workingDirectory, $"restore {projectB.ProjectName}.csproj --configfile ./NuGet.config");
Assert.Contains("SSL connection could not be established", _result.AllOutput);
var _result = _msbuildFixture.RunDotnetExpectFailure(workingDirectory, $"restore {projectB.ProjectName}.csproj --configfile {pathContext.Settings.ConfigPath} -v d");
tcpListenerServer.StopServer();
}

Expand All @@ -83,30 +70,23 @@ public async Task DotnetRestore_withAnotherSourceTLSCertificateValidationDisbale
{
// Arrange
using var pathContext = _msbuildFixture.CreateSimpleTestPathContext();
TestDirectory packageSourceDirectory = TestDirectory.Create();
var packageB100 = new SimpleTestPackageContext("myPackg", "1.0.0");
await SimpleTestPackageUtility.CreateFolderFeedV3Async(
pathContext.PackageSource,
packageSourceDirectory,
PackageSaveMode.Defaultv3,
packageB100);
var projectB = XPlatTestUtils.CreateProject("ProjectB", pathContext, packageB100, "net472");
var workingDirectory = Path.Combine(pathContext.SolutionRoot, projectB.ProjectName);
SelfSignedCertificateMockServer tcpListenerServer1 = new SelfSignedCertificateMockServer(pathContext.PackageSource);
SelfSignedCertificateMockServer tcpListenerServer2 = new SelfSignedCertificateMockServer(pathContext.PackageSource);
SelfSignedCertificateMockServer tcpListenerServer1 = new SelfSignedCertificateMockServer(packageSourceDirectory);
SelfSignedCertificateMockServer tcpListenerServer2 = new SelfSignedCertificateMockServer(packageSourceDirectory);
var serverTask = tcpListenerServer1.StartServerAsync();
var serverTask2 = tcpListenerServer2.StartServerAsync();
var configFile = @$"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""source1"" value=""{tcpListenerServer1.URI}v3/index.json""/>
<add key=""source2"" value=""{tcpListenerServer2.URI}v3/index.json"" disableTLSCertificateValidation=""true""/>
</packageSources>
</configuration>
";
File.WriteAllText(Path.Combine(workingDirectory, "NuGet.Config"), configFile);
pathContext.Settings.AddSource("https-feed1", $"{tcpListenerServer1.URI}v3/index.json");
pathContext.Settings.AddSource("https-feed2", $"{tcpListenerServer2.URI}v3/index.json", "disableTLSCertificateValidation", "true");

// Act & Assert
var _result = _msbuildFixture.RunDotnetExpectFailure(workingDirectory, $"restore {projectB.ProjectName}.csproj --configfile ./NuGet.config");
Assert.Contains("SSL connection could not be established", _result.AllOutput);
var _result = _msbuildFixture.RunDotnetExpectFailure(workingDirectory, $"restore {projectB.ProjectName}.csproj --configfile {pathContext.Settings.ConfigPath}");
tcpListenerServer1.StopServer();
tcpListenerServer2.StopServer();
}
Expand All @@ -116,29 +96,23 @@ public async Task DotnetRestore_withAnotherSourceTLSCertificateValidationEnabled
{
// Arrange
using var pathContext = _msbuildFixture.CreateSimpleTestPathContext();
TestDirectory packageSourceDirectory = TestDirectory.Create();
var packageB100 = new SimpleTestPackageContext("myPackg", "1.0.0");
await SimpleTestPackageUtility.CreateFolderFeedV3Async(
pathContext.PackageSource,
packageSourceDirectory,
PackageSaveMode.Defaultv3,
packageB100);
var projectB = XPlatTestUtils.CreateProject("ProjectB", pathContext, packageB100, "net472");
var workingDirectory = Path.Combine(pathContext.SolutionRoot, projectB.ProjectName);
SelfSignedCertificateMockServer tcpListenerServer1 = new SelfSignedCertificateMockServer(pathContext.PackageSource);
SelfSignedCertificateMockServer tcpListenerServer2 = new SelfSignedCertificateMockServer(pathContext.PackageSource);
SelfSignedCertificateMockServer tcpListenerServer1 = new SelfSignedCertificateMockServer(packageSourceDirectory);
SelfSignedCertificateMockServer tcpListenerServer2 = new SelfSignedCertificateMockServer(packageSourceDirectory);
var serverTask = tcpListenerServer1.StartServerAsync();
var serverTask2 = tcpListenerServer2.StartServerAsync();
var configFile = @$"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""source1"" value=""{tcpListenerServer1.URI}v3/index.json""/>
<add key=""source2"" value=""{tcpListenerServer2.URI}v3/index.json"" disableTLSCertificateValidation=""true""/>
</packageSources>
</configuration>
";
File.WriteAllText(Path.Combine(workingDirectory, "NuGet.Config"), configFile);
pathContext.Settings.AddSource("https-feed1", $"{tcpListenerServer1.URI}v3/index.json");
pathContext.Settings.AddSource("https-feed2", $"{tcpListenerServer2.URI}v3/index.json", "disableTLSCertificateValidation", "true");

// Act & Assert
var _result = _msbuildFixture.RunDotnetExpectSuccess(workingDirectory, $"restore {projectB.ProjectName}.csproj --configfile ./NuGet.config --source {tcpListenerServer2.URI}v3/index.json");
var _result = _msbuildFixture.RunDotnetExpectSuccess(workingDirectory, $"restore {projectB.ProjectName}.csproj --configfile {pathContext.Settings.ConfigPath} --source {tcpListenerServer2.URI}v3/index.json");
tcpListenerServer1.StopServer();
tcpListenerServer2.StopServer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ public void AddSource(string sourceName, string sourceUri, string allowInsecureC
Save();
}

public void AddSource(string sourceName, string sourceUri, string attributeName, string attributeValue)
{
var section = GetOrAddSection(XML, "packageSources");
AddEntry(section, sourceName, sourceUri, attributeName, attributeValue);
Save();
}

public void AddPackageSourceMapping(string sourceName, params string[] patterns)
{
XElement packageSourceMappingSection = GetOrAddSection(XML, "packageSourceMapping");
Expand Down

0 comments on commit 666889d

Please sign in to comment.