KubeMonitorJob implements Job { private void publishError(Throwable e) { var message = new StatMessage(); Instant now = Instant.now(); message.id = LogManager.ID_GENERATOR.next(now); message.date = now; message.result = "ERROR"; message.app = "kubernetes"; message.errorCode = "FAILED_TO_COLLECT"; message.errorMessage = e.getMessage(); message.info = Map.of("stack_trace", Exceptions.stackTrace(e)); publisher.publish(message); } KubeMonitorJob(List<String> namespaces, KubeClient kubeClient, MessagePublisher<StatMessage> publisher); @Override void execute(JobContext context); final MessagePublisher<StatMessage> publisher; final KubeClient kubeClient; final List<String> namespaces; }
@Test void publishError() throws IOException { when(kubeClient.listPods("ns")).thenThrow(new Error("mock")); job.execute(null); verify(publisher).publish(argThat(message -> "kubernetes".equals(message.app) && "ERROR".equals(message.result) && "FAILED_TO_COLLECT".equals(message.errorCode))); }