/
GerritMissedEventsFunctionalTest.java
209 lines (184 loc) · 9.23 KB
/
GerritMissedEventsFunctionalTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* The MIT License
*
* Copyright 2014 rinrinne All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.playback;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer;
import com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl;
import com.sonyericsson.hudson.plugins.gerrit.trigger.api.GerritTriggerApi;
import com.sonyericsson.hudson.plugins.gerrit.trigger.api.exception.GerritTriggerException;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.Config;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause;
import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.DuplicatesUtil;
import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.Setup;
import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.TestUtils;
import com.sonymobile.tools.gerrit.gerritevents.Handler;
import com.sonymobile.tools.gerrit.gerritevents.mock.SshdServerMock;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import org.apache.sshd.SshServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static com.sonymobile.tools.gerrit.gerritevents.mock.SshdServerMock.GERRIT_STREAM_EVENTS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
//CS IGNORE AvoidStarImport FOR NEXT 1 LINES. REASON: UnitTest.
/**
* Functional Test for Missed Events Playback.
*
* @author scott.heber@ericsson.com;
*/
public class GerritMissedEventsFunctionalTest {
/**
* An instance of Jenkins Rule.
*/
// CS IGNORE VisibilityModifier FOR NEXT 2 LINES. REASON: JenkinsRule.
@Rule
public final JenkinsRule j = new JenkinsRule();
/**
* An instance of WireMock Rule.
*/
// CS IGNORE VisibilityModifier FOR NEXT 2 LINES. REASON: WireMockRule.
@Rule
public final WireMockRule wireMockRule = new WireMockRule(8089); // No-args constructor defaults to port 8089
private static final int HTTPOK = 200;
private static final int SLEEPTIME = 1000;
private final String gerritServerName = "testServer";
private final String projectName = "testProject";
private final int port = 29418;
private SshdServerMock server;
private SshServer sshd;
private SshdServerMock.KeyPairFiles sshKey;
/**
* Runs before test method.
*
* @throws Exception throw if so.
*/
@Before
public void setUp() throws Exception {
sshKey = SshdServerMock.generateKeyPair();
System.setProperty(PluginImpl.TEST_SSH_KEYFILE_LOCATION_PROPERTY, sshKey.getPrivateKey().getAbsolutePath());
server = new SshdServerMock();
sshd = SshdServerMock.startServer(port, server);
server.returnCommandFor("gerrit ls-projects", SshdServerMock.EofCommandMock.class);
server.returnCommandFor(GERRIT_STREAM_EVENTS, SshdServerMock.CommandMock.class);
server.returnCommandFor("gerrit review.*", SshdServerMock.EofCommandMock.class);
server.returnCommandFor("gerrit query.*", SshdServerMock.EofCommandMock.class);
server.returnCommandFor("gerrit version", SshdServerMock.EofCommandMock.class);
stubFor(get(urlEqualTo("/plugins/" + GerritMissedEventsPlaybackManager.EVENTS_LOG_PLUGIN_NAME + "/"))
.willReturn(aResponse()
.withStatus(HTTPOK)
.withHeader("Content-Type", "text/html")
.withBody("ok")));
}
/**
* Runs after test method.
*
* @throws Exception throw if so.
*/
@After
public void tearDown() throws Exception {
sshd.stop(true);
sshd = null;
}
/**
* Test the scenario whereby connection is restarted and events are missed
* but replayed.
* @throws Exception Error creating job.
*/
@Test
public void testRestartWithMissedEvents() throws Exception {
GerritServer gerritServer = new GerritServer(gerritServerName);
Config config = (Config)gerritServer.getConfig();
config.setUseRestApi(true);
config.setGerritHttpUserName("scott");
config.setGerritHttpPassword("scott");
config.setGerritFrontEndURL("http://localhost:8089");
config.setGerritHostName("localhost");
config.setGerritProxy("");
config.setGerritAuthKeyFile(sshKey.getPrivateKey());
gerritServer.setConfig(config);
PluginImpl.getInstance().addServer(gerritServer);
gerritServer.start();
gerritServer.startConnection();
FreeStyleProject project = DuplicatesUtil.createGerritTriggeredJob(j, projectName, gerritServerName);
GerritTriggerApi api = new GerritTriggerApi();
Handler handler = null;
try {
handler = api.getHandler();
} catch (GerritTriggerException ex) {
fail(ex.getMessage());
}
assertNotNull(handler);
handler.post(Setup.createPatchsetCreated(gerritServerName));
TestUtils.waitForBuilds(project, 1);
assertNotNull(gerritServer.getMissedEventsPlaybackManager().getServerTimestamp());
FreeStyleBuild buildOne = project.getLastCompletedBuild();
assertSame(Result.SUCCESS, buildOne.getResult());
assertEquals(1, project.getLastCompletedBuild().getNumber());
assertSame(gerritServerName, buildOne.getCause(GerritCause.class).getEvent().getProvider().getName());
gerritServer.stopConnection();
Thread.currentThread().sleep(SLEEPTIME);
String json = "{\"type\":\"patchset-created\",\"change\":{\"project\":\""
+ projectName
+ "\",\"branch\":\"develop\","
+ "\"id\":\"Icae2322236e0e521950a0232effda08d6ffcdab7\",\"number\":\"392335\",\"subject\":\""
+ "IPSEC: Small test code fixes due to Sonar warnings\",\"owner\":{\"name\":\"Szymon L\","
+ "\"email\":\"szymon.l@abc.com\",\"username\":\"eszyabc\"},\"url\":"
+ "\"https://abc.aaa.se/392335\","
+ "\"commitMessage\":\"IPSEC: Small test code fixes due to Sonar warnings\\n\\nChange-Id: "
+ "Icae2322236e0e521950a0232effda08d6ffcdab7\\nSigned-off-by:Szymon L \\u003cszymo"
+ "n.l@abc.com\\u003e\\n\",\"status\":\"NEW\"},\"patchSet\":{\"number\":\"2\",\"revision"
+ "\":\"607eea8f472235b3ee47483b630003250764dab2\",\"parents\":"
+ "[\"87c0e57d2497ab334584ec9d1a7953ebcf016e10\"],"
+ "\"ref\":\"refs/changes/35/392335/2\",\"uploader\":{\"name\":\"Szymon L\",\"email\":"
+ "\"szymon"
+ "@abc.com\",\"username\":\"eszyabc\"},\"createdOn\":1413448337,\"author\":{\"name\":\"Szy"
+ "mon L\",\"email\":\"szymon.l@abc.com\",\"username\":\"eszyabc\"},\"isDraft\""
+ ":false,\""
+ "sizeInsertions\":6,\"sizeDeletions\":-7},\"author\":{\"name\":\"Build user for \","
+ "\"email\":\"tnbuilder@"
+ "abc.se\",\"username\":\"tnabc\"},\"approvals\":[{\"type\":\"Verified\""
+ ",\"description\":"
+ "\"Verified\",\"value\":\"-1\"}],\"comment\":\"Patch Set 2: Verified-1\\n\\nBuild Failed \\n\\nhttp:"
+ "//jenkins/tn/job/tn-review/22579/ : FAILURE\",\"eventCreatedOn\":1418133772}\n";
stubFor(get(urlMatching(GerritMissedEventsPlaybackManagerTest.EVENTS_LOG_CHANGE_EVENTS_URL_REGEXP))
.willReturn(aResponse()
.withStatus(HTTPOK)
.withHeader("Content-Type", "text/html")
.withBody(json)));
gerritServer.restartConnection();
TestUtils.waitForBuilds(project, 2);
}
}