Skip to content

Commit

Permalink
Give human readable errors when schedule name is invalid.
Browse files Browse the repository at this point in the history
resolves #320
  • Loading branch information
cppwfs authored and chrisjs committed Aug 22, 2019
1 parent 878104d commit 5304c78
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import io.fabric8.kubernetes.api.model.Container;
Expand Down Expand Up @@ -67,6 +68,9 @@ public KubernetesScheduler(KubernetesClient kubernetesClient,

@Override
public void schedule(ScheduleRequest scheduleRequest) {
if(scheduleRequest != null) {
validateScheduleName(scheduleRequest);
}
try {
createCronJob(scheduleRequest);
}
Expand All @@ -81,6 +85,16 @@ public void schedule(ScheduleRequest scheduleRequest) {
}
}

public void validateScheduleName(ScheduleRequest request) {
if(request.getScheduleName() == null) {
throw new CreateScheduleException("The name for the schedule request is null", null);
}
if(!Pattern.matches("^[a-z0-9]([-a-z0-9]*[a-z0-9])?$", request.getScheduleName())) {
throw new CreateScheduleException("Invalid Format for Schedule Name. Name can only contain lowercase letters, numbers 0-9 and hyphens.", null);
}

}

@Override
public void unschedule(String scheduleName) {
boolean unscheduled = this.kubernetesClient.batch().cronjobs().withName(scheduleName).delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ public void testMissingSchedule() {
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(expected = CreateScheduleException.class)
public void testInvalidCronSyntax() {
Map<String, String> schedulerProperties = Collections.singletonMap(CRON_EXPRESSION, "1 2 3 4");
Expand Down

0 comments on commit 5304c78

Please sign in to comment.