|
| 1 | +package com.gitee.jenkins.webhook.status; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.when; |
| 4 | + |
| 5 | +import hudson.Launcher; |
| 6 | +import hudson.model.AbstractBuild; |
| 7 | +import hudson.model.BuildListener; |
| 8 | +import hudson.model.FreeStyleBuild; |
| 9 | +import hudson.model.FreeStyleProject; |
| 10 | +import hudson.model.Result; |
| 11 | +import hudson.plugins.git.GitSCM; |
| 12 | +import hudson.util.OneShotEvent; |
| 13 | +import jakarta.servlet.ServletException; |
| 14 | +import jakarta.servlet.ServletOutputStream; |
| 15 | +import java.io.ByteArrayOutputStream; |
| 16 | +import java.io.File; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.PrintWriter; |
| 19 | +import org.eclipse.jgit.api.Git; |
| 20 | +import org.eclipse.jgit.revwalk.RevCommit; |
| 21 | +import org.junit.jupiter.api.BeforeAll; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 25 | +import org.junit.jupiter.api.io.TempDir; |
| 26 | +import org.jvnet.hudson.test.JenkinsRule; |
| 27 | +import org.jvnet.hudson.test.TestBuilder; |
| 28 | +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; |
| 29 | +import org.kohsuke.stapler.StaplerResponse2; |
| 30 | +import org.mockito.Mock; |
| 31 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 32 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 33 | +import org.mockito.quality.Strictness; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author Robin Müller |
| 37 | + */ |
| 38 | +@WithJenkins |
| 39 | +@ExtendWith(MockitoExtension.class) |
| 40 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 41 | +abstract class BuildStatusActionTest { |
| 42 | + |
| 43 | + private static JenkinsRule jenkins; |
| 44 | + |
| 45 | + @TempDir |
| 46 | + private File tmp; |
| 47 | + |
| 48 | + protected String commitSha1; |
| 49 | + protected String branch = "master"; |
| 50 | + |
| 51 | + @Mock |
| 52 | + private StaplerResponse2 response; |
| 53 | + |
| 54 | + private String gitRepoUrl; |
| 55 | + |
| 56 | + @BeforeAll |
| 57 | + static void setUp(JenkinsRule rule) { |
| 58 | + jenkins = rule; |
| 59 | + } |
| 60 | + |
| 61 | + @BeforeEach |
| 62 | + void setUp() throws Exception { |
| 63 | + Git.init().setDirectory(tmp).call(); |
| 64 | + File.createTempFile("test", null, tmp); |
| 65 | + Git git = Git.open(tmp); |
| 66 | + git.add().addFilepattern("test"); |
| 67 | + RevCommit commit = git.commit().setSign(false).setMessage("test").call(); |
| 68 | + commitSha1 = commit.getId().getName(); |
| 69 | + gitRepoUrl = tmp.toURI().toString(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void successfulBuild() throws Exception { |
| 74 | + FreeStyleProject testProject = jenkins.createFreeStyleProject(); |
| 75 | + testProject.setScm(new GitSCM(gitRepoUrl)); |
| 76 | + FreeStyleBuild build = testProject.scheduleBuild2(0).get(); |
| 77 | + |
| 78 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 79 | + mockResponse(out); |
| 80 | + getBuildStatusAction(testProject).execute(response); |
| 81 | + |
| 82 | + assertSuccessfulBuild(build, out, response); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void failedBuild() throws Exception { |
| 87 | + FreeStyleProject testProject = jenkins.createFreeStyleProject(); |
| 88 | + testProject.setScm(new GitSCM(gitRepoUrl)); |
| 89 | + testProject.getBuildersList().add(new TestBuilder() { |
| 90 | + @Override |
| 91 | + public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) { |
| 92 | + build.setResult(Result.FAILURE); |
| 93 | + return true; |
| 94 | + } |
| 95 | + }); |
| 96 | + FreeStyleBuild build = testProject.scheduleBuild2(0).get(); |
| 97 | + |
| 98 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 99 | + mockResponse(out); |
| 100 | + getBuildStatusAction(testProject).execute(response); |
| 101 | + |
| 102 | + assertFailedBuild(build, out, response); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void runningBuild() throws Exception { |
| 107 | + final OneShotEvent buildStarted = new OneShotEvent(); |
| 108 | + final OneShotEvent keepRunning = new OneShotEvent(); |
| 109 | + FreeStyleProject testProject = jenkins.createFreeStyleProject(); |
| 110 | + testProject.setScm(new GitSCM(gitRepoUrl)); |
| 111 | + testProject.getBuildersList().add(new TestBuilder() { |
| 112 | + @Override |
| 113 | + public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) |
| 114 | + throws InterruptedException { |
| 115 | + buildStarted.signal(); |
| 116 | + keepRunning.block(); |
| 117 | + return true; |
| 118 | + } |
| 119 | + }); |
| 120 | + FreeStyleBuild build = testProject.scheduleBuild2(0).waitForStart(); |
| 121 | + buildStarted.block(); |
| 122 | + |
| 123 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 124 | + mockResponse(out); |
| 125 | + getBuildStatusAction(testProject).execute(response); |
| 126 | + keepRunning.signal(); |
| 127 | + |
| 128 | + assertRunningBuild(build, out, response); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void canceledBuild() throws Exception { |
| 133 | + FreeStyleProject testProject = jenkins.createFreeStyleProject(); |
| 134 | + testProject.setScm(new GitSCM(gitRepoUrl)); |
| 135 | + testProject.getBuildersList().add(new TestBuilder() { |
| 136 | + @Override |
| 137 | + public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) |
| 138 | + throws IOException { |
| 139 | + try { |
| 140 | + build.doStop(); |
| 141 | + } catch (ServletException e) { |
| 142 | + throw new IOException(e); |
| 143 | + } |
| 144 | + return true; |
| 145 | + } |
| 146 | + }); |
| 147 | + FreeStyleBuild build = testProject.scheduleBuild2(0).get(); |
| 148 | + |
| 149 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 150 | + mockResponse(out); |
| 151 | + getBuildStatusAction(testProject).execute(response); |
| 152 | + |
| 153 | + assertCanceledBuild(build, out, response); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void unstableBuild() throws Exception { |
| 158 | + FreeStyleProject testProject = jenkins.createFreeStyleProject(); |
| 159 | + testProject.setScm(new GitSCM(gitRepoUrl)); |
| 160 | + testProject.getBuildersList().add(new TestBuilder() { |
| 161 | + @Override |
| 162 | + public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) { |
| 163 | + build.setResult(Result.UNSTABLE); |
| 164 | + return true; |
| 165 | + } |
| 166 | + }); |
| 167 | + FreeStyleBuild build = testProject.scheduleBuild2(0).get(); |
| 168 | + |
| 169 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 170 | + mockResponse(out); |
| 171 | + getBuildStatusAction(testProject).execute(response); |
| 172 | + |
| 173 | + assertUnstableBuild(build, out, response); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + void notFoundBuild() throws Exception { |
| 178 | + FreeStyleProject testProject = jenkins.createFreeStyleProject(); |
| 179 | + testProject.setScm(new GitSCM(gitRepoUrl)); |
| 180 | + |
| 181 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 182 | + mockResponse(out); |
| 183 | + getBuildStatusAction(testProject).execute(response); |
| 184 | + |
| 185 | + assertNotFoundBuild(out, response); |
| 186 | + } |
| 187 | + |
| 188 | + protected abstract BuildStatusAction getBuildStatusAction(FreeStyleProject project); |
| 189 | + |
| 190 | + protected abstract void assertSuccessfulBuild( |
| 191 | + FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) throws Exception; |
| 192 | + |
| 193 | + protected abstract void assertFailedBuild( |
| 194 | + FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) throws Exception; |
| 195 | + |
| 196 | + protected abstract void assertRunningBuild( |
| 197 | + FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) throws Exception; |
| 198 | + |
| 199 | + protected abstract void assertCanceledBuild( |
| 200 | + FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) throws Exception; |
| 201 | + |
| 202 | + protected abstract void assertUnstableBuild( |
| 203 | + FreeStyleBuild build, ByteArrayOutputStream out, StaplerResponse2 response) throws Exception; |
| 204 | + |
| 205 | + protected abstract void assertNotFoundBuild(ByteArrayOutputStream out, StaplerResponse2 response) throws Exception; |
| 206 | + |
| 207 | + private void mockResponse(final ByteArrayOutputStream out) throws Exception { |
| 208 | + ServletOutputStream servletOutputStream = new ServletOutputStream() { |
| 209 | + @Override |
| 210 | + public void write(int b) { |
| 211 | + out.write(b); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public void setWriteListener(jakarta.servlet.WriteListener writeListener) {} |
| 216 | + |
| 217 | + @Override |
| 218 | + public boolean isReady() { |
| 219 | + return true; |
| 220 | + } |
| 221 | + }; |
| 222 | + when(response.getOutputStream()).thenReturn(servletOutputStream); |
| 223 | + when(response.getWriter()).thenReturn(new PrintWriter(servletOutputStream)); |
| 224 | + } |
| 225 | +} |
0 commit comments