Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to store all created resoruces as yaml files for easy reprodcing issues #188

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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