Java tutorial
/* * The MIT License * * Copyright (c) 2016, Carlos Sanchez * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package org.csanchez.jenkins.plugins.kubernetes.pipeline; import static java.util.Arrays.*; import static org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil.*; import java.net.InetAddress; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.compress.utils.IOUtils; import org.csanchez.jenkins.plugins.kubernetes.ContainerEnvVar; import org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate; import org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud; import org.csanchez.jenkins.plugins.kubernetes.PodTemplate; import org.csanchez.jenkins.plugins.kubernetes.model.KeyValueEnvVar; import org.csanchez.jenkins.plugins.kubernetes.model.SecretEnvVar; import org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Rule; import org.jvnet.hudson.test.BuildWatcher; import org.jvnet.hudson.test.JenkinsRuleNonLocalhost; import org.jvnet.hudson.test.LoggerRule; import com.google.common.collect.ImmutableMap; import hudson.EnvVars; import hudson.slaves.EnvironmentVariablesNodeProperty; import hudson.slaves.NodeProperty; import hudson.slaves.NodePropertyDescriptor; import hudson.util.DescribableList; import io.fabric8.kubernetes.api.model.Secret; import io.fabric8.kubernetes.api.model.SecretBuilder; import io.fabric8.kubernetes.client.KubernetesClient; import jenkins.model.JenkinsLocationConfiguration; public class AbstractKubernetesPipelineTest { protected static final String CONTAINER_ENV_VAR_VALUE = "container-env-var-value"; protected static final String POD_ENV_VAR_VALUE = "pod-env-var-value"; protected static final String SECRET_KEY = "password"; protected static final String CONTAINER_ENV_VAR_FROM_SECRET_VALUE = "container-pa55w0rd"; protected static final String POD_ENV_VAR_FROM_SECRET_VALUE = "pod-pa55w0rd"; protected static final String GLOBAL = "GLOBAL"; @ClassRule public static BuildWatcher buildWatcher = new BuildWatcher(); protected KubernetesCloud cloud; @Rule public JenkinsRuleNonLocalhost r = new JenkinsRuleNonLocalhost(); @Rule public LoggerRule logs = new LoggerRule().record(Logger.getLogger(KubernetesCloud.class.getPackage().getName()), Level.ALL); @BeforeClass public static void isKubernetesConfigured() throws Exception { assumeKubernetes(); } @Before public void configureCloud() throws Exception { cloud = setupCloud(this); createSecret(cloud.connect()); cloud.getTemplates().clear(); cloud.addTemplate(buildBusyboxTemplate("busybox")); // Agents running in Kubernetes (minikube) need to connect to this server, so localhost does not work URL url = r.getURL(); String hostAddress = System.getProperty("jenkins.host.address"); if (hostAddress == null) { hostAddress = InetAddress.getLocalHost().getHostAddress(); } URL nonLocalhostUrl = new URL(url.getProtocol(), hostAddress, url.getPort(), url.getFile()); JenkinsLocationConfiguration.get().setUrl(nonLocalhostUrl.toString()); r.jenkins.clouds.add(cloud); DescribableList<NodeProperty<?>, NodePropertyDescriptor> list = r.jenkins.getGlobalNodeProperties(); EnvironmentVariablesNodeProperty newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty(); list.add(newEnvVarsNodeProperty); EnvVars envVars = newEnvVarsNodeProperty.getEnvVars(); envVars.put("GLOBAL", "GLOBAL"); envVars.put("JAVA_HOME_X", "java-home-x"); r.jenkins.save(); } private PodTemplate buildBusyboxTemplate(String label) { // Create a busybox template PodTemplate podTemplate = new PodTemplate(); podTemplate.setLabel(label); ContainerTemplate containerTemplate = new ContainerTemplate("busybox", "busybox", "cat", ""); containerTemplate.setTtyEnabled(true); podTemplate.getContainers().add(containerTemplate); setEnvVariables(podTemplate); return podTemplate; } protected String loadPipelineScript(String name) { return loadPipelineScript(getClass(), name); } public static String loadPipelineScript(Class<?> clazz, String name) { try { return new String(IOUtils.toByteArray(clazz.getResourceAsStream(name))); } catch (Throwable t) { throw new RuntimeException("Could not read resource:[" + name + "]."); } } private static void createSecret(KubernetesClient client) { Secret secret = new SecretBuilder() .withStringData(ImmutableMap.of(SECRET_KEY, CONTAINER_ENV_VAR_FROM_SECRET_VALUE)).withNewMetadata() .withName("container-secret").endMetadata().build(); client.secrets().createOrReplace(secret); secret = new SecretBuilder().withStringData(ImmutableMap.of(SECRET_KEY, POD_ENV_VAR_FROM_SECRET_VALUE)) .withNewMetadata().withName("pod-secret").endMetadata().build(); client.secrets().createOrReplace(secret); } private static void setEnvVariables(PodTemplate podTemplate) { TemplateEnvVar podSecretEnvVar = new SecretEnvVar("POD_ENV_VAR_FROM_SECRET", "pod-secret", SECRET_KEY); TemplateEnvVar podSimpleEnvVar = new KeyValueEnvVar("POD_ENV_VAR", POD_ENV_VAR_VALUE); podTemplate.setEnvVars(asList(podSecretEnvVar, podSimpleEnvVar)); TemplateEnvVar containerEnvVariable = new KeyValueEnvVar("CONTAINER_ENV_VAR", CONTAINER_ENV_VAR_VALUE); TemplateEnvVar containerEnvVariableLegacy = new ContainerEnvVar("CONTAINER_ENV_VAR_LEGACY", CONTAINER_ENV_VAR_VALUE); TemplateEnvVar containerSecretEnvVariable = new SecretEnvVar("CONTAINER_ENV_VAR_FROM_SECRET", "container-secret", SECRET_KEY); podTemplate.getContainers().get(0) .setEnvVars(asList(containerEnvVariable, containerEnvVariableLegacy, containerSecretEnvVariable)); } }