feat: 设置与保留期清理(里程碑 5)
- settings.yaml:审计开关(默认开)+ 保留天数(默认 90、最小 7),FormKit 声明式 - plugin.yaml 声明 settingName/configMapName(源码级核对 Plugin.spec 字段名) - AuditSettingProvider:60s TTL 缓存读取(D6),失败/缺省回退默认且默认值同样入缓存 - AuditRecorder 写入前检查开关;设置故障按默认配置继续记录(审计设置故障不影响捕获) - RetentionCleaner:Reactor 调度(风险项 #3 预案,未验证 @Scheduled 不赌插件上下文), 启动补跑一次 + 每日 03:00;按时间索引升序每批 200 循环至不足一批(D5) - 插件 start/stop 联动清理器生命周期 - 新增 17 个测试:TTL 命中/过期重取/降级、批量删除/边界/调度时机、开关跳过
This commit is contained in:
+50
-27
@@ -4,30 +4,51 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import reactor.core.publisher.Mono;
|
||||
import run.halo.app.extension.ReactiveExtensionClient;
|
||||
import run.halo.operationaudit.housekeeping.AuditSettingProvider;
|
||||
import run.halo.operationaudit.housekeeping.AuditSettings;
|
||||
|
||||
/**
|
||||
* 直写降级测试(技术方案 D2):成功直写、失败降级为 warn 且不抛出。
|
||||
* 直写降级测试(技术方案 D2):成功直写、失败降级为 warn 且不抛出、开关关闭跳过(D6)。
|
||||
*/
|
||||
class AuditRecorderTest {
|
||||
|
||||
private final ReactiveExtensionClient client = mock(ReactiveExtensionClient.class);
|
||||
private final AuditRecorder recorder = new AuditRecorder(client);
|
||||
private ReactiveExtensionClient client;
|
||||
private AuditSettingProvider settingProvider;
|
||||
private AuditRecorder recorder;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
client = mock(ReactiveExtensionClient.class);
|
||||
settingProvider = mock(AuditSettingProvider.class);
|
||||
recorder = new AuditRecorder(client, settingProvider);
|
||||
when(settingProvider.get()).thenReturn(Mono.just(AuditSettings.defaults()));
|
||||
when(client.create(any(AuditLog.class))).thenReturn(Mono.just(new AuditLog()));
|
||||
}
|
||||
|
||||
private RequestSnapshot snapshotOf(String url) {
|
||||
return RequestSnapshot.of(MockServerHttpRequest.post("http://localhost" + url)
|
||||
.remoteAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 45678))
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAssembleAndCreateAuditLog() {
|
||||
when(client.create(any(AuditLog.class))).thenReturn(Mono.just(new AuditLog()));
|
||||
var snapshot = RequestSnapshot.of(
|
||||
org.springframework.mock.http.server.reactive.MockServerHttpRequest
|
||||
MockServerHttpRequest
|
||||
.delete("http://localhost/apis/api.content.halo.run/v1alpha1/posts/xyz?foo=bar")
|
||||
.remoteAddress(new java.net.InetSocketAddress(
|
||||
java.net.InetAddress.getLoopbackAddress(), 45678))
|
||||
.remoteAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 45678))
|
||||
.header("User-Agent", "UA-1")
|
||||
.build());
|
||||
|
||||
@@ -52,12 +73,7 @@ class AuditRecorderTest {
|
||||
|
||||
@Test
|
||||
void shouldUseUnknownUsernameWhenOperatorMissing() {
|
||||
when(client.create(any(AuditLog.class))).thenReturn(Mono.just(new AuditLog()));
|
||||
var snapshot = RequestSnapshot.of(
|
||||
org.springframework.mock.http.server.reactive.MockServerHttpRequest
|
||||
.post("http://localhost/login").build());
|
||||
|
||||
recorder.record(snapshot, 401, null);
|
||||
recorder.record(snapshotOf("/login"), 401, null);
|
||||
|
||||
var captor = ArgumentCaptor.forClass(AuditLog.class);
|
||||
verify(client).create(captor.capture());
|
||||
@@ -68,24 +84,31 @@ class AuditRecorderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotThrowWhenCreateFails() {
|
||||
when(client.create(any(AuditLog.class))).thenReturn(Mono.error(
|
||||
new IllegalStateException("storage unavailable")));
|
||||
var snapshot = RequestSnapshot.of(
|
||||
org.springframework.mock.http.server.reactive.MockServerHttpRequest
|
||||
.post("http://localhost/apis/core.halo.run/v1alpha1/users").build());
|
||||
void shouldSkipRecordingWhenAuditDisabled() {
|
||||
when(settingProvider.get()).thenReturn(Mono.just(new AuditSettings(false, 30)));
|
||||
|
||||
assertThatCode(() -> recorder.record(snapshot, 500, "admin")).doesNotThrowAnyException();
|
||||
recorder.record(snapshotOf("/apis/core.halo.run/v1alpha1/users"), 200, "admin");
|
||||
|
||||
verify(client, never()).create(any(AuditLog.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotThrowWhenAssemblyFails() {
|
||||
// path 为 null 会在资源解析前返回 other,不会抛异常;这里模拟客户端抛异常场景
|
||||
when(client.create(any(AuditLog.class))).thenThrow(new IllegalStateException("boom"));
|
||||
var snapshot = RequestSnapshot.of(
|
||||
org.springframework.mock.http.server.reactive.MockServerHttpRequest
|
||||
.post("http://localhost/apis/core.halo.run/v1alpha1/users").build());
|
||||
void shouldSkipRecordingWhenSettingsUnavailable() {
|
||||
when(settingProvider.get()).thenReturn(Mono.error(new IllegalStateException("boom")));
|
||||
|
||||
assertThatCode(() -> recorder.record(snapshot, 200, "admin")).doesNotThrowAnyException();
|
||||
assertThatCode(() -> recorder.record(snapshotOf("/apis/core.halo.run/v1alpha1/users"),
|
||||
200, "admin")).doesNotThrowAnyException();
|
||||
|
||||
// 设置读取失败属于降级路径:按默认配置(开启)继续记录
|
||||
verify(client).create(any(AuditLog.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotThrowWhenCreateFails() {
|
||||
when(client.create(any(AuditLog.class))).thenReturn(Mono.error(
|
||||
new IllegalStateException("storage unavailable")));
|
||||
|
||||
assertThatCode(() -> recorder.record(snapshotOf("/apis/core.halo.run/v1alpha1/users"),
|
||||
500, "admin")).doesNotThrowAnyException();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user