Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/DaoTests.cs @ 6701

Last change on this file since 6701 was 6463, checked in by cneumuel, 13 years ago

#1233

  • created user interface for experiment sharing
  • created UserManager which provides access to the users
  • inserted a lot of security and authorization checks serverside
  • minor fixes in experiment manager
File size: 13.3 KB
RevLine 
[6431]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
22using System;
23using System.Collections.Generic;
[5106]24using System.Linq;
[6431]25using System.Threading;
[5106]26using HeuristicLab.Services.Hive.Common.DataTransfer;
27using HeuristicLab.Services.Hive.Common.ServiceContracts;
28using HeuristicLab.Services.Hive.DataAccess;
[5511]29using Microsoft.VisualStudio.TestTools.UnitTesting;
[6431]30using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
[5106]31
32namespace HeuristicLab.Services.Hive.Tests {
33  [TestClass]
34  public class DaoTests {
35
36    [ClassInitialize]
37    public static void MyClassInitialize(TestContext testContext) {
38      ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance);
39    }
[5526]40
[5106]41    private IHiveService GetLocalService() {
42      return new HiveService();
43    }
44
45    [TestMethod]
46    public void TestJobDao() {
47      IHiveDao dao = ServiceLocator.Instance.HiveDao;
[5526]48
[6006]49      DT.HiveExperiment he = new DT.HiveExperiment();
50      he.DateCreated = DateTime.Now;
51      he.Name = "TestExperiment";
52      he.OwnerUserId = Guid.NewGuid();
53      he.ResourceNames = "HEAL";
54      he.Id = dao.AddHiveExperiment(he);
55
[5106]56      DT.Job job1 = new DT.Job();
[5636]57      job1.State = JobState.Offline;
58      job1.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now });
[5779]59      job1.Command = Command.Pause;
[6006]60      job1.HiveExperimentId = he.Id;
[6267]61      job1.IsPrivileged = true;
[5402]62
[5786]63      DT.JobData jobData1 = new DT.JobData();
64      jobData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
65      jobData1.LastUpdate = DateTime.Now;
66
[5402]67      DT.Plugin plugin1 = new DT.Plugin();
68      plugin1.Name = "Tests.MyPlugin";
69      plugin1.Version = new Version("1.0.0.0");
70      plugin1.UserId = Guid.Empty;
71      plugin1.DateCreated = DateTime.Now;
[6452]72      plugin1.Hash = new byte[] { 1, 2, 3 };
[5402]73
74      DT.PluginData pluginData1 = new DT.PluginData();
75      pluginData1.PluginId = plugin1.Id;
76      pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
77      pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
78
79      plugin1.Id = dao.AddPlugin(plugin1);
80      pluginData1.PluginId = plugin1.Id;
81      pluginData1.Id = dao.AddPluginData(pluginData1);
82
83      job1.PluginsNeededIds.Add(plugin1.Id);
84
[5106]85      job1.Id = dao.AddJob(job1);
[5786]86      jobData1.JobId = job1.Id;
87      dao.AddJobData(jobData1);
[5106]88
89      DT.Job job1loaded = dao.GetJob(job1.Id);
90      Assert.AreEqual(job1.Id, job1loaded.Id);
91      Assert.AreEqual(job1.CoresNeeded, job1loaded.CoresNeeded);
92      Assert.AreEqual(job1.DateCreated.ToString(), job1loaded.DateCreated.ToString());
93      Assert.AreEqual(null, job1loaded.DateFinished);
[5402]94      Assert.IsTrue(job1.PluginsNeededIds.SequenceEqual(job1loaded.PluginsNeededIds));
[5511]95      Assert.AreEqual(job1.StateLog.Count, job1loaded.StateLog.Count);
[5779]96      Assert.AreEqual(job1.Command, job1loaded.Command);
[6006]97      Assert.AreEqual(job1.HiveExperimentId, job1loaded.HiveExperimentId);
[6267]98      Assert.AreEqual(job1.IsPrivileged, job1loaded.IsPrivileged);
[5786]99      Assert.IsTrue(Math.Abs((job1loaded.LastJobDataUpdate - jobData1.LastUpdate).TotalSeconds) < 1);
[5511]100      for (int i = 0; i < job1.StateLog.Count; i++) {
101        Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId);
102        Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State);
103        Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId);
104        Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId);
105        Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception);
106        Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
107      }
[6229]108
[5526]109      job1 = job1loaded;
[5511]110
[5526]111      // test jobstates
[5636]112      job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10);
113      job1.StateLog.Add(new DT.StateLog { State = JobState.Calculating, DateTime = DateTime.Now }); Thread.Sleep(10);
114      job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10);
115      job1.StateLog.Add(new DT.StateLog { State = JobState.Finished, DateTime = DateTime.Now }); Thread.Sleep(10);
[5526]116      dao.UpdateJob(job1);
117
118      job1loaded = dao.GetJob(job1.Id);
119      for (int i = 0; i < job1.StateLog.Count; i++) {
120        Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId);
121        Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State);
122        Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId);
123        Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId);
124        Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception);
125        Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
126      }
127
[5786]128      DT.JobData jobData1Loaded = dao.GetJobData(job1.Id);
129      Assert.AreEqual(jobData1.JobId, jobData1Loaded.JobId);
130      Assert.IsTrue(Math.Abs((jobData1.LastUpdate - jobData1Loaded.LastUpdate).TotalSeconds) < 1);
131      Assert.IsTrue(jobData1.Data.SequenceEqual(jobData1Loaded.Data));
[5106]132
[6006]133      dao.DeleteHiveExperiment(he.Id);
134
[5106]135      Assert.AreEqual(null, dao.GetJob(job1.Id));
[5786]136      Assert.AreEqual(null, dao.GetJobData(job1.Id));
[6006]137      Assert.AreEqual(null, dao.GetHiveExperiment(he.Id));
[6452]138
139      dao.DeletePlugin(plugin1.Id);
140      Assert.AreEqual(null, dao.GetPlugin(plugin1.Id));
[5155]141    }
[5106]142
[5155]143    [TestMethod]
144    public void TestSlaveDao() {
145      IHiveDao dao = ServiceLocator.Instance.HiveDao;
[5106]146      DT.SlaveGroup slaveGroup = new DT.SlaveGroup() {
147        Name = "Test"
148      };
149      slaveGroup.Id = dao.AddSlaveGroup(slaveGroup);
150
[5155]151      DT.Slave slave = new DT.Slave() {
[5405]152        Id = Guid.NewGuid(),
[5404]153        Name = "Test",
[5405]154        OperatingSystem = null, //"Windows 3.11",
[5404]155        Cores = 2,
[5405]156        Memory = 1024,
157        FreeMemory = 512
[5155]158      };
159
[5405]160      Assert.AreEqual(slave.Id, dao.AddSlave(slave));
161
162      // update
163      slave.FreeMemory = 255;
164      slave.OperatingSystem = Environment.OSVersion.VersionString;
165      dao.UpdateSlave(slave);
166
167      DT.Slave slaveLoaded = dao.GetSlave(slave.Id);
168      Assert.AreEqual(slave.FreeMemory, slaveLoaded.FreeMemory);
169
[5155]170      dao.DeleteSlave(slave.Id);
[5106]171      dao.DeleteSlaveGroup(slaveGroup.Id);
172    }
[5155]173
174    [TestMethod]
[5402]175    public void TestPluginDao() {
176      IHiveDao dao = ServiceLocator.Instance.HiveDao;
177
178      DT.Plugin plugin1 = new DT.Plugin();
179      plugin1.DateCreated = DateTime.Now;
180      plugin1.Name = "Tests.MyPlugin";
181      plugin1.Version = new Version("1.0.0.0");
182      plugin1.UserId = Guid.Empty;
[6452]183      plugin1.Hash = new byte[] { 1, 2, 3 };
[5402]184
185      plugin1.Id = dao.AddPlugin(plugin1);
186
187      DT.Plugin plugin1loaded = dao.GetPlugin(plugin1.Id);
188      Assert.AreEqual(plugin1.Id, plugin1loaded.Id);
189      Assert.AreEqual(plugin1.Name, plugin1loaded.Name);
190      Assert.AreEqual(plugin1.Version, plugin1loaded.Version);
191      Assert.AreEqual(plugin1.UserId, plugin1loaded.UserId);
192      Assert.AreEqual(plugin1.DateCreated.ToString(), plugin1loaded.DateCreated.ToString());
[6452]193      Assert.IsTrue(plugin1.Hash.SequenceEqual(plugin1loaded.Hash));
[5402]194
195      DT.PluginData pluginData1 = new DT.PluginData();
196      pluginData1.PluginId = plugin1.Id;
197      pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
198      pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
199
200      pluginData1.Id = dao.AddPluginData(pluginData1);
201
202      DT.PluginData pluginData1loaded = dao.GetPluginData(pluginData1.Id);
203      Assert.AreEqual(pluginData1.Id, pluginData1loaded.Id);
204
205      Assert.AreEqual(pluginData1.PluginId, pluginData1loaded.PluginId);
206      Assert.AreEqual(pluginData1.FileName, pluginData1loaded.FileName);
207      Assert.IsTrue(pluginData1.Data.SequenceEqual(pluginData1loaded.Data));
208
209      dao.DeletePluginData(pluginData1.Id);
210      dao.DeletePlugin(plugin1.Id);
211
212      Assert.AreEqual(null, dao.GetPlugin(plugin1.Id));
213      Assert.AreEqual(null, dao.GetPluginData(pluginData1.Id));
[5155]214    }
[5402]215
[5511]216    [TestMethod]
217    public void TestHiveExperimentDao() {
218      IHiveDao dao = ServiceLocator.Instance.HiveDao;
219
220      DT.HiveExperiment he = new DT.HiveExperiment();
[6006]221      he.DateCreated = DateTime.Now;
222      he.Name = "TestExperiment";
223      he.OwnerUserId = Guid.NewGuid();
224      he.ResourceNames = "HEAL";
225      he.Id = dao.AddHiveExperiment(he);
[5511]226
227      DT.Job job = new DT.Job();
[5636]228      job.State = JobState.Offline;
229      job.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now });
[6006]230      job.HiveExperimentId = he.Id;
[5511]231      job.Id = dao.AddJob(job);
232
233      DT.HiveExperimentPermission perm = new DT.HiveExperimentPermission();
234      perm.HiveExperimentId = he.Id;
235      perm.GrantedByUserId = he.OwnerUserId;
236      perm.GrantedUserId = Guid.NewGuid();
[6463]237      perm.Permission = Permission.Full;
[5511]238      dao.AddHiveExperimentPermission(perm);
239
240      DT.HiveExperiment heLoaded = dao.GetHiveExperiment(he.Id);
241      Assert.AreEqual(he.Id, heLoaded.Id);
242      Assert.AreEqual(he.Name, heLoaded.Name);
243      Assert.AreEqual(he.ResourceNames, heLoaded.ResourceNames);
244      //Assert.AreEqual(he.LastAccessed, heLoaded.LastAccessed);
245      //Assert.AreEqual(he.DateCreated, heLoaded.DateCreated);
[5526]246
[6006]247      DT.Job jobLoaded = dao.GetJob(job.Id);
[5511]248      Assert.AreEqual(job.Id, jobLoaded.Id);
249      Assert.AreEqual(job.State, jobLoaded.State);
[6006]250      Assert.AreEqual(job.HiveExperimentId, jobLoaded.HiveExperimentId);
[5511]251
252      DT.HiveExperimentPermission permLoaded = dao.GetHiveExperimentPermission(he.Id, perm.GrantedUserId);
253      Assert.AreEqual(perm.HiveExperimentId, permLoaded.HiveExperimentId);
254      Assert.AreEqual(perm.GrantedUserId, permLoaded.GrantedUserId);
255      Assert.AreEqual(perm.GrantedByUserId, permLoaded.GrantedByUserId);
256      Assert.AreEqual(perm.Permission, permLoaded.Permission);
257
258      dao.DeleteHiveExperiment(he.Id);
259      Assert.AreEqual(null, dao.GetHiveExperiment(he.Id));
[6006]260      Assert.AreEqual(null, dao.GetJob(job.Id));
[5511]261      Assert.AreEqual(null, dao.GetHiveExperimentPermission(perm.HiveExperimentId, perm.GrantedUserId));
262
263    }
[6229]264
265
266    [TestMethod]
267    public void TestStatistics() {
268      IHiveDao dao = ServiceLocator.Instance.HiveDao;
269
270      var stats = new DT.Statistics();
271      stats.TimeStamp = DateTime.Now;
272      var slaveStats = new List<DT.SlaveStatistics>();
273      slaveStats.Add(new DT.SlaveStatistics() { Cores = 8, FreeCores = 3, Memory = 8000, FreeMemory = 3000, CpuUtilization = 0.8, SlaveId = Guid.NewGuid() });
274      slaveStats.Add(new DT.SlaveStatistics() { Cores = 2, FreeCores = 0, Memory = 1024, FreeMemory = 100, CpuUtilization = 0.99, SlaveId = Guid.NewGuid() });
275      slaveStats.Add(new DT.SlaveStatistics() { Cores = 4, FreeCores = 4, Memory = 3024, FreeMemory = 2300, CpuUtilization = 0.01, SlaveId = Guid.NewGuid() });
276      stats.SlaveStatistics = slaveStats;
277
278      var userStats = new List<DT.UserStatistics>();
279      userStats.Add(new DT.UserStatistics() { UsedCores = 15, ExecutionTime = TimeSpan.FromHours(123), UserId = Guid.NewGuid() });
280      userStats.Add(new DT.UserStatistics() { UsedCores = 42, ExecutionTime = TimeSpan.FromHours(945), UserId = Guid.NewGuid() });
281      stats.UserStatistics = userStats;
282
283      stats.Id = dao.AddStatistics(stats);
284
285      var statsLoaded = dao.GetStatistic(stats.Id);
286      Assert.AreEqual(stats.Id, statsLoaded.Id);
287      Assert.IsTrue(Math.Abs((stats.TimeStamp - statsLoaded.TimeStamp).TotalSeconds) < 1);
288
[6431]289      for (int i = 0; i < slaveStats.Count; i++) {
[6229]290        Assert.AreEqual(slaveStats[i].Cores, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).Cores);
291        Assert.AreEqual(slaveStats[i].FreeCores, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).FreeCores);
292        Assert.AreEqual(slaveStats[i].Memory, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).Memory);
293        Assert.AreEqual(slaveStats[i].FreeMemory, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).FreeMemory);
294        Assert.AreEqual(slaveStats[i].CpuUtilization, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).CpuUtilization);
295      }
296
297      for (int i = 0; i < userStats.Count; i++) {
298        Assert.AreEqual(userStats[i].ExecutionTime, statsLoaded.UserStatistics.Single(x => x.UserId == userStats[i].UserId).ExecutionTime);
[6431]299        Assert.AreEqual(userStats[i].UsedCores, statsLoaded.UserStatistics.Single(x => x.UserId == userStats[i].UserId).UsedCores);
[6229]300      }
301
302      dao.DeleteStatistics(stats.Id);
303      Assert.AreEqual(null, dao.GetStatistic(stats.Id));
304    }
[5106]305  }
306}
Note: See TracBrowser for help on using the repository browser.