1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Services.Hive.Common;
|
---|
26 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
27 | using HeuristicLab.Services.Hive.Common.ServiceContracts;
|
---|
28 | using HeuristicLab.Services.Hive.Tests.Mocks;
|
---|
29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
30 | using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Services.Hive.Tests {
|
---|
33 | [TestClass]
|
---|
34 | public class ServiceTests {
|
---|
35 | // use the mock service locator to modify service properties (such as current user)
|
---|
36 | private static MockServiceLocator mockServiceLocator;
|
---|
37 |
|
---|
38 | [ClassInitialize]
|
---|
39 | public static void MyClassInitialize(TestContext testContext) {
|
---|
40 | mockServiceLocator = new MockServiceLocator(ServiceLocator.Instance);
|
---|
41 | ServiceLocator.Instance = mockServiceLocator;
|
---|
42 | }
|
---|
43 |
|
---|
44 | private IHiveService GetLocalService() {
|
---|
45 | return new HiveService();
|
---|
46 | }
|
---|
47 |
|
---|
48 | [TestMethod]
|
---|
49 | public void TestJobs() {
|
---|
50 | var service = GetLocalService();
|
---|
51 |
|
---|
52 | // create hive experiment
|
---|
53 | DT.HiveExperiment experiment = new DT.HiveExperiment() { Name = "TestExperiment", Description = "" };
|
---|
54 |
|
---|
55 | // create job
|
---|
56 | DT.Job job = new DT.Job() { CoresNeeded = 1, MemoryNeeded = 0, Priority = 0 };
|
---|
57 | job.State = JobState.Offline;
|
---|
58 | job.StateLog.Add(new StateLog { State = JobState.Offline, DateTime = DateTime.Now });
|
---|
59 |
|
---|
60 | DT.JobData jobData = new DT.JobData() {
|
---|
61 | //Data = PersistenceUtil.Serialize(new MockJob(500, true))
|
---|
62 | Data = new byte[10000]
|
---|
63 | };
|
---|
64 |
|
---|
65 | // delete plugin first (otherwise the system would not allow it because of the same hash code
|
---|
66 | var hash = new byte[] { 1, 2, 3 };
|
---|
67 | var p = service.GetPluginByHash(hash);
|
---|
68 | if (p != null) service.DeletePlugin(p.Id);
|
---|
69 |
|
---|
70 | // create plugin
|
---|
71 | DT.Plugin plugin1 = new DT.Plugin();
|
---|
72 | plugin1.Name = "Tests.MyPlugin";
|
---|
73 | plugin1.Version = new Version("1.0.0.0");
|
---|
74 | plugin1.UserId = Guid.Empty;
|
---|
75 | plugin1.DateCreated = DateTime.Now;
|
---|
76 | plugin1.Hash = hash;
|
---|
77 |
|
---|
78 | DT.PluginData pluginData1 = new DT.PluginData();
|
---|
79 | pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
|
---|
80 | pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
|
---|
81 |
|
---|
82 | plugin1.Id = service.AddPlugin(plugin1, new List<PluginData> { pluginData1 });
|
---|
83 | pluginData1.PluginId = plugin1.Id;
|
---|
84 |
|
---|
85 | // add plugin
|
---|
86 | job.PluginsNeededIds.Add(plugin1.Id);
|
---|
87 |
|
---|
88 | // create slave
|
---|
89 | DT.Slave slave = new Slave();
|
---|
90 | slave.Id = Guid.NewGuid();
|
---|
91 | slave.Name = "TestSlave";
|
---|
92 | slave.Memory = 1024;
|
---|
93 | slave.Cores = 4;
|
---|
94 | slave.CpuSpeed = 2800;
|
---|
95 | slave.OperatingSystem = "Windows 3.11";
|
---|
96 | slave.CpuArchitecture = CpuArchitecture.x64;
|
---|
97 |
|
---|
98 | // add slave
|
---|
99 | service.AddSlave(slave);
|
---|
100 |
|
---|
101 | // add hive experiment
|
---|
102 | experiment.Id = service.AddHiveExperiment(experiment);
|
---|
103 |
|
---|
104 | // add job
|
---|
105 | job.HiveExperimentId = experiment.Id;
|
---|
106 | job.Id = service.AddJob(job, jobData, new List<Guid> { slave.Id });
|
---|
107 |
|
---|
108 | // test job
|
---|
109 | DT.Job jobLoaded = service.GetJob(job.Id);
|
---|
110 | Assert.AreEqual(job.Id, jobLoaded.Id);
|
---|
111 | Assert.AreEqual(job.CoresNeeded, jobLoaded.CoresNeeded);
|
---|
112 | Assert.AreEqual(job.MemoryNeeded, jobLoaded.MemoryNeeded);
|
---|
113 | Assert.AreEqual(job.Priority, jobLoaded.Priority);
|
---|
114 | Assert.AreEqual(JobState.Waiting, jobLoaded.State);
|
---|
115 | Assert.IsTrue(job.PluginsNeededIds.SequenceEqual(jobLoaded.PluginsNeededIds));
|
---|
116 | Assert.AreEqual(job.HiveExperimentId, jobLoaded.HiveExperimentId);
|
---|
117 |
|
---|
118 | DT.JobData jobDataLoaded = service.GetJobData(job.Id);
|
---|
119 | Assert.AreEqual(job.Id, jobDataLoaded.JobId);
|
---|
120 | Assert.IsTrue(jobData.Data.SequenceEqual(jobDataLoaded.Data));
|
---|
121 |
|
---|
122 | // test hive experiment
|
---|
123 | DT.HiveExperiment experimentLoaded = service.GetHiveExperiment(experiment.Id);
|
---|
124 | Assert.AreEqual(experiment.Id, experimentLoaded.Id);
|
---|
125 | Assert.AreEqual(experiment.Name, experimentLoaded.Name);
|
---|
126 | Assert.AreEqual(experiment.Description, experimentLoaded.Description);
|
---|
127 |
|
---|
128 | // test assigned ressources
|
---|
129 | var actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
|
---|
130 | Assert.AreEqual(1, actions.Count);
|
---|
131 | Assert.AreEqual(MessageContainer.MessageType.CalculateJob, actions[0].Message);
|
---|
132 | Assert.AreEqual(job.Id, actions[0].JobId);
|
---|
133 |
|
---|
134 | jobLoaded = service.GetJob(job.Id);
|
---|
135 | Assert.AreEqual(JobState.Transferring, jobLoaded.State);
|
---|
136 |
|
---|
137 | // slave is responsible for updating state to 'Calculating'
|
---|
138 | service.UpdateJobState(jobLoaded.Id, JobState.Calculating, slave.Id, null, null);
|
---|
139 |
|
---|
140 | // send progress
|
---|
141 | var progress = new Dictionary<Guid, TimeSpan>();
|
---|
142 | progress.Add(job.Id, new TimeSpan(1, 5, 10, 20, 30));
|
---|
143 | actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 3, FreeMemory = 1024, JobProgress = progress });
|
---|
144 | Assert.AreEqual(0, actions.Count);
|
---|
145 |
|
---|
146 | // the job should be in state 'Calculating' now
|
---|
147 | jobLoaded = service.GetJob(job.Id);
|
---|
148 | Assert.AreEqual(JobState.Calculating, jobLoaded.State);
|
---|
149 | Assert.AreEqual(new TimeSpan(1, 5, 10, 20, 30), jobLoaded.ExecutionTime);
|
---|
150 |
|
---|
151 | // test if the job is returned for the resource
|
---|
152 | var jobsBySlave = service.GetJobsByResourceId(slave.Id);
|
---|
153 | Assert.AreEqual(1, jobsBySlave.Count());
|
---|
154 | Assert.AreEqual(job.Id, jobsBySlave.Single().Id);
|
---|
155 |
|
---|
156 | // set it to finished
|
---|
157 | service.UpdateJobState(jobLoaded.Id, JobState.Finished, slave.Id, null, null);
|
---|
158 |
|
---|
159 | // test if the job is returned for the resource (it should not be)
|
---|
160 | var jobsBySlave2 = service.GetJobsByResourceId(slave.Id);
|
---|
161 | Assert.AreEqual(0, jobsBySlave2.Count());
|
---|
162 |
|
---|
163 | // set job waiting again
|
---|
164 | service.UpdateJobState(job.Id, JobState.Waiting, null, null, string.Empty);
|
---|
165 |
|
---|
166 | // get job again
|
---|
167 | actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
|
---|
168 | Assert.AreEqual(1, actions.Count);
|
---|
169 | Assert.AreEqual(MessageContainer.MessageType.CalculateJob, actions[0].Message);
|
---|
170 | Assert.AreEqual(job.Id, actions[0].JobId);
|
---|
171 |
|
---|
172 | // create downtime which should make slave unavailable for calculation
|
---|
173 | Guid downtimeId = service.AddDowntime(new Downtime { ResourceId = slave.Id, StartDate = DateTime.Now - TimeSpan.FromMinutes(1), EndDate = DateTime.Now + TimeSpan.FromMinutes(1), Recurring = false });
|
---|
174 |
|
---|
175 | progress.Clear();
|
---|
176 | progress.Add(job.Id, new TimeSpan(1, 5, 10, 20, 30));
|
---|
177 | actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
|
---|
178 | Assert.AreEqual(1, actions.Count);
|
---|
179 | Assert.AreEqual(MessageContainer.MessageType.PauseAll, actions[0].Message);
|
---|
180 | Assert.AreEqual(Guid.Empty, actions[0].JobId);
|
---|
181 |
|
---|
182 | service.DeleteDowntime(downtimeId);
|
---|
183 |
|
---|
184 | // delete
|
---|
185 | service.DeleteHiveExperiment(experiment.Id);
|
---|
186 | Assert.AreEqual(null, service.GetHiveExperiment(experiment.Id));
|
---|
187 | Assert.AreEqual(null, service.GetJob(job.Id));
|
---|
188 | Assert.AreEqual(null, service.GetJobData(job.Id));
|
---|
189 |
|
---|
190 | // send another heartbeat with the deleted job; the server should command the abortion of the job
|
---|
191 | actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 3, FreeMemory = 1024, JobProgress = progress });
|
---|
192 | Assert.AreEqual(1, actions.Count);
|
---|
193 | Assert.AreEqual(MessageContainer.MessageType.AbortJob, actions[0].Message);
|
---|
194 | Assert.AreEqual(job.Id, actions[0].JobId);
|
---|
195 |
|
---|
196 | // delete slave
|
---|
197 | service.DeleteSlave(slave.Id);
|
---|
198 | }
|
---|
199 |
|
---|
200 | [TestMethod]
|
---|
201 | public void TestParentJobs() {
|
---|
202 | var service = GetLocalService();
|
---|
203 |
|
---|
204 | // create hive experiment
|
---|
205 | DT.HiveExperiment experiment = new DT.HiveExperiment() { Name = "TestExperiment", Description = "" };
|
---|
206 |
|
---|
207 | // create parent job
|
---|
208 | DT.Job parentJob = new DT.Job() {
|
---|
209 | CoresNeeded = 1,
|
---|
210 | MemoryNeeded = 0,
|
---|
211 | Priority = 0,
|
---|
212 | IsParentJob = true,
|
---|
213 | FinishWhenChildJobsFinished = true
|
---|
214 | };
|
---|
215 | parentJob.State = JobState.Offline;
|
---|
216 | parentJob.StateLog.Add(new StateLog { State = JobState.Offline, DateTime = DateTime.Now });
|
---|
217 |
|
---|
218 | DT.JobData parentJobData = new DT.JobData() { Data = new byte[0] };
|
---|
219 |
|
---|
220 | // create child job
|
---|
221 | DT.Job childJob = new DT.Job() {
|
---|
222 | CoresNeeded = 1,
|
---|
223 | MemoryNeeded = 0,
|
---|
224 | Priority = 0
|
---|
225 | };
|
---|
226 | childJob.State = JobState.Offline;
|
---|
227 | childJob.StateLog.Add(new StateLog { State = JobState.Offline, DateTime = DateTime.Now });
|
---|
228 |
|
---|
229 | DT.JobData childJobData = new DT.JobData() { Data = new byte[1000] };
|
---|
230 |
|
---|
231 | // create slave
|
---|
232 | DT.Slave slave = new Slave();
|
---|
233 | slave.Id = Guid.NewGuid();
|
---|
234 | slave.Name = "TestSlave";
|
---|
235 | slave.Memory = 1024;
|
---|
236 | slave.Cores = 4;
|
---|
237 | slave.CpuSpeed = 2800;
|
---|
238 | slave.OperatingSystem = "Windows 3.11";
|
---|
239 | slave.CpuArchitecture = CpuArchitecture.x64;
|
---|
240 |
|
---|
241 | // add slave
|
---|
242 | service.AddSlave(slave);
|
---|
243 |
|
---|
244 | // add hive experiment
|
---|
245 | experiment.Id = service.AddHiveExperiment(experiment);
|
---|
246 |
|
---|
247 | // add parent job
|
---|
248 | parentJob.HiveExperimentId = experiment.Id;
|
---|
249 | parentJob.Id = service.AddJob(parentJob, parentJobData, new List<Guid> { slave.Id });
|
---|
250 |
|
---|
251 | // add child job
|
---|
252 | childJob.HiveExperimentId = experiment.Id;
|
---|
253 | childJob.Id = service.AddChildJob(parentJob.Id, childJob, childJobData);
|
---|
254 | childJob.ParentJobId = parentJob.Id;
|
---|
255 |
|
---|
256 | // test child job
|
---|
257 | var childJobLoaded = service.GetJob(childJob.Id);
|
---|
258 | Assert.AreEqual(childJob.ParentJobId, childJobLoaded.ParentJobId);
|
---|
259 | Assert.AreEqual(childJob.HiveExperimentId, childJobLoaded.HiveExperimentId);
|
---|
260 | Assert.AreEqual(JobState.Waiting, childJobLoaded.State);
|
---|
261 | Assert.AreEqual(false, childJobLoaded.FinishWhenChildJobsFinished);
|
---|
262 | Assert.AreEqual(false, childJobLoaded.IsParentJob);
|
---|
263 |
|
---|
264 | // test parent job
|
---|
265 | var parentJobLoaded = service.GetJob(parentJob.Id);
|
---|
266 | Assert.AreEqual(parentJob.HiveExperimentId, parentJobLoaded.HiveExperimentId);
|
---|
267 | Assert.AreEqual(JobState.Waiting, parentJobLoaded.State);
|
---|
268 | Assert.AreEqual(true, parentJobLoaded.FinishWhenChildJobsFinished);
|
---|
269 | Assert.AreEqual(true, parentJobLoaded.IsParentJob);
|
---|
270 |
|
---|
271 | // test heartbeat
|
---|
272 | var actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
|
---|
273 | Assert.AreEqual(1, actions.Count); // only the child job should be assigned
|
---|
274 | Assert.AreEqual(MessageContainer.MessageType.CalculateJob, actions[0].Message);
|
---|
275 | Assert.AreEqual(childJob.Id, actions[0].JobId);
|
---|
276 |
|
---|
277 | // lifecycle - let it process one server-heartbeat; the parent job must NOT be set to finished
|
---|
278 | service.TriggerLifecycle(true);
|
---|
279 |
|
---|
280 | parentJobLoaded = service.GetJob(parentJob.Id);
|
---|
281 | Assert.AreEqual(JobState.Waiting, parentJobLoaded.State);
|
---|
282 |
|
---|
283 | // set child job to finished
|
---|
284 | childJobLoaded = service.UpdateJobState(childJobLoaded.Id, JobState.Finished, slave.Id, null, null);
|
---|
285 |
|
---|
286 | // lifecycle - let it process one server-heartbeat; this should set the parent job to finished
|
---|
287 | service.TriggerLifecycle(true);
|
---|
288 |
|
---|
289 | // test if parent job is finished
|
---|
290 | parentJobLoaded = service.GetJob(parentJob.Id);
|
---|
291 | Assert.AreEqual(JobState.Finished, parentJobLoaded.State);
|
---|
292 |
|
---|
293 | // delete experiment
|
---|
294 | service.DeleteHiveExperiment(experiment.Id);
|
---|
295 | Assert.AreEqual(null, service.GetJob(parentJob.Id));
|
---|
296 | Assert.AreEqual(null, service.GetJob(childJob.Id));
|
---|
297 |
|
---|
298 | service.DeleteSlave(slave.Id);
|
---|
299 | }
|
---|
300 |
|
---|
301 |
|
---|
302 | [TestMethod]
|
---|
303 | public void TestHiveExperimentPermissions() {
|
---|
304 | var service = GetLocalService();
|
---|
305 | mockServiceLocator.SetCurrentUserId(MockUserManager.MockUserId1);
|
---|
306 |
|
---|
307 | // create hive experiment
|
---|
308 | DT.HiveExperiment e1 = new DT.HiveExperiment() { Name = "TestExperiment", Description = "" };
|
---|
309 | e1.Id = service.AddHiveExperiment(e1);
|
---|
310 |
|
---|
311 | var e1loaded = service.GetHiveExperiment(e1.Id);
|
---|
312 | Assert.AreEqual(Permission.Full, e1loaded.Permission);
|
---|
313 | var allExp = service.GetHiveExperiments();
|
---|
314 | Assert.AreEqual(1, allExp.Count(x => x.Id == e1.Id));
|
---|
315 |
|
---|
316 | // change to user2
|
---|
317 | mockServiceLocator.SetCurrentUserId(MockUserManager.MockUserId2);
|
---|
318 | try {
|
---|
319 | e1loaded = service.GetHiveExperiment(e1.Id);
|
---|
320 | Assert.Fail("Access should not be possible");
|
---|
321 | } catch { /* ok, cool */ }
|
---|
322 | allExp = service.GetHiveExperiments();
|
---|
323 | Assert.AreEqual(0, allExp.Count(x => x.Id == e1.Id));
|
---|
324 |
|
---|
325 | // user2 should not be able to grant permissions
|
---|
326 | try {
|
---|
327 | service.GrantPermission(e1.Id, MockUserManager.MockUserId2, Permission.Read);
|
---|
328 | Assert.Fail("Should not be possible to grant permission due to missing permission for User2");
|
---|
329 | }
|
---|
330 | catch { /* ok, cool */ }
|
---|
331 |
|
---|
332 | // switch back to user1 (owner) and grant user2 permissions
|
---|
333 | mockServiceLocator.SetCurrentUserId(MockUserManager.MockUserId1);
|
---|
334 | service.GrantPermission(e1.Id, MockUserManager.MockUserId2, Permission.Read);
|
---|
335 |
|
---|
336 | // back to user2
|
---|
337 | mockServiceLocator.SetCurrentUserId(MockUserManager.MockUserId2);
|
---|
338 | e1loaded = service.GetHiveExperiment(e1.Id);
|
---|
339 | Assert.AreEqual(Permission.Read, e1loaded.Permission);
|
---|
340 | allExp = service.GetHiveExperiments();
|
---|
341 | Assert.AreEqual(1, allExp.Count(x => x.Id == e1.Id));
|
---|
342 |
|
---|
343 | // user2 should still not be able to grant permissions
|
---|
344 | try {
|
---|
345 | service.GrantPermission(e1.Id, MockUserManager.MockUserId2, Permission.Read);
|
---|
346 | Assert.Fail("Should not be possible to grant permission due to missing permission for User2");
|
---|
347 | }
|
---|
348 | catch { /* ok, cool */ }
|
---|
349 |
|
---|
350 | // back to user1
|
---|
351 | mockServiceLocator.SetCurrentUserId(MockUserManager.MockUserId1);
|
---|
352 | service.GrantPermission(e1.Id, MockUserManager.MockUserId2, Permission.Full);
|
---|
353 |
|
---|
354 | // back to user2
|
---|
355 | mockServiceLocator.SetCurrentUserId(MockUserManager.MockUserId2);
|
---|
356 | e1loaded = service.GetHiveExperiment(e1.Id);
|
---|
357 | Assert.AreEqual(Permission.Full, e1loaded.Permission);
|
---|
358 | allExp = service.GetHiveExperiments();
|
---|
359 | Assert.AreEqual(1, allExp.Count(x => x.Id == e1.Id));
|
---|
360 |
|
---|
361 | // grant rights to user3, now this should be possible due to full permissions
|
---|
362 | service.GrantPermission(e1.Id, MockUserManager.MockUserId3, Permission.Read);
|
---|
363 |
|
---|
364 | // back to user1 and revoke rights for user2
|
---|
365 | mockServiceLocator.SetCurrentUserId(MockUserManager.MockUserId1);
|
---|
366 | service.RevokePermission(e1.Id, MockUserManager.MockUserId2);
|
---|
367 |
|
---|
368 | // back to user2
|
---|
369 | mockServiceLocator.SetCurrentUserId(MockUserManager.MockUserId2);
|
---|
370 | try {
|
---|
371 | e1loaded = service.GetHiveExperiment(e1.Id);
|
---|
372 | Assert.Fail("Access should not be possible");
|
---|
373 | }
|
---|
374 | catch { /* ok, cool */ }
|
---|
375 | allExp = service.GetHiveExperiments();
|
---|
376 | Assert.AreEqual(0, allExp.Count(x => x.Id == e1.Id));
|
---|
377 |
|
---|
378 | // back to user1
|
---|
379 | mockServiceLocator.SetCurrentUserId(MockUserManager.MockUserId1);
|
---|
380 | service.DeleteHiveExperiment(e1.Id);
|
---|
381 | }
|
---|
382 | }
|
---|
383 | }
|
---|