Skip to content

Commit 4e4c250

Browse files
authored
Re-enable integration tests (#332)
1 parent eb574b3 commit 4e4c250

File tree

13 files changed

+906
-0
lines changed

13 files changed

+906
-0
lines changed

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@
8383
<version>3.24.2</version>
8484
<scope>test</scope>
8585
</dependency>
86+
<dependency>
87+
<groupId>com.github.tomakehurst</groupId>
88+
<artifactId>wiremock-jre8-standalone</artifactId>
89+
<version>2.35.0</version>
90+
<scope>test</scope>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
94+
<artifactId>workflow-durable-task-step</artifactId>
95+
<scope>test</scope>
96+
</dependency>
97+
<dependency>
98+
<groupId>org.jenkins-ci.plugins</groupId>
99+
<artifactId>pipeline-stage-step</artifactId>
100+
<scope>test</scope>
101+
</dependency>
86102
</dependencies>
87103

88104
<repositories>

src/test/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherITest.java

Lines changed: 444 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package io.jenkins.plugins.checks.github;
2+
3+
import hudson.model.Action;
4+
import hudson.model.Result;
5+
import java.util.Collections;
6+
7+
import jenkins.model.ParameterizedJobMixIn;
8+
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
9+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
13+
import static org.assertj.core.api.Assertions.*;
14+
15+
import hudson.model.FreeStyleProject;
16+
import hudson.model.Run;
17+
import hudson.plugins.git.BranchSpec;
18+
import hudson.plugins.git.GitSCM;
19+
import org.jvnet.hudson.test.JenkinsRule;
20+
21+
/**
22+
* Integration tests for {@link GitSCMChecksContext}.
23+
*/
24+
public class GitSCMChecksContextITest {
25+
private static final String EXISTING_HASH = "4ecc8623b06d99d5f029b66927438554fdd6a467";
26+
private static final String HTTP_URL = "https://github.com/jenkinsci/github-checks-plugin.git";
27+
private static final String CREDENTIALS_ID = "credentials";
28+
private static final String URL_NAME = "url";
29+
30+
@Rule
31+
public JenkinsRule j = new JenkinsRule();
32+
33+
/**
34+
* Creates a FreeStyle job that uses {@link hudson.plugins.git.GitSCM} and runs a successful build.
35+
* Then this build is used to create a new {@link GitSCMChecksContext}. So the build actually is not publishing
36+
* the checks we just ensure that we can create the context with the successful build (otherwise we would need
37+
* Wiremock to handle the requests to GitHub).
38+
*/
39+
@Test
40+
public void shouldRetrieveContextFromFreeStyleBuild() throws Exception {
41+
FreeStyleProject job = j.createFreeStyleProject();
42+
43+
BranchSpec branchSpec = new BranchSpec(EXISTING_HASH);
44+
GitSCM scm = new GitSCM(GitSCM.createRepoList(HTTP_URL, CREDENTIALS_ID),
45+
Collections.singletonList(branchSpec), false, Collections.emptyList(),
46+
null, null, Collections.emptyList());
47+
job.setScm(scm);
48+
49+
Run<?, ?> run = buildSuccessfully(job);
50+
51+
GitSCMChecksContext gitSCMChecksContext = new GitSCMChecksContext(run, URL_NAME);
52+
53+
assertThat(gitSCMChecksContext.getRepository()).isEqualTo("jenkinsci/github-checks-plugin");
54+
assertThat(gitSCMChecksContext.getHeadSha()).isEqualTo(EXISTING_HASH);
55+
assertThat(gitSCMChecksContext.getCredentialsId()).isEqualTo(CREDENTIALS_ID);
56+
}
57+
58+
private Run<?, ?> buildSuccessfully(ParameterizedJobMixIn.ParameterizedJob<?, ?> job) throws Exception {
59+
return j.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0, new Action[0]));
60+
}
61+
62+
/**
63+
* Creates a pipeline that uses {@link hudson.plugins.git.GitSCM} and runs a successful build.
64+
* Then this build is used to create a new {@link GitSCMChecksContext}.
65+
*/
66+
@Test
67+
public void shouldRetrieveContextFromPipeline() throws Exception {
68+
WorkflowJob job = j.createProject(WorkflowJob.class);
69+
70+
job.setDefinition(new CpsFlowDefinition("node {\n"
71+
+ " stage ('Checkout') {\n"
72+
+ " checkout scm: ([\n"
73+
+ " $class: 'GitSCM',\n"
74+
+ " userRemoteConfigs: [[credentialsId: '" + CREDENTIALS_ID + "', url: '" + HTTP_URL + "']],\n"
75+
+ " branches: [[name: '" + EXISTING_HASH + "']]\n"
76+
+ " ])"
77+
+ " }\n"
78+
+ "}\n", true));
79+
80+
Run<?, ?> run = buildSuccessfully(job);
81+
82+
GitSCMChecksContext gitSCMChecksContext = new GitSCMChecksContext(run, URL_NAME);
83+
84+
assertThat(gitSCMChecksContext.getRepository()).isEqualTo("jenkinsci/github-checks-plugin");
85+
assertThat(gitSCMChecksContext.getCredentialsId()).isEqualTo(CREDENTIALS_ID);
86+
assertThat(gitSCMChecksContext.getHeadSha()).isEqualTo(EXISTING_HASH);
87+
}
88+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.jenkins.plugins.checks.github.config;
2+
3+
import hudson.util.StreamTaskListener;
4+
import io.jenkins.plugins.checks.github.GitHubChecksPublisherFactory;
5+
import java.io.IOException;
6+
import org.junit.Rule;
7+
import org.junit.Test;
8+
9+
import java.io.ByteArrayOutputStream;
10+
import org.jvnet.hudson.test.JenkinsRule;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
/**
15+
* Integration test for {@link GitHubChecksConfig}.
16+
*/
17+
public class GitHubChecksConfigITest {
18+
19+
@Rule
20+
public JenkinsRule j = new JenkinsRule();
21+
22+
/**
23+
* When a job has not {@link org.jenkinsci.plugins.github_branch_source.GitHubSCMSource} or
24+
* {@link hudson.plugins.git.GitSCM}, the default config should be used and no verbose log should be output.
25+
*/
26+
@Test
27+
public void shouldUseDefaultConfigWhenNoSCM() throws IOException {
28+
ByteArrayOutputStream os = new ByteArrayOutputStream();
29+
GitHubChecksPublisherFactory.fromJob(j.createFreeStyleProject(), new StreamTaskListener(os));
30+
31+
assertThat(os.toString()).doesNotContain("Causes for no suitable publisher found: ");
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.jenkins.plugins.checks.github.status;
2+
3+
import jenkins.scm.api.SCMHeadObserver;
4+
import jenkins.scm.api.SCMSourceCriteria;
5+
import org.jenkinsci.plugins.github_branch_source.GitHubSCMSourceContext;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.mockito.Mockito.mock;
10+
11+
class GitHubSCMSourceStatusChecksTraitTest {
12+
@Test
13+
void shouldOnlyApplyTraitConfigurationsToGitHubBranchSourceNotificationsWhenItsNotDisabled() {
14+
GitHubSCMSourceContext context = new GitHubSCMSourceContext(mock(SCMSourceCriteria.class),
15+
mock(SCMHeadObserver.class));
16+
GitHubSCMSourceStatusChecksTrait trait = new GitHubSCMSourceStatusChecksTrait();
17+
18+
// disable notifications, the trait configuration should be ignored
19+
context.withNotificationsDisabled(true);
20+
trait.setSkipNotifications(false);
21+
trait.decorateContext(context);
22+
assertThat(context.notificationsDisabled()).isTrue();
23+
24+
// enable notifications, the trait configuration should be applied
25+
context.withNotificationsDisabled(false);
26+
trait.setSkipNotifications(true);
27+
trait.decorateContext(context);
28+
assertThat(context.notificationsDisabled()).isTrue();
29+
}
30+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"token": "bogus",
3+
"expires_at": "2019-08-10T05:54:58Z",
4+
"permissions": {
5+
"checks": "write",
6+
"pull_requests": "write",
7+
"contents": "read",
8+
"metadata": "read"
9+
},
10+
"repository_selection": "selected",
11+
"repositories": [
12+
{
13+
"id": 111111111,
14+
"node_id": "asdfasdf",
15+
"name": "bogus",
16+
"full_name": "bogus/bogus",
17+
"private": true,
18+
"owner": {
19+
"login": "bogus",
20+
"id": 11111111,
21+
"node_id": "asdfasdf",
22+
"avatar_url": "https://avatars2.githubusercontent.com/u/11111111?v=4",
23+
"gravatar_id": "",
24+
"url": "https://api.github.com/users/bogus",
25+
"html_url": "https://github.com/bogus",
26+
"followers_url": "https://api.github.com/users/bogus/followers",
27+
"following_url": "https://api.github.com/users/bogus/following{/other_user}",
28+
"gists_url": "https://api.github.com/users/bogus/gists{/gist_id}",
29+
"starred_url": "https://api.github.com/users/bogus/starred{/owner}{/repo}",
30+
"subscriptions_url": "https://api.github.com/users/bogus/subscriptions",
31+
"organizations_url": "https://api.github.com/users/bogus/orgs",
32+
"repos_url": "https://api.github.com/users/bogus/repos",
33+
"events_url": "https://api.github.com/users/bogus/events{/privacy}",
34+
"received_events_url": "https://api.github.com/users/bogus/received_events",
35+
"type": "Organization",
36+
"site_admin": false
37+
},
38+
"html_url": "https://github.com/bogus/bogus",
39+
"description": null,
40+
"fork": false,
41+
"url": "https://api.github.com/repos/bogus/bogus",
42+
"forks_url": "https://api.github.com/repos/bogus/bogus/forks",
43+
"keys_url": "https://api.github.com/repos/bogus/bogus/keys{/key_id}",
44+
"collaborators_url": "https://api.github.com/repos/bogus/bogus/collaborators{/collaborator}",
45+
"teams_url": "https://api.github.com/repos/bogus/bogus/teams",
46+
"hooks_url": "https://api.github.com/repos/bogus/bogus/hooks",
47+
"issue_events_url": "https://api.github.com/repos/bogus/bogus/issues/events{/number}",
48+
"events_url": "https://api.github.com/repos/bogus/bogus/events",
49+
"assignees_url": "https://api.github.com/repos/bogus/bogus/assignees{/user}",
50+
"branches_url": "https://api.github.com/repos/bogus/bogus/branches{/branch}",
51+
"tags_url": "https://api.github.com/repos/bogus/bogus/tags",
52+
"blobs_url": "https://api.github.com/repos/bogus/bogus/git/blobs{/sha}",
53+
"git_tags_url": "https://api.github.com/repos/bogus/bogus/git/tags{/sha}",
54+
"git_refs_url": "https://api.github.com/repos/bogus/bogus/git/refs{/sha}",
55+
"trees_url": "https://api.github.com/repos/bogus/bogus/git/trees{/sha}",
56+
"statuses_url": "https://api.github.com/repos/bogus/bogus/statuses/{sha}",
57+
"languages_url": "https://api.github.com/repos/bogus/bogus/languages",
58+
"stargazers_url": "https://api.github.com/repos/bogus/bogus/stargazers",
59+
"contributors_url": "https://api.github.com/repos/bogus/bogus/contributors",
60+
"subscribers_url": "https://api.github.com/repos/bogus/bogus/subscribers",
61+
"subscription_url": "https://api.github.com/repos/bogus/bogus/subscription",
62+
"commits_url": "https://api.github.com/repos/bogus/bogus/commits{/sha}",
63+
"git_commits_url": "https://api.github.com/repos/bogus/bogus/git/commits{/sha}",
64+
"comments_url": "https://api.github.com/repos/bogus/bogus/comments{/number}",
65+
"issue_comment_url": "https://api.github.com/repos/bogus/bogus/issues/comments{/number}",
66+
"contents_url": "https://api.github.com/repos/bogus/bogus/contents/{+path}",
67+
"compare_url": "https://api.github.com/repos/bogus/bogus/compare/{base}...{head}",
68+
"merges_url": "https://api.github.com/repos/bogus/bogus/merges",
69+
"archive_url": "https://api.github.com/repos/bogus/bogus/{archive_format}{/ref}",
70+
"downloads_url": "https://api.github.com/repos/bogus/bogus/downloads",
71+
"issues_url": "https://api.github.com/repos/bogus/bogus/issues{/number}",
72+
"pulls_url": "https://api.github.com/repos/bogus/bogus/pulls{/number}",
73+
"milestones_url": "https://api.github.com/repos/bogus/bogus/milestones{/number}",
74+
"notifications_url": "https://api.github.com/repos/bogus/bogus/notifications{?since,all,participating}",
75+
"labels_url": "https://api.github.com/repos/bogus/bogus/labels{/name}",
76+
"releases_url": "https://api.github.com/repos/bogus/bogus/releases{/id}",
77+
"deployments_url": "https://api.github.com/repos/bogus/bogus/deployments",
78+
"created_at": "2018-09-06T03:25:38Z",
79+
"updated_at": "2018-09-30T22:04:06Z",
80+
"pushed_at": "2019-08-08T22:22:34Z",
81+
"git_url": "git://github.com/bogus/bogus.git",
82+
"ssh_url": "git@github.com:bogus/bogus.git",
83+
"clone_url": "https://github.com/bogus/bogus.git",
84+
"svn_url": "https://github.com/bogus/bogus",
85+
"homepage": null,
86+
"size": 618,
87+
"stargazers_count": 0,
88+
"watchers_count": 0,
89+
"language": "Java",
90+
"has_issues": true,
91+
"has_projects": true,
92+
"has_downloads": true,
93+
"has_wiki": true,
94+
"has_pages": false,
95+
"forks_count": 0,
96+
"mirror_url": null,
97+
"archived": false,
98+
"disabled": false,
99+
"open_issues_count": 5,
100+
"license": null,
101+
"forks": 0,
102+
"open_issues": 5,
103+
"watchers": 0,
104+
"default_branch": "main"
105+
}
106+
]
107+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"id": 11111,
3+
"node_id": "MDM6QXBwMzI2MTY=",
4+
"owner": {
5+
"login": "bogus",
6+
"id": 111111111,
7+
"node_id": "asdfasdfasdf",
8+
"avatar_url": "https://avatars2.githubusercontent.com/u/111111111?v=4",
9+
"gravatar_id": "",
10+
"url": "https://api.github.com/users/bogus",
11+
"html_url": "https://github.com/bogus",
12+
"followers_url": "https://api.github.com/users/bogus/followers",
13+
"following_url": "https://api.github.com/users/bogus/following{/other_user}",
14+
"gists_url": "https://api.github.com/users/bogus/gists{/gist_id}",
15+
"starred_url": "https://api.github.com/users/bogus/starred{/owner}{/repo}",
16+
"subscriptions_url": "https://api.github.com/users/bogus/subscriptions",
17+
"organizations_url": "https://api.github.com/users/bogus/orgs",
18+
"repos_url": "https://api.github.com/users/bogus/repos",
19+
"events_url": "https://api.github.com/users/bogus/events{/privacy}",
20+
"received_events_url": "https://api.github.com/users/bogus/received_events",
21+
"type": "Organization",
22+
"site_admin": false
23+
},
24+
"name": "Bogus-Development",
25+
"description": "",
26+
"external_url": "https://bogus.domain.com",
27+
"html_url": "https://github.com/apps/bogus-development",
28+
"created_at": "2019-06-10T04:21:41Z",
29+
"updated_at": "2019-06-10T04:21:41Z",
30+
"permissions": {
31+
"checks": "write",
32+
"contents": "read",
33+
"metadata": "read",
34+
"pull_requests": "write"
35+
},
36+
"events": [
37+
"pull_request",
38+
"push"
39+
],
40+
"installations_count": 1
41+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
[
2+
{
3+
"id": 11111111,
4+
"account": {
5+
"login": "bogus",
6+
"id": 111111111,
7+
"node_id": "asdfasdfasdf",
8+
"avatar_url": "https://avatars2.githubusercontent.com/u/111111111?v=4",
9+
"gravatar_id": "",
10+
"url": "https://api.github.com/users/bogus",
11+
"html_url": "https://github.com/bogus",
12+
"followers_url": "https://api.github.com/users/bogus/followers",
13+
"following_url": "https://api.github.com/users/bogus/following{/other_user}",
14+
"gists_url": "https://api.github.com/users/bogus/gists{/gist_id}",
15+
"starred_url": "https://api.github.com/users/bogus/starred{/owner}{/repo}",
16+
"subscriptions_url": "https://api.github.com/users/bogus/subscriptions",
17+
"organizations_url": "https://api.github.com/users/bogus/orgs",
18+
"repos_url": "https://api.github.com/users/bogus/repos",
19+
"events_url": "https://api.github.com/users/bogus/events{/privacy}",
20+
"received_events_url": "https://api.github.com/users/bogus/received_events",
21+
"type": "Organization",
22+
"site_admin": false
23+
},
24+
"repository_selection": "selected",
25+
"access_tokens_url": "https://api.github.com/app/installations/11111111/access_tokens",
26+
"repositories_url": "https://api.github.com/installation/repositories",
27+
"html_url": "https://github.com/organizations/bogus/settings/installations/11111111",
28+
"app_id": 11111,
29+
"target_id": 111111111,
30+
"target_type": "Organization",
31+
"permissions": {
32+
"checks": "write",
33+
"pull_requests": "write",
34+
"contents": "read",
35+
"metadata": "read"
36+
},
37+
"events": [
38+
"pull_request",
39+
"push"
40+
],
41+
"created_at": "2019-07-04T01:19:36.000Z",
42+
"updated_at": "2019-07-30T22:48:09.000Z",
43+
"single_file_name": null
44+
}
45+
]

0 commit comments

Comments
 (0)