use of com.aws.greengrass.logging.api.Logger in project aws-greengrass-nucleus by aws-greengrass.
the class LifecycleTest method GIVEN_state_running_WHEN_errored_3_times_THEN_broken.
@Test
void GIVEN_state_running_WHEN_errored_3_times_THEN_broken() throws InterruptedException {
lifecycle = spy(new Lifecycle(greengrassService, logger, greengrassService.getPrivateConfig()));
initLifecycleState(lifecycle, State.NEW);
CountDownLatch reachedRunning1 = new CountDownLatch(1);
CountDownLatch reachedRunning2 = new CountDownLatch(1);
CountDownLatch reachedRunning3 = new CountDownLatch(1);
Mockito.doAnswer(mock -> {
lifecycle.reportState(State.RUNNING);
reachedRunning1.countDown();
return null;
}).doAnswer(mock -> {
lifecycle.reportState(State.RUNNING);
reachedRunning2.countDown();
return null;
}).doAnswer(mock -> {
lifecycle.reportState(State.RUNNING);
reachedRunning3.countDown();
return null;
}).when(greengrassService).startup();
lifecycle.initLifecycleThread();
lifecycle.requestStart();
assertTrue(reachedRunning1.await(5, TimeUnit.SECONDS));
verify(lifecycle, timeout(2000)).setState(any(), eq(State.RUNNING));
// We verify that setState is called, but that doesn't verify that the call to setState ended which is what
// we actually need to know in order to move on to the next part of the test.
// So, validate that it has actually set the state to be running before reporting
// the next error. Otherwise, it may register an error from STARTING instead of from RUNNING
assertThat(greengrassService::getState, eventuallyEval(is(State.RUNNING)));
// Report 1st error
lifecycle.reportState(State.ERRORED);
assertTrue(reachedRunning2.await(5, TimeUnit.SECONDS));
verify(lifecycle, timeout(2000).times(2)).setState(any(), eq(State.RUNNING));
assertThat(greengrassService::getState, eventuallyEval(is(State.RUNNING)));
// Report 2nd error
lifecycle.reportState(State.ERRORED);
assertTrue(reachedRunning3.await(5, TimeUnit.SECONDS));
verify(lifecycle, timeout(2000).times(3)).setState(any(), eq(State.RUNNING));
assertThat(greengrassService::getState, eventuallyEval(is(State.RUNNING)));
// Report 3rd error
lifecycle.reportState(State.ERRORED);
verify(lifecycle, timeout(10_000)).setState(any(), eq(State.BROKEN));
}
use of com.aws.greengrass.logging.api.Logger in project aws-greengrass-nucleus by aws-greengrass.
the class LogManagerHelperTest method GIVEN_mock_service_WHEN_getComponentLogger_THEN_logs_to_correct_log_file.
@Test
void GIVEN_mock_service_WHEN_getComponentLogger_THEN_logs_to_correct_log_file() throws IOException {
when(mockGreengrassService.getServiceName()).thenReturn("MockService");
LogConfig.getRootLogConfig().setStore(LogStore.FILE);
Logger componentLogger = LogManagerHelper.getComponentLogger(mockGreengrassService);
componentLogger.atInfo().log("Something");
LogConfig logConfig = LogManager.getLogConfigurations().get("MockService");
File logFile = new File(logConfig.getStoreName());
assertThat(logFile, aFileNamed(equalToIgnoringCase("MockService.log")));
List<String> lines = Files.readAllLines(logFile.toPath());
assertThat(lines.get(0), containsString("Something"));
File ggLogFile = new File(LogManager.getRootLogConfiguration().getStoreName());
assertThat(ggLogFile, aFileNamed(equalToIgnoringCase("greengrass.log")));
assertEquals(0, ggLogFile.length());
}
use of com.aws.greengrass.logging.api.Logger in project aws-greengrass-nucleus by aws-greengrass.
the class SystemServiceUtils method runCommand.
/**
* Simply run a command with privileges.
*
* @param logger Logger to use
* @param eventName logging event
* @param command command to run
* @param ignoreError ignore errors from this command
* @throws IOException for command failure
* @throws InterruptedException if interrupted while running
*/
@SuppressWarnings("PMD.CloseResource")
static void runCommand(Logger logger, String eventName, String command, boolean ignoreError) throws IOException, InterruptedException {
logger.atDebug(eventName).log(command);
Exec exec = Platform.getInstance().createNewProcessRunner().withShell(command);
if (Platform.getInstance().getPrivilegedUser() != null) {
exec.withUser(Platform.getInstance().getPrivilegedUser());
}
if (Platform.getInstance().getPrivilegedGroup() != null) {
exec.withGroup(Platform.getInstance().getPrivilegedGroup());
}
boolean success = exec.withOut(s -> logger.atWarn(eventName).kv("command", command).kv("stdout", s.toString().trim()).log()).withErr(s -> logger.atError(eventName).kv("command", command).kv("stderr", s.toString().trim()).log()).successful(true);
if (!success && !ignoreError) {
throw new IOException(String.format("Command %s failed", command));
}
}
use of com.aws.greengrass.logging.api.Logger in project aws-greengrass-nucleus by aws-greengrass.
the class LifecycleTest method GIVEN_state_running_WHEN_errored_long_time_in_between_THEN_not_broken.
@Test
void GIVEN_state_running_WHEN_errored_long_time_in_between_THEN_not_broken() throws InterruptedException {
Clock clock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
context.put(Clock.class, clock);
lifecycle = spy(new Lifecycle(greengrassService, logger, greengrassService.getPrivateConfig()));
initLifecycleState(lifecycle, State.NEW);
CountDownLatch reachedRunning1 = new CountDownLatch(1);
CountDownLatch reachedRunning2 = new CountDownLatch(1);
CountDownLatch reachedRunning3 = new CountDownLatch(1);
CountDownLatch reachedRunning4 = new CountDownLatch(1);
Mockito.doAnswer(mock -> {
lifecycle.reportState(State.RUNNING);
reachedRunning1.countDown();
return null;
}).doAnswer(mock -> {
lifecycle.reportState(State.RUNNING);
reachedRunning2.countDown();
return null;
}).doAnswer(mock -> {
lifecycle.reportState(State.RUNNING);
reachedRunning3.countDown();
return null;
}).doAnswer(mock -> {
lifecycle.reportState(State.RUNNING);
reachedRunning4.countDown();
return null;
}).when(greengrassService).startup();
lifecycle.initLifecycleThread();
lifecycle.requestStart();
assertTrue(reachedRunning1.await(5, TimeUnit.SECONDS));
verify(lifecycle, timeout(1000)).setState(any(), eq(State.RUNNING));
// Report 1st error
lifecycle.reportState(State.ERRORED);
assertTrue(reachedRunning2.await(5, TimeUnit.SECONDS));
verify(lifecycle, timeout(1000).times(2)).setState(any(), eq(State.RUNNING));
// Report 2nd error
lifecycle.reportState(State.ERRORED);
assertTrue(reachedRunning3.await(5, TimeUnit.SECONDS));
verify(lifecycle, timeout(1000).times(3)).setState(any(), eq(State.RUNNING));
// Report 3rd error, but after a while
clock = Clock.offset(clock, Duration.ofHours(1).plusMillis(1));
context.put(Clock.class, clock);
lifecycle.reportState(State.ERRORED);
assertTrue(reachedRunning4.await(5, TimeUnit.SECONDS));
verify(lifecycle, timeout(1000).times(4)).setState(any(), eq(State.RUNNING));
}
use of com.aws.greengrass.logging.api.Logger in project aws-greengrass-nucleus by aws-greengrass.
the class LogManagerHelperTest method GIVEN_nondefault_options_on_root_logger_WHEN_create_component_logger_THEN_inherits_options.
@Test
void GIVEN_nondefault_options_on_root_logger_WHEN_create_component_logger_THEN_inherits_options() {
LogManager.reconfigureAllLoggers(LogConfigUpdate.builder().level(Level.TRACE).format(LogFormat.JSON).build());
when(mockGreengrassService.getServiceName()).thenReturn("MockService2");
Logger logger = LogManagerHelper.getComponentLogger(mockGreengrassService);
assertTrue(logger.isTraceEnabled());
assertEquals(LogFormat.JSON, LogManager.getLogConfigurations().get("MockService2").getFormat());
}
Aggregations