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

Add method for loading multi kube resource file #25

Merged
merged 1 commit into from
Feb 29, 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 @@ -4,8 +4,14 @@
*/
package io.skodjob.testframe.clients;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
Expand Down Expand Up @@ -40,6 +46,28 @@ public String getKubeconfigPath() {
return kubeconfigPath;
}

/**
* Return kubernetes resources from yaml/json file
* @param file path
* @return list of the {@link HasMetadata} resources
* @throws IOException
*/
public List<HasMetadata> readResourcesFromFile(Path file) throws IOException {
return readResourcesFromFile(Files.newInputStream(file));
}

/**
* Return kubernetes resources from yaml/json file
* @param is stream
* @return list of the {@link HasMetadata} resources
* @throws IOException
*/
public List<HasMetadata> readResourcesFromFile(InputStream is) throws IOException {
try (is) {
return client.load(is).items();
}
}

private Config getConfig() {
if (TestFrameEnv.KUBE_USERNAME != null
&& TestFrameEnv.KUBE_PASSWORD != null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.skodjob.testframe.test.integration;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.skodjob.testframe.annotations.ResourceManager;
import io.skodjob.testframe.annotations.TestVisualSeparator;
import io.skodjob.testframe.resources.KubeResourceManager;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import java.io.IOException;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@ResourceManager
@TestVisualSeparator
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class KubeClientIT {

@Test
void testCreateResourcesFromYaml() throws IOException {
List<HasMetadata> resoruces = KubeResourceManager.getKubeClient()
.readResourcesFromFile(getClass().getClassLoader().getResourceAsStream("resources.yaml"));

KubeResourceManager.getInstance().createResourceWithWait(resoruces.toArray(new HasMetadata[0]));

assertNotNull(KubeResourceManager.getKubeClient().getClient().namespaces().withName("test4").get());
assertNotNull(KubeResourceManager.getKubeClient().getClient().serviceAccounts()
.inNamespace("test4").withName("skodjob-test-user").get());
}
}
14 changes: 14 additions & 0 deletions test-frame-test/src/test/resources/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
apiVersion: v1
kind: Namespace
metadata:
labels:
test: true
name: test4

---
apiVersion: v1
kind: ServiceAccount
metadata:
name: skodjob-test-user
namespace: test4
Loading