Skip to content

Commit

Permalink
test: check cw logs where applicable (#194)
Browse files Browse the repository at this point in the history
improve existing tests to verify log files in cloudwatch
  • Loading branch information
Nelson Ochoa authored Mar 13, 2023
1 parent 24a6a8b commit 0802078
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package com.aws.greengrass;
package com.aws.greengrass.steps;

import com.aws.greengrass.resources.CloudWatchLogStreamSpec;
import com.aws.greengrass.resources.CloudWatchLogsLifecycle;
Expand Down Expand Up @@ -83,18 +83,6 @@ private String getLogGroupName(String componentType, String componentName) {
return String.format("/aws/greengrass/%s/%s/%s", componentType, region, componentName);
}

@Then("I delete the log group of type {word} for component {word} if it exists")
public void deleteLogGroup(String componentType, String componentName) {
String logGroupName = getLogGroupName(componentType, componentName);
DeleteLogGroupRequest request = DeleteLogGroupRequest.builder().logGroupName(logGroupName).build();

try {
this.cwClient.deleteLogGroup(request);
} catch (ResourceNotFoundException notFound) {
LOGGER.debug("ResourceNotFound: Failed to delete log group {}", logGroupName);
}
}

/**
* Verifies if a group with the name /aws/greengrass/[componentType]/[region]/[componentName] was created
* in cloudwatch and additionally verifies if there is a stream named /[yyyy\/MM\/dd]/thing/[thingName] that
Expand Down Expand Up @@ -141,6 +129,8 @@ public void verifyLogs(int numberOfLogLines, String componentName, String compon

if (!result) {
// Print all the cloudwatch logs it fetched, so we can debug what failed to get uploaded.
LOGGER.error("Failed to verify {} logs were uploaded to cloudwatch. Below are the logs in CW",
numberOfLogLines);
this.lastReceivedCloudWatchEvents.forEach(e -> LOGGER.info(e.message()));

throw new Exception(String.format("Failed to verify that %d logs were uploaded to CloudWatch",
Expand All @@ -157,9 +147,6 @@ private boolean haveAllLogsBeenUploaded(int numberOfLogLines, String componentNa
.limit(numberOfLogLines) // limit of 10000 logs (this method could be optimized
.build();

LOGGER.info("Verifying {} logs present on group {} stream {}", numberOfLogLines, request.logGroupName(),
request.logStreamName());

try {
// The OTF watch steps check evey 100ms this to avoids hammering the api. Ideally OTF
// can allow us to configure the check interval rate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -38,7 +39,6 @@ public class FileSteps {

private static final RandomStringGenerator RANDOM_STRING_GENERATOR =
new RandomStringGenerator.Builder().withinRange('a', 'z').build();
private static final String ACTIVEFILE = "ActiveFile";
private static Logger LOGGER = LogManager.getLogger(FileSteps.class);
private final Platform platform;
private final TestContext testContext;
Expand Down Expand Up @@ -70,7 +70,12 @@ private static List<String> generateRandomMessages(int n, int length) {
}

private static List<File> getComponentLogFiles(String componentName, Path logsDirectory) {
return Arrays.stream(logsDirectory.toFile().listFiles()).filter(File::isFile).filter(file -> file.getName().startsWith(componentName)).sorted(Comparator.comparingLong(File::lastModified)).collect(Collectors.toList());
return Arrays.stream(logsDirectory.toFile().listFiles())
.filter(File::isFile)
.filter(file -> file.getName()
.startsWith(componentName))
.sorted(Comparator.comparingLong(File::lastModified))
.collect(Collectors.toList());
}

/**
Expand Down Expand Up @@ -98,16 +103,15 @@ public void arrangeComponentLogFiles(int numFiles, String componentName) throws
fileName = String.format("%s_%s.log", filePrefix, UUID.randomUUID());
createFileAndWriteData(logsDirectory, fileName, false);
}
scenarioContext.put(componentName + ACTIVEFILE, logsDirectory.resolve(fileName).toAbsolutePath().toString());
}

@Given("I create a log directory for component {word}")
public void arrangeLogDirectory(String componentName) {
@Given("I create a log directory for component called {word}")
public void arrangeLogDirectory(String directoryAlias) {
Path logsDirectory = testContext.installRoot().resolve("logs");
File componentLogsDirectory = new File(logsDirectory.toFile().getAbsolutePath() + "/" + UUID.randomUUID());
componentLogsDirectory.mkdirs();
LOGGER.info("Log directory for component {} is {}", componentName, componentLogsDirectory.getAbsolutePath());
scenarioContext.put(componentName + "LogDirectory", componentLogsDirectory.getAbsolutePath());
LOGGER.info("Log directory alias {} referencing {}", directoryAlias, componentLogsDirectory.getAbsolutePath());
scenarioContext.put(directoryAlias, componentLogsDirectory.getAbsolutePath());
}

private void createFileAndWriteData(Path tempDirectoryPath, String fileNamePrefix, boolean isTemp) throws
Expand Down Expand Up @@ -154,18 +158,14 @@ public void verifyRotatedFilesAvailable(int nfiles, String componentName) {
*
* @param componentName name of the component.
*/
@And("I verify the rotated files are deleted and that the active log file is present for component {word}")
public void verifyActiveFile(String componentName) {
Path logsDirectory = testContext.installRoot().resolve("logs");
@And("I verify the rotated files are deleted and that the active log file is present for component {word} on directory {word}")
public void verifyActiveFile(String componentName, String directoryAlias) {
Path logsDirectory = Paths.get(scenarioContext.get(directoryAlias));

if (!platform.files().exists(logsDirectory)) {
throw new IllegalStateException("No logs directory");
}
List<File> sortedFileList = getComponentLogFiles(componentName, logsDirectory);
String expectedActiveFilePath = scenarioContext.get(componentName + this.ACTIVEFILE);

File activeFile = sortedFileList.get(sortedFileList.size() - 1);
assertEquals(1, sortedFileList.size());
assertEquals(expectedActiveFilePath, activeFile.getAbsolutePath());
}
}
Loading

0 comments on commit 0802078

Please sign in to comment.