Skip to content

Commit

Permalink
fix: remove test docker image from device
Browse files Browse the repository at this point in the history
  • Loading branch information
guoje committed Mar 11, 2022
1 parent 249ea1a commit 3575f7a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ public class DockerSteps {
*
* @param image fully qualified image name in <code>name:tag</code> representation
*/
@Given("the docker image {word} does not exist on the device")
@Given("deleted the docker image {word} if exists on the device")
public void checkDockerImageIsMissing(String image) {
Predicate<Set<String>> predicate = Set::isEmpty;
checkDockerImagePresence(image, predicate.negate(),
"The image " + image + " is already on the device. Please remove the image and try again.");
if (isDockerImagePresence(image, predicate.negate())) {
removeDockerImage(image);
}

}

@Then("I can check that the docker image {word} exists on the device")
Expand All @@ -65,18 +67,30 @@ public void removeDockerImage(String image) {
LOGGER.debug("Removed docker image {}: {}", image, result);
}

private void checkDockerImagePresence(String image, Predicate<Set<String>> validity, String message) {
private boolean isDockerImagePresence(String image, Predicate<Set<String>> validity) {
// This could be improved by using the API on a local host.

Set<String> parts = new HashSet<>(Arrays.stream(image.split(":")).collect(Collectors.toSet()));
String[] result = platform.commands().executeToString(CommandInput.builder()
.line("docker").addArgs("images")
.build())
.split("\\r?\\n");

Arrays.stream(result)
.map(String::trim)
.flatMap(line -> Arrays.stream(line.split("\\s+")))
.forEach(parts::remove);

if (!validity.test(parts)) {
return false;
}

return true;
}

private void checkDockerImagePresence(String image, Predicate<Set<String>> validity, String message) {

if (!isDockerImagePresence(image, validity)) {
throw new IllegalStateException(message);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Feature: Greengrass V2 Docker Component

@Docker @IDT
Scenario: I can deploy Docker containers as Greengrass Components
Given the docker image amazon/amazon-ec2-metadata-mock:v1.9.0 does not exist on the device
Given deleted the docker image amazon/amazon-ec2-metadata-mock:v1.9.0 if exists on the device
And I create a Greengrass deployment with components
| DockerHubAmazonContainer | classpath:/greengrass/component/recipes/DockerHubAmazonContainer.yaml |
When I deploy the Greengrass deployment configuration
Expand Down

0 comments on commit 3575f7a

Please sign in to comment.