#region License Information /* HeuristicLab * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Threading; using HeuristicLab.Services.Hive.Common.DataTransfer; using HeuristicLab.Services.Hive.Common.ServiceContracts; using HeuristicLab.Services.Hive.DataAccess; using Microsoft.VisualStudio.TestTools.UnitTesting; using DT = HeuristicLab.Services.Hive.Common.DataTransfer; namespace HeuristicLab.Services.Hive.Tests { [TestClass] public class DaoTests { [ClassInitialize] public static void MyClassInitialize(TestContext testContext) { ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance); } private IHiveService GetLocalService() { return new HiveService(); } [TestMethod] public void TestJobDao() { IHiveDao dao = ServiceLocator.Instance.HiveDao; DT.HiveExperiment he = new DT.HiveExperiment(); he.DateCreated = DateTime.Now; he.Name = "TestExperiment"; he.OwnerUserId = Guid.NewGuid(); he.ResourceNames = "HEAL"; he.Id = dao.AddHiveExperiment(he); DT.Job job1 = new DT.Job(); job1.State = JobState.Offline; job1.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now }); job1.Command = Command.Pause; job1.HiveExperimentId = he.Id; job1.IsPrivileged = true; DT.JobData jobData1 = new DT.JobData(); jobData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 }; jobData1.LastUpdate = DateTime.Now; DT.Plugin plugin1 = new DT.Plugin(); plugin1.Name = "Tests.MyPlugin"; plugin1.Version = new Version("1.0.0.0"); plugin1.UserId = Guid.Empty; plugin1.DateCreated = DateTime.Now; plugin1.Hash = new byte[] { 1, 2, 3 }; DT.PluginData pluginData1 = new DT.PluginData(); pluginData1.PluginId = plugin1.Id; pluginData1.FileName = "Tests.MyPlugin-1.0.dll"; pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 }; plugin1.Id = dao.AddPlugin(plugin1); pluginData1.PluginId = plugin1.Id; pluginData1.Id = dao.AddPluginData(pluginData1); job1.PluginsNeededIds.Add(plugin1.Id); job1.Id = dao.AddJob(job1); jobData1.JobId = job1.Id; dao.AddJobData(jobData1); DT.Job job1loaded = dao.GetJob(job1.Id); Assert.AreEqual(job1.Id, job1loaded.Id); Assert.AreEqual(job1.CoresNeeded, job1loaded.CoresNeeded); Assert.AreEqual(job1.DateCreated.ToString(), job1loaded.DateCreated.ToString()); Assert.AreEqual(null, job1loaded.DateFinished); Assert.IsTrue(job1.PluginsNeededIds.SequenceEqual(job1loaded.PluginsNeededIds)); Assert.AreEqual(job1.StateLog.Count, job1loaded.StateLog.Count); Assert.AreEqual(job1.Command, job1loaded.Command); Assert.AreEqual(job1.HiveExperimentId, job1loaded.HiveExperimentId); Assert.AreEqual(job1.IsPrivileged, job1loaded.IsPrivileged); Assert.IsTrue(Math.Abs((job1loaded.LastJobDataUpdate - jobData1.LastUpdate).TotalSeconds) < 1); for (int i = 0; i < job1.StateLog.Count; i++) { Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId); Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State); Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId); Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId); Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception); Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1); } job1 = job1loaded; // test jobstates job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10); job1.StateLog.Add(new DT.StateLog { State = JobState.Calculating, DateTime = DateTime.Now }); Thread.Sleep(10); job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10); job1.StateLog.Add(new DT.StateLog { State = JobState.Finished, DateTime = DateTime.Now }); Thread.Sleep(10); dao.UpdateJob(job1); job1loaded = dao.GetJob(job1.Id); for (int i = 0; i < job1.StateLog.Count; i++) { Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId); Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State); Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId); Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId); Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception); Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1); } DT.JobData jobData1Loaded = dao.GetJobData(job1.Id); Assert.AreEqual(jobData1.JobId, jobData1Loaded.JobId); Assert.IsTrue(Math.Abs((jobData1.LastUpdate - jobData1Loaded.LastUpdate).TotalSeconds) < 1); Assert.IsTrue(jobData1.Data.SequenceEqual(jobData1Loaded.Data)); dao.DeleteHiveExperiment(he.Id); Assert.AreEqual(null, dao.GetJob(job1.Id)); Assert.AreEqual(null, dao.GetJobData(job1.Id)); Assert.AreEqual(null, dao.GetHiveExperiment(he.Id)); dao.DeletePlugin(plugin1.Id); Assert.AreEqual(null, dao.GetPlugin(plugin1.Id)); } [TestMethod] public void TestSlaveDao() { IHiveDao dao = ServiceLocator.Instance.HiveDao; DT.SlaveGroup slaveGroup = new DT.SlaveGroup() { Name = "Test" }; slaveGroup.Id = dao.AddSlaveGroup(slaveGroup); DT.Slave slave = new DT.Slave() { Id = Guid.NewGuid(), Name = "Test", OperatingSystem = null, //"Windows 3.11", Cores = 2, Memory = 1024, FreeMemory = 512 }; Assert.AreEqual(slave.Id, dao.AddSlave(slave)); // update slave.FreeMemory = 255; slave.OperatingSystem = Environment.OSVersion.VersionString; dao.UpdateSlave(slave); DT.Slave slaveLoaded = dao.GetSlave(slave.Id); Assert.AreEqual(slave.FreeMemory, slaveLoaded.FreeMemory); dao.DeleteSlave(slave.Id); dao.DeleteSlaveGroup(slaveGroup.Id); } [TestMethod] public void TestPluginDao() { IHiveDao dao = ServiceLocator.Instance.HiveDao; DT.Plugin plugin1 = new DT.Plugin(); plugin1.DateCreated = DateTime.Now; plugin1.Name = "Tests.MyPlugin"; plugin1.Version = new Version("1.0.0.0"); plugin1.UserId = Guid.Empty; plugin1.Hash = new byte[] { 1, 2, 3 }; plugin1.Id = dao.AddPlugin(plugin1); DT.Plugin plugin1loaded = dao.GetPlugin(plugin1.Id); Assert.AreEqual(plugin1.Id, plugin1loaded.Id); Assert.AreEqual(plugin1.Name, plugin1loaded.Name); Assert.AreEqual(plugin1.Version, plugin1loaded.Version); Assert.AreEqual(plugin1.UserId, plugin1loaded.UserId); Assert.AreEqual(plugin1.DateCreated.ToString(), plugin1loaded.DateCreated.ToString()); Assert.IsTrue(plugin1.Hash.SequenceEqual(plugin1loaded.Hash)); DT.PluginData pluginData1 = new DT.PluginData(); pluginData1.PluginId = plugin1.Id; pluginData1.FileName = "Tests.MyPlugin-1.0.dll"; pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 }; pluginData1.Id = dao.AddPluginData(pluginData1); DT.PluginData pluginData1loaded = dao.GetPluginData(pluginData1.Id); Assert.AreEqual(pluginData1.Id, pluginData1loaded.Id); Assert.AreEqual(pluginData1.PluginId, pluginData1loaded.PluginId); Assert.AreEqual(pluginData1.FileName, pluginData1loaded.FileName); Assert.IsTrue(pluginData1.Data.SequenceEqual(pluginData1loaded.Data)); dao.DeletePluginData(pluginData1.Id); dao.DeletePlugin(plugin1.Id); Assert.AreEqual(null, dao.GetPlugin(plugin1.Id)); Assert.AreEqual(null, dao.GetPluginData(pluginData1.Id)); } [TestMethod] public void TestHiveExperimentDao() { IHiveDao dao = ServiceLocator.Instance.HiveDao; DT.HiveExperiment he = new DT.HiveExperiment(); he.DateCreated = DateTime.Now; he.Name = "TestExperiment"; he.OwnerUserId = Guid.NewGuid(); he.ResourceNames = "HEAL"; he.Id = dao.AddHiveExperiment(he); DT.Job job = new DT.Job(); job.State = JobState.Offline; job.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now }); job.HiveExperimentId = he.Id; job.Id = dao.AddJob(job); DT.HiveExperimentPermission perm = new DT.HiveExperimentPermission(); perm.HiveExperimentId = he.Id; perm.GrantedByUserId = he.OwnerUserId; perm.GrantedUserId = Guid.NewGuid(); perm.Permission = Permission.Full; dao.AddHiveExperimentPermission(perm); DT.HiveExperiment heLoaded = dao.GetHiveExperiment(he.Id); Assert.AreEqual(he.Id, heLoaded.Id); Assert.AreEqual(he.Name, heLoaded.Name); Assert.AreEqual(he.ResourceNames, heLoaded.ResourceNames); //Assert.AreEqual(he.LastAccessed, heLoaded.LastAccessed); //Assert.AreEqual(he.DateCreated, heLoaded.DateCreated); DT.Job jobLoaded = dao.GetJob(job.Id); Assert.AreEqual(job.Id, jobLoaded.Id); Assert.AreEqual(job.State, jobLoaded.State); Assert.AreEqual(job.HiveExperimentId, jobLoaded.HiveExperimentId); DT.HiveExperimentPermission permLoaded = dao.GetHiveExperimentPermission(he.Id, perm.GrantedUserId); Assert.AreEqual(perm.HiveExperimentId, permLoaded.HiveExperimentId); Assert.AreEqual(perm.GrantedUserId, permLoaded.GrantedUserId); Assert.AreEqual(perm.GrantedByUserId, permLoaded.GrantedByUserId); Assert.AreEqual(perm.Permission, permLoaded.Permission); dao.DeleteHiveExperiment(he.Id); Assert.AreEqual(null, dao.GetHiveExperiment(he.Id)); Assert.AreEqual(null, dao.GetJob(job.Id)); Assert.AreEqual(null, dao.GetHiveExperimentPermission(perm.HiveExperimentId, perm.GrantedUserId)); } [TestMethod] public void TestStatistics() { IHiveDao dao = ServiceLocator.Instance.HiveDao; var stats = new DT.Statistics(); stats.TimeStamp = DateTime.Now; var slaveStats = new List(); slaveStats.Add(new DT.SlaveStatistics() { Cores = 8, FreeCores = 3, Memory = 8000, FreeMemory = 3000, CpuUtilization = 0.8, SlaveId = Guid.NewGuid() }); slaveStats.Add(new DT.SlaveStatistics() { Cores = 2, FreeCores = 0, Memory = 1024, FreeMemory = 100, CpuUtilization = 0.99, SlaveId = Guid.NewGuid() }); slaveStats.Add(new DT.SlaveStatistics() { Cores = 4, FreeCores = 4, Memory = 3024, FreeMemory = 2300, CpuUtilization = 0.01, SlaveId = Guid.NewGuid() }); stats.SlaveStatistics = slaveStats; var userStats = new List(); userStats.Add(new DT.UserStatistics() { UsedCores = 15, ExecutionTime = TimeSpan.FromHours(123), UserId = Guid.NewGuid() }); userStats.Add(new DT.UserStatistics() { UsedCores = 42, ExecutionTime = TimeSpan.FromHours(945), UserId = Guid.NewGuid() }); stats.UserStatistics = userStats; stats.Id = dao.AddStatistics(stats); var statsLoaded = dao.GetStatistic(stats.Id); Assert.AreEqual(stats.Id, statsLoaded.Id); Assert.IsTrue(Math.Abs((stats.TimeStamp - statsLoaded.TimeStamp).TotalSeconds) < 1); for (int i = 0; i < slaveStats.Count; i++) { Assert.AreEqual(slaveStats[i].Cores, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).Cores); Assert.AreEqual(slaveStats[i].FreeCores, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).FreeCores); Assert.AreEqual(slaveStats[i].Memory, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).Memory); Assert.AreEqual(slaveStats[i].FreeMemory, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).FreeMemory); Assert.AreEqual(slaveStats[i].CpuUtilization, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).CpuUtilization); } for (int i = 0; i < userStats.Count; i++) { Assert.AreEqual(userStats[i].ExecutionTime, statsLoaded.UserStatistics.Single(x => x.UserId == userStats[i].UserId).ExecutionTime); Assert.AreEqual(userStats[i].UsedCores, statsLoaded.UserStatistics.Single(x => x.UserId == userStats[i].UserId).UsedCores); } dao.DeleteStatistics(stats.Id); Assert.AreEqual(null, dao.GetStatistic(stats.Id)); } } }