2627 |
07 Sep 06 |
nicklas |
1 |
/** |
2627 |
07 Sep 06 |
nicklas |
$Id$ |
2627 |
07 Sep 06 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2006 Nicklas Nordborg |
2627 |
07 Sep 06 |
nicklas |
5 |
|
2627 |
07 Sep 06 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
2627 |
07 Sep 06 |
nicklas |
Available at http://base.thep.lu.se/ |
2627 |
07 Sep 06 |
nicklas |
8 |
|
2627 |
07 Sep 06 |
nicklas |
BASE is free software; you can redistribute it and/or |
2627 |
07 Sep 06 |
nicklas |
modify it under the terms of the GNU General Public License |
4480 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
2627 |
07 Sep 06 |
nicklas |
of the License, or (at your option) any later version. |
2627 |
07 Sep 06 |
nicklas |
13 |
|
2627 |
07 Sep 06 |
nicklas |
BASE is distributed in the hope that it will be useful, |
2627 |
07 Sep 06 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2627 |
07 Sep 06 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2627 |
07 Sep 06 |
nicklas |
GNU General Public License for more details. |
2627 |
07 Sep 06 |
nicklas |
18 |
|
2627 |
07 Sep 06 |
nicklas |
You should have received a copy of the GNU General Public License |
4514 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
2627 |
07 Sep 06 |
nicklas |
21 |
*/ |
2627 |
07 Sep 06 |
nicklas |
22 |
|
2627 |
07 Sep 06 |
nicklas |
23 |
import java.net.InetAddress; |
2634 |
12 Sep 06 |
nicklas |
24 |
import java.net.Socket; |
2627 |
07 Sep 06 |
nicklas |
25 |
import java.util.Date; |
2641 |
14 Sep 06 |
nicklas |
26 |
import java.util.Properties; |
5340 |
10 May 10 |
nicklas |
27 |
import java.util.Set; |
2627 |
07 Sep 06 |
nicklas |
28 |
|
2634 |
12 Sep 06 |
nicklas |
29 |
import net.sf.basedb.clients.jobagent.Agent; |
5446 |
15 Oct 10 |
nicklas |
30 |
import net.sf.basedb.clients.jobagent.handlers.AbstractCustomRequestHandler; |
2634 |
12 Sep 06 |
nicklas |
31 |
import net.sf.basedb.clients.jobagent.handlers.DefaultRequestHandler; |
2634 |
12 Sep 06 |
nicklas |
32 |
|
2627 |
07 Sep 06 |
nicklas |
33 |
import net.sf.basedb.core.BaseException; |
2627 |
07 Sep 06 |
nicklas |
34 |
import net.sf.basedb.core.DbControl; |
2627 |
07 Sep 06 |
nicklas |
35 |
import net.sf.basedb.core.Item; |
5340 |
10 May 10 |
nicklas |
36 |
import net.sf.basedb.core.ItemProxy; |
2627 |
07 Sep 06 |
nicklas |
37 |
import net.sf.basedb.core.ItemResultList; |
2627 |
07 Sep 06 |
nicklas |
38 |
import net.sf.basedb.core.Job; |
2627 |
07 Sep 06 |
nicklas |
39 |
import net.sf.basedb.core.JobAgent; |
2627 |
07 Sep 06 |
nicklas |
40 |
import net.sf.basedb.core.JobAgentSettings; |
2627 |
07 Sep 06 |
nicklas |
41 |
import net.sf.basedb.core.Permission; |
2627 |
07 Sep 06 |
nicklas |
42 |
import net.sf.basedb.core.PluginDefinition; |
2627 |
07 Sep 06 |
nicklas |
43 |
import net.sf.basedb.core.StringParameterType; |
2634 |
12 Sep 06 |
nicklas |
44 |
import net.sf.basedb.util.jobagent.JobAgentConnection; |
2632 |
08 Sep 06 |
nicklas |
45 |
import net.sf.basedb.util.jobagent.RequestHandler; |
2627 |
07 Sep 06 |
nicklas |
46 |
|
2627 |
07 Sep 06 |
nicklas |
47 |
/** |
2627 |
07 Sep 06 |
nicklas |
48 |
|
2627 |
07 Sep 06 |
nicklas |
@author nicklas |
2627 |
07 Sep 06 |
nicklas |
@version 2.0 |
2627 |
07 Sep 06 |
nicklas |
@base.modified $Date$ |
2627 |
07 Sep 06 |
nicklas |
52 |
*/ |
2627 |
07 Sep 06 |
nicklas |
53 |
public class TestJobAgent |
2627 |
07 Sep 06 |
nicklas |
54 |
{ |
2627 |
07 Sep 06 |
nicklas |
55 |
static boolean ok = true; |
2627 |
07 Sep 06 |
nicklas |
56 |
|
2627 |
07 Sep 06 |
nicklas |
57 |
public static void main(String[] args) |
2627 |
07 Sep 06 |
nicklas |
58 |
{ |
2627 |
07 Sep 06 |
nicklas |
59 |
TestUtil.checkArgs(args); |
2627 |
07 Sep 06 |
nicklas |
60 |
TestUtil.begin(); |
2627 |
07 Sep 06 |
nicklas |
61 |
ok = test_all(); |
2627 |
07 Sep 06 |
nicklas |
62 |
TestUtil.stop(); |
2627 |
07 Sep 06 |
nicklas |
63 |
} |
2627 |
07 Sep 06 |
nicklas |
64 |
|
2627 |
07 Sep 06 |
nicklas |
65 |
static boolean test_all() |
2627 |
07 Sep 06 |
nicklas |
66 |
{ |
2627 |
07 Sep 06 |
nicklas |
67 |
write("++Testing job agents"); |
2627 |
07 Sep 06 |
nicklas |
68 |
write_header(); |
2627 |
07 Sep 06 |
nicklas |
// Standard tests: create, load, list |
2641 |
14 Sep 06 |
nicklas |
70 |
create_fake_jobagent(8888, "net.sf.baseb.clients.agent.test.1"); |
2641 |
14 Sep 06 |
nicklas |
71 |
create_fake_jobagent(8889, "net.sf.baseb.clients.agent.test.2"); |
2627 |
07 Sep 06 |
nicklas |
72 |
|
2627 |
07 Sep 06 |
nicklas |
73 |
int id = test_create("net.sf.baseb.clients.agent.test.1", 8888, true); |
2627 |
07 Sep 06 |
nicklas |
74 |
int id2 = test_create("net.sf.baseb.clients.agent.test.2", 8889, false); |
2627 |
07 Sep 06 |
nicklas |
75 |
test_load(id); |
2627 |
07 Sep 06 |
nicklas |
76 |
test_list(-1); |
2627 |
07 Sep 06 |
nicklas |
77 |
|
2627 |
07 Sep 06 |
nicklas |
// Extra test: create settings for plugin |
2627 |
07 Sep 06 |
nicklas |
79 |
int pluginId = TestPluginDefinition.test_get("net.sf.basedb.plugins.HelpExporter"); |
2627 |
07 Sep 06 |
nicklas |
80 |
test_create_settings(id, pluginId); |
2627 |
07 Sep 06 |
nicklas |
81 |
test_list_settings(id, 1); |
2627 |
07 Sep 06 |
nicklas |
82 |
|
2627 |
07 Sep 06 |
nicklas |
// Extra test: create job and list it for the agent |
2627 |
07 Sep 06 |
nicklas |
84 |
int jobId = test_create_job(id, pluginId); |
2627 |
07 Sep 06 |
nicklas |
85 |
test_list_jobs(id, 1); |
2627 |
07 Sep 06 |
nicklas |
86 |
|
5446 |
15 Oct 10 |
nicklas |
// Extra test: send custom control command |
5446 |
15 Oct 10 |
nicklas |
88 |
test_send_control_command(id, "foo://hello"); |
5446 |
15 Oct 10 |
nicklas |
89 |
test_send_control_command(id2, "foo://world"); |
5446 |
15 Oct 10 |
nicklas |
90 |
|
2627 |
07 Sep 06 |
nicklas |
91 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
2627 |
07 Sep 06 |
nicklas |
92 |
TestJob.test_delete(jobId); |
2627 |
07 Sep 06 |
nicklas |
93 |
test_delete_settings(id, pluginId); |
2627 |
07 Sep 06 |
nicklas |
94 |
|
2627 |
07 Sep 06 |
nicklas |
// Standard test: Delete |
2627 |
07 Sep 06 |
nicklas |
96 |
test_delete(id2); |
2627 |
07 Sep 06 |
nicklas |
97 |
test_delete(id); |
2634 |
12 Sep 06 |
nicklas |
98 |
|
2634 |
12 Sep 06 |
nicklas |
99 |
stop_fake_jobagent(8888); |
2634 |
12 Sep 06 |
nicklas |
100 |
stop_fake_jobagent(8889); |
2627 |
07 Sep 06 |
nicklas |
101 |
write("++Testing job agents "+(ok ? "OK" : "Failed")+"\n"); |
2627 |
07 Sep 06 |
nicklas |
102 |
return ok; |
2627 |
07 Sep 06 |
nicklas |
103 |
} |
2627 |
07 Sep 06 |
nicklas |
104 |
|
2627 |
07 Sep 06 |
nicklas |
105 |
static int test_create(String externalId, Integer port, boolean setAll) |
2627 |
07 Sep 06 |
nicklas |
106 |
{ |
2627 |
07 Sep 06 |
nicklas |
107 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.JOBAGENT)) return 0; |
2627 |
07 Sep 06 |
nicklas |
108 |
int id = 0; |
2627 |
07 Sep 06 |
nicklas |
109 |
DbControl dc = null; |
2627 |
07 Sep 06 |
nicklas |
110 |
try |
2627 |
07 Sep 06 |
nicklas |
111 |
{ |
2627 |
07 Sep 06 |
nicklas |
112 |
dc = TestUtil.getDbControl(); |
2627 |
07 Sep 06 |
nicklas |
113 |
JobAgent j = JobAgent.getNew(dc, externalId); |
2627 |
07 Sep 06 |
nicklas |
114 |
if (setAll) |
2627 |
07 Sep 06 |
nicklas |
115 |
{ |
2627 |
07 Sep 06 |
nicklas |
116 |
j.setName("Test job agent"); |
2627 |
07 Sep 06 |
nicklas |
117 |
j.setDescription("Added at "+new Date()); |
2627 |
07 Sep 06 |
nicklas |
118 |
} |
2627 |
07 Sep 06 |
nicklas |
119 |
if (port != null) |
2627 |
07 Sep 06 |
nicklas |
120 |
{ |
2627 |
07 Sep 06 |
nicklas |
121 |
j.setServer(InetAddress.getLocalHost().getHostAddress()); |
2627 |
07 Sep 06 |
nicklas |
122 |
j.setPort(port); |
2627 |
07 Sep 06 |
nicklas |
123 |
} |
2627 |
07 Sep 06 |
nicklas |
124 |
dc.saveItem(j); |
2627 |
07 Sep 06 |
nicklas |
125 |
dc.commit(); |
2627 |
07 Sep 06 |
nicklas |
126 |
id = j.getId(); |
2627 |
07 Sep 06 |
nicklas |
127 |
write_item(0, j); |
2627 |
07 Sep 06 |
nicklas |
128 |
write("--Create job agent OK"); |
2627 |
07 Sep 06 |
nicklas |
129 |
} |
2627 |
07 Sep 06 |
nicklas |
130 |
catch (Throwable ex) |
2627 |
07 Sep 06 |
nicklas |
131 |
{ |
2627 |
07 Sep 06 |
nicklas |
132 |
write("--Create job agent FAILED"); |
2627 |
07 Sep 06 |
nicklas |
133 |
ex.printStackTrace(); |
2627 |
07 Sep 06 |
nicklas |
134 |
ok = false; |
2627 |
07 Sep 06 |
nicklas |
135 |
} |
2627 |
07 Sep 06 |
nicklas |
136 |
finally |
2627 |
07 Sep 06 |
nicklas |
137 |
{ |
2627 |
07 Sep 06 |
nicklas |
138 |
if (dc != null) dc.close(); |
2627 |
07 Sep 06 |
nicklas |
139 |
} |
2627 |
07 Sep 06 |
nicklas |
140 |
return id; |
2627 |
07 Sep 06 |
nicklas |
141 |
} |
2627 |
07 Sep 06 |
nicklas |
142 |
|
2627 |
07 Sep 06 |
nicklas |
143 |
static void test_load(int id) |
2627 |
07 Sep 06 |
nicklas |
144 |
{ |
2627 |
07 Sep 06 |
nicklas |
145 |
if (id == 0) return; |
2627 |
07 Sep 06 |
nicklas |
146 |
DbControl dc = null; |
2627 |
07 Sep 06 |
nicklas |
147 |
try |
2627 |
07 Sep 06 |
nicklas |
148 |
{ |
2627 |
07 Sep 06 |
nicklas |
149 |
dc = TestUtil.getDbControl(); |
2627 |
07 Sep 06 |
nicklas |
150 |
JobAgent j = JobAgent.getById(dc, id); |
2627 |
07 Sep 06 |
nicklas |
151 |
write_item(0, j); |
2627 |
07 Sep 06 |
nicklas |
152 |
write("--Load job agent OK"); |
2627 |
07 Sep 06 |
nicklas |
153 |
} |
2627 |
07 Sep 06 |
nicklas |
154 |
catch (Throwable ex) |
2627 |
07 Sep 06 |
nicklas |
155 |
{ |
2627 |
07 Sep 06 |
nicklas |
156 |
write("--Load job agent FAILED"); |
2627 |
07 Sep 06 |
nicklas |
157 |
ex.printStackTrace(); |
2627 |
07 Sep 06 |
nicklas |
158 |
ok = false; |
2627 |
07 Sep 06 |
nicklas |
159 |
} |
2627 |
07 Sep 06 |
nicklas |
160 |
finally |
2627 |
07 Sep 06 |
nicklas |
161 |
{ |
2627 |
07 Sep 06 |
nicklas |
162 |
if (dc != null) dc.close(); |
2627 |
07 Sep 06 |
nicklas |
163 |
} |
2627 |
07 Sep 06 |
nicklas |
164 |
} |
2627 |
07 Sep 06 |
nicklas |
165 |
|
2627 |
07 Sep 06 |
nicklas |
166 |
static void test_list(int expectedResults) |
2627 |
07 Sep 06 |
nicklas |
167 |
{ |
2627 |
07 Sep 06 |
nicklas |
168 |
DbControl dc = null; |
2627 |
07 Sep 06 |
nicklas |
169 |
try |
2627 |
07 Sep 06 |
nicklas |
170 |
{ |
2627 |
07 Sep 06 |
nicklas |
171 |
dc = TestUtil.getDbControl(); |
2627 |
07 Sep 06 |
nicklas |
172 |
ItemResultList<JobAgent> l = JobAgent.getQuery().list(dc); |
2627 |
07 Sep 06 |
nicklas |
173 |
for (int i = 0; i<l.size(); i++) |
2627 |
07 Sep 06 |
nicklas |
174 |
{ |
2627 |
07 Sep 06 |
nicklas |
175 |
write_item(i, l.get(i)); |
2627 |
07 Sep 06 |
nicklas |
176 |
} |
2627 |
07 Sep 06 |
nicklas |
177 |
if (expectedResults >= 0 && expectedResults != l.size()) |
2627 |
07 Sep 06 |
nicklas |
178 |
{ |
2627 |
07 Sep 06 |
nicklas |
179 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
2627 |
07 Sep 06 |
nicklas |
180 |
} |
2627 |
07 Sep 06 |
nicklas |
181 |
write("--List job agents OK ("+l.size()+")"); |
2627 |
07 Sep 06 |
nicklas |
182 |
} |
2627 |
07 Sep 06 |
nicklas |
183 |
catch (Throwable ex) |
2627 |
07 Sep 06 |
nicklas |
184 |
{ |
2627 |
07 Sep 06 |
nicklas |
185 |
write("--List job agents FAILED"); |
2627 |
07 Sep 06 |
nicklas |
186 |
ex.printStackTrace(); |
2627 |
07 Sep 06 |
nicklas |
187 |
ok = false; |
2627 |
07 Sep 06 |
nicklas |
188 |
} |
2627 |
07 Sep 06 |
nicklas |
189 |
finally |
2627 |
07 Sep 06 |
nicklas |
190 |
{ |
2627 |
07 Sep 06 |
nicklas |
191 |
if (dc != null) dc.close(); |
2627 |
07 Sep 06 |
nicklas |
192 |
} |
2627 |
07 Sep 06 |
nicklas |
193 |
} |
2627 |
07 Sep 06 |
nicklas |
194 |
|
2627 |
07 Sep 06 |
nicklas |
195 |
static void test_delete(int id) |
2627 |
07 Sep 06 |
nicklas |
196 |
{ |
2627 |
07 Sep 06 |
nicklas |
197 |
if (id == 0) return; |
2627 |
07 Sep 06 |
nicklas |
198 |
DbControl dc = null; |
2627 |
07 Sep 06 |
nicklas |
199 |
try |
2627 |
07 Sep 06 |
nicklas |
200 |
{ |
2627 |
07 Sep 06 |
nicklas |
201 |
dc = TestUtil.getDbControl(); |
2627 |
07 Sep 06 |
nicklas |
202 |
JobAgent j = JobAgent.getById(dc, id); |
2627 |
07 Sep 06 |
nicklas |
203 |
dc.deleteItem(j); |
5340 |
10 May 10 |
nicklas |
204 |
Set<ItemProxy> using = j.getUsingItems(); |
5340 |
10 May 10 |
nicklas |
205 |
if (using.size() > 0) |
5340 |
10 May 10 |
nicklas |
206 |
{ |
5340 |
10 May 10 |
nicklas |
207 |
throw new BaseException(j + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
208 |
} |
2627 |
07 Sep 06 |
nicklas |
209 |
dc.commit(); |
2627 |
07 Sep 06 |
nicklas |
210 |
write("--Delete job agent OK"); |
2627 |
07 Sep 06 |
nicklas |
211 |
} |
2627 |
07 Sep 06 |
nicklas |
212 |
catch (Throwable ex) |
2627 |
07 Sep 06 |
nicklas |
213 |
{ |
2627 |
07 Sep 06 |
nicklas |
214 |
write("--Delete job agent FAILED"); |
2627 |
07 Sep 06 |
nicklas |
215 |
ex.printStackTrace(); |
2627 |
07 Sep 06 |
nicklas |
216 |
ok = false; |
2627 |
07 Sep 06 |
nicklas |
217 |
} |
2627 |
07 Sep 06 |
nicklas |
218 |
finally |
2627 |
07 Sep 06 |
nicklas |
219 |
{ |
2627 |
07 Sep 06 |
nicklas |
220 |
if (dc != null) dc.close(); |
2627 |
07 Sep 06 |
nicklas |
221 |
} |
2627 |
07 Sep 06 |
nicklas |
222 |
} |
2627 |
07 Sep 06 |
nicklas |
223 |
|
2627 |
07 Sep 06 |
nicklas |
224 |
static void write(String message) |
2627 |
07 Sep 06 |
nicklas |
225 |
{ |
2627 |
07 Sep 06 |
nicklas |
226 |
System.out.println(message); |
2627 |
07 Sep 06 |
nicklas |
227 |
} |
2627 |
07 Sep 06 |
nicklas |
228 |
static void write_header() |
2627 |
07 Sep 06 |
nicklas |
229 |
{ |
2627 |
07 Sep 06 |
nicklas |
230 |
if (!TestUtil.getSilent()) |
2627 |
07 Sep 06 |
nicklas |
231 |
{ |
2627 |
07 Sep 06 |
nicklas |
232 |
write(" \tID \tName \tDescription\tExternal id\tServer\tPort\tCPU Usage\tMemory usage"); |
2627 |
07 Sep 06 |
nicklas |
233 |
write("-- \t-- \t--------- \t-----------\t-----------\t------\t----\t---------\t------------"); |
2627 |
07 Sep 06 |
nicklas |
234 |
} |
2627 |
07 Sep 06 |
nicklas |
235 |
} |
2627 |
07 Sep 06 |
nicklas |
236 |
static void write_item(int i, JobAgent j) |
2627 |
07 Sep 06 |
nicklas |
237 |
throws BaseException |
2627 |
07 Sep 06 |
nicklas |
238 |
{ |
2627 |
07 Sep 06 |
nicklas |
239 |
if (!TestUtil.getSilent()) |
2627 |
07 Sep 06 |
nicklas |
240 |
{ |
2627 |
07 Sep 06 |
nicklas |
241 |
System.out.println(i+":\t"+j.getId()+"\t"+j.getName()+"\t"+j.getDescription()+"\t"+ |
2627 |
07 Sep 06 |
nicklas |
242 |
j.getExternalId()+"\t"+j.getServer()+"\t"+j.getPort()+"\t"+ |
2627 |
07 Sep 06 |
nicklas |
243 |
j.getCpuUsage()+"%\t"+j.getUsedMemory()); |
2627 |
07 Sep 06 |
nicklas |
244 |
} |
2627 |
07 Sep 06 |
nicklas |
245 |
} |
2627 |
07 Sep 06 |
nicklas |
246 |
static void write_item(int i, JobAgentSettings j) |
2627 |
07 Sep 06 |
nicklas |
247 |
throws BaseException |
2627 |
07 Sep 06 |
nicklas |
248 |
{ |
2627 |
07 Sep 06 |
nicklas |
249 |
if (!TestUtil.getSilent()) |
2627 |
07 Sep 06 |
nicklas |
250 |
{ |
5595 |
17 Mar 11 |
nicklas |
251 |
System.out.println(i+":\t"+j.getId()+"\t"+j.getPluginDefinition()+"\t"+j.isTrusted()+"\t"+ |
2627 |
07 Sep 06 |
nicklas |
252 |
j.getMaxMemory()+"\t"+j.getPriorityBoost()); |
2627 |
07 Sep 06 |
nicklas |
253 |
} |
2627 |
07 Sep 06 |
nicklas |
254 |
} |
2627 |
07 Sep 06 |
nicklas |
255 |
static void write_item(int i, Job j, JobAgentSettings settings) |
2627 |
07 Sep 06 |
nicklas |
256 |
throws BaseException |
2627 |
07 Sep 06 |
nicklas |
257 |
{ |
2627 |
07 Sep 06 |
nicklas |
258 |
if (!TestUtil.getSilent()) |
2627 |
07 Sep 06 |
nicklas |
259 |
{ |
2627 |
07 Sep 06 |
nicklas |
260 |
System.out.println(i+":\t"+j.getId()+"\t"+j.getName()+"\t"+j.getDescription()+"\t"+j.getPluginDefinition()+"\t"+ |
2627 |
07 Sep 06 |
nicklas |
261 |
j.getPriority()+"\t"+settings.getPriorityBoost()+"\t"+settings.getEffectivePriority(j)); |
2627 |
07 Sep 06 |
nicklas |
262 |
} |
2627 |
07 Sep 06 |
nicklas |
263 |
} |
2627 |
07 Sep 06 |
nicklas |
264 |
|
2627 |
07 Sep 06 |
nicklas |
265 |
static void test_create_settings(int agentId, int pluginId) |
2627 |
07 Sep 06 |
nicklas |
266 |
{ |
2627 |
07 Sep 06 |
nicklas |
267 |
if (agentId == 0 || pluginId == 0) return; |
2627 |
07 Sep 06 |
nicklas |
268 |
DbControl dc = null; |
2627 |
07 Sep 06 |
nicklas |
269 |
try |
2627 |
07 Sep 06 |
nicklas |
270 |
{ |
2627 |
07 Sep 06 |
nicklas |
271 |
dc = TestUtil.getDbControl(); |
2627 |
07 Sep 06 |
nicklas |
272 |
JobAgent j = JobAgent.getById(dc, agentId); |
2627 |
07 Sep 06 |
nicklas |
273 |
PluginDefinition plugin = PluginDefinition.getById(dc, pluginId); |
2630 |
08 Sep 06 |
nicklas |
274 |
JobAgentSettings settings = j.getSettings(plugin, true); |
2627 |
07 Sep 06 |
nicklas |
275 |
settings.setPriorityBoost(1); |
2627 |
07 Sep 06 |
nicklas |
276 |
settings.setTrusted(false); |
2627 |
07 Sep 06 |
nicklas |
277 |
settings.setMaxMemory(50l*1024l*1024l); |
2627 |
07 Sep 06 |
nicklas |
278 |
if (!settings.isInDatabase()) dc.saveItem(settings); |
2627 |
07 Sep 06 |
nicklas |
279 |
dc.commit(); |
2627 |
07 Sep 06 |
nicklas |
280 |
dc = TestUtil.getDbControl(); |
5060 |
19 Aug 09 |
nicklas |
281 |
dc.reattachItem(settings, false); |
2627 |
07 Sep 06 |
nicklas |
282 |
write_item(0, settings); |
2627 |
07 Sep 06 |
nicklas |
283 |
write("--Create settings for job agent OK"); |
2627 |
07 Sep 06 |
nicklas |
284 |
} |
2627 |
07 Sep 06 |
nicklas |
285 |
catch (Throwable ex) |
2627 |
07 Sep 06 |
nicklas |
286 |
{ |
2627 |
07 Sep 06 |
nicklas |
287 |
write("--Create settings for job agent FAILED"); |
2627 |
07 Sep 06 |
nicklas |
288 |
ex.printStackTrace(); |
2627 |
07 Sep 06 |
nicklas |
289 |
ok = false; |
2627 |
07 Sep 06 |
nicklas |
290 |
} |
2627 |
07 Sep 06 |
nicklas |
291 |
finally |
2627 |
07 Sep 06 |
nicklas |
292 |
{ |
2627 |
07 Sep 06 |
nicklas |
293 |
if (dc != null) dc.close(); |
2627 |
07 Sep 06 |
nicklas |
294 |
} |
2627 |
07 Sep 06 |
nicklas |
295 |
} |
2627 |
07 Sep 06 |
nicklas |
296 |
|
2627 |
07 Sep 06 |
nicklas |
297 |
static void test_list_settings(int agentId, int expectedResults) |
2627 |
07 Sep 06 |
nicklas |
298 |
{ |
2627 |
07 Sep 06 |
nicklas |
299 |
if (agentId == 0) return; |
2627 |
07 Sep 06 |
nicklas |
300 |
DbControl dc = null; |
2627 |
07 Sep 06 |
nicklas |
301 |
try |
2627 |
07 Sep 06 |
nicklas |
302 |
{ |
2627 |
07 Sep 06 |
nicklas |
303 |
dc = TestUtil.getDbControl(); |
2627 |
07 Sep 06 |
nicklas |
304 |
JobAgent j = JobAgent.getById(dc, agentId); |
2627 |
07 Sep 06 |
nicklas |
305 |
ItemResultList<JobAgentSettings> l = j.getSettings().list(dc); |
2627 |
07 Sep 06 |
nicklas |
306 |
for (int i = 0; i<l.size(); i++) |
2627 |
07 Sep 06 |
nicklas |
307 |
{ |
2627 |
07 Sep 06 |
nicklas |
308 |
write_item(i, l.get(i)); |
2627 |
07 Sep 06 |
nicklas |
309 |
} |
2627 |
07 Sep 06 |
nicklas |
310 |
if (expectedResults >= 0 && expectedResults != l.size()) |
2627 |
07 Sep 06 |
nicklas |
311 |
{ |
2627 |
07 Sep 06 |
nicklas |
312 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
2627 |
07 Sep 06 |
nicklas |
313 |
} |
2627 |
07 Sep 06 |
nicklas |
314 |
write("--List settings for job agent OK ("+l.size()+")"); |
2627 |
07 Sep 06 |
nicklas |
315 |
} |
2627 |
07 Sep 06 |
nicklas |
316 |
catch (Throwable ex) |
2627 |
07 Sep 06 |
nicklas |
317 |
{ |
2627 |
07 Sep 06 |
nicklas |
318 |
write("--List settings for job agent FAILED"); |
2627 |
07 Sep 06 |
nicklas |
319 |
ex.printStackTrace(); |
2627 |
07 Sep 06 |
nicklas |
320 |
ok = false; |
2627 |
07 Sep 06 |
nicklas |
321 |
} |
2627 |
07 Sep 06 |
nicklas |
322 |
finally |
2627 |
07 Sep 06 |
nicklas |
323 |
{ |
2627 |
07 Sep 06 |
nicklas |
324 |
if (dc != null) dc.close(); |
2627 |
07 Sep 06 |
nicklas |
325 |
} |
2627 |
07 Sep 06 |
nicklas |
326 |
} |
2627 |
07 Sep 06 |
nicklas |
327 |
|
2627 |
07 Sep 06 |
nicklas |
328 |
static void test_delete_settings(int agentId, int pluginId) |
2627 |
07 Sep 06 |
nicklas |
329 |
{ |
2627 |
07 Sep 06 |
nicklas |
330 |
if (agentId == 0 || pluginId == 0) return; |
2627 |
07 Sep 06 |
nicklas |
331 |
DbControl dc = null; |
2627 |
07 Sep 06 |
nicklas |
332 |
try |
2627 |
07 Sep 06 |
nicklas |
333 |
{ |
2627 |
07 Sep 06 |
nicklas |
334 |
dc = TestUtil.getDbControl(); |
2627 |
07 Sep 06 |
nicklas |
335 |
JobAgent j = JobAgent.getById(dc, agentId); |
2627 |
07 Sep 06 |
nicklas |
336 |
PluginDefinition plugin = PluginDefinition.getById(dc, pluginId); |
2630 |
08 Sep 06 |
nicklas |
337 |
JobAgentSettings settings = j.getSettings(plugin, false); |
2630 |
08 Sep 06 |
nicklas |
338 |
if (settings != null && settings.isInDatabase()) dc.deleteItem(settings); |
2627 |
07 Sep 06 |
nicklas |
339 |
dc.commit(); |
2627 |
07 Sep 06 |
nicklas |
340 |
write("--Delete settings for job agent OK"); |
2627 |
07 Sep 06 |
nicklas |
341 |
} |
2627 |
07 Sep 06 |
nicklas |
342 |
catch (Throwable ex) |
2627 |
07 Sep 06 |
nicklas |
343 |
{ |
2627 |
07 Sep 06 |
nicklas |
344 |
write("--Delete settings for job agent FAILED"); |
2627 |
07 Sep 06 |
nicklas |
345 |
ex.printStackTrace(); |
2627 |
07 Sep 06 |
nicklas |
346 |
ok = false; |
2627 |
07 Sep 06 |
nicklas |
347 |
} |
2627 |
07 Sep 06 |
nicklas |
348 |
finally |
2627 |
07 Sep 06 |
nicklas |
349 |
{ |
2627 |
07 Sep 06 |
nicklas |
350 |
if (dc != null) dc.close(); |
2627 |
07 Sep 06 |
nicklas |
351 |
} |
2627 |
07 Sep 06 |
nicklas |
352 |
} |
2627 |
07 Sep 06 |
nicklas |
353 |
|
2627 |
07 Sep 06 |
nicklas |
354 |
static int test_create_job(int agentId, int pluginId) |
2627 |
07 Sep 06 |
nicklas |
355 |
{ |
2627 |
07 Sep 06 |
nicklas |
356 |
if (pluginId == 0 || !TestUtil.hasPermission(Permission.CREATE, Item.JOB)) return 0; |
2627 |
07 Sep 06 |
nicklas |
357 |
int id = 0; |
2627 |
07 Sep 06 |
nicklas |
358 |
DbControl dc = null; |
2627 |
07 Sep 06 |
nicklas |
359 |
try |
2627 |
07 Sep 06 |
nicklas |
360 |
{ |
2627 |
07 Sep 06 |
nicklas |
361 |
dc = TestUtil.getDbControl(); |
2627 |
07 Sep 06 |
nicklas |
362 |
PluginDefinition plugin = PluginDefinition.getById(dc, pluginId); |
4254 |
28 Apr 08 |
nicklas |
363 |
Job job = Job.getNew(dc, plugin, null, null); |
2627 |
07 Sep 06 |
nicklas |
364 |
job.setName(plugin.getName()); |
6433 |
14 Mar 14 |
nicklas |
365 |
job.setParameterValue("dummy", new StringParameterType(), "dummy"); |
6433 |
14 Mar 14 |
nicklas |
366 |
job.setScheduled(null, JobAgent.getById(dc, agentId)); // To end up with status = WAITING |
2627 |
07 Sep 06 |
nicklas |
367 |
dc.saveItem(job); |
2627 |
07 Sep 06 |
nicklas |
368 |
dc.commit(); |
2627 |
07 Sep 06 |
nicklas |
369 |
id = job.getId(); |
2627 |
07 Sep 06 |
nicklas |
370 |
dc = TestUtil.getDbControl(); |
5060 |
19 Aug 09 |
nicklas |
371 |
dc.reattachItem(job, false); |
2630 |
08 Sep 06 |
nicklas |
372 |
write_item(0, job, JobAgent.getById(dc, agentId).getSettings(plugin, true)); |
2627 |
07 Sep 06 |
nicklas |
373 |
write("--Create job OK"); |
2627 |
07 Sep 06 |
nicklas |
374 |
} |
2627 |
07 Sep 06 |
nicklas |
375 |
catch (Throwable ex) |
2627 |
07 Sep 06 |
nicklas |
376 |
{ |
2627 |
07 Sep 06 |
nicklas |
377 |
write("--Create job FAILED"); |
2627 |
07 Sep 06 |
nicklas |
378 |
ex.printStackTrace(); |
2627 |
07 Sep 06 |
nicklas |
379 |
ok = false; |
2627 |
07 Sep 06 |
nicklas |
380 |
} |
2627 |
07 Sep 06 |
nicklas |
381 |
finally |
2627 |
07 Sep 06 |
nicklas |
382 |
{ |
2627 |
07 Sep 06 |
nicklas |
383 |
if (dc != null) dc.close(); |
2627 |
07 Sep 06 |
nicklas |
384 |
} |
2627 |
07 Sep 06 |
nicklas |
385 |
|
2627 |
07 Sep 06 |
nicklas |
386 |
return id; |
2627 |
07 Sep 06 |
nicklas |
387 |
} |
2627 |
07 Sep 06 |
nicklas |
388 |
|
2627 |
07 Sep 06 |
nicklas |
389 |
static void test_list_jobs(int agentId, int expectedResults) |
2627 |
07 Sep 06 |
nicklas |
390 |
{ |
2627 |
07 Sep 06 |
nicklas |
391 |
if (agentId == 0) return; |
2627 |
07 Sep 06 |
nicklas |
392 |
DbControl dc = null; |
2627 |
07 Sep 06 |
nicklas |
393 |
try |
2627 |
07 Sep 06 |
nicklas |
394 |
{ |
2627 |
07 Sep 06 |
nicklas |
395 |
dc = TestUtil.getDbControl(); |
2627 |
07 Sep 06 |
nicklas |
396 |
JobAgent j = JobAgent.getById(dc, agentId); |
2627 |
07 Sep 06 |
nicklas |
397 |
ItemResultList<Job> l = j.getWaitingJobs().list(dc); |
2627 |
07 Sep 06 |
nicklas |
398 |
for (int i = 0; i<l.size(); i++) |
2627 |
07 Sep 06 |
nicklas |
399 |
{ |
2627 |
07 Sep 06 |
nicklas |
400 |
Job job = l.get(i); |
2627 |
07 Sep 06 |
nicklas |
401 |
PluginDefinition plugin = job.getPluginDefinition(); |
2630 |
08 Sep 06 |
nicklas |
402 |
JobAgentSettings settings = j.getSettings(plugin, true); |
2627 |
07 Sep 06 |
nicklas |
403 |
write_item(i, job, settings); |
2627 |
07 Sep 06 |
nicklas |
404 |
} |
2627 |
07 Sep 06 |
nicklas |
405 |
if (expectedResults >= 0 && expectedResults != l.size()) |
2627 |
07 Sep 06 |
nicklas |
406 |
{ |
2627 |
07 Sep 06 |
nicklas |
407 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
2627 |
07 Sep 06 |
nicklas |
408 |
} |
2627 |
07 Sep 06 |
nicklas |
409 |
write("--List jobs for job agent OK ("+l.size()+")"); |
2627 |
07 Sep 06 |
nicklas |
410 |
} |
2627 |
07 Sep 06 |
nicklas |
411 |
catch (Throwable ex) |
2627 |
07 Sep 06 |
nicklas |
412 |
{ |
2627 |
07 Sep 06 |
nicklas |
413 |
write("--List jobs for job agent FAILED"); |
2627 |
07 Sep 06 |
nicklas |
414 |
ex.printStackTrace(); |
2627 |
07 Sep 06 |
nicklas |
415 |
ok = false; |
2627 |
07 Sep 06 |
nicklas |
416 |
} |
2627 |
07 Sep 06 |
nicklas |
417 |
finally |
2627 |
07 Sep 06 |
nicklas |
418 |
{ |
2627 |
07 Sep 06 |
nicklas |
419 |
if (dc != null) dc.close(); |
2627 |
07 Sep 06 |
nicklas |
420 |
} |
2627 |
07 Sep 06 |
nicklas |
421 |
} |
2627 |
07 Sep 06 |
nicklas |
422 |
|
5446 |
15 Oct 10 |
nicklas |
423 |
static void create_fake_jobagent(final int port, final String externalId) |
2627 |
07 Sep 06 |
nicklas |
424 |
{ |
2632 |
08 Sep 06 |
nicklas |
425 |
try |
2632 |
08 Sep 06 |
nicklas |
426 |
{ |
2641 |
14 Sep 06 |
nicklas |
427 |
Properties p = new Properties(); |
2641 |
14 Sep 06 |
nicklas |
428 |
p.setProperty("agent.port", Integer.toString(port)); |
2641 |
14 Sep 06 |
nicklas |
429 |
p.setProperty("agent.user", TestUtil.getLogin()); |
2641 |
14 Sep 06 |
nicklas |
430 |
p.setProperty("agent.password", TestUtil.getPassword()); |
2641 |
14 Sep 06 |
nicklas |
431 |
p.setProperty("agent.id", externalId); |
5446 |
15 Oct 10 |
nicklas |
432 |
p.setProperty("agent.request-handler.foo", FooRequestHandler.class.getName()); |
2641 |
14 Sep 06 |
nicklas |
433 |
Agent agent = new Agent(p); |
2634 |
12 Sep 06 |
nicklas |
434 |
RequestHandler requestHandler = new DefaultRequestHandler(agent) |
2627 |
07 Sep 06 |
nicklas |
435 |
{ |
6127 |
14 Sep 12 |
nicklas |
436 |
@Override |
2634 |
12 Sep 06 |
nicklas |
437 |
public String handleCmd(Socket incoming, String cmd) |
2627 |
07 Sep 06 |
nicklas |
438 |
{ |
5446 |
15 Oct 10 |
nicklas |
439 |
if (!TestUtil.getSilent()) write("--Job agent received cmd: " + cmd + " (id=" + externalId + "; port="+port+")"); |
2634 |
12 Sep 06 |
nicklas |
440 |
String answer = super.handleCmd(incoming, cmd); |
2634 |
12 Sep 06 |
nicklas |
441 |
if (!TestUtil.getSilent()) write("--Job agent answers: " + answer); |
2632 |
08 Sep 06 |
nicklas |
442 |
return answer; |
2627 |
07 Sep 06 |
nicklas |
443 |
} |
2632 |
08 Sep 06 |
nicklas |
444 |
}; |
2634 |
12 Sep 06 |
nicklas |
445 |
agent.service(requestHandler); // NOTE! Creates new thread for the listener |
5446 |
15 Oct 10 |
nicklas |
446 |
write("--Create fake job agent OK (id=" + externalId + "; port " + port + ")"); |
2632 |
08 Sep 06 |
nicklas |
447 |
} |
2632 |
08 Sep 06 |
nicklas |
448 |
catch (Throwable t) |
2632 |
08 Sep 06 |
nicklas |
449 |
{ |
5446 |
15 Oct 10 |
nicklas |
450 |
write("--Create fake job agent FAILED (id=" + externalId + "; port " + port + ")"); |
2632 |
08 Sep 06 |
nicklas |
451 |
t.printStackTrace(); |
2632 |
08 Sep 06 |
nicklas |
452 |
ok = false; |
2632 |
08 Sep 06 |
nicklas |
453 |
} |
2627 |
07 Sep 06 |
nicklas |
454 |
} |
2632 |
08 Sep 06 |
nicklas |
455 |
|
2634 |
12 Sep 06 |
nicklas |
456 |
|
2634 |
12 Sep 06 |
nicklas |
457 |
static void stop_fake_jobagent(int port) |
2634 |
12 Sep 06 |
nicklas |
458 |
{ |
2634 |
12 Sep 06 |
nicklas |
459 |
JobAgentConnection conn = null; |
2634 |
12 Sep 06 |
nicklas |
460 |
try |
2634 |
12 Sep 06 |
nicklas |
461 |
{ |
2634 |
12 Sep 06 |
nicklas |
462 |
conn = new JobAgentConnection(port, 1000); |
2634 |
12 Sep 06 |
nicklas |
463 |
conn.sendStop(); |
2634 |
12 Sep 06 |
nicklas |
464 |
write("--Stop fake job agent OK (port " + port + ")"); |
2634 |
12 Sep 06 |
nicklas |
465 |
} |
2634 |
12 Sep 06 |
nicklas |
466 |
catch (Throwable t) |
2634 |
12 Sep 06 |
nicklas |
467 |
{ |
2634 |
12 Sep 06 |
nicklas |
468 |
write("--Stop fake job agent FAILED (port " + port + ")"); |
2634 |
12 Sep 06 |
nicklas |
469 |
t.printStackTrace(); |
2634 |
12 Sep 06 |
nicklas |
470 |
ok = false; |
2634 |
12 Sep 06 |
nicklas |
471 |
} |
2634 |
12 Sep 06 |
nicklas |
472 |
finally |
2634 |
12 Sep 06 |
nicklas |
473 |
{ |
2634 |
12 Sep 06 |
nicklas |
474 |
if (conn != null) conn.close(); |
2634 |
12 Sep 06 |
nicklas |
475 |
} |
2634 |
12 Sep 06 |
nicklas |
476 |
} |
5446 |
15 Oct 10 |
nicklas |
477 |
|
5446 |
15 Oct 10 |
nicklas |
478 |
static void test_send_control_command(int jobAgentId, String cmd) |
5446 |
15 Oct 10 |
nicklas |
479 |
{ |
5446 |
15 Oct 10 |
nicklas |
480 |
if (jobAgentId == 0) return; |
5446 |
15 Oct 10 |
nicklas |
481 |
DbControl dc = null; |
5446 |
15 Oct 10 |
nicklas |
482 |
try |
5446 |
15 Oct 10 |
nicklas |
483 |
{ |
5446 |
15 Oct 10 |
nicklas |
484 |
dc = TestUtil.getDbControl(); |
5446 |
15 Oct 10 |
nicklas |
485 |
JobAgent j = JobAgent.getById(dc, jobAgentId); |
5446 |
15 Oct 10 |
nicklas |
486 |
JobAgentConnection conn = j.getConnection(null); |
5446 |
15 Oct 10 |
nicklas |
487 |
String answer = conn.send(cmd); |
5446 |
15 Oct 10 |
nicklas |
488 |
write("--Send custom command OK (" + cmd + " --> " + answer + ")"); |
5446 |
15 Oct 10 |
nicklas |
489 |
} |
5446 |
15 Oct 10 |
nicklas |
490 |
catch (Throwable ex) |
5446 |
15 Oct 10 |
nicklas |
491 |
{ |
5446 |
15 Oct 10 |
nicklas |
492 |
write("--Send custom command FAILED (" + cmd + ")"); |
5446 |
15 Oct 10 |
nicklas |
493 |
ex.printStackTrace(); |
5446 |
15 Oct 10 |
nicklas |
494 |
ok = false; |
5446 |
15 Oct 10 |
nicklas |
495 |
} |
5446 |
15 Oct 10 |
nicklas |
496 |
finally |
5446 |
15 Oct 10 |
nicklas |
497 |
{ |
5446 |
15 Oct 10 |
nicklas |
498 |
if (dc != null) dc.close(); |
5446 |
15 Oct 10 |
nicklas |
499 |
} |
5446 |
15 Oct 10 |
nicklas |
500 |
} |
5446 |
15 Oct 10 |
nicklas |
501 |
|
5446 |
15 Oct 10 |
nicklas |
502 |
|
5446 |
15 Oct 10 |
nicklas |
503 |
public static class FooRequestHandler |
5446 |
15 Oct 10 |
nicklas |
504 |
extends AbstractCustomRequestHandler |
5446 |
15 Oct 10 |
nicklas |
505 |
{ |
5446 |
15 Oct 10 |
nicklas |
506 |
|
5446 |
15 Oct 10 |
nicklas |
507 |
public FooRequestHandler() |
5446 |
15 Oct 10 |
nicklas |
508 |
{} |
5446 |
15 Oct 10 |
nicklas |
509 |
|
5446 |
15 Oct 10 |
nicklas |
510 |
@Override |
5446 |
15 Oct 10 |
nicklas |
511 |
public void init(Agent agent) |
5446 |
15 Oct 10 |
nicklas |
512 |
{ |
5446 |
15 Oct 10 |
nicklas |
513 |
super.init(agent); |
5446 |
15 Oct 10 |
nicklas |
514 |
if (!TestUtil.getSilent()) |
5446 |
15 Oct 10 |
nicklas |
515 |
{ |
5446 |
15 Oct 10 |
nicklas |
516 |
write("--Initializing 'foo' handler for job agent (id=" + agent.getId() + "; port=" + agent.getPort() + ")"); |
5446 |
15 Oct 10 |
nicklas |
517 |
} |
5446 |
15 Oct 10 |
nicklas |
518 |
} |
5446 |
15 Oct 10 |
nicklas |
519 |
@Override |
5446 |
15 Oct 10 |
nicklas |
520 |
public void close() |
5446 |
15 Oct 10 |
nicklas |
521 |
{ |
5446 |
15 Oct 10 |
nicklas |
522 |
Agent agent = getAgent(); |
5446 |
15 Oct 10 |
nicklas |
523 |
int port = agent.getPort(); |
5446 |
15 Oct 10 |
nicklas |
524 |
String id = agent.getId(); |
5446 |
15 Oct 10 |
nicklas |
525 |
if (!TestUtil.getSilent()) |
5446 |
15 Oct 10 |
nicklas |
526 |
{ |
5446 |
15 Oct 10 |
nicklas |
527 |
write("--Closing 'foo' handler for job agent (id=" + id + "; port=" + port + ")"); |
5446 |
15 Oct 10 |
nicklas |
528 |
} |
5446 |
15 Oct 10 |
nicklas |
529 |
super.close(); |
5446 |
15 Oct 10 |
nicklas |
530 |
} |
5446 |
15 Oct 10 |
nicklas |
531 |
|
5446 |
15 Oct 10 |
nicklas |
532 |
@Override |
5446 |
15 Oct 10 |
nicklas |
533 |
public String handleCmd(Socket socket, String cmd) |
5446 |
15 Oct 10 |
nicklas |
534 |
{ |
5446 |
15 Oct 10 |
nicklas |
535 |
Agent agent = getAgent(); |
5446 |
15 Oct 10 |
nicklas |
536 |
int port = agent.getPort(); |
5446 |
15 Oct 10 |
nicklas |
537 |
String id = agent.getId(); |
5446 |
15 Oct 10 |
nicklas |
538 |
if (!TestUtil.getSilent()) |
5446 |
15 Oct 10 |
nicklas |
539 |
{ |
5446 |
15 Oct 10 |
nicklas |
540 |
write("--Job agent received cmd: " + cmd + " (id=" + id + "; port="+port+")"); |
5446 |
15 Oct 10 |
nicklas |
541 |
} |
5446 |
15 Oct 10 |
nicklas |
542 |
return "OK " + cmd; |
5446 |
15 Oct 10 |
nicklas |
543 |
} |
5446 |
15 Oct 10 |
nicklas |
544 |
|
5446 |
15 Oct 10 |
nicklas |
545 |
} |
5446 |
15 Oct 10 |
nicklas |
546 |
|
2627 |
07 Sep 06 |
nicklas |
547 |
} |