Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
477 changes: 477 additions & 0 deletions src/ImageBuilder.Tests/Build/BuildPlannerTests.cs

Large diffs are not rendered by default.

55 changes: 30 additions & 25 deletions src/ImageBuilder.Tests/BuildCommandTests.cs

Large diffs are not rendered by default.

133 changes: 96 additions & 37 deletions src/ImageBuilder.Tests/GenerateBuildMatrixCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.ImageBuilder.Build;
using Microsoft.DotNet.ImageBuilder.Commands;
using Microsoft.DotNet.ImageBuilder.Models.Image;
using Microsoft.DotNet.ImageBuilder.Models.Manifest;
Expand Down Expand Up @@ -159,56 +161,82 @@ public async Task GenerateBuildMatrixCommand_PlatformDependencyGraph(string filt
}
}

private static void SetCacheResult(Mock<IImageCacheService> imageCacheServiceMock, string dockerfilePath, ImageCacheState cacheState)
[TestMethod]
public async Task PlatformDependencyGraph_SharedTagDependency()
{
imageCacheServiceMock
.Setup(o => o.CheckForCachedImageAsync(
It.IsAny<ImageData>(),
It.Is<PlatformData>(platform => platform.Dockerfile == dockerfilePath),
It.IsAny<ImageDigestCache>(),
It.IsAny<ImageNameResolver>(),
It.IsAny<string>(),
It.IsAny<bool>(),
It.IsAny<bool>()))
.ReturnsAsync(new ImageCacheResult(cacheState, false, null));
using TempFolderContext tempFolder = TestHelper.UseTempFolder();
string parentDockerfile = CreateDockerfile(
"parent",
tempFolder,
"base:tag");
string childDockerfile = CreateDockerfile(
"child",
tempFolder,
"parent:shared");
Manifest manifest = CreateManifest(
CreateRepo(
"parent",
CreateImage(
["shared"],
CreatePlatform(parentDockerfile, ["specific"]))),
CreateRepo(
"child",
CreateImage(CreatePlatform(childDockerfile, ["tag"]))));
GenerateBuildMatrixCommand command = CreateCommand();
command.Options.Manifest = Path.Combine(tempFolder.Path, "manifest.json");
command.Options.MatrixType = MatrixType.PlatformDependencyGraph;
File.WriteAllText(
command.Options.Manifest,
JsonConvert.SerializeObject(manifest));

command.LoadManifest();
BuildMatrixInfo matrix = (await command.GenerateMatrixInfoAsync())
.ShouldHaveSingleItem();
BuildLegInfo leg = matrix.Legs.ShouldHaveSingleItem();

leg.Variables
.Single(variable => variable.Name == "imageBuilderPaths")
.Value
.ShouldBe("--path parent/Dockerfile --path child/Dockerfile");
}

[TestMethod]
[DataRow(
ImageCacheState.NotCached,
ImageCacheState.NotCached,
BuildAction.BuildImage,
BuildAction.BuildImage,
"--path 1.0/runtime/os/amd64/Dockerfile --path 1.0/sdk/os/amd64/Dockerfile",
"--path 2.0/runtime/os/amd64/Dockerfile --path 2.0/sdk/os/amd64/Dockerfile")]
[DataRow(
ImageCacheState.Cached,
ImageCacheState.Cached,
BuildAction.NoAction,
BuildAction.NoAction,
"--path 2.0/runtime/os/amd64/Dockerfile --path 2.0/sdk/os/amd64/Dockerfile")]
[DataRow(
ImageCacheState.Cached,
ImageCacheState.Cached,
BuildAction.NoAction,
BuildAction.NoAction,
"--path 1.0/standalone/os/amd64/Dockerfile",
"--path 2.0/standalone/os/amd64/Dockerfile",
null,
"*standalone*")]
[DataRow(
ImageCacheState.CachedWithMissingTags,
ImageCacheState.Cached,
BuildAction.UsePublishedImage,
BuildAction.NoAction,
"--path 1.0/runtime/os/amd64/Dockerfile",
"--path 2.0/runtime/os/amd64/Dockerfile --path 2.0/sdk/os/amd64/Dockerfile")]
[DataRow(
ImageCacheState.Cached,
ImageCacheState.NotCached,
BuildAction.UsePublishedImage,
BuildAction.BuildImage,
"--path 1.0/runtime/os/amd64/Dockerfile --path 1.0/sdk/os/amd64/Dockerfile",
"--path 2.0/runtime/os/amd64/Dockerfile --path 2.0/sdk/os/amd64/Dockerfile")]
[DataRow(
ImageCacheState.NotCached,
ImageCacheState.NotCached,
BuildAction.BuildImage,
BuildAction.BuildImage,
"--path 1.0/runtime/os/amd64/Dockerfile --path 1.0/sdk/os/amd64/Dockerfile",
"--path 1.0/standalone/os/amd64/Dockerfile",
"--path 2.0/runtime/os/amd64/Dockerfile --path 2.0/sdk/os/amd64/Dockerfile",
"")] // Clear out the path filters to ensure all images are included
public async Task FilterOutCachedImages(
ImageCacheState runtime1CacheState,
ImageCacheState sdk1CacheState,
BuildAction runtime1Action,
BuildAction sdk1Action,
string leg1ExpectedPaths,
string leg2ExpectedPaths = null,
string leg3ExpectedPaths = null,
Expand Down Expand Up @@ -251,14 +279,41 @@ public async Task FilterOutCachedImages(
CreatePlatform(dockerfileSdk2Path = CreateDockerfile(Sdk2RelativeDir, tempFolderContext, "runtime:2.0"), ["2.0"])))
);

Mock<IImageCacheService> imageCacheServiceMock = new();
SetCacheResult(imageCacheServiceMock, dockerfileStandalone1Path, ImageCacheState.NotCached);
SetCacheResult(imageCacheServiceMock, dockerfileRuntime1Path, runtime1CacheState);
SetCacheResult(imageCacheServiceMock, dockerfileSdk1Path, sdk1CacheState);
SetCacheResult(imageCacheServiceMock, dockerfileRuntime2Path, ImageCacheState.NotCached);
SetCacheResult(imageCacheServiceMock, dockerfileSdk2Path, ImageCacheState.NotCached);
Dictionary<string, BuildAction> actions = new()
{
[dockerfileRuntime1Path] = runtime1Action,
[dockerfileSdk1Path] = sdk1Action,
};
Mock<BuildPlanner> buildPlannerMock = new(
Mock.Of<ILogger<BuildPlanner>>());
buildPlannerMock
.Setup(planner => planner.CreatePlanAsync(
It.IsAny<BuildGraph>(),
It.IsAny<ImageArtifactDetails>(),
It.IsAny<IBuildPolicy>(),
It.IsAny<CancellationToken>()))
.Returns((
BuildGraph graph,
ImageArtifactDetails imageInfo,
IBuildPolicy policy,
CancellationToken _) => Task.FromResult(
graph.Targets.Select(target =>
new BuildPlanItem(
target,
new BuildPolicyResult(
actions.GetValueOrDefault(
target.Platform.DockerfilePathRelativeToManifest,
BuildAction.BuildImage),
new BuildReason("Test-selected action.")),
PublishedImage: null))
.ToArray()));

GenerateBuildMatrixCommand command = new(TestHelper.CreateManifestJsonService(), imageCacheServiceMock.Object, Mock.Of<IManifestServiceFactory>(), Mock.Of<ILogger<GenerateBuildMatrixCommand>>());
GenerateBuildMatrixCommand command = new(
TestHelper.CreateManifestJsonService(),
buildPlannerMock.Object,
Mock.Of<IGitService>(),
Mock.Of<IManifestServiceFactory>(),
Mock.Of<ILogger<GenerateBuildMatrixCommand>>());
command.Options.Manifest = Path.Combine(tempFolderContext.Path, "manifest.json");
command.Options.MatrixType = MatrixType.PlatformDependencyGraph;
command.Options.ImageInfoPath = Path.Combine(tempFolderContext.Path, "imageinfo.json");
Expand Down Expand Up @@ -1707,13 +1762,12 @@ private static GenerateBuildMatrixCommand SetupTrimCacheTest(
externalImageDigestResults: externalImageDigestResults ?? []);
}

ImageCacheService imageCacheService = new(
Mock.Of<ILogger<ImageCacheService>>(),
gitServiceMock.Object);
BuildPlanner buildPlanner = new(Mock.Of<ILogger<BuildPlanner>>());

GenerateBuildMatrixCommand command = new(
TestHelper.CreateManifestJsonService(),
imageCacheService,
buildPlanner,
gitServiceMock.Object,
manifestServiceFactoryMock.Object,
Mock.Of<ILogger<GenerateBuildMatrixCommand>>());

Expand Down Expand Up @@ -1758,6 +1812,11 @@ private static GenerateBuildMatrixCommand SetupTrimCacheTest(
}

private static GenerateBuildMatrixCommand CreateCommand() =>
new(TestHelper.CreateManifestJsonService(), Mock.Of<IImageCacheService>(), Mock.Of<IManifestServiceFactory>(), Mock.Of<ILogger<GenerateBuildMatrixCommand>>());
new(
TestHelper.CreateManifestJsonService(),
new BuildPlanner(Mock.Of<ILogger<BuildPlanner>>()),
Mock.Of<IGitService>(),
Mock.Of<IManifestServiceFactory>(),
Mock.Of<ILogger<GenerateBuildMatrixCommand>>());
}
}
21 changes: 16 additions & 5 deletions src/ImageBuilder.Tests/GetStaleImagesCommandTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
Expand All @@ -12,6 +12,7 @@
using System.Text;
using System.Threading.Tasks;
using LibGit2Sharp;
using Microsoft.DotNet.ImageBuilder.Build;
using Microsoft.DotNet.ImageBuilder.Commands;
using Microsoft.DotNet.ImageBuilder.Models.Image;
using Microsoft.DotNet.ImageBuilder.Models.Manifest;
Expand Down Expand Up @@ -1373,6 +1374,7 @@ public async Task GetStaleImagesCommand_InternalFromOnly()
const string repo1 = "test-repo";
const string dockerfile1Path = "dockerfile1/Dockerfile";
const string dockerfile2Path = "dockerfile2/Dockerfile";
string parentDigest = $"sha256:{new string('0', 64)}";

SubscriptionInfo[] subscriptionInfos = new SubscriptionInfo[]
{
Expand All @@ -1398,11 +1400,15 @@ public async Task GetStaleImagesCommand_InternalFromOnly()
{
Platforms =
{
CreatePlatform(dockerfile1Path),
CreatePlatform(
dockerfile1Path,
baseImageDigest: $"{repo1}@{parentDigest}",
simpleTags: new List<string> { "tag1" }),
CreatePlatform(
dockerfile2Path,
digest: parentDigest,
baseImageDigest: "base1@base1digest",
simpleTags: new List<string> { "tag1" })
simpleTags: new List<string> { "tag2" })
}
}
}
Expand Down Expand Up @@ -1754,7 +1760,7 @@ public void Verify(IDictionary<Subscription, IList<string>> expectedPathsBySubsc
string[] actualPaths = pathsBySubscription
.First(imagePaths => imagePaths.SubscriptionId == kvp.Key.Id).ImagePaths;

actualPaths.ShouldBe(kvp.Value);
actualPaths.ShouldBe(kvp.Value, ignoreOrder: true);
}
}

Expand All @@ -1769,7 +1775,12 @@ private string SerializeJsonObjectToTempFile(object jsonObject)
private GetStaleImagesCommand CreateCommand()
{
GetStaleImagesCommand command = new(
this.ManifestServiceFactoryMock.Object, TestHelper.CreateManifestJsonService(), this.loggerServiceMock.Object, this.octokitClientFactory, this.gitService);
this.ManifestServiceFactoryMock.Object,
TestHelper.CreateManifestJsonService(),
this.loggerServiceMock.Object,
this.octokitClientFactory,
this.gitService,
new BuildPlanner(Mock.Of<ILogger<BuildPlanner>>()));
command.Options.SubscriptionOptions.SubscriptionsPath = this.subscriptionsPath;
command.Options.VariableName = VariableName;
command.Options.FilterOptions.Platform.OsType = this.osType;
Expand Down
Loading