Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Digdag push validation on the server side. #1026

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
import io.digdag.core.repository.StoredProject;
import io.digdag.core.repository.StoredRevision;
import io.digdag.core.repository.StoredWorkflowDefinition;
import io.digdag.core.repository.WorkflowDefinition;
import io.digdag.core.repository.WorkflowDefinitionList;
import io.digdag.core.schedule.ScheduleStore;
import io.digdag.core.schedule.ScheduleStoreManager;
import io.digdag.core.schedule.SchedulerManager;
Expand All @@ -82,7 +84,9 @@
import io.digdag.core.session.SessionStoreManager;
import io.digdag.core.session.StoredSessionWithLastAttempt;
import io.digdag.core.storage.ArchiveManager;
import io.digdag.core.workflow.Workflow;
import io.digdag.core.workflow.WorkflowCompiler;
import io.digdag.core.workflow.WorkflowTask;
import io.digdag.server.GenericJsonExceptionHandler;
import io.digdag.spi.DirectDownloadHandle;
import io.digdag.spi.SecretControlStore;
Expand Down Expand Up @@ -549,6 +553,8 @@ public RestProject putProject(@QueryParam("project") String name, @QueryParam("r
if (md5Count.getCount() != contentLength) {
throw new IllegalArgumentException("Content-Length header doesn't match with uploaded data size");
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep the logic of putProject method simple, can you separate your validation logic from this method and copy the logic to another private method that you will create?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separated validation method.

validateWorkflowAndSchedule(meta);
}

ArchiveManager.Location location =
Expand Down Expand Up @@ -681,6 +687,26 @@ private void validateTarEntry(TarArchiveEntry entry)
}
}

private void validateWorkflowAndSchedule(ArchiveMetadata meta)
{
WorkflowDefinitionList defs = meta.getWorkflowList();
for (WorkflowDefinition def : defs.get()) {
Workflow wf = compiler.compile(def.getName(), def.getConfig());

// validate workflow and schedule
for (WorkflowTask task : wf.getTasks()) {
// raise an exception if task doesn't valid.
task.getConfig();
}
Revision rev = Revision.builderFromArchive("check", meta, getUserInfo())
.archiveType(ArchiveType.NONE)
.build();
// raise an exception if "schedule:" is invalid.
srm.tryGetScheduler(rev, def);

}
}

@PUT
@Consumes("application/json")
@Path("/api/projects/{id}/secrets/{key}")
Expand Down
87 changes: 87 additions & 0 deletions digdag-tests/src/test/java/acceptance/ValidateProjectIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package acceptance;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import utils.CommandStatus;
import utils.TemporaryDigdagServer;

import java.nio.file.Path;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static utils.TestUtils.copyResource;
import static utils.TestUtils.main;

//
// This file doesn't contain normal case.
// It defined in another test.
//
public class ValidateProjectIT
{
@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Rule
public TemporaryDigdagServer server = TemporaryDigdagServer.builder()
.build();

private Path config;
private Path projectDir;

@Before
public void setUp()
throws Exception
{
projectDir = folder.getRoot().toPath().resolve("foobar");
config = folder.newFile().toPath();
}

@Test
public void uploadInvalidTaskProject()
throws Exception
{
// Create new project
CommandStatus initStatus = main("init",
"-c", config.toString(),
projectDir.toString());
assertThat(initStatus.code(), is(0));

copyResource("acceptance/error_task/invalid_at_group.dig", projectDir.resolve("invalid_at_group.dig"));

// Push the project
CommandStatus pushStatus = main(
"push",
"--project", projectDir.toString(),
"foobar",
"-c", config.toString(),
"-e", server.endpoint());
assertThat(pushStatus.code(), is(1));
assertThat(pushStatus.errUtf8(), containsString("A task can't have more than one operator"));
}

@Test
public void uploadInvalidScheduleProject()
throws Exception
{
// Create new project
CommandStatus initStatus = main("init",
"-c", config.toString(),
projectDir.toString());
assertThat(initStatus.code(), is(0));

copyResource("acceptance/schedule/invalid_schedule.dig", projectDir.resolve("invalid_schedule.dig"));

// Push the project
CommandStatus pushStatus = main(
"push",
"--project", projectDir.toString(),
"foobar",
"-c", config.toString(),
"-e", server.endpoint());
assertThat(pushStatus.code(), is(1));
assertThat(pushStatus.errUtf8(), containsString("scheduler requires mm:ss format"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
timezone: UTC

schedule:
hourly>: "10:11:12" # correct format is "MM:SS"

+foo:
sh>: "touch foo.out"