Java源码示例:org.jvnet.hudson.test.TestBuilder
示例1
@Test
public void push_ciSkip() throws IOException, InterruptedException {
final OneShotEvent buildTriggered = new OneShotEvent();
FreeStyleProject project = jenkins.createFreeStyleProject();
project.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
buildTriggered.signal();
return true;
}
});
project.setQuietPeriod(0);
pushHookTriggerHandler.handle(project, pushHook()
.withCommits(Arrays.asList(commit().withMessage("some message").build(),
commit().withMessage("[ci-skip]").build()))
.build(), true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(false));
}
示例2
@Test
/**
* always triggers since pipeline events do not contain ci skip message
*/
public void pipeline_ciSkip() throws IOException, InterruptedException {
final OneShotEvent buildTriggered = new OneShotEvent();
FreeStyleProject project = jenkins.createFreeStyleProject();
project.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
buildTriggered.signal();
return true;
}
});
project.setQuietPeriod(0);
pipelineHookTriggerHandler.handle(project, pipelineHook , true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(true));
}
示例3
@Test
public void pipeline_build() throws IOException, InterruptedException, GitAPIException, ExecutionException {
final OneShotEvent buildTriggered = new OneShotEvent();
FreeStyleProject project = jenkins.createFreeStyleProject();
project.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
buildTriggered.signal();
return true;
}
});
project.setQuietPeriod(0);
pipelineHookTriggerHandler.handle(project, pipelineHook, false, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(true));
}
示例4
private boolean ciSkipTestHelper(String MRDescription, String lastCommitMsg) throws IOException, InterruptedException {
final OneShotEvent buildTriggered = new OneShotEvent();
FreeStyleProject project = jenkins.createFreeStyleProject();
project.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
buildTriggered.signal();
return true;
}
});
project.setQuietPeriod(0);
MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.opened, State.reopened), Arrays.asList(Action.approved), false, false, false);
mergeRequestHookTriggerHandler.handle(project, mergeRequestHook()
.withObjectAttributes(defaultMergeRequestObjectAttributes().withDescription(MRDescription).withLastCommit(commit().withMessage(lastCommitMsg).withAuthor(user().withName("test").build()).withId("testid").build()).build())
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
return buildTriggered.isSignaled();
}
示例5
protected void subscribeProject(final ProjectFixture fixture) throws Exception {
String name = UUID.randomUUID().toString();
final FreeStyleProject job = jenkinsRule.getInstance().createProject(FreeStyleProject.class, name);
job.setScm(new NullSCM());
if (fixture.getScm() != null) {
job.setScm(fixture.getScm());
}
final String uuid = this.sqsQueue.getUuid();
SQSTrigger trigger = null;
if (fixture.isHasTrigger()) {
trigger = new SQSTrigger(uuid, fixture.isSubscribeInternalScm(), fixture.getScmConfigs());
}
// final OneShotEvent event = new OneShotEvent();
fixture.setEvent(new OneShotEvent());
job.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
fixture.setLastBuild(job.getLastBuild());
fixture.getEvent().signal();
return true;
}
});
job.setQuietPeriod(0);
if (trigger != null) {
trigger.start(job, false);
job.addTrigger(trigger);
}
// fixture.setEvent(event);
}
示例6
@Test
public void testArtifactsRunApi() throws Exception {
FreeStyleProject p = j.createFreeStyleProject("pipeline1");
p.getBuildersList().add(new TestBuilder() {
@Override public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
FilePath ws = build.getWorkspace();
if (ws == null) {
return false;
}
FilePath dir = ws.child("dir");
dir.mkdirs();
dir.child("fizz").write("contents", null);
dir.child("lodge").symlinkTo("fizz", listener);
return true;
}
});
ArtifactArchiver aa = new ArtifactArchiver("dir/fizz");
aa.setAllowEmptyArchive(true);
p.getPublishersList().add(aa);
FreeStyleBuild b = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
List artifacts = get("/organizations/jenkins/pipelines/pipeline1/runs/"+b.getId()+"/artifacts", List.class);
assertEquals(1, artifacts.size());
assertEquals("fizz", ((Map) artifacts.get(0)).get("name"));
String artifactId = (String) ((Map) artifacts.get(0)).get("id");
ArtifactContainerImpl container = new ArtifactContainerImpl(b, new Reachable() {
@Override
public Link getLink() {
return new Link("/blue/rest/organizations/jenkins/pipelines/pipeline1/runs/1/artifacts/");
}
});
BlueArtifact blueArtifact = container.get(artifactId);
assertNotNull(blueArtifact);
}
示例7
private TestBuilder createMarathonFileBuilder(final String payload) {
return new TestBuilder() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("marathon.json").write(payload, "UTF-8");
return true;
}
};
}
示例8
public static void addCopyBuildStep(
FreeStyleProject p,
final String fileName,
final Class resourceClass,
final String resourceName) {
p.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener buildListener) throws
InterruptedException, IOException {
build.getWorkspace().child(fileName).copyFrom(resourceClass.getResourceAsStream(resourceName));
return true;
}
});
}
示例9
private Builder echoBuilder(final String fileName, final String content) {
return new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws
InterruptedException, IOException {
build.getWorkspace().child(fileName).write(content, "UTF-8");
return true;
}
};
}
示例10
private Builder echoBuilder(final String fileName, final String content) {
return new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws
InterruptedException, IOException {
build.getWorkspace().child(fileName).write(content, "UTF-8");
return true;
}
};
}
示例11
@Test
public void testGetZipWithZip() throws Exception {
final OneShotEvent buildEnded = new OneShotEvent();
FreeStyleProject p = j.createFreeStyleProject();
p.getBuildersList().add(new TestBuilder() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("echo.zip").copyFrom(new FileInputStream(testUtil.getResource("echo.zip")));
buildEnded.signal();
return true;
}
});
p.scheduleBuild2(0);
buildEnded.block();
JenkinsLogger logger = new JenkinsLogger(System.out);
WorkSpaceZipper workSpaceZipper = new WorkSpaceZipper(p.getSomeWorkspace(), logger);
File zip = workSpaceZipper.getZip("echo.zip");
assertTrue(zip.exists());
assertTrue(zip.getAbsolutePath().contains("awslambda-"));
ZipFile zipFile = new ZipFile(zip);
assertNotNull(zipFile);
assertNotNull(zipFile.getEntry("index.js"));
}
示例12
@Test
public void testGetZipFolder() throws Exception {
final OneShotEvent buildEnded = new OneShotEvent();
FreeStyleProject p = j.createFreeStyleProject();
p.getBuildersList().add(new TestBuilder() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("echo").child("index.js").copyFrom(new FileInputStream(testUtil.getResource("echo/index.js")));
buildEnded.signal();
return true;
}
});
p.scheduleBuild2(0);
buildEnded.block();
JenkinsLogger logger = new JenkinsLogger(System.out);
WorkSpaceZipper workSpaceZipper = new WorkSpaceZipper(p.getSomeWorkspace(), logger);
File zip = workSpaceZipper.getZip("echo");
assertTrue(zip.exists());
assertTrue(zip.getAbsolutePath().contains("awslambda-"));
ZipFile zipFile = new ZipFile(zip);
assertNotNull(zipFile);
assertNotNull(zipFile.getEntry("index.js"));
}
示例13
@Test
public void testGetZipFolderEmpty() throws Exception {
final OneShotEvent buildEnded = new OneShotEvent();
FreeStyleProject p = j.createFreeStyleProject();
p.getBuildersList().add(new TestBuilder() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("echo").mkdirs();
buildEnded.signal();
return true;
}
});
p.scheduleBuild2(0);
buildEnded.block();
JenkinsLogger logger = new JenkinsLogger(System.out);
WorkSpaceZipper workSpaceZipper = new WorkSpaceZipper(p.getSomeWorkspace(), logger);
File zip = workSpaceZipper.getZip("echo");
assertTrue(zip.exists());
assertTrue(zip.getAbsolutePath().contains("awslambda-"));
ZipFile zipFile = new ZipFile(zip);
assertNotNull(zipFile);
assertFalse(zipFile.entries().hasMoreElements());
}
示例14
private FreeStyleBuild configureTestBuild(String projectName) throws Exception {
FreeStyleProject p = projectName == null ? rule.createFreeStyleProject() : rule.createFreeStyleProject(projectName);
p.getBuildersList().add(new TestBuilder() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("junit.xml").copyFrom(
getClass().getResource("junit-report-20090516.xml"));
return true;
}
});
p.getPublishersList().add(new JUnitResultArchiver("*.xml"));
return rule.assertBuildStatus(Result.UNSTABLE, p.scheduleBuild2(0).get());
}
示例15
/**
* Creates a freestyle project & build with UNSTABLE status
* containing a test report from: /hudson/tasks/junit/junit-report-20090516.xml
*/
private FreeStyleBuild configureTestBuild(String projectName) throws Exception {
FreeStyleProject p = j.createFreeStyleProject(projectName);
p.getBuildersList().add(new TestBuilder() {
@Override
@SuppressWarnings("null")
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("junit.xml").copyFrom(
getClass().getResource("/hudson/tasks/junit/junit-report-20090516.xml"));
return true;
}
});
p.getPublishersList().add(new JUnitResultArchiver("*.xml"));
return j.assertBuildStatus(Result.UNSTABLE, p.scheduleBuild2(0).get());
}
示例16
@Test
public void note_ciSkip() throws IOException, InterruptedException {
final OneShotEvent buildTriggered = new OneShotEvent();
FreeStyleProject project = jenkins.createFreeStyleProject();
project.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
buildTriggered.signal();
return true;
}
});
Date currentDate = new Date();
project.setQuietPeriod(0);
noteHookTriggerHandler.handle(project, noteHook()
.withObjectAttributes(noteObjectAttributes()
.withId(1)
.withNote("ci-run")
.withAuthorId(1)
.withProjectId(1)
.withCreatedAt(currentDate)
.withUpdatedAt(currentDate)
.withUrl("https://gitlab.org/test/merge_requests/1#note_1")
.build())
.withMergeRequest(mergeRequestObjectAttributes().withDescription("[ci-skip]").build())
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(false));
}
示例17
private OneShotEvent doHandle(MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler,
MergeRequestObjectAttributesBuilder objectAttributes) throws GitAPIException, IOException,
InterruptedException {
Git.init().setDirectory(tmp.getRoot()).call();
tmp.newFile("test");
Git git = Git.open(tmp.getRoot());
git.add().addFilepattern("test");
RevCommit commit = git.commit().setMessage("test").call();
ObjectId head = git.getRepository().resolve(Constants.HEAD);
String repositoryUrl = tmp.getRoot().toURI().toString();
final OneShotEvent buildTriggered = new OneShotEvent();
FreeStyleProject project = jenkins.createFreeStyleProject();
project.setScm(new GitSCM(repositoryUrl));
project.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
buildTriggered.signal();
return true;
}
});
project.setQuietPeriod(0);
mergeRequestHookTriggerHandler.handle(project, mergeRequestHook()
.withObjectAttributes(objectAttributes
.withTargetBranch("refs/heads/" + git.nameRev().add(head).call().get(head))
.withLastCommit(commit().withAuthor(user().withName("test").build()).withId(commit.getName()).build())
.build())
.withProject(project()
.withWebUrl("https://gitlab.org/test.git")
.build()
)
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
return buildTriggered;
}
示例18
@Test
public void interoperability() throws Exception {
final Semaphore semaphore = new Semaphore(1);
LockableResourcesManager.get().createResource("resource1");
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(
new CpsFlowDefinition(
"lock('resource1') {\n" + " echo 'Locked'\n" + "}\n" + "echo 'Finish'", true));
FreeStyleProject f = j.createFreeStyleProject("f");
f.addProperty(new RequiredResourcesProperty("resource1", null, null, null, null));
f.getBuildersList()
.add(
new TestBuilder() {
@Override
public boolean perform(
AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
semaphore.acquire();
return true;
}
});
semaphore.acquire();
FreeStyleBuild f1 = f.scheduleBuild2(0).waitForStart();
WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
j.waitForMessage("[resource1] is locked by " + f1.getFullDisplayName() + ", waiting...", b1);
isPaused(b1, 1, 1);
semaphore.release();
// Wait for lock after the freestyle finishes
j.waitForMessage("Lock released on resource [resource1]", b1);
isPaused(b1, 1, 0);
}
示例19
public static TestBuilder createFile(final @Nonnull String pathToFile) {
return createFile(pathToFile, "all of us with wings");
}
示例20
public static TestBuilder createFile(final @Nonnull String pathToFile, final @Nonnull String content) {
return new TestAppWriter(pathToFile, content);
}
示例21
@Test
public void push_build() throws IOException, InterruptedException, GitAPIException, ExecutionException {
Git.init().setDirectory(tmp.getRoot()).call();
tmp.newFile("test");
Git git = Git.open(tmp.getRoot());
git.add().addFilepattern("test");
RevCommit commit = git.commit().setMessage("test").call();
ObjectId head = git.getRepository().resolve(Constants.HEAD);
String repositoryUrl = tmp.getRoot().toURI().toString();
final OneShotEvent buildTriggered = new OneShotEvent();
FreeStyleProject project = jenkins.createFreeStyleProject();
project.setScm(new GitSCM(repositoryUrl));
project.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
buildTriggered.signal();
return true;
}
});
project.setQuietPeriod(0);
pushHookTriggerHandler.handle(project, pushHook()
.withBefore("0000000000000000000000000000000000000000")
.withProjectId(1)
.withUserName("test")
.withObjectKind("tag_push")
.withRepository(repository()
.withName("test")
.withHomepage("https://gitlab.org/test")
.withUrl("[email protected]:test.git")
.withGitSshUrl("[email protected]:test.git")
.withGitHttpUrl("https://gitlab.org/test.git")
.build())
.withProject(project()
.withNamespace("test-namespace")
.withWebUrl("https://gitlab.org/test")
.build())
.withAfter(commit.name())
.withRef("refs/heads/" + git.nameRev().add(head).call().get(head))
.build(), true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(true));
}
示例22
@Test
public void push_build2DifferentBranchesButSameCommit() throws IOException, InterruptedException, GitAPIException, ExecutionException {
Git.init().setDirectory(tmp.getRoot()).call();
tmp.newFile("test");
Git git = Git.open(tmp.getRoot());
git.add().addFilepattern("test");
RevCommit commit = git.commit().setMessage("test").call();
ObjectId head = git.getRepository().resolve(Constants.HEAD);
String repositoryUrl = tmp.getRoot().toURI().toString();
final AtomicInteger buildCount = new AtomicInteger(0);
final OneShotEvent buildTriggered = new OneShotEvent();
FreeStyleProject project = jenkins.createFreeStyleProject();
project.setScm(new GitSCM(repositoryUrl));
project.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
int count = buildCount.incrementAndGet();
if (count == 2) {
buildTriggered.signal();
}
return true;
}
});
project.setQuietPeriod(0);
PushHookBuilder pushHookBuilder = pushHook()
.withBefore("0000000000000000000000000000000000000000")
.withProjectId(1)
.withUserName("test")
.withObjectKind("push")
.withRepository(repository()
.withName("test")
.withHomepage("https://gitlab.org/test")
.withUrl("[email protected]:test.git")
.withGitSshUrl("[email protected]:test.git")
.withGitHttpUrl("https://gitlab.org/test.git")
.build())
.withProject(project()
.withNamespace("test-namespace")
.withWebUrl("https://gitlab.org/test")
.build())
.withAfter(commit.name())
.withRef("refs/heads/" + git.nameRev().add(head).call().get(head));
pushHookTriggerHandler.handle(project, pushHookBuilder.build(), true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
pushHookTriggerHandler.handle(project, pushHookBuilder
.but().withRef("refs/heads/" + git.nameRev().add(head).call().get(head) + "-2").build(), true,
newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(true));
assertThat(buildCount.intValue(), is(2));
}
示例23
@Test
public void note_build() throws IOException, InterruptedException, GitAPIException, ExecutionException {
Git.init().setDirectory(tmp.getRoot()).call();
tmp.newFile("test");
Git git = Git.open(tmp.getRoot());
git.add().addFilepattern("test");
RevCommit commit = git.commit().setMessage("test").call();
ObjectId head = git.getRepository().resolve(Constants.HEAD);
String repositoryUrl = tmp.getRoot().toURI().toString();
final OneShotEvent buildTriggered = new OneShotEvent();
FreeStyleProject project = jenkins.createFreeStyleProject();
project.setScm(new GitSCM(repositoryUrl));
project.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
buildTriggered.signal();
return true;
}
});
Date currentDate = new Date();
project.setQuietPeriod(0);
noteHookTriggerHandler.handle(project, noteHook()
.withObjectAttributes(noteObjectAttributes()
.withId(1)
.withNote("ci-run")
.withAuthorId(1)
.withProjectId(1)
.withCreatedAt(currentDate)
.withUpdatedAt(currentDate)
.withUrl("https://gitlab.org/test/merge_requests/1#note_1")
.build())
.withMergeRequest(mergeRequestObjectAttributes()
.withTargetBranch("refs/heads/" + git.nameRev().add(head).call().get(head))
.withState(State.opened)
.withIid(1)
.withTitle("test")
.withTargetProjectId(1)
.withSourceProjectId(1)
.withSourceBranch("feature")
.withTargetBranch("master")
.withLastCommit(commit().withAuthor(user().withName("test").build()).withId(commit.getName()).build())
.withSource(project()
.withName("test")
.withNamespace("test-namespace")
.withHomepage("https://gitlab.org/test")
.withUrl("[email protected]:test.git")
.withSshUrl("[email protected]:test.git")
.withHttpUrl("https://gitlab.org/test.git")
.build())
.withTarget(project()
.withName("test")
.withNamespace("test-namespace")
.withHomepage("https://gitlab.org/test")
.withUrl("[email protected]:test.git")
.withSshUrl("[email protected]:test.git")
.withHttpUrl("https://gitlab.org/test.git")
.withWebUrl("https://gitlab.org/test.git")
.build())
.build())
.build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(true));
}