@Override public Map<String, Object> asJson() { LinkedHashMap<String, Object> json = new LinkedHashMap<>(); ServerHealthStates allLogs = service.logs(); json.put("Messages Count", allLogs.size()); ArrayList<Map<String, String>> messages = new ArrayList<>(); for (ServerHealthState log : allLogs) { messages.add(log.asJson()); } json.put("Messages", messages); return json; }
@Test public void shouldRemoveErrorLogWhenCorrespondingGroupIsMissing() throws Exception { serverHealthService.update(ServerHealthState.error("message", "description", groupId)); serverHealthService.purgeStaleHealthMessages(new BasicCruiseConfig()); assertThat(serverHealthService.logs().size(), is(0)); }
@Test public void shouldAddMultipleLogToServerHealth() throws Exception { assertThat(serverHealthService.update(ServerHealthState.error("message", "description", globalId)), is(globalId)); assertThat(serverHealthService.update(ServerHealthState.error("message", "description", pipelineId)), is(pipelineId)); assertThat(serverHealthService.logs().size(), is(2)); assertThat(serverHealthService, ServerHealthMatcher.containsState(globalId)); assertThat(serverHealthService, ServerHealthMatcher.containsState(pipelineId)); }
@Test public void shouldRemoveLogByCategoryFromServerHealth() throws Exception { HealthStateScope scope = forPipeline(PIPELINE_NAME); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(scope))); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.invalidLicense(scope))); serverHealthService.update(ServerHealthState.error("message", "description", globalId)); assertThat(serverHealthService.logs().size(), is(3)); serverHealthService.removeByScope(scope); assertThat(serverHealthService.logs().size(), is(1)); assertThat(serverHealthService, ServerHealthMatcher.containsState(globalId)); }
@Test public void shouldUpdateLogInServerHealth() throws Exception { ServerHealthState serverHealthState = ServerHealthState.error("message", "description", globalId); serverHealthService.update(serverHealthState); ServerHealthState newServerHealthState = ServerHealthState.error("updated message", "updated description", globalId); serverHealthService.update(newServerHealthState); assertThat(serverHealthService.logs().size(), is(1)); assertThat(serverHealthService, ServerHealthMatcher.containsState(globalId, HealthStateLevel.ERROR, "updated message")); }
@Test public void shouldReturnErrorLogs() throws Exception { serverHealthService.update(ServerHealthState.error("message", "description", pipelineId)); CruiseConfig cruiseConfig = new BasicCruiseConfig(); new GoConfigMother().addPipeline(cruiseConfig, PIPELINE_NAME, "stageName", "jon"); serverHealthService.purgeStaleHealthMessages(cruiseConfig); assertThat(serverHealthService.logs().size(), is(1)); }
@Test public void shouldRemoveErrorLogWhenCorrespondingPipelineIsMissing() throws Exception { serverHealthService.update(ServerHealthState.error("message", "description", pipelineId)); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(forPipeline("other")))); serverHealthService.purgeStaleHealthMessages(new BasicCruiseConfig()); assertThat(serverHealthService.logs().size(), is(0)); }
@Test public void shouldRemoveExpiredLogMessages() throws Exception { testingClock.setTime(new DateTime(2002,10,10,10,10,10,10)); ServerHealthState expiresInNintySecs = warning("hg-message1", "description", HealthStateType.databaseDiskFull(), Timeout.NINETY_SECONDS); ServerHealthState expiresInThreeMins = warning("hg-message2", "description", HealthStateType.artifactsDirChanged(), Timeout.THREE_MINUTES); ServerHealthState expiresNever = warning("hg-message3", "description", HealthStateType.artifactsDiskFull(), Timeout.NEVER); serverHealthService.update(expiresInThreeMins); serverHealthService.update(expiresInNintySecs); serverHealthService.update(expiresNever); serverHealthService.purgeStaleHealthMessages(new BasicCruiseConfig()); ServerHealthStates logs = serverHealthService.logs(); assertThat(logs.size(), is(3)); assertThat(logs, hasItems(expiresInThreeMins,expiresInNintySecs, expiresNever)); testingClock.addSeconds(100); serverHealthService.purgeStaleHealthMessages(new BasicCruiseConfig()); logs = serverHealthService.logs(); assertThat(logs.size(), is(2)); assertThat(logs,hasItems(expiresInThreeMins, expiresNever)); testingClock.addMillis((int) Timeout.TWO_MINUTES.inMillis()); serverHealthService.purgeStaleHealthMessages(new BasicCruiseConfig()); logs = serverHealthService.logs(); assertThat(logs.size(), is(1)); assertThat(logs,hasItem(expiresNever)); }
@Test public void shouldRemoveErrorLogWhenCorrespondingMaterialIsMissing() throws Exception { serverHealthService.update(ServerHealthState.error("hg-message", "description", HealthStateType.general(forMaterial(MaterialsMother.hgMaterial())))); SvnMaterialConfig svnMaterialConfig = MaterialConfigsMother.svnMaterialConfig(); serverHealthService.update(ServerHealthState.error("svn-message", "description", HealthStateType.general(forMaterialConfig(svnMaterialConfig)))); CruiseConfig cruiseConfig = new BasicCruiseConfig(); cruiseConfig.addPipeline("defaultGroup", new PipelineConfig(new CaseInsensitiveString("dev"), new MaterialConfigs(svnMaterialConfig), new StageConfig(new CaseInsensitiveString("first"), new JobConfigs()))); serverHealthService.purgeStaleHealthMessages(cruiseConfig); assertThat(serverHealthService.logs().size(), is(1)); }