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