4
4
using System . Formats . Tar ;
5
5
using System . Runtime . CompilerServices ;
6
6
using System . Text . Json ;
7
+ using FakeItEasy ;
8
+ using Microsoft . Build . Framework ;
7
9
using Microsoft . DotNet . Cli . Utils ;
8
10
using Microsoft . NET . Build . Containers . LocalDaemons ;
9
11
using Microsoft . NET . Build . Containers . Resources ;
@@ -1429,9 +1431,10 @@ static string[] DecideEntrypoint(string rid, string appName, string workingDir)
1429
1431
}
1430
1432
1431
1433
[ DockerAvailableFact ]
1432
- public async Task CheckErrorMessageWhenSourceRepositoryThrows ( )
1434
+ public async void CheckDownloadErrorMessageWhenSourceRepositoryThrows ( )
1433
1435
{
1434
- ILogger logger = _loggerFactory . CreateLogger ( nameof ( CheckErrorMessageWhenSourceRepositoryThrows ) ) ;
1436
+ var loggerFactory = new TestLoggerFactory ( _testOutput ) ;
1437
+ var logger = loggerFactory . CreateLogger ( nameof ( CheckDownloadErrorMessageWhenSourceRepositoryThrows ) ) ;
1435
1438
string rid = "win-x64" ;
1436
1439
string publishDirectory = BuildLocalApp ( tfm : ToolsetInfo . CurrentTargetFramework , rid : rid ) ;
1437
1440
@@ -1457,24 +1460,39 @@ public async Task CheckErrorMessageWhenSourceRepositoryThrows()
1457
1460
1458
1461
// Load the image into the local registry
1459
1462
var sourceReference = new SourceImageReference ( registry , "some_random_image" , DockerRegistryManager . Net8ImageTag ) ;
1460
- var destinationReference = new DestinationImageReference ( registry , NewImageName ( ) , new [ ] { rid } ) ;
1461
- var sawMyException = false ;
1462
- try
1463
- {
1464
- await new DockerCli ( _loggerFactory ) . LoadAsync ( builtImage , sourceReference , destinationReference , default ) . ConfigureAwait ( false ) ;
1465
- }
1466
- catch ( UnableToDownloadFromRepositoryException e )
1467
- {
1468
- sawMyException = true ;
1469
- Assert . Contains ( "The download of the image from repository some_random_image has failed" , e . ToString ( ) ) ;
1470
- }
1471
- Assert . True ( sawMyException ) ;
1463
+ string archivePath = Path . Combine ( TestSettings . TestArtifactsDirectory , nameof ( CheckDownloadErrorMessageWhenSourceRepositoryThrows ) ) ;
1464
+ var destinationReference = new DestinationImageReference ( new ArchiveFileRegistry ( archivePath ) , NewImageName ( ) , new [ ] { rid } ) ;
1465
+
1466
+ ( var taskLog , var errors ) = SetupTaskLog ( ) ;
1467
+ var telemetry = new Telemetry ( sourceReference , destinationReference , taskLog ) ;
1468
+
1469
+ await ImagePublisher . PublishImageAsync ( builtImage , sourceReference , destinationReference , taskLog , telemetry , CancellationToken . None )
1470
+ . ConfigureAwait ( false ) ;
1471
+
1472
+ // Assert the error message
1473
+ Assert . True ( taskLog . HasLoggedErrors ) ;
1474
+ Assert . NotNull ( errors ) ;
1475
+ Assert . Single ( errors ) ;
1476
+ Assert . Contains ( "Unable to download image from the repository" , errors [ 0 ] ) ;
1472
1477
1473
1478
static string [ ] DecideEntrypoint ( string rid , string appName , string workingDir )
1474
1479
{
1475
1480
var binary = rid . StartsWith ( "win" , StringComparison . Ordinal ) ? $ "{ appName } .exe" : appName ;
1476
1481
return new [ ] { $ "{ workingDir } /{ binary } " } ;
1477
1482
}
1483
+
1484
+ static ( Microsoft . Build . Utilities . TaskLoggingHelper , List < string ? > errors ) SetupTaskLog ( )
1485
+ {
1486
+ // We can use any Task, we just need TaskLoggingHelper
1487
+ Tasks . CreateNewImage cni = new ( ) ;
1488
+ List < string ? > errors = new ( ) ;
1489
+ IBuildEngine buildEngine = A . Fake < IBuildEngine > ( ) ;
1490
+ A . CallTo ( ( ) => buildEngine . LogWarningEvent ( A < BuildWarningEventArgs > . Ignored ) ) . Invokes ( ( BuildWarningEventArgs e ) => errors . Add ( e . Message ) ) ;
1491
+ A . CallTo ( ( ) => buildEngine . LogErrorEvent ( A < BuildErrorEventArgs > . Ignored ) ) . Invokes ( ( BuildErrorEventArgs e ) => errors . Add ( e . Message ) ) ;
1492
+ A . CallTo ( ( ) => buildEngine . LogMessageEvent ( A < BuildMessageEventArgs > . Ignored ) ) . Invokes ( ( BuildMessageEventArgs e ) => errors . Add ( e . Message ) ) ;
1493
+ cni . BuildEngine = buildEngine ;
1494
+ return ( cni . Log , errors ) ;
1495
+ }
1478
1496
}
1479
1497
1480
1498
[ DockerAvailableFact ( checkContainerdStoreAvailability : true ) ]
0 commit comments