Skip to content

Commit e3d170c

Browse files
authored
Fixes to life cycle commands state (#9149)
1 parent f7a571e commit e3d170c

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/Aspire.Hosting/ApplicationModel/CommandsConfigurationExtensions.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal static void AddLifeCycleCommands(this IResource resource)
2929
updateState: context =>
3030
{
3131
var state = context.ResourceSnapshot.State?.Text;
32-
if (IsStarting(state) || IsRuntimeUnhealthy(state))
32+
if (IsStarting(state) || IsRuntimeUnhealthy(state) || HasNoState(state))
3333
{
3434
return ResourceCommandState.Disabled;
3535
}
@@ -66,7 +66,7 @@ internal static void AddLifeCycleCommands(this IResource resource)
6666
{
6767
return ResourceCommandState.Disabled;
6868
}
69-
else if (!IsStopped(state) && !IsStarting(state) && !IsWaiting(state) && !IsRuntimeUnhealthy(state) && context.ResourceSnapshot.State is not null)
69+
else if (!IsStopped(state) && !IsStarting(state) && !IsWaiting(state) && !IsRuntimeUnhealthy(state) && !HasNoState(state))
7070
{
7171
return ResourceCommandState.Enabled;
7272
}
@@ -96,7 +96,7 @@ internal static void AddLifeCycleCommands(this IResource resource)
9696
updateState: context =>
9797
{
9898
var state = context.ResourceSnapshot.State?.Text;
99-
if (IsStarting(state) || IsStopping(state) || IsStopped(state) || IsWaiting(state) || IsRuntimeUnhealthy(state) || context.ResourceSnapshot.State is null)
99+
if (IsStarting(state) || IsStopping(state) || IsStopped(state) || IsWaiting(state) || IsRuntimeUnhealthy(state) || HasNoState(state))
100100
{
101101
return ResourceCommandState.Disabled;
102102
}
@@ -119,5 +119,6 @@ internal static void AddLifeCycleCommands(this IResource resource)
119119
static bool IsStarting(string? state) => state == KnownResourceStates.Starting;
120120
static bool IsWaiting(string? state) => state == KnownResourceStates.Waiting;
121121
static bool IsRuntimeUnhealthy(string? state) => state == KnownResourceStates.RuntimeUnhealthy;
122+
static bool HasNoState(string? state) => string.IsNullOrEmpty(state);
122123
}
123124
}

tests/Aspire.Hosting.Tests/ResourceCommandAnnotationTests.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class ResourceCommandAnnotationTests
1919
[InlineData(KnownResourceCommands.StartCommand, "Unknown", ResourceCommandState.Enabled)]
2020
[InlineData(KnownResourceCommands.StartCommand, "Waiting", ResourceCommandState.Enabled)]
2121
[InlineData(KnownResourceCommands.StartCommand, "RuntimeUnhealthy", ResourceCommandState.Disabled)]
22+
[InlineData(KnownResourceCommands.StartCommand, "", ResourceCommandState.Disabled)]
23+
[InlineData(KnownResourceCommands.StartCommand, null, ResourceCommandState.Disabled)]
2224
[InlineData(KnownResourceCommands.StopCommand, "Starting", ResourceCommandState.Hidden)]
2325
[InlineData(KnownResourceCommands.StopCommand, "Stopping", ResourceCommandState.Disabled)]
2426
[InlineData(KnownResourceCommands.StopCommand, "Running", ResourceCommandState.Enabled)]
@@ -28,6 +30,8 @@ public class ResourceCommandAnnotationTests
2830
[InlineData(KnownResourceCommands.StopCommand, "Unknown", ResourceCommandState.Hidden)]
2931
[InlineData(KnownResourceCommands.StopCommand, "Waiting", ResourceCommandState.Hidden)]
3032
[InlineData(KnownResourceCommands.StopCommand, "RuntimeUnhealthy", ResourceCommandState.Hidden)]
33+
[InlineData(KnownResourceCommands.StopCommand, "", ResourceCommandState.Hidden)]
34+
[InlineData(KnownResourceCommands.StopCommand, null, ResourceCommandState.Hidden)]
3135
[InlineData(KnownResourceCommands.RestartCommand, "Starting", ResourceCommandState.Disabled)]
3236
[InlineData(KnownResourceCommands.RestartCommand, "Stopping", ResourceCommandState.Disabled)]
3337
[InlineData(KnownResourceCommands.RestartCommand, "Running", ResourceCommandState.Enabled)]
@@ -37,7 +41,9 @@ public class ResourceCommandAnnotationTests
3741
[InlineData(KnownResourceCommands.RestartCommand, "Unknown", ResourceCommandState.Disabled)]
3842
[InlineData(KnownResourceCommands.RestartCommand, "Waiting", ResourceCommandState.Disabled)]
3943
[InlineData(KnownResourceCommands.RestartCommand, "RuntimeUnhealthy", ResourceCommandState.Disabled)]
40-
public void LifeCycleCommands_CommandState(string commandName, string resourceState, ResourceCommandState commandState)
44+
[InlineData(KnownResourceCommands.RestartCommand, "", ResourceCommandState.Disabled)]
45+
[InlineData(KnownResourceCommands.RestartCommand, null, ResourceCommandState.Disabled)]
46+
public void LifeCycleCommands_CommandState(string commandName, string? resourceState, ResourceCommandState commandState)
4147
{
4248
// Arrange
4349
var builder = DistributedApplication.CreateBuilder();

0 commit comments

Comments
 (0)