Skip to content
180 changes: 180 additions & 0 deletions source/Calamari.Tests/ArgoCD/ArgoCDDeploymentReporterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Calamari.ArgoCD;
using Calamari.ArgoCD.Models;
using Calamari.Common.Plumbing.Logging;
using Calamari.Common.Plumbing.ServiceMessages;
using Calamari.Testing.Helpers;
using FluentAssertions;
using NUnit.Framework;

namespace Calamari.Tests.ArgoCD
{
[TestFixture]
public class ArgoCDDeploymentReporterTests
{
[Test]
public void ReportDeployments_WithNoUpdatedApplications_WritesNoServiceMessages()
{
var log = new InMemoryLog();
var reporter = new ArgoCDDeploymentReporter(log);

var applicationResults = new List<ProcessApplicationResult>
{
new ProcessApplicationResult("gateway1", new ApplicationName("app1"))
{
TotalSourceCount = 2,
MatchingSourceCount = 2
}
};

reporter.ReportDeployments(applicationResults);

var messages = log.ServiceMessages;
messages.Should().BeEmpty();
}

[Test]
public void ReportDeployments_WithSingleUpdatedApplication_WritesOneServiceMessage()
{
var log = new InMemoryLog();
var reporter = new ArgoCDDeploymentReporter(log);

var applicationResults = new List<ProcessApplicationResult>
{
new ProcessApplicationResult("gateway1", new ApplicationName("app1"))
{
TotalSourceCount = 2,
MatchingSourceCount = 2,
UpdatedSourceDetails =
{
new UpdatedSourceDetail("abc123", 0, [], [])
}
}
};

reporter.ReportDeployments(applicationResults);

var messages = log.ServiceMessages;
messages.Should().HaveCount(1);

var message = messages.First();
message.Name.Should().Be("argocd-deployment");
message.Properties["gatewayId"].Should().Be("gateway1");
message.Properties["applicationName"].Should().Be("app1");
}

[Test]
public void ReportDeployments_WithMultipleUpdatedApplications_WritesMultipleServiceMessages()
{
var log = new InMemoryLog();
var reporter = new ArgoCDDeploymentReporter(log);

var applicationResults = new List<ProcessApplicationResult>
{
new ProcessApplicationResult("gateway1", new ApplicationName("app1"))
{
TotalSourceCount = 2,
MatchingSourceCount = 2,
UpdatedSourceDetails =
{
new UpdatedSourceDetail("abc123", 0, [], [])
}
},
new ProcessApplicationResult("gateway2", new ApplicationName("app2"))
{
TotalSourceCount = 1,
MatchingSourceCount = 1,
UpdatedSourceDetails =
{
new UpdatedSourceDetail("def456", 0, [], [])
}
}
};

reporter.ReportDeployments(applicationResults);

var messages = log.ServiceMessages;
messages.Should().HaveCount(2);

messages[0].Properties["gatewayId"].Should().Be("gateway1");
messages[0].Properties["applicationName"].Should().Be("app1");

messages[1].Properties["gatewayId"].Should().Be("gateway2");
messages[1].Properties["applicationName"].Should().Be("app2");
}

[Test]
public void ReportDeployments_SerializesSourcesCorrectly()
{
var log = new InMemoryLog();
var reporter = new ArgoCDDeploymentReporter(log);

var applicationResults = new List<ProcessApplicationResult>
{
new ProcessApplicationResult("gateway1", new ApplicationName("app1"))
{
TotalSourceCount = 2,
MatchingSourceCount = 2,
UpdatedSourceDetails =
{
new UpdatedSourceDetail("abc123", 0, [], []),
new UpdatedSourceDetail("def456", 1, [], [])
}
}
};

reporter.ReportDeployments(applicationResults);

var messages = log.ServiceMessages;
messages.Should().HaveCount(1);

var sourcesJson = messages.First().Properties["sources"];
var sources = JsonSerializer.Deserialize<List<UpdatedSourceDetail>>(sourcesJson);

sources.Should().HaveCount(2);
sources[0].CommitSha.Should().Be("abc123");
sources[0].SourceIndex.Should().Be(0);
sources[1].CommitSha.Should().Be("def456");
sources[1].SourceIndex.Should().Be(1);
}

[Test]
public void ReportDeployments_WithMixedUpdatedAndNonUpdatedApplications_WritesOnlyUpdatedMessages()
{
var log = new InMemoryLog();
var reporter = new ArgoCDDeploymentReporter(log);

var applicationResults = new List<ProcessApplicationResult>
{
new ProcessApplicationResult("gateway1", new ApplicationName("app1"))
{
TotalSourceCount = 2,
MatchingSourceCount = 2
},
new ProcessApplicationResult("gateway2", new ApplicationName("app2"))
{
TotalSourceCount = 1,
MatchingSourceCount = 1,
UpdatedSourceDetails =
{
new UpdatedSourceDetail("abc123", 0, [], [])
}
},
new ProcessApplicationResult("gateway3", new ApplicationName("app3"))
{
TotalSourceCount = 1,
MatchingSourceCount = 1
}
};

reporter.ReportDeployments(applicationResults);

var messages = log.ServiceMessages;
messages.Should().HaveCount(1);
messages[0].Properties["gatewayId"].Should().Be("gateway2");
messages[0].Properties["applicationName"].Should().Be("app2");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public void HelmSource_NoPath_DontUpdate()
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[PackageVariables.IndexedImage("nginx")] = "index.docker.io/nginx:1.27.1",
Expand Down Expand Up @@ -199,7 +200,8 @@ public void NoImages_DontUpdate()
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[ProjectVariables.Slug] = ProjectSlug,
Expand Down Expand Up @@ -237,7 +239,8 @@ public void NoAnnotations_DontUpdate()
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[ProjectVariables.Slug] = ProjectSlug,
Expand Down Expand Up @@ -284,7 +287,8 @@ public void HelmSourceWithMultipleMatchesInSameValuesFile_Update()
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[ProjectVariables.Slug] = ProjectSlug,
Expand Down Expand Up @@ -372,7 +376,8 @@ public void SingleSourceWithWrongScoping_DontUpdate()
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[ProjectVariables.Slug] = ProjectSlug,
Expand Down Expand Up @@ -462,7 +467,8 @@ public void RefSourceWithWrongScoping_DontUpdate()
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[ProjectVariables.Slug] = ProjectSlug,
Expand Down Expand Up @@ -546,7 +552,8 @@ public void HelmSourceWithExplicitValuesFile_MissingImagePath_WarnAndDontUpdate(
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[ProjectVariables.Slug] = ProjectSlug,
Expand Down Expand Up @@ -625,7 +632,8 @@ public void HelmSourceWithImplicitValuesFile_MissingImagePath_WarnAndDontUpdate(
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[ProjectVariables.Slug] = ProjectSlug,
Expand Down Expand Up @@ -716,7 +724,8 @@ public void RefSourceWithMissingImagePath_WarnAndDontUpdate()
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[ProjectVariables.Slug] = ProjectSlug,
Expand Down Expand Up @@ -806,7 +815,8 @@ public void RefSourceWithMissingImagePathIfRefSourceNotInScope_DontWarnAndDontUp
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[ProjectVariables.Slug] = ProjectSlug,
Expand Down Expand Up @@ -841,7 +851,8 @@ public void HelmSourceWithHelmConfigurationAndImplicitValuesFile_IncludeValuesFi
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[PackageVariables.IndexedImage("nginx")] = "index.docker.io/nginx:1.27.1",
Expand Down Expand Up @@ -943,7 +954,8 @@ public void HelmSourceWithHelmConfigurationAndNoImplicitValuesFile_ExcludeValues
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[PackageVariables.IndexedImage("nginx")] = "index.docker.io/nginx:1.27.1",
Expand Down Expand Up @@ -1022,7 +1034,8 @@ public void HelmSourceWithHelmConfigurationIncludesImplicitValuesFile_IncludeVal
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[PackageVariables.IndexedImage("nginx")] = "index.docker.io/nginx:1.27.1",
Expand Down Expand Up @@ -1124,7 +1137,8 @@ public void HelmSourceWithImplicitValuesFile_Update()
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[PackageVariables.IndexedImage("nginx")] = "index.docker.io/nginx:1.27.1",
Expand Down Expand Up @@ -1204,7 +1218,8 @@ public void RefSourceWithHelmImageMatches_Update()
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[PackageVariables.IndexedImage("nginx")] = "index.docker.io/nginx:1.27.1",
Expand Down Expand Up @@ -1299,7 +1314,8 @@ public void RefSourceWithHelmImageMatchesAndPath_IgnoresFilesUnderPath()
customPropertiesLoader,
argoCdApplicationManifestParser,
Substitute.For<IGitVendorAgnosticApiAdapterFactory>(),
new SystemClock());
new SystemClock(),
Substitute.For<IArgoCDDeploymentReporter>());
var variables = new CalamariVariables
{
[PackageVariables.IndexedImage("nginx")] = "index.docker.io/nginx:1.27.1",
Expand Down
Loading