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