Skip to content

Commit d83b9f3

Browse files
committed
Add more tests
1 parent d1f6818 commit d83b9f3

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed

src/Microsoft.ComponentDetection.Detectors/linux/LinuxScanner.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ public async Task<IEnumerable<LayerMappedLinuxComponents>> ScanLinuxAsync(
112112
var stdout = string.Empty;
113113
var stderr = string.Empty;
114114

115+
var scopeParameters = scope switch
116+
{
117+
LinuxScannerScope.AllLayers => ScopeAllLayersParameter,
118+
LinuxScannerScope.Squashed => ScopeSquashedParameter,
119+
_ => throw new ArgumentOutOfRangeException(
120+
nameof(scope),
121+
$"Unsupported scope value: {scope}"
122+
),
123+
};
124+
115125
using var syftTelemetryRecord = new LinuxScannerSyftTelemetryRecord();
116126

117127
try
@@ -123,17 +133,7 @@ public async Task<IEnumerable<LayerMappedLinuxComponents>> ScanLinuxAsync(
123133
{
124134
var command = new List<string> { imageHash }
125135
.Concat(CmdParameters)
126-
.Concat(
127-
scope switch
128-
{
129-
LinuxScannerScope.AllLayers => ScopeAllLayersParameter,
130-
LinuxScannerScope.Squashed => ScopeSquashedParameter,
131-
_ => throw new ArgumentOutOfRangeException(
132-
nameof(scope),
133-
$"Unsupported scope value: {scope}"
134-
),
135-
}
136-
)
136+
.Concat(scopeParameters)
137137
.ToList();
138138
(stdout, stderr) = await this.dockerService.CreateAndRunContainerAsync(
139139
ScannerImage,

test/Microsoft.ComponentDetection.Detectors.Tests/LinuxScannerTests.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#nullable disable
22
namespace Microsoft.ComponentDetection.Detectors.Tests;
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Threading;
@@ -729,4 +730,61 @@ public async Task TestLinuxScanner_FiltersComponentsByEnabledTypes_OnlyNpmAndPip
729730
var pipComponent = allComponents.OfType<PipComponent>().Single();
730731
pipComponent.Name.Should().Be("requests");
731732
}
733+
734+
[TestMethod]
735+
[DataRow(LinuxScannerScope.AllLayers, "all-layers")]
736+
[DataRow(LinuxScannerScope.Squashed, "squashed")]
737+
public async Task TestLinuxScanner_ScopeParameter_IncludesCorrectFlagAsync(
738+
LinuxScannerScope scope,
739+
string expectedFlag
740+
)
741+
{
742+
this.mockDockerService.Setup(service =>
743+
service.CreateAndRunContainerAsync(
744+
It.IsAny<string>(),
745+
It.IsAny<List<string>>(),
746+
It.IsAny<CancellationToken>()
747+
)
748+
)
749+
.ReturnsAsync((SyftOutputNoAuthorOrLicense, string.Empty));
750+
751+
var enabledTypes = new HashSet<ComponentType> { ComponentType.Linux };
752+
await this.linuxScanner.ScanLinuxAsync(
753+
"fake_hash",
754+
[new DockerLayer { LayerIndex = 0, DiffId = "sha256:layer1" }],
755+
0,
756+
enabledTypes,
757+
scope
758+
);
759+
760+
this.mockDockerService.Verify(
761+
service =>
762+
service.CreateAndRunContainerAsync(
763+
It.IsAny<string>(),
764+
It.Is<List<string>>(cmd =>
765+
cmd.Contains("--scope") && cmd.Contains(expectedFlag)
766+
),
767+
It.IsAny<CancellationToken>()
768+
),
769+
Times.Once
770+
);
771+
}
772+
773+
[TestMethod]
774+
public async Task TestLinuxScanner_InvalidScopeParameter_ThrowsArgumentOutOfRangeExceptionAsync()
775+
{
776+
var enabledTypes = new HashSet<ComponentType> { ComponentType.Linux };
777+
var invalidScope = (LinuxScannerScope)999; // Invalid enum value
778+
779+
Func<Task> action = async () =>
780+
await this.linuxScanner.ScanLinuxAsync(
781+
"fake_hash",
782+
[new DockerLayer { LayerIndex = 0, DiffId = "sha256:layer1" }],
783+
0,
784+
enabledTypes,
785+
invalidScope
786+
);
787+
788+
await action.Should().ThrowAsync<ArgumentOutOfRangeException>();
789+
}
732790
}

0 commit comments

Comments
 (0)