Skip to content

Commit

Permalink
Add kubernetes-file test
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Dec 24, 2024
1 parent 5681c09 commit dae3c0e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static class ReferenceFindSearchResultVisitor extends TreeVisitor<Tree,
Map<Tree, List<Reference>> matches;

@Override
public Tree postVisit(Tree tree, ExecutionContext ctx) {
public @Nullable Tree postVisit(Tree tree, ExecutionContext ctx) {
List<Reference> references = matches.get(tree);
if (references != null) {
if (tree instanceof PlainText) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ public static class Provider extends YamlProvider {
@Override
public SimpleTraitMatcher<YamlReference> getMatcher() {
return new SimpleTraitMatcher<YamlReference>() {
private final Predicate<String> image = Pattern.compile("image").asPredicate();
private final AtomicBoolean found = new AtomicBoolean(false);

@Override
protected @Nullable YamlReference test(Cursor cursor) {
Object value = cursor.getValue();
if (value instanceof Yaml.Scalar) {
if (image.test(((Yaml.Scalar) value).getValue())) {
found.set(true);
} else if (found.get()) {
if (found.get()) {
found.set(false);
return new YamlImageReference(cursor);
} else if ("image".equals(((Yaml.Scalar) value).getValue())) {
found.set(true);
}
}
return null;
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,59 @@ void gitlabCIFile() {
);
}

@Test
void kubernetesFile() {
rewriteRun(
assertImages("image", "app:v1.2.3", "account/image:latest", "repo.id/account/bucket/image:v1.2.3@digest"),
//language=yaml
yaml(
"""
apiVersion: v1
kind: Pod
spec:
containers:
- image: image
---
apiVersion: v1
kind: Pod
spec:
containers:
- image: app:v1.2.3
initContainers:
- image: account/image:latest
---
apiVersion: v1
kind: Pod
spec:
containers:
- image: repo.id/account/bucket/image:v1.2.3@digest
""",
"""
apiVersion: v1
kind: Pod
spec:
containers:
- image: ~~(image)~~>image
---
apiVersion: v1
kind: Pod
spec:
containers:
- image: ~~(app:v1.2.3)~~>app:v1.2.3
initContainers:
- image: ~~(account/image:latest)~~>account/image:latest
---
apiVersion: v1
kind: Pod
spec:
containers:
- image: ~~(repo.id/account/bucket/image:v1.2.3@digest)~~>repo.id/account/bucket/image:v1.2.3@digest
""",
spec -> spec.path(".gitlab-ci")
)
);
}

private static Consumer<RecipeSpec> assertImages(String... expected) {
return spec -> spec.recipe(new FindDockerImageUses())
.dataTable(DockerBaseImages.Row.class,rows ->
Expand Down

0 comments on commit dae3c0e

Please sign in to comment.