Skip to content
164 changes: 164 additions & 0 deletions source/Calamari.Tests/ArgoCD/ArgoCDDeploymentReporterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System.Collections.Generic;
using Calamari.ArgoCD;
using Calamari.ArgoCD.Models;
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("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("gateway1", new ApplicationName("app1"))
{
TotalSourceCount = 2,
MatchingSourceCount = 2,
UpdatedSourceDetails =
{
new UpdatedSourceDetail("abc123", 0, [], [])
}
}
};

reporter.ReportDeployments(applicationResults);

log.ServiceMessages.Should().ContainSingle().Which.Should().BeEquivalentTo(new
{
Name = "argocd-deployment",
Properties = new Dictionary<string, string>
{
["gatewayId"] = "gateway1",
["applicationName"] = "app1",
["sources"] = "[{\"CommitSha\":\"abc123\",\"SourceIndex\":0,\"ReplacedFiles\":[],\"PatchedFiles\":[]}]"
}
});
}

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

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

reporter.ReportDeployments(applicationResults);

log.ServiceMessages.Should().BeEquivalentTo([
new
{
Name = "argocd-deployment",
Properties = new Dictionary<string, string>
{
["gatewayId"] = "gateway1",
["applicationName"] = "app1",
["sources"] = "[{\"CommitSha\":\"abc123\",\"SourceIndex\":0,\"ReplacedFiles\":[],\"PatchedFiles\":[]}]"
}
},
new
{
Name = "argocd-deployment",
Properties = new Dictionary<string, string>
{
["gatewayId"] = "gateway2",
["applicationName"] = "app2",
["sources"] = "[{\"CommitSha\":\"def456\",\"SourceIndex\":0,\"ReplacedFiles\":[],\"PatchedFiles\":[]}]"
}
}
]);
}

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

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

reporter.ReportDeployments(applicationResults);

log.ServiceMessages.Should().ContainSingle().Which.Should().BeEquivalentTo(new
{
Name = "argocd-deployment",
Properties = new Dictionary<string, string>
{
["gatewayId"] = "gateway2",
["applicationName"] = "app2",
["sources"] = "[{\"CommitSha\":\"abc123\",\"SourceIndex\":0,\"ReplacedFiles\":[],\"PatchedFiles\":[]}]"
}
});
}
}
}
Loading
Loading