下面列出了org.springframework.core.io.FileUrlResource#org.springframework.cloud.deployer.spi.core.AppDefinition 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testSchedulerPropertiesMerge() {
final String baseScheduleName = "test-schedule1";
Map<String, String> schedulerProperties = new HashMap<>();
schedulerProperties.put(CRON_EXPRESSION, "0/10 * * * *");
schedulerProperties.put(KubernetesSchedulerProperties.KUBERNETES_SCHEDULER_PROPERTIES_PREFIX + ".imagePullPolicy", "Never");
Map<String, String> deploymentProperties = new HashMap<>();
deploymentProperties.put(KubernetesDeployerProperties.KUBERNETES_DEPLOYER_PROPERTIES_PREFIX + ".environmentVariables", "MYVAR1=MYVAL1,MYVAR2=MYVAL2");
deploymentProperties.put(KubernetesDeployerProperties.KUBERNETES_DEPLOYER_PROPERTIES_PREFIX + ".imagePullPolicy", "Always");
AppDefinition appDefinition = new AppDefinition(randomName(), null);
ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, schedulerProperties, deploymentProperties, null,
baseScheduleName, testApplication());
Map<String, String> mergedProperties = KubernetesScheduler.mergeSchedulerProperties(scheduleRequest);
assertTrue("Expected value from Scheduler properties, but found in Deployer properties", mergedProperties.get(KubernetesSchedulerProperties.KUBERNETES_SCHEDULER_PROPERTIES_PREFIX + ".imagePullPolicy").equals("Never"));
assertTrue("Deployer property is expected to be merged as scheduler property", mergedProperties.get(KubernetesSchedulerProperties.KUBERNETES_SCHEDULER_PROPERTIES_PREFIX + ".environmentVariables").equals("MYVAR1=MYVAL1,MYVAR2=MYVAL2"));
}
@Test
public void testImagePullSecretDefault() {
KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
if (kubernetesSchedulerProperties.getNamespace() == null) {
kubernetesSchedulerProperties.setNamespace("default");
}
KubernetesClient kubernetesClient = new DefaultKubernetesClient()
.inNamespace(kubernetesSchedulerProperties.getNamespace());
KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
kubernetesSchedulerProperties);
AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, getSchedulerProperties(),
getDeploymentProperties(), getCommandLineArgs(), randomName(), testApplication());
CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
CronJobSpec cronJobSpec = cronJob.getSpec();
List<LocalObjectReference> secrets = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec()
.getImagePullSecrets();
assertTrue("There should be no secrets", secrets.isEmpty());
kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
@Test
public void testTaskServiceAccountNameDefault() {
KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
if (kubernetesSchedulerProperties.getNamespace() == null) {
kubernetesSchedulerProperties.setNamespace("default");
}
KubernetesClient kubernetesClient = new DefaultKubernetesClient()
.inNamespace(kubernetesSchedulerProperties.getNamespace());
KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
kubernetesSchedulerProperties);
AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, getSchedulerProperties(),
getDeploymentProperties(), getCommandLineArgs(), randomName(), testApplication());
CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
CronJobSpec cronJobSpec = cronJob.getSpec();
String serviceAccountName = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec()
.getServiceAccountName();
assertEquals("Unexpected service account name", KubernetesSchedulerProperties.DEFAULT_TASK_SERVICE_ACCOUNT_NAME,
serviceAccountName);
kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
@Test
public void deployWithEnvironmentWithMultipleCommaDelimitedValue() throws Exception {
AppDefinition definition = new AppDefinition("app-test", null);
Map<String, String> props = new HashMap<>();
props.put("spring.cloud.deployer.kubernetes.environmentVariables",
"JAVA_TOOL_OPTIONS='thing1,thing2',OPTS='thing3, thing4'");
AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);
deployer = new KubernetesAppDeployer(bindDeployerProperties(), null);
PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);
assertThat(podSpec.getContainers().get(0).getEnv())
.contains(
new EnvVar("JAVA_TOOL_OPTIONS", "thing1,thing2", null),
new EnvVar("OPTS", "thing3, thing4", null));
}
@Test
public void testCommandBuilderSpringApplicationJson() {
LocalDeployerProperties properties = new LocalDeployerProperties();
LocalAppDeployer deployer = new LocalAppDeployer(properties);
AppDefinition definition = new AppDefinition("foo", Collections.singletonMap("foo","bar"));
deploymentProperties.put(LocalDeployerProperties.DEBUG_PORT, "9999");
deploymentProperties.put(LocalDeployerProperties.DEBUG_SUSPEND, "y");
deploymentProperties.put(LocalDeployerProperties.INHERIT_LOGGING, "true");
AppDeploymentRequest request = new AppDeploymentRequest(definition, testResource(), deploymentProperties);
ProcessBuilder builder = deployer.buildProcessBuilder(request, definition.getProperties(), Optional.of(1), "foo" );
assertThat(builder.environment().keySet(), hasItem(AbstractLocalDeployerSupport.SPRING_APPLICATION_JSON));
assertThat(builder.environment().get(AbstractLocalDeployerSupport.SPRING_APPLICATION_JSON), is("{\"foo\":\"bar\"}"));
}
@Test
public void testSecretKeyRefGlobalFromYaml() throws Exception {
AppDefinition definition = new AppDefinition("app-test", null);
AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), null);
deployer = new KubernetesAppDeployer(bindDeployerProperties(), null);
PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);
List<EnvVar> envVars = podSpec.getContainers().get(0).getEnv();
assertEquals("Invalid number of env vars", 3, envVars.size());
EnvVar secretKeyRefEnvVar = envVars.get(0);
assertEquals("Unexpected env var name", "SECRET_PASSWORD", secretKeyRefEnvVar.getName());
SecretKeySelector secretKeySelector = secretKeyRefEnvVar.getValueFrom().getSecretKeyRef();
assertEquals("Unexpected secret name", "mySecret", secretKeySelector.getName());
assertEquals("Unexpected secret data key", "myPassword", secretKeySelector.getKey());
}
@Test
public void testConfigMapKeyRef() {
Map<String, String> props = new HashMap<>();
props.put("spring.cloud.deployer.kubernetes.configMapKeyRefs",
"[{envVarName: 'MY_ENV', configMapName: 'myConfigMap', dataKey: 'envName'}]");
AppDefinition definition = new AppDefinition("app-test", null);
AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);
deployer = new KubernetesAppDeployer(new KubernetesDeployerProperties(), null);
PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);
List<EnvVar> envVars = podSpec.getContainers().get(0).getEnv();
assertEquals("Invalid number of env vars", 2, envVars.size());
EnvVar configMapKeyRefEnvVar = envVars.get(0);
assertEquals("Unexpected env var name", "MY_ENV", configMapKeyRefEnvVar.getName());
ConfigMapKeySelector configMapKeySelector = configMapKeyRefEnvVar.getValueFrom().getConfigMapKeyRef();
assertEquals("Unexpected config map name", "myConfigMap", configMapKeySelector.getName());
assertEquals("Unexpected config map data key", "envName", configMapKeySelector.getKey());
}
/**
* Tests that an app which takes a long time to deploy is correctly reported as deploying.
* Test that such an app can be undeployed.
*/
@Test
public void testDeployingStateCalculationAndCancel() {
Map<String, String> properties = new HashMap<>();
properties.put("initDelay", "" + 1000 * 60 * 60); // 1hr
AppDefinition definition = new AppDefinition(randomName(), properties);
Resource resource = testApplication();
AppDeploymentRequest request = new AppDeploymentRequest(definition, resource, properties);
log.info("Deploying {}...", request.getDefinition().getName());
String deploymentId = appDeployer().deploy(request);
Timeout timeout = deploymentTimeout();
assertThat(deploymentId, eventually(hasStatusThat(
Matchers.<AppStatus>hasProperty("state", is(deploying))), timeout.maxAttempts, timeout.pause));
log.info("Undeploying {}...", deploymentId);
timeout = undeploymentTimeout();
appDeployer().undeploy(deploymentId);
assertThat(deploymentId, eventually(hasStatusThat(
Matchers.<AppStatus>hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause));
}
@Test
public void testSimpleLaunch() throws InterruptedException {
Map<String, String> appProperties = new HashMap<>();
appProperties.put("killDelay", "0");
appProperties.put("exitCode", "0");
AppDefinition definition = new AppDefinition(randomName(), appProperties);
Resource resource = testApplication();
AppDeploymentRequest request = new AppDeploymentRequest(definition, resource);
log.info("Launching {}...", request.getDefinition().getName());
String launchId = taskLauncher().launch(request);
Timeout timeout = deploymentTimeout();
assertThat(launchId, eventually(hasStatusThat(
Matchers.<TaskStatus>hasProperty("state", Matchers.is(LaunchState.complete))), timeout.maxAttempts, timeout.pause));
taskLauncher().destroy(definition.getName());
}
@Test
public void testFailureToCallShutdownOnUndeploy() {
Map<String, String> properties = new HashMap<>();
// disable shutdown endpoint
properties.put("endpoints.shutdown.enabled", "false");
AppDefinition definition = new AppDefinition(randomName(), properties);
Resource resource = testApplication();
AppDeploymentRequest request = new AppDeploymentRequest(definition, resource);
log.info("Deploying {}...", request.getDefinition().getName());
String deploymentId = appDeployer().deploy(request);
Timeout timeout = deploymentTimeout();
assertThat(deploymentId, eventually(hasStatusThat(
Matchers.<AppStatus>hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause));
log.info("Undeploying {}...", deploymentId);
timeout = undeploymentTimeout();
appDeployer().undeploy(deploymentId);
assertThat(deploymentId, eventually(hasStatusThat(
Matchers.<AppStatus>hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause));
}
@Test
public void testPodSecurityContextGlobalProperty() {
AppDefinition definition = new AppDefinition("app-test", null);
AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), null);
KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
KubernetesDeployerProperties.PodSecurityContext securityContext = new KubernetesDeployerProperties.PodSecurityContext();
securityContext.setFsGroup(65534L);
securityContext.setRunAsUser(65534L);
kubernetesDeployerProperties.setPodSecurityContext(securityContext);
deployer = new KubernetesAppDeployer(kubernetesDeployerProperties, null);
PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);
PodSecurityContext podSecurityContext = podSpec.getSecurityContext();
assertNotNull("Pod security context should not be null", podSecurityContext);
assertEquals("Unexpected run as user", Long.valueOf("65534"), podSecurityContext.getRunAsUser());
assertEquals("Unexpected fs group", Long.valueOf("65534"), podSecurityContext.getFsGroup());
}
@Test
public void testPodSecurityContextUIDOnly() {
Map<String, String> props = new HashMap<>();
props.put("spring.cloud.deployer.kubernetes.podSecurityContext", "{runAsUser: 65534}");
AppDefinition definition = new AppDefinition("app-test", null);
AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);
deployer = new KubernetesAppDeployer(new KubernetesDeployerProperties(), null);
PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);
PodSecurityContext podSecurityContext = podSpec.getSecurityContext();
assertNotNull("Pod security context should not be null", podSecurityContext);
assertEquals("Unexpected run as user", Long.valueOf("65534"), podSecurityContext.getRunAsUser());
assertNull("Unexpected fs group", podSecurityContext.getFsGroup());
}
@Test
public void testPodSecurityContextPropertyOverrideGlobal() {
Map<String, String> props = new HashMap<>();
props.put("spring.cloud.deployer.kubernetes.podSecurityContext", "{runAsUser: 65534, fsGroup: 65534}");
AppDefinition definition = new AppDefinition("app-test", null);
AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);
KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
KubernetesDeployerProperties.PodSecurityContext securityContext = new KubernetesDeployerProperties.PodSecurityContext();
securityContext.setFsGroup(1000L);
securityContext.setRunAsUser(1000L);
kubernetesDeployerProperties.setPodSecurityContext(securityContext);
deployer = new KubernetesAppDeployer(kubernetesDeployerProperties, null);
PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);
PodSecurityContext podSecurityContext = podSpec.getSecurityContext();
assertNotNull("Pod security context should not be null", podSecurityContext);
assertEquals("Unexpected run as user", Long.valueOf("65534"), podSecurityContext.getRunAsUser());
assertEquals("Unexpected fs group", Long.valueOf("65534"), podSecurityContext.getFsGroup());
}
@Test
public void testAppLogRetrieval() {
Map<String, String> appProperties = new HashMap<>();
appProperties.put("killDelay", "0");
appProperties.put("exitCode", "0");
AppDefinition definition = new AppDefinition(randomName(), appProperties);
Resource resource = testApplication();
AppDeploymentRequest request = new AppDeploymentRequest(definition, resource);
String launchId1 = taskLauncher().launch(request);
Timeout timeout = deploymentTimeout();
assertThat(launchId1, eventually(hasStatusThat(
Matchers.<TaskStatus>hasProperty("state", Matchers.is(LaunchState.complete))), timeout.maxAttempts, timeout.pause));
String logContent = taskLauncher().getLog(launchId1);
assertThat(logContent, containsString("Starting DeployerIntegrationTestApplication"));
}
/**
* Creates an {@link AppDeploymentRequest}.
*
* @param applicationSpec the Spring Cloud Deployer application spec
* @param releaseName the release name
* @param version the release version
* @return a created AppDeploymentRequest
*/
public AppDeploymentRequest createAppDeploymentRequest(SpringCloudDeployerApplicationManifest applicationSpec, String releaseName,
String version) {
SpringCloudDeployerApplicationSpec spec = applicationSpec.getSpec();
Map<String, String> applicationProperties = new TreeMap<>();
if (spec.getApplicationProperties() != null) {
applicationProperties.putAll(spec.getApplicationProperties());
}
// we need to keep group name same for consumer groups not getting broken, but
// app name needs to differentiate as otherwise it may result same deployment id and
// failure on a deployer.
AppDefinition appDefinition = new AppDefinition(applicationSpec.getApplicationName() + "-v" + version,
applicationProperties);
Resource resource;
try {
resource = delegatingResourceLoader.getResource(getResourceLocation(spec.getResource(), spec.getVersion()));
}
catch (Exception e) {
throw new SkipperException(
"Could not load Resource " + spec.getResource() + ". Message = " + e.getMessage(), e);
}
Map<String, String> deploymentProperties = new TreeMap<>();
if (spec.getDeploymentProperties() != null) {
deploymentProperties.putAll(spec.getDeploymentProperties());
}
if (!deploymentProperties.containsKey(AppDeployer.GROUP_PROPERTY_KEY)) {
logger.debug("Defaulting spring.cloud.deployer.group=" + releaseName);
deploymentProperties.put(AppDeployer.GROUP_PROPERTY_KEY, releaseName);
}
AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(appDefinition, resource,
deploymentProperties);
return appDeploymentRequest;
}
private static AppDeploymentRequest createAppDeploymentRequest(String app) {
MavenResource resource = new MavenResource.Builder()
.artifactId(app)
.groupId("org.springframework.cloud.task.app")
.version("1.0.0.BUILD-SNAPSHOT")
.build();
Map<String, String> properties = new HashMap<>();
properties.put("server.port", "0");
AppDefinition definition = new AppDefinition(app, properties);
AppDeploymentRequest request = new AppDeploymentRequest(definition, resource);
return request;
}
public ApplicationDefinition(String name, String dslText) {
super();
this.name = name;
this.dslText = dslText;
// ApplicationParser
AppNode applicationNode = new ApplicationParser(name, dslText).parse();
Map<String, String> properties = new HashMap<>();
if (applicationNode.hasArguments()) {
for (ArgumentNode argumentNode : applicationNode.getArguments()) {
properties.put(argumentNode.getName(), argumentNode.getValue());
}
}
setRegisteredAppName(applicationNode.getName());
this.appDefinition = new AppDefinition(name, properties);
}
@Test(expected = CreateScheduleException.class)
public void testMissingSchedule() {
AppDefinition appDefinition = new AppDefinition(randomName(), null);
ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, null, null, null, null, testApplication());
scheduler.schedule(scheduleRequest);
fail();
}
@Test(expected = CreateScheduleException.class)
public void testInvalidNameSchedule() {
AppDefinition appDefinition = new AppDefinition("AAAAAA", null);
ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, null, null, null, "AAAAA", testApplication());
scheduler.schedule(scheduleRequest);
fail();
}
@Test
public void testWithExecEntryPoint() {
KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
if (kubernetesSchedulerProperties.getNamespace() == null) {
kubernetesSchedulerProperties.setNamespace("default");
}
kubernetesSchedulerProperties.setEntryPointStyle(EntryPointStyle.exec);
KubernetesClient kubernetesClient = new DefaultKubernetesClient()
.inNamespace(kubernetesSchedulerProperties.getNamespace());
KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
kubernetesSchedulerProperties);
AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, getSchedulerProperties(), null,
getCommandLineArgs(), randomName(), testApplication());
CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
CronJobSpec cronJobSpec = cronJob.getSpec();
Container container = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec().getContainers().get(0);
assertNotNull("Command line arguments should not be null", container.getArgs());
assertNotNull("Environment variables should not be null", container.getEnv());
assertTrue("Environment variables should only have SPRING_CLOUD_APPLICATION_GUID", container.getEnv().size() == 1);
kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
@Before
public void setUp() throws Exception {
this.deploymentProperties = new HashMap<>();
this.deploymentRequest = new AppDeploymentRequest(new AppDefinition("foo", Collections.emptyMap()), new FileSystemResource(""), deploymentProperties);
this.kubernetesDeployerProperties = new KubernetesDeployerProperties();
this.deploymentPropertiesResolver = new DeploymentPropertiesResolver(
KubernetesDeployerProperties.KUBERNETES_DEPLOYER_PROPERTIES_PREFIX, this.kubernetesDeployerProperties);
}
@Test
public void testWithBootEntryPoint() throws IOException {
KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
kubernetesSchedulerProperties.setEntryPointStyle(EntryPointStyle.boot);
if (kubernetesSchedulerProperties.getNamespace() == null) {
kubernetesSchedulerProperties.setNamespace("default");
}
KubernetesClient kubernetesClient = new DefaultKubernetesClient()
.inNamespace(kubernetesSchedulerProperties.getNamespace());
KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
kubernetesSchedulerProperties);
AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, getSchedulerProperties(), null,
getCommandLineArgs(), randomName(), testApplication());
CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
CronJobSpec cronJobSpec = cronJob.getSpec();
Container container = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec().getContainers().get(0);
assertNotNull("Command line arguments should not be null", container.getArgs());
assertEquals("Invalid number of command line arguments", 2, container.getArgs().size());
assertNotNull("Environment variables should not be null", container.getEnv());
assertTrue("Invalid number of environment variables", container.getEnv().size() > 1);
String springApplicationJson = container.getEnv().get(0).getValue();
Map<String, String> springApplicationJsonValues = new ObjectMapper().readValue(springApplicationJson,
new TypeReference<HashMap<String, String>>() {
});
assertNotNull("SPRING_APPLICATION_JSON should not be null", springApplicationJsonValues);
assertEquals("Invalid number of SPRING_APPLICATION_JSON entries", 2, springApplicationJsonValues.size());
kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
@Test
public void testEntryPointStyleOverride() throws Exception {
KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
if (kubernetesSchedulerProperties.getNamespace() == null) {
kubernetesSchedulerProperties.setNamespace("default");
}
KubernetesClient kubernetesClient = new DefaultKubernetesClient()
.inNamespace(kubernetesSchedulerProperties.getNamespace());
KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
kubernetesSchedulerProperties);
String prefix = KubernetesSchedulerProperties.KUBERNETES_SCHEDULER_PROPERTIES_PREFIX;
Map<String, String> schedulerProperties = new HashMap<>(getSchedulerProperties());
schedulerProperties.put(prefix + ".entryPointStyle", "boot");
AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, schedulerProperties,
null, getCommandLineArgs(), randomName(), testApplication());
CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
CronJobSpec cronJobSpec = cronJob.getSpec();
Container container = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec().getContainers().get(0);
assertTrue("Environment variables should not be empty", !container.getEnv().isEmpty());
assertTrue("Invalid number of environment variables", container.getEnv().size() > 1);
String springApplicationJson = container.getEnv().get(0).getValue();
Map<String, String> springApplicationJsonValues = new ObjectMapper().readValue(springApplicationJson,
new TypeReference<HashMap<String, String>>() {
});
assertNotNull("SPRING_APPLICATION_JSON should not be null", springApplicationJsonValues);
assertEquals("Invalid number of SPRING_APPLICATION_JSON entries", 2, springApplicationJsonValues.size());
kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
@Test
public void testDefaultServicePort() {
log.info("Testing {}...", "DefaultServicePort");
KubernetesAppDeployer kubernetesAppDeployer = new KubernetesAppDeployer(new KubernetesDeployerProperties(), kubernetesClient);
AppDefinition definition = new AppDefinition(randomName(), null);
Resource resource = testApplication();
AppDeploymentRequest request = new AppDeploymentRequest(definition, resource);
log.info("Deploying {}...", request.getDefinition().getName());
String deploymentId = kubernetesAppDeployer.deploy(request);
Timeout timeout = deploymentTimeout();
assertThat(deploymentId, eventually(hasStatusThat(
Matchers.hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause));
List<ServicePort> servicePorts = kubernetesClient.services().withName(request.getDefinition().getName()).get()
.getSpec().getPorts();
assertThat(servicePorts, is(notNullValue()));
assertThat(servicePorts.size(), is(1));
assertTrue(servicePorts.stream().anyMatch(o -> o.getPort().equals(8080)));
assertTrue(servicePorts.stream().anyMatch(o -> o.getName().equals("port-8080")));
log.info("Undeploying {}...", deploymentId);
timeout = undeploymentTimeout();
kubernetesAppDeployer.undeploy(deploymentId);
assertThat(deploymentId, eventually(hasStatusThat(
Matchers.hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause));
}
@Test
public void testImagePullSecret() {
KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
if (kubernetesSchedulerProperties.getNamespace() == null) {
kubernetesSchedulerProperties.setNamespace("default");
}
KubernetesClient kubernetesClient = new DefaultKubernetesClient()
.inNamespace(kubernetesSchedulerProperties.getNamespace());
KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
kubernetesSchedulerProperties);
String secretName = "mysecret";
String prefix = KubernetesSchedulerProperties.KUBERNETES_SCHEDULER_PROPERTIES_PREFIX;
Map<String, String> schedulerProperties = new HashMap<>(getSchedulerProperties());
schedulerProperties.put(prefix + ".imagePullSecret", secretName);
AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, schedulerProperties,
null, getCommandLineArgs(), randomName(), testApplication());
CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
CronJobSpec cronJobSpec = cronJob.getSpec();
List<LocalObjectReference> secrets = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec()
.getImagePullSecrets();
assertEquals("Unexpected image pull secret", secretName, secrets.get(0).getName());
kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
@Test
public void testTaskServiceAccountNameOverride() {
KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
if (kubernetesSchedulerProperties.getNamespace() == null) {
kubernetesSchedulerProperties.setNamespace("default");
}
KubernetesClient kubernetesClient = new DefaultKubernetesClient()
.inNamespace(kubernetesSchedulerProperties.getNamespace());
KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
kubernetesSchedulerProperties);
String taskServiceAccountName = "mysa";
String prefix = KubernetesSchedulerProperties.KUBERNETES_SCHEDULER_PROPERTIES_PREFIX;
Map<String, String> schedulerProperties = new HashMap<>(getSchedulerProperties());
schedulerProperties.put(prefix + ".taskServiceAccountName", taskServiceAccountName);
AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, schedulerProperties,
null, getCommandLineArgs(), randomName(), testApplication());
CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
CronJobSpec cronJobSpec = cronJob.getSpec();
String serviceAccountName = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec()
.getServiceAccountName();
assertEquals("Unexpected service account name", taskServiceAccountName, serviceAccountName);
kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
@Test
public void testJavaCmdOption() throws Exception {
Map<String, String> properties = new HashMap<>();
properties.put(PREFIX + ".javaCmd", "/test/java");
Resource resource = mock(Resource.class);
when(resource.getFile()).thenReturn(new File("/"));
AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(mock(AppDefinition.class), resource, properties);
String[] commands = commandBuilder.buildExecutionCommand(appDeploymentRequest, Collections.EMPTY_MAP, Optional.of(1));
assertThat(commands[0], is("/test/java"));
}
@Test
public void deployWithNodeSelectorDeploymentPropertyGlobalOverride() throws Exception {
AppDefinition definition = new AppDefinition("app-test", null);
Map<String, String> props = new HashMap<>();
props.put(KubernetesDeployerProperties.KUBERNETES_DEPLOYMENT_NODE_SELECTOR, "disktype:ssd, os: openbsd");
AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);
KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
kubernetesDeployerProperties.setNodeSelector("disktype:ssd, os:qnx");
deployer = new KubernetesAppDeployer(kubernetesDeployerProperties, null);
PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);
assertThat(podSpec.getNodeSelector()).containsOnly(entry("disktype", "ssd"), entry("os", "openbsd"));
}
@Test
public void testNameTooLong() {
thrown.expect(CreateScheduleException.class);
thrown.expectMessage("Schedule can not be created because its name " +
"'j1-scdf-itcouldbesaidthatthisislongtoowaytoo-oopsitcouldbesaidthatthisis" +
"longtoowaytoo-oopsitcouldbesaidthatthisislongtoowaytoo-oopsitcouldbe" +
"saidthatthisislongtoowaytoo-oopsitcouldbesaidthatthisislongtoowaytoo-" +
"oopsitcouldbesaidthatthisislongtoowaytoo-oops12' has too many characters. " +
"Schedule name length must be 255 characters or less");
Resource resource = new FileSystemResource("src/test/resources/demo-0.0.1-SNAPSHOT.jar");
mockAppResultsInAppList();
AppDefinition definition = new AppDefinition("test-application-1", null);
Map cronMap = new HashMap<String, String>();
cronMap.put(CRON_EXPRESSION, DEFAULT_CRON_EXPRESSION);
ScheduleRequest request = new ScheduleRequest(definition, cronMap, null,
"j1-scdf-itcouldbesaidthatthisislongtoowaytoo-oopsitcouldbesaidthatthisis" +
"longtoowaytoo-oopsitcouldbesaidthatthisislongtoowaytoo-oopsitcouldbe" +
"saidthatthisislongtoowaytoo-oopsitcouldbesaidthatthisislongtoowaytoo-" +
"oopsitcouldbesaidthatthisislongtoowaytoo-oops12", resource);
this.cloudFoundryAppScheduler.schedule(request);
assertThat(((TestJobs) this.client.jobs()).getCreateJobResponse()).isNull();
}
@Test
public void deployWithEnvironmentWithSingleCommaDelimitedValue() throws Exception {
AppDefinition definition = new AppDefinition("app-test", null);
Map<String, String> props = new HashMap<>();
props.put("spring.cloud.deployer.kubernetes.environmentVariables",
"JAVA_TOOL_OPTIONS='thing1,thing2'");
AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);
deployer = new KubernetesAppDeployer(bindDeployerProperties(), null);
PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);
assertThat(podSpec.getContainers().get(0).getEnv())
.contains(new EnvVar("JAVA_TOOL_OPTIONS", "thing1,thing2", null));
}