Skip to content

Commit 492a936

Browse files
Update mockserver usage for package download runner tests (#6948)
1 parent d13453f commit 492a936

2 files changed

Lines changed: 167 additions & 46 deletions

File tree

build/xunit.runsettings

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<DataCollector friendlyName="blame" enabled="True">
5050
<Configuration>
5151
<!-- Enables hang dump or testhost and its child processes when a test hangs for more than 5 minutes. Dump type "Full", "Mini" or "None" (just kill the processes). -->
52-
<CollectDumpOnTestSessionHang TestTimeout="5min" HangDumpType="Mini" />
52+
<CollectDumpOnTestSessionHang TestTimeout="5min" HangDumpType="Full" />
5353
</Configuration>
5454
</DataCollector>
5555
</DataCollectors>

test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/Package/Download/PackageDownloadRunnerTests.cs

Lines changed: 166 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ public static IEnumerable<object[]> Cases()
433433
{
434434
// Parameters:
435435
// A-packages, B-packages, sourceMappings, sourcesArgs, downloadId, downloadVersion,
436-
// allowInsecureConnections, expectSuccess, expectedInstalled
436+
//expectSuccess, expectedInstalled
437437

438438
// --source specified, mapping ignored, package only in A -> success
439439
yield return new object[]
@@ -443,7 +443,6 @@ public static IEnumerable<object[]> Cases()
443443
new List<(string,string)> { ("B", "Contoso.*") }, // mapping ignored
444444
new List<string> { "A" }, // --source A
445445
"Contoso.Lib", "1.0.0", // downloadId, downloadVersion
446-
true, // allow insecure
447446
true, // expect success
448447
("Contoso.Lib", "1.0.0") // expectedInstalled
449448
};
@@ -456,7 +455,6 @@ public static IEnumerable<object[]> Cases()
456455
new List<(string,string)> { ("B", "Contoso.*") }, // mapping -> B
457456
null, // no --source
458457
"Contoso.Mapped", "2.0.0", // downloadId, downloadVersion
459-
true, // allow insecure
460458
true, // expect success
461459
("Contoso.Mapped", "2.0.0") // expectedInstalled
462460
};
@@ -469,33 +467,6 @@ public static IEnumerable<object[]> Cases()
469467
new List<(string,string)> { ("A", "Contoso.*") }, // mapped to A
470468
null,
471469
"Contoso.Mapped", "2.0.0",
472-
true,
473-
false,
474-
null!
475-
};
476-
477-
// --source specified, no source mapping with an insecure source
478-
yield return new object[]
479-
{
480-
new List<(string,string)> { ("Contoso.Lib", "1.0.0") }, // A
481-
new List<(string,string)>(),
482-
new List<(string,string)> { ("A", "Contoso.*") },
483-
new List<string> { "A" }, // --source
484-
"Contoso.Lib", "1.0.0",
485-
false, // allow insecure connections false / not set to true
486-
false,
487-
null!
488-
};
489-
490-
// no --source, mapping -> B, allow insecure not enabled -> fail
491-
yield return new object[]
492-
{
493-
new List<(string,string)>(), // A
494-
new List<(string,string)> { ("Contoso.Mapped", "1.0.0") },
495-
new List<(string,string)> { ("B", "Contoso.*") },
496-
null,
497-
"Contoso.Mapped", "1.0.0",
498-
false, // allow insecure connections false / not set to true
499470
false,
500471
null!
501472
};
@@ -510,7 +481,6 @@ public async Task RunAsync_WithSourceMapping(
510481
IReadOnlyList<string> sourcesArgs,
511482
string downloadId,
512483
string downloadVersion,
513-
bool allowInsecureConnections,
514484
bool expectSuccess,
515485
(string id, string version)? expectedInstalled)
516486
{
@@ -519,9 +489,6 @@ public async Task RunAsync_WithSourceMapping(
519489
string srcADirectory = Path.Combine(context.PackageSource, "SourceA");
520490
string srcBDirectory = Path.Combine(context.PackageSource, "SourceB");
521491

522-
using var serverA = new FileSystemBackedV3MockServer(srcADirectory);
523-
using var serverB = new FileSystemBackedV3MockServer(srcBDirectory);
524-
525492
foreach (var (id, ver) in sourceAPackages)
526493
{
527494
await SimpleTestPackageUtility.CreateFullPackageAsync(srcADirectory, id, ver);
@@ -532,12 +499,9 @@ public async Task RunAsync_WithSourceMapping(
532499
await SimpleTestPackageUtility.CreateFullPackageAsync(srcBDirectory, id, ver);
533500
}
534501

535-
serverA.Start();
536-
serverB.Start();
537-
538502
// sources
539-
context.Settings.AddSource("A", serverA.ServiceIndexUri);
540-
context.Settings.AddSource("B", serverB.ServiceIndexUri);
503+
context.Settings.AddSource("A", srcADirectory);
504+
context.Settings.AddSource("B", srcBDirectory);
541505

542506
// mapping
543507
foreach (var (src, pattern) in sourceMappings)
@@ -549,8 +513,8 @@ public async Task RunAsync_WithSourceMapping(
549513

550514
var packageSources = new List<PackageSource>
551515
{
552-
new(serverA.ServiceIndexUri, "A"),
553-
new(serverB.ServiceIndexUri, "B")
516+
new(srcADirectory, "A"),
517+
new(srcBDirectory, "B")
554518
};
555519

556520
// args
@@ -565,7 +529,6 @@ public async Task RunAsync_WithSourceMapping(
565529
}
566530
],
567531
OutputDirectory = context.WorkingDirectory,
568-
AllowInsecureConnections = allowInsecureConnections,
569532
Sources = sourcesArgs == null ? [] : sourcesArgs.ToList()
570533
};
571534

@@ -583,9 +546,6 @@ public async Task RunAsync_WithSourceMapping(
583546
settings,
584547
CancellationToken.None);
585548

586-
serverA.Stop();
587-
serverB.Stop();
588-
589549
// Assert
590550
if (expectSuccess)
591551
{
@@ -603,6 +563,167 @@ public async Task RunAsync_WithSourceMapping(
603563
}
604564
}
605565

566+
[Fact]
567+
public async Task RunAsync_WhenPsmMapsToInsecureSource_AndAllowInsecureConnectionsFalse_LogsErrorForMappedSource()
568+
{
569+
// Arrange
570+
using var context = new SimpleTestPathContext();
571+
var insecureSourceUrl = "http://contoso.test/v3/index.json";
572+
573+
context.Settings.AddSource("InsecureMapped", insecureSourceUrl);
574+
context.Settings.AddPackageSourceMapping("InsecureMapped", "Contoso.*");
575+
576+
var settings = Settings.LoadSettingsGivenConfigPaths([context.Settings.ConfigPath]);
577+
578+
var args = new PackageDownloadArgs
579+
{
580+
Packages =
581+
[
582+
new PackageWithNuGetVersion
583+
{
584+
Id = "Contoso.Insecure",
585+
NuGetVersion = NuGetVersion.Parse("1.0.0")
586+
}
587+
],
588+
OutputDirectory = context.WorkingDirectory,
589+
AllowInsecureConnections = false,
590+
};
591+
592+
var logger = new Mock<ILoggerWithColor>(MockBehavior.Loose);
593+
var packageSources = new List<PackageSource>
594+
{
595+
new(insecureSourceUrl, "InsecureMapped")
596+
};
597+
598+
// Act
599+
var exit = await PackageDownloadRunner.RunAsync(
600+
args,
601+
logger.Object,
602+
packageSources,
603+
settings,
604+
CancellationToken.None);
605+
606+
// Assert
607+
exit.Should().Be(PackageDownloadRunner.ExitCodeError);
608+
}
609+
610+
[Fact]
611+
public async Task RunAsync_WhenPsmMapsToSecureLocalSource_DoesNotLogErrorEvenWhenOtherSourceIsHttp()
612+
{
613+
// Arrange
614+
using var context = new SimpleTestPathContext();
615+
var localSourcePath = context.PackageSource;
616+
var insecureSourceUrl = "http://contoso.test/v3/index.json";
617+
var id = "Contoso.LocalOnly";
618+
var version = "1.0.0";
619+
await SimpleTestPackageUtility.CreateFullPackageAsync(localSourcePath, id, version);
620+
context.Settings.AddSource("LocalSecure", localSourcePath);
621+
context.Settings.AddSource("InsecureOther", insecureSourceUrl);
622+
623+
// map package to the secure local source only
624+
context.Settings.AddPackageSourceMapping("LocalSecure", "Contoso.*");
625+
var settings = Settings.LoadSettingsGivenConfigPaths([context.Settings.ConfigPath]);
626+
627+
var args = new PackageDownloadArgs
628+
{
629+
Packages =
630+
[
631+
new PackageWithNuGetVersion
632+
{
633+
Id = id,
634+
NuGetVersion = NuGetVersion.Parse(version)
635+
}
636+
],
637+
OutputDirectory = context.WorkingDirectory,
638+
AllowInsecureConnections = false,
639+
};
640+
641+
var logger = new Mock<ILoggerWithColor>(MockBehavior.Loose);
642+
643+
var packageSources = new List<PackageSource>
644+
{
645+
new(localSourcePath, "LocalSecure"),
646+
new(insecureSourceUrl, "InsecureOther"),
647+
};
648+
649+
// Act
650+
var exit = await PackageDownloadRunner.RunAsync(
651+
args,
652+
logger.Object,
653+
packageSources,
654+
settings,
655+
CancellationToken.None);
656+
657+
// Assert
658+
exit.Should().Be(PackageDownloadRunner.ExitCodeSuccess);
659+
var installDir = Path.Combine(context.WorkingDirectory, id.ToLowerInvariant(), version);
660+
Directory.Exists(installDir).Should().BeTrue();
661+
File.Exists(Path.Combine(installDir, $"{id.ToLowerInvariant()}.{version}.nupkg")).Should().BeTrue();
662+
}
663+
664+
[Fact]
665+
public async Task RunAsync_WhenPsmMapsToInsecureSource_AndAllowInsecureConnectionsTrue_DownloadsWithoutError()
666+
{
667+
// Arrange
668+
using var context = new SimpleTestPathContext();
669+
string srcDirectory = Path.Combine(context.PackageSource, "HttpSource");
670+
Directory.CreateDirectory(srcDirectory);
671+
672+
var id = "Contoso.HttpMapped";
673+
var version = "1.0.0";
674+
await SimpleTestPackageUtility.CreateFullPackageAsync(srcDirectory, id, version);
675+
using var server = new FileSystemBackedV3MockServer(srcDirectory);
676+
server.Start();
677+
678+
var mappedHttpSource = server.ServiceIndexUri;
679+
context.Settings.AddSource("MappedHttp", mappedHttpSource);
680+
681+
context.Settings.AddPackageSourceMapping("MappedHttp", "Contoso.*");
682+
var settings = Settings.LoadSettingsGivenConfigPaths([context.Settings.ConfigPath]);
683+
684+
var args = new PackageDownloadArgs
685+
{
686+
Packages =
687+
[
688+
new PackageWithNuGetVersion
689+
{
690+
Id = id,
691+
NuGetVersion = NuGetVersion.Parse(version)
692+
}
693+
],
694+
OutputDirectory = context.WorkingDirectory,
695+
AllowInsecureConnections = true,
696+
};
697+
698+
string capturedErrors = string.Empty;
699+
var logger = new Mock<ILoggerWithColor>(MockBehavior.Loose);
700+
logger
701+
.Setup(l => l.LogError(It.IsAny<string>()))
702+
.Callback<string>(msg => capturedErrors += msg + Environment.NewLine);
703+
704+
var packageSources = new List<PackageSource>
705+
{
706+
new(mappedHttpSource, "MappedHttp"),
707+
};
708+
709+
// Act
710+
var exit = await PackageDownloadRunner.RunAsync(
711+
args,
712+
logger.Object,
713+
packageSources,
714+
settings,
715+
CancellationToken.None);
716+
717+
server.Stop();
718+
719+
// Assert
720+
exit.Should().Be(PackageDownloadRunner.ExitCodeSuccess, because: capturedErrors);
721+
722+
var installDir = Path.Combine(context.WorkingDirectory, id.ToLowerInvariant(), version);
723+
Directory.Exists(installDir).Should().BeTrue();
724+
File.Exists(Path.Combine(installDir, $"{id.ToLowerInvariant()}.{version}.nupkg")).Should().BeTrue();
725+
}
726+
606727
[Fact]
607728
public async Task RunAsync_WhenMappedSourceMissing_LogsVerbose()
608729
{

0 commit comments

Comments
 (0)