Skip to content
Merged
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
57 changes: 57 additions & 0 deletions source/Calamari.Tests/AutofacRegistrationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Autofac;
using Autofac.Features.Metadata;
using Calamari.Commands;
using Calamari.Commands.Support;
using Calamari.Common.Plumbing.Commands;
using Calamari.Common.Plumbing.Logging;
using FluentAssertions;
using NUnit.Framework;

namespace Calamari.Tests;

[TestFixture]
public class AutofacRegistrationTests
{
class TestableProgram : Program
{
public TestableProgram() : base(ConsoleLog.Instance) { }

// Pin to the production assembly so the test assembly (and its StubCommand) isn't scanned
protected override Assembly GetProgramAssemblyToRegister() => typeof(Program).Assembly;

public IContainer BuildTestContainer()
{
var options = CommonOptions.Parse(["version"]);
var builder = new ContainerBuilder();
ConfigureContainer(builder, options);
return builder.Build();
}
}

[Test]
[Category("PlatformAgnostic")]
public void AllCommandsCanBeConstructed()
{
using var container = new TestableProgram().BuildTestContainer();

var commands = container.Resolve<IEnumerable<Meta<Lazy<ICommandWithArgs>, CommandMeta>>>();

var failures = new List<string>();
foreach (var command in commands)
{
try
{
var _ = command.Value.Value;
}
catch (Exception ex)
{
failures.Add($"'{command.Metadata.Name}': {ex.Message}");
}
}

failures.Should().BeEmpty("all commands must be constructable from the DI container");
}
}
4 changes: 2 additions & 2 deletions source/Calamari/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using IContainer = Autofac.IContainer;
using Calamari.Aws.Deployment;
using Calamari.Azure.Kubernetes.Discovery;
using Calamari.Deployment.PackageRetention;
using Calamari.Kubernetes;
using Calamari.Kubernetes.Commands.Executors;

Expand Down Expand Up @@ -104,8 +105,6 @@ protected override void ConfigureContainer(ContainerBuilder builder, CommonOptio
//Add decorator to commands with the RetentionLockingCommand attribute. Also need to include commands defined in external assemblies.
var assembliesToRegister = GetAllAssembliesToRegister().ToArray();

builder.RegisterAssemblyModules(assembliesToRegister);

builder.RegisterAssemblyTypes(assembliesToRegister)
.AssignableTo<IKubernetesDiscoverer>()
.As<IKubernetesDiscoverer>();
Expand All @@ -127,6 +126,7 @@ protected override void ConfigureContainer(ContainerBuilder builder, CommonOptio
.As<ILaunchTool>();

builder.RegisterModule<ArgoCDModule>();
builder.RegisterModule<PackageRetentionModule>();
}

IEnumerable<Assembly> GetExtensionAssemblies()
Expand Down