Skip to content

Commit

Permalink
Add method for loading multi kube resource file (#25)
Browse files Browse the repository at this point in the history
Signed-off-by: David Kornel <[email protected]>
  • Loading branch information
kornys authored Feb 29, 2024
1 parent 697fa4c commit a32aea0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
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

0 comments on commit a32aea0

Please sign in to comment.