Skip to content

Commit

Permalink
Ability to store all created resoruces as yaml files for easy reprodc…
Browse files Browse the repository at this point in the history
…ing issues (#188)

## Description

Store yaml files created by RM to easy reproduce test/issues


## Type of Change

Please delete options that are not relevant.

* New feature (non-breaking change which adds functionality)

## Checklist

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit/integration tests pass locally with my
changes

Signed-off-by: David Kornel <[email protected]>
  • Loading branch information
kornys authored Oct 3, 2024
1 parent db86533 commit 4481ed8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ public class ResourceManagerExtension
implements BeforeAllCallback, BeforeEachCallback, AfterAllCallback, AfterEachCallback {

private ResourceManagerExtension() {
// Private constructor to prevent instantiation
KubeResourceManager.getInstance();
}

@Override
public void beforeAll(ExtensionContext extensionContext) {
KubeResourceManager.getInstance();
KubeResourceManager.setTestContext(extensionContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
*/
package io.skodjob.testframe.resources;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -24,6 +28,7 @@
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.ReplicaSet;
import io.fabric8.kubernetes.api.model.apps.StatefulSet;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.skodjob.testframe.utils.LoggerUtils;
import io.skodjob.testframe.TestFrameConstants;
import io.skodjob.testframe.TestFrameEnv;
Expand Down Expand Up @@ -57,6 +62,8 @@ public class KubeResourceManager {
private static final ThreadLocal<ExtensionContext> TEST_CONTEXT = new ThreadLocal<>();
private static final Map<String, Stack<ResourceItem<?>>> STORED_RESOURCES = new LinkedHashMap<>();

private static String storeYamlPath = null;

private KubeResourceManager() {
// Private constructor to prevent instantiation
}
Expand Down Expand Up @@ -143,6 +150,24 @@ public final void addDeleteCallback(Consumer<HasMetadata> callback) {
this.deleteCallbacks.add(callback);
}

/**
* Set path for storing yaml resources
*
* @param path root path for storing
*/
public static void setStoreYamlPath(String path) {
storeYamlPath = path;
}

/**
* Returns root path of stored yaml resources
*
* @return path
*/
public static String getStoreYamlPath() {
return storeYamlPath;
}

/**
* Reads Kubernetes resources from a file at the specified path.
*
Expand Down Expand Up @@ -309,6 +334,9 @@ private <T extends HasMetadata> void createOrUpdateResource(boolean async,
for (T resource : resources) {
ResourceType<T> type = findResourceType(resource);
pushToStack(resource);
if (storeYamlPath != null) {
writeResourceAsYaml(resource);
}

if (type == null) {
// Generic create for any resource
Expand Down Expand Up @@ -538,4 +566,31 @@ private <T extends HasMetadata> boolean isResourceWithReadiness(T resource) {
|| resource instanceof Node
|| resource instanceof StatefulSet;
}

private void writeResourceAsYaml(HasMetadata resource) {
File logDir = Paths.get(storeYamlPath)
.resolve("test-files").resolve(getTestContext().getRequiredTestClass().getName()).toFile();
if (getTestContext().getTestMethod().isPresent()) {
logDir = logDir.toPath().resolve(getTestContext().getRequiredTestMethod().getName()).toFile();
}

if (!logDir.exists()) {
if (!logDir.mkdirs()) {
throw new RuntimeException(
String.format("Failed to create root log directories on path: %s", logDir.getAbsolutePath())
);
}
}

String r = Serialization.asYaml(resource);
try {
Files.writeString(logDir.toPath().resolve(
resource.getKind() + "-" +
(resource.getMetadata().getNamespace() == null ? "" :
(resource.getMetadata().getNamespace() + "-")) +
resource.getMetadata().getName() + ".yaml"), r, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public abstract class AbstractIT {
new DeploymentType()
);

// Allow storing yaml files
KubeResourceManager.setStoreYamlPath(LOG_DIR.toString());

// Register callback which are called with every create resource method for every resource
KubeResourceManager.getInstance().addCreateCallback(r -> {
isCreateHandlerCalled.set(true);
Expand Down

0 comments on commit 4481ed8

Please sign in to comment.