244032f7a9
Restart… and Shut Down… in the device, selection and group menus start a
run of a built-in script (never stored) with Restart After Run on, so the
delay, the warning, cancel-while-scheduled, AllowRestart and the run's
targets, exclusions, Only Devices Online Now (on by default) and stagger
spread all come from the existing pipeline. POST /api/power, permission
devices.power (Admin, Operator, Help Desk).
Install Pending Updates (Windows, off by default) passes
SHUTDOWN_INSTALL_UPDATES only when asked; off, updates waiting for
"Update and restart" stay waiting. Shutting down is SHUTDOWN_POWEROFF on
Windows (wpeutil shutdown on PE) and logind PowerOff / ScheduleShutdown
("poweroff") / poweroff.target on Linux. Console actions give Windows a
planned Other: Maintenance reason. Shut down and updates need agent
1.14.0: older agents get their job failed instead of restarting.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
7.3 KiB
C#
142 lines
7.3 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using Backend.Jobs;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Shared.Contracts.Api;
|
|
using Shared.Contracts.Messages;
|
|
using Shared.Contracts.Models;
|
|
using Shared.Contracts.Serialization;
|
|
using Xunit;
|
|
|
|
namespace Backend.Tests;
|
|
|
|
/// <summary>
|
|
/// Restart and Shut Down: a run of the built-in power script with Restart After Run on, the
|
|
/// spread of any run, and never a shutdown (or updates) sent to an agent that would restart instead.
|
|
/// </summary>
|
|
public class PowerTests(BackendTestFixture fixture) : IClassFixture<BackendTestFixture>
|
|
{
|
|
private const string Old = "1.13.0";
|
|
|
|
private static Task<HttpResponseMessage> PostAsync(HttpClient client, PowerRequest request)
|
|
=> client.PostAsJsonAsync(ApiRoutes.Power, request, JsonDefaults.Options);
|
|
|
|
private static async Task<GroupRunDto> PowerAsync(HttpClient client, PowerRequest request)
|
|
{
|
|
var response = await PostAsync(client, request);
|
|
response.EnsureSuccessStatusCode();
|
|
return (await response.Content.ReadFromJsonAsync<GroupRunDto>(JsonDefaults.Options))!;
|
|
}
|
|
|
|
private List<ScriptCommandPayload> Sent(string clientId)
|
|
{
|
|
lock (fixture.Scripts.Sent)
|
|
{
|
|
return fixture.Scripts.Sent.Where(s => s.ClientId == clientId).Select(s => s.Command).ToList();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ARestartIsARunOfTheBuiltInScriptWithRestartAfterRunAlways()
|
|
{
|
|
fixture.Scripts.Accept = true;
|
|
using var client = await fixture.SignedInAsync("power-admin");
|
|
var device = await fixture.SeedDeviceAsync("POWER-RESTART", agentVersion: Old);
|
|
|
|
var run = await PowerAsync(client, new PowerRequest(PowerAction.Restart, [device], DelaySeconds: 120, WarningSeconds: 60));
|
|
|
|
var job = Assert.Single(run.Jobs);
|
|
Assert.Equal((PowerActions.RestartScriptId, PowerActions.RestartName), (job.ScriptId, job.ScriptName));
|
|
Assert.NotNull(run.RunId);
|
|
var sent = Sent(device).Single();
|
|
Assert.Equal(
|
|
(RestartAfterRun.Always, 120, 60, PowerAction.Restart, false),
|
|
(sent.RestartAfterRun, sent.RestartDelaySeconds, sent.RestartWarningSeconds, sent.RestartAction, sent.RestartInstallsUpdates));
|
|
Assert.Equal(PowerActions.RestartScriptId, sent.ScriptId);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ShuttingDownGoesOnlyToAgentsThatCanAndOlderOnesGetAFailedJob()
|
|
{
|
|
fixture.Scripts.Accept = true;
|
|
using var client = await fixture.SignedInAsync("power-admin");
|
|
var current = await fixture.SeedDeviceAsync("POWER-OFF-NEW", agentVersion: PowerActions.MinimumAgentVersion);
|
|
var old = await fixture.SeedDeviceAsync("POWER-OFF-OLD", agentVersion: Old);
|
|
|
|
var run = await PowerAsync(client, new PowerRequest(PowerAction.ShutDown, [current, old], InstallUpdates: true));
|
|
|
|
Assert.Equal(2, run.Jobs.Count);
|
|
var sent = Sent(current).Single();
|
|
Assert.Equal((PowerAction.ShutDown, true), (sent.RestartAction, sent.RestartInstallsUpdates));
|
|
Assert.Empty(Sent(old));
|
|
var refused = await fixture.WithDbAsync(db => db.Jobs.AsNoTracking().SingleAsync(j => j.ClientId == old && j.RunId == run.RunId));
|
|
Assert.Equal(JobState.Failed, refused.State);
|
|
Assert.Contains("cannot shut down", refused.OutputSummary);
|
|
Assert.Contains(PowerActions.MinimumAgentVersion, refused.OutputSummary);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task InstallingUpdatesNeedsACurrentAgentButAPlainRestartDoesNot()
|
|
{
|
|
Assert.Null(PowerActions.Refusal(PowerAction.Restart, false, Old));
|
|
Assert.Contains("install updates", PowerActions.Refusal(PowerAction.Restart, true, Old));
|
|
Assert.Null(PowerActions.Refusal(PowerAction.ShutDown, true, PowerActions.MinimumAgentVersion));
|
|
Assert.NotNull(PowerActions.Refusal(PowerAction.ShutDown, false, null));
|
|
|
|
fixture.Scripts.Accept = true;
|
|
using var client = await fixture.SignedInAsync("power-admin");
|
|
var old = await fixture.SeedDeviceAsync("POWER-UPDATES-OLD", agentVersion: Old);
|
|
await PowerAsync(client, new PowerRequest(PowerAction.Restart, [old], InstallUpdates: true));
|
|
Assert.Empty(Sent(old));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ASpreadShutdownIsSentByTheDispatcherFromTheRunsSnapshot()
|
|
{
|
|
fixture.Scripts.Accept = true;
|
|
using var client = await fixture.SignedInAsync("power-admin");
|
|
var current = await fixture.SeedDeviceAsync("POWER-SPREAD-NEW", agentVersion: PowerActions.MinimumAgentVersion);
|
|
var old = await fixture.SeedDeviceAsync("POWER-SPREAD-OLD", agentVersion: Old);
|
|
|
|
var run = await PowerAsync(client, new PowerRequest(
|
|
PowerAction.ShutDown, [current, old], DelaySeconds: 30, StaggerSeconds: 600, StaggerBatchSize: 1));
|
|
Assert.True(Sent(current).Count + Sent(old).Count <= 1); // only the first slot may be due now
|
|
|
|
var latest = await fixture.WithDbAsync(db => db.Jobs.AsNoTracking().Where(j => j.RunId == run.RunId).MaxAsync(j => j.NotBeforeUtc));
|
|
await new JobDispatcher(fixture.Services.GetRequiredService<IServiceScopeFactory>(), NullLogger<JobDispatcher>.Instance)
|
|
.RunPassAsync(latest!.Value.AddSeconds(1), CancellationToken.None);
|
|
|
|
var sent = Sent(current).Single();
|
|
Assert.Equal((PowerAction.ShutDown, RestartAfterRun.Always, 30), (sent.RestartAction, sent.RestartAfterRun, sent.RestartDelaySeconds));
|
|
Assert.Empty(Sent(old));
|
|
var refused = await fixture.WithDbAsync(db => db.Jobs.AsNoTracking().SingleAsync(j => j.RunId == run.RunId && j.ClientId == old));
|
|
Assert.Equal(JobState.Failed, refused.State);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ItNeedsThePowerPermissionAndValidTargetsAndTimes()
|
|
{
|
|
using var viewer = await fixture.SignedInAsync("power-viewer", "Viewer");
|
|
var device = await fixture.SeedDeviceAsync("POWER-PERM");
|
|
Assert.Equal(HttpStatusCode.Forbidden, (await PostAsync(viewer, new PowerRequest(PowerAction.Restart, [device]))).StatusCode);
|
|
|
|
using var client = await fixture.SignedInAsync("power-admin");
|
|
Assert.Equal(HttpStatusCode.BadRequest, (await PostAsync(client, new PowerRequest(PowerAction.Restart))).StatusCode);
|
|
Assert.Equal(HttpStatusCode.BadRequest, (await PostAsync(client, new PowerRequest(PowerAction.Restart, [device], DelaySeconds: 3601))).StatusCode);
|
|
Assert.Equal(HttpStatusCode.BadRequest, (await PostAsync(client, new PowerRequest(PowerAction.Restart, [device], WarningSeconds: 5))).StatusCode);
|
|
Assert.Equal(HttpStatusCode.BadRequest, (await PostAsync(client, new PowerRequest(PowerAction.Restart, [device], StaggerSeconds: -1))).StatusCode);
|
|
Assert.Equal(HttpStatusCode.BadRequest, (await PostAsync(client, new PowerRequest((PowerAction)7, [device]))).StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public void OperatorsAndHelpDeskMayRestartAndViewersMayNot()
|
|
{
|
|
Assert.Contains(Backend.Auth.Permissions.DevicesPower, Backend.Auth.Permissions.OperatorSet);
|
|
Assert.Contains(Backend.Auth.Permissions.DevicesPower, Backend.Auth.Permissions.HelpDeskSet);
|
|
Assert.DoesNotContain(Backend.Auth.Permissions.DevicesPower, Backend.Auth.Permissions.ViewerSet);
|
|
Assert.Contains(Backend.Auth.Permissions.DevicesPower, Backend.Auth.Permissions.KnownKeys);
|
|
}
|
|
}
|