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