|
| 1 | +/** |
| 2 | + * ContainerProxy |
| 3 | + * |
| 4 | + * Copyright (C) 2016-2020 Open Analytics |
| 5 | + * |
| 6 | + * =========================================================================== |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the Apache License as published by |
| 10 | + * The Apache Software Foundation, either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * Apache License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the Apache License |
| 19 | + * along with this program. If not, see <http://www.apache.org/licenses/> |
| 20 | + */ |
| 21 | +package eu.openanalytics.containerproxy.test.helpers; |
| 22 | + |
| 23 | +import io.fabric8.kubernetes.api.model.Namespace; |
| 24 | +import io.fabric8.kubernetes.api.model.NamespaceBuilder; |
| 25 | +import io.fabric8.kubernetes.client.DefaultKubernetesClient; |
| 26 | +import io.fabric8.kubernetes.client.NamespacedKubernetesClient; |
| 27 | + |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +public abstract class KubernetesTestBase { |
| 32 | + |
| 33 | + public static interface TestBody { |
| 34 | + public void run(NamespacedKubernetesClient client, String namespace, String overriddenNamespace) throws InterruptedException; |
| 35 | + } |
| 36 | + |
| 37 | + public static final String namespace = "itest"; |
| 38 | + public static final String overriddenNamespace = "itest-overridden"; |
| 39 | + private final List<String> managedNamespaces = Arrays.asList(namespace, overriddenNamespace); |
| 40 | + |
| 41 | + static protected final DefaultKubernetesClient client = new DefaultKubernetesClient(); |
| 42 | + |
| 43 | + protected void setup(TestBody test) { |
| 44 | + Runtime.getRuntime().addShutdownHook(new Thread(this::deleteNamespaces)); |
| 45 | + |
| 46 | + deleteNamespaces(); |
| 47 | + createNamespaces(); |
| 48 | + |
| 49 | + try { |
| 50 | + Thread.sleep(1000); // wait for namespaces and tokens to become ready |
| 51 | + |
| 52 | + NamespacedKubernetesClient namespacedKubernetesClient = client.inNamespace(namespace); |
| 53 | + |
| 54 | + test.run(namespacedKubernetesClient, namespace, overriddenNamespace); |
| 55 | + } catch (InterruptedException e) { |
| 56 | + } finally { |
| 57 | + deleteNamespaces(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private void deleteNamespaces() { |
| 62 | + try { |
| 63 | + for (String namespace : managedNamespaces) { |
| 64 | + Namespace ns = client.namespaces().withName(namespace).get(); |
| 65 | + if (ns == null) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + client.namespaces().delete(ns); |
| 70 | + |
| 71 | + while (client.namespaces().withName(namespace).get() != null) { |
| 72 | + Thread.sleep(1000); |
| 73 | + } |
| 74 | + } |
| 75 | + } catch (InterruptedException e) { |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private void createNamespaces() { |
| 80 | + for (String namespace : managedNamespaces) { |
| 81 | + client.namespaces().create(new NamespaceBuilder() |
| 82 | + .withNewMetadata() |
| 83 | + .withName(namespace) |
| 84 | + .endMetadata() |
| 85 | + .build()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments