[5106] | 1 | using System;
|
---|
| 2 | using System.Linq;
|
---|
| 3 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
| 4 | using HeuristicLab.Services.Hive.Common.ServiceContracts;
|
---|
| 5 | using HeuristicLab.Services.Hive.DataAccess;
|
---|
[5511] | 6 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[5106] | 7 |
|
---|
| 8 | namespace HeuristicLab.Services.Hive.Tests {
|
---|
[5526] | 9 | using System.Threading;
|
---|
[5106] | 10 | using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
| 11 |
|
---|
| 12 | [TestClass]
|
---|
| 13 | public class DaoTests {
|
---|
| 14 |
|
---|
| 15 | [ClassInitialize]
|
---|
| 16 | public static void MyClassInitialize(TestContext testContext) {
|
---|
| 17 | ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance);
|
---|
| 18 | }
|
---|
[5526] | 19 |
|
---|
[5106] | 20 | private IHiveService GetLocalService() {
|
---|
| 21 | return new HiveService();
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | [TestMethod]
|
---|
| 25 | public void TestJobDao() {
|
---|
| 26 | IHiveDao dao = ServiceLocator.Instance.HiveDao;
|
---|
[5526] | 27 |
|
---|
[6006] | 28 | DT.HiveExperiment he = new DT.HiveExperiment();
|
---|
| 29 | he.DateCreated = DateTime.Now;
|
---|
| 30 | he.Name = "TestExperiment";
|
---|
| 31 | he.OwnerUserId = Guid.NewGuid();
|
---|
| 32 | he.ResourceNames = "HEAL";
|
---|
| 33 | he.Id = dao.AddHiveExperiment(he);
|
---|
| 34 |
|
---|
[5106] | 35 | DT.Job job1 = new DT.Job();
|
---|
[5636] | 36 | job1.State = JobState.Offline;
|
---|
| 37 | job1.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now });
|
---|
[5779] | 38 | job1.Command = Command.Pause;
|
---|
[6006] | 39 | job1.HiveExperimentId = he.Id;
|
---|
[5402] | 40 |
|
---|
[5786] | 41 | DT.JobData jobData1 = new DT.JobData();
|
---|
| 42 | jobData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
|
---|
| 43 | jobData1.LastUpdate = DateTime.Now;
|
---|
| 44 |
|
---|
[5402] | 45 | DT.Plugin plugin1 = new DT.Plugin();
|
---|
| 46 | plugin1.Name = "Tests.MyPlugin";
|
---|
| 47 | plugin1.Version = new Version("1.0.0.0");
|
---|
| 48 | plugin1.UserId = Guid.Empty;
|
---|
| 49 | plugin1.IsLocal = true;
|
---|
| 50 | plugin1.DateCreated = DateTime.Now;
|
---|
| 51 |
|
---|
| 52 | DT.PluginData pluginData1 = new DT.PluginData();
|
---|
| 53 | pluginData1.PluginId = plugin1.Id;
|
---|
| 54 | pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
|
---|
| 55 | pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
|
---|
| 56 |
|
---|
| 57 | plugin1.Id = dao.AddPlugin(plugin1);
|
---|
| 58 | pluginData1.PluginId = plugin1.Id;
|
---|
| 59 | pluginData1.Id = dao.AddPluginData(pluginData1);
|
---|
| 60 |
|
---|
| 61 | job1.PluginsNeededIds.Add(plugin1.Id);
|
---|
| 62 |
|
---|
[5106] | 63 | job1.Id = dao.AddJob(job1);
|
---|
[5786] | 64 | jobData1.JobId = job1.Id;
|
---|
| 65 | dao.AddJobData(jobData1);
|
---|
[5106] | 66 |
|
---|
| 67 | DT.Job job1loaded = dao.GetJob(job1.Id);
|
---|
| 68 | Assert.AreEqual(job1.Id, job1loaded.Id);
|
---|
| 69 | Assert.AreEqual(job1.CoresNeeded, job1loaded.CoresNeeded);
|
---|
| 70 | Assert.AreEqual(job1.DateCreated.ToString(), job1loaded.DateCreated.ToString());
|
---|
| 71 | Assert.AreEqual(null, job1loaded.DateFinished);
|
---|
[5402] | 72 | Assert.IsTrue(job1.PluginsNeededIds.SequenceEqual(job1loaded.PluginsNeededIds));
|
---|
[5511] | 73 | Assert.AreEqual(job1.StateLog.Count, job1loaded.StateLog.Count);
|
---|
[5779] | 74 | Assert.AreEqual(job1.Command, job1loaded.Command);
|
---|
[6006] | 75 | Assert.AreEqual(job1.HiveExperimentId, job1loaded.HiveExperimentId);
|
---|
[5786] | 76 | Assert.IsTrue(Math.Abs((job1loaded.LastJobDataUpdate - jobData1.LastUpdate).TotalSeconds) < 1);
|
---|
[5511] | 77 | for (int i = 0; i < job1.StateLog.Count; i++) {
|
---|
| 78 | Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId);
|
---|
| 79 | Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State);
|
---|
| 80 | Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId);
|
---|
| 81 | Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId);
|
---|
| 82 | Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception);
|
---|
| 83 | Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
|
---|
| 84 | }
|
---|
[5526] | 85 |
|
---|
| 86 | job1 = job1loaded;
|
---|
[5511] | 87 |
|
---|
[5526] | 88 | // test jobstates
|
---|
[5636] | 89 | job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10);
|
---|
| 90 | job1.StateLog.Add(new DT.StateLog { State = JobState.Calculating, DateTime = DateTime.Now }); Thread.Sleep(10);
|
---|
| 91 | job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10);
|
---|
| 92 | job1.StateLog.Add(new DT.StateLog { State = JobState.Finished, DateTime = DateTime.Now }); Thread.Sleep(10);
|
---|
[5526] | 93 | dao.UpdateJob(job1);
|
---|
| 94 |
|
---|
| 95 | job1loaded = dao.GetJob(job1.Id);
|
---|
| 96 | for (int i = 0; i < job1.StateLog.Count; i++) {
|
---|
| 97 | Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId);
|
---|
| 98 | Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State);
|
---|
| 99 | Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId);
|
---|
| 100 | Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId);
|
---|
| 101 | Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception);
|
---|
| 102 | Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[5786] | 105 | DT.JobData jobData1Loaded = dao.GetJobData(job1.Id);
|
---|
| 106 | Assert.AreEqual(jobData1.JobId, jobData1Loaded.JobId);
|
---|
| 107 | Assert.IsTrue(Math.Abs((jobData1.LastUpdate - jobData1Loaded.LastUpdate).TotalSeconds) < 1);
|
---|
| 108 | Assert.IsTrue(jobData1.Data.SequenceEqual(jobData1Loaded.Data));
|
---|
[5106] | 109 |
|
---|
[6006] | 110 | dao.DeleteHiveExperiment(he.Id);
|
---|
| 111 |
|
---|
[5106] | 112 | Assert.AreEqual(null, dao.GetJob(job1.Id));
|
---|
[5786] | 113 | Assert.AreEqual(null, dao.GetJobData(job1.Id));
|
---|
[6006] | 114 | Assert.AreEqual(null, dao.GetHiveExperiment(he.Id));
|
---|
[5155] | 115 | }
|
---|
[5106] | 116 |
|
---|
[5155] | 117 | [TestMethod]
|
---|
| 118 | public void TestSlaveDao() {
|
---|
| 119 | IHiveDao dao = ServiceLocator.Instance.HiveDao;
|
---|
[5106] | 120 | DT.SlaveGroup slaveGroup = new DT.SlaveGroup() {
|
---|
| 121 | Name = "Test"
|
---|
| 122 | };
|
---|
| 123 | slaveGroup.Id = dao.AddSlaveGroup(slaveGroup);
|
---|
| 124 |
|
---|
[5155] | 125 | DT.Slave slave = new DT.Slave() {
|
---|
[5405] | 126 | Id = Guid.NewGuid(),
|
---|
[5404] | 127 | Name = "Test",
|
---|
[5405] | 128 | OperatingSystem = null, //"Windows 3.11",
|
---|
[5404] | 129 | Cores = 2,
|
---|
[5405] | 130 | Memory = 1024,
|
---|
| 131 | FreeMemory = 512
|
---|
[5155] | 132 | };
|
---|
| 133 |
|
---|
[5405] | 134 | Assert.AreEqual(slave.Id, dao.AddSlave(slave));
|
---|
| 135 |
|
---|
| 136 | // update
|
---|
| 137 | slave.FreeMemory = 255;
|
---|
| 138 | slave.OperatingSystem = Environment.OSVersion.VersionString;
|
---|
| 139 | dao.UpdateSlave(slave);
|
---|
| 140 |
|
---|
| 141 | DT.Slave slaveLoaded = dao.GetSlave(slave.Id);
|
---|
| 142 | Assert.AreEqual(slave.FreeMemory, slaveLoaded.FreeMemory);
|
---|
| 143 |
|
---|
[5155] | 144 | dao.DeleteSlave(slave.Id);
|
---|
[5106] | 145 | dao.DeleteSlaveGroup(slaveGroup.Id);
|
---|
| 146 | }
|
---|
[5155] | 147 |
|
---|
| 148 | [TestMethod]
|
---|
[5402] | 149 | public void TestPluginDao() {
|
---|
| 150 | IHiveDao dao = ServiceLocator.Instance.HiveDao;
|
---|
| 151 |
|
---|
| 152 | DT.Plugin plugin1 = new DT.Plugin();
|
---|
| 153 | plugin1.DateCreated = DateTime.Now;
|
---|
| 154 | plugin1.IsLocal = false;
|
---|
| 155 | plugin1.Name = "Tests.MyPlugin";
|
---|
| 156 | plugin1.Version = new Version("1.0.0.0");
|
---|
| 157 | plugin1.UserId = Guid.Empty;
|
---|
| 158 |
|
---|
| 159 | plugin1.Id = dao.AddPlugin(plugin1);
|
---|
| 160 |
|
---|
| 161 | DT.Plugin plugin1loaded = dao.GetPlugin(plugin1.Id);
|
---|
| 162 | Assert.AreEqual(plugin1.Id, plugin1loaded.Id);
|
---|
| 163 | Assert.AreEqual(plugin1.Name, plugin1loaded.Name);
|
---|
| 164 | Assert.AreEqual(plugin1.Version, plugin1loaded.Version);
|
---|
| 165 | Assert.AreEqual(plugin1.UserId, plugin1loaded.UserId);
|
---|
| 166 | Assert.AreEqual(plugin1.DateCreated.ToString(), plugin1loaded.DateCreated.ToString());
|
---|
| 167 | Assert.AreEqual(plugin1.IsLocal, plugin1loaded.IsLocal);
|
---|
| 168 |
|
---|
| 169 | DT.PluginData pluginData1 = new DT.PluginData();
|
---|
| 170 | pluginData1.PluginId = plugin1.Id;
|
---|
| 171 | pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
|
---|
| 172 | pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
|
---|
| 173 |
|
---|
| 174 | pluginData1.Id = dao.AddPluginData(pluginData1);
|
---|
| 175 |
|
---|
| 176 | DT.PluginData pluginData1loaded = dao.GetPluginData(pluginData1.Id);
|
---|
| 177 | Assert.AreEqual(pluginData1.Id, pluginData1loaded.Id);
|
---|
| 178 |
|
---|
| 179 | Assert.AreEqual(pluginData1.PluginId, pluginData1loaded.PluginId);
|
---|
| 180 | Assert.AreEqual(pluginData1.FileName, pluginData1loaded.FileName);
|
---|
| 181 | Assert.IsTrue(pluginData1.Data.SequenceEqual(pluginData1loaded.Data));
|
---|
| 182 |
|
---|
| 183 | dao.DeletePluginData(pluginData1.Id);
|
---|
| 184 | dao.DeletePlugin(plugin1.Id);
|
---|
| 185 |
|
---|
| 186 | Assert.AreEqual(null, dao.GetPlugin(plugin1.Id));
|
---|
| 187 | Assert.AreEqual(null, dao.GetPluginData(pluginData1.Id));
|
---|
[5155] | 188 | }
|
---|
[5402] | 189 |
|
---|
[5511] | 190 | [TestMethod]
|
---|
| 191 | public void TestHiveExperimentDao() {
|
---|
| 192 | IHiveDao dao = ServiceLocator.Instance.HiveDao;
|
---|
| 193 |
|
---|
| 194 | DT.HiveExperiment he = new DT.HiveExperiment();
|
---|
[6006] | 195 | he.DateCreated = DateTime.Now;
|
---|
| 196 | he.Name = "TestExperiment";
|
---|
| 197 | he.OwnerUserId = Guid.NewGuid();
|
---|
| 198 | he.ResourceNames = "HEAL";
|
---|
| 199 | he.Id = dao.AddHiveExperiment(he);
|
---|
[5511] | 200 |
|
---|
| 201 | DT.Job job = new DT.Job();
|
---|
[5636] | 202 | job.State = JobState.Offline;
|
---|
| 203 | job.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now });
|
---|
[6006] | 204 | job.HiveExperimentId = he.Id;
|
---|
[5511] | 205 | job.Id = dao.AddJob(job);
|
---|
| 206 |
|
---|
| 207 | DT.HiveExperimentPermission perm = new DT.HiveExperimentPermission();
|
---|
| 208 | perm.HiveExperimentId = he.Id;
|
---|
| 209 | perm.GrantedByUserId = he.OwnerUserId;
|
---|
| 210 | perm.GrantedUserId = Guid.NewGuid();
|
---|
| 211 | perm.Permission = Permission.Write;
|
---|
| 212 | dao.AddHiveExperimentPermission(perm);
|
---|
| 213 |
|
---|
| 214 | DT.HiveExperiment heLoaded = dao.GetHiveExperiment(he.Id);
|
---|
| 215 | Assert.AreEqual(he.Id, heLoaded.Id);
|
---|
| 216 | Assert.AreEqual(he.Name, heLoaded.Name);
|
---|
| 217 | Assert.AreEqual(he.ResourceNames, heLoaded.ResourceNames);
|
---|
| 218 | //Assert.AreEqual(he.LastAccessed, heLoaded.LastAccessed);
|
---|
| 219 | //Assert.AreEqual(he.DateCreated, heLoaded.DateCreated);
|
---|
[5526] | 220 |
|
---|
[6006] | 221 | DT.Job jobLoaded = dao.GetJob(job.Id);
|
---|
[5511] | 222 | Assert.AreEqual(job.Id, jobLoaded.Id);
|
---|
| 223 | Assert.AreEqual(job.State, jobLoaded.State);
|
---|
[6006] | 224 | Assert.AreEqual(job.HiveExperimentId, jobLoaded.HiveExperimentId);
|
---|
[5511] | 225 |
|
---|
| 226 | DT.HiveExperimentPermission permLoaded = dao.GetHiveExperimentPermission(he.Id, perm.GrantedUserId);
|
---|
| 227 | Assert.AreEqual(perm.HiveExperimentId, permLoaded.HiveExperimentId);
|
---|
| 228 | Assert.AreEqual(perm.GrantedUserId, permLoaded.GrantedUserId);
|
---|
| 229 | Assert.AreEqual(perm.GrantedByUserId, permLoaded.GrantedByUserId);
|
---|
| 230 | Assert.AreEqual(perm.Permission, permLoaded.Permission);
|
---|
| 231 |
|
---|
| 232 | dao.DeleteHiveExperiment(he.Id);
|
---|
| 233 | Assert.AreEqual(null, dao.GetHiveExperiment(he.Id));
|
---|
[6006] | 234 | Assert.AreEqual(null, dao.GetJob(job.Id));
|
---|
[5511] | 235 | Assert.AreEqual(null, dao.GetHiveExperimentPermission(perm.HiveExperimentId, perm.GrantedUserId));
|
---|
| 236 |
|
---|
| 237 | }
|
---|
[5106] | 238 | }
|
---|
| 239 | }
|
---|