|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace Aspire.Hosting.Tests; |
| 7 | + |
| 8 | +public class ResourceCommandAnnotationTests |
| 9 | +{ |
| 10 | + [Fact] |
| 11 | + public void AddContainer_HasKnownCommandAnnotations() |
| 12 | + { |
| 13 | + HasKnownCommandAnnotationsCore(builder => builder.AddContainer("name", "image")); |
| 14 | + } |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public void AddProject_HasKnownCommandAnnotations() |
| 18 | + { |
| 19 | + HasKnownCommandAnnotationsCore(builder => builder.AddProject("name", "path", o => o.ExcludeLaunchProfile = true)); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void AddExecutable_HasKnownCommandAnnotations() |
| 24 | + { |
| 25 | + HasKnownCommandAnnotationsCore(builder => builder.AddExecutable("name", "command", "workingDirectory")); |
| 26 | + } |
| 27 | + |
| 28 | + [Theory] |
| 29 | + [InlineData(CommandsConfigurationExtensions.StartType, "Starting", ResourceCommandState.Disabled)] |
| 30 | + [InlineData(CommandsConfigurationExtensions.StartType, "Stopping", ResourceCommandState.Hidden)] |
| 31 | + [InlineData(CommandsConfigurationExtensions.StartType, "Running", ResourceCommandState.Hidden)] |
| 32 | + [InlineData(CommandsConfigurationExtensions.StartType, "Exited", ResourceCommandState.Enabled)] |
| 33 | + [InlineData(CommandsConfigurationExtensions.StopType, "Starting", ResourceCommandState.Hidden)] |
| 34 | + [InlineData(CommandsConfigurationExtensions.StopType, "Stopping", ResourceCommandState.Disabled)] |
| 35 | + [InlineData(CommandsConfigurationExtensions.StopType, "Running", ResourceCommandState.Enabled)] |
| 36 | + [InlineData(CommandsConfigurationExtensions.StopType, "Exited", ResourceCommandState.Hidden)] |
| 37 | + [InlineData(CommandsConfigurationExtensions.RestartType, "Starting", ResourceCommandState.Disabled)] |
| 38 | + [InlineData(CommandsConfigurationExtensions.RestartType, "Stopping", ResourceCommandState.Disabled)] |
| 39 | + [InlineData(CommandsConfigurationExtensions.RestartType, "Running", ResourceCommandState.Enabled)] |
| 40 | + [InlineData(CommandsConfigurationExtensions.RestartType, "Exited", ResourceCommandState.Disabled)] |
| 41 | + public void LifeCycleCommands_CommandState(string commandType, string resourceState, ResourceCommandState commandState) |
| 42 | + { |
| 43 | + // Arrange |
| 44 | + var builder = DistributedApplication.CreateBuilder(); |
| 45 | + var resourceBuilder = builder.AddContainer("name", "image"); |
| 46 | + |
| 47 | + var startCommand = resourceBuilder.Resource.Annotations.OfType<ResourceCommandAnnotation>().Single(a => a.Type == commandType); |
| 48 | + |
| 49 | + // Act |
| 50 | + var state = startCommand.UpdateState(new UpdateCommandStateContext |
| 51 | + { |
| 52 | + ResourceSnapshot = new CustomResourceSnapshot |
| 53 | + { |
| 54 | + Properties = [], |
| 55 | + ResourceType = "test", |
| 56 | + State = resourceState |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + // Assert |
| 61 | + Assert.Equal(commandState, state); |
| 62 | + } |
| 63 | + |
| 64 | + private static void HasKnownCommandAnnotationsCore<T>(Func<IDistributedApplicationBuilder, IResourceBuilder<T>> createResourceBuilder) where T : IResource |
| 65 | + { |
| 66 | + // Arrange |
| 67 | + var builder = DistributedApplication.CreateBuilder(); |
| 68 | + |
| 69 | + // Act |
| 70 | + var resourceBuilder = createResourceBuilder(builder); |
| 71 | + |
| 72 | + // Assert |
| 73 | + var commandAnnotations = resourceBuilder.Resource.Annotations.OfType<ResourceCommandAnnotation>().ToList(); |
| 74 | + Assert.Collection(commandAnnotations, |
| 75 | + a => Assert.Equal(CommandsConfigurationExtensions.StartType, a.Type), |
| 76 | + a => Assert.Equal(CommandsConfigurationExtensions.StopType, a.Type), |
| 77 | + a => Assert.Equal(CommandsConfigurationExtensions.RestartType, a.Type)); |
| 78 | + } |
| 79 | +} |
0 commit comments