/ClusterAware/ClusterAwareTest/NetworkMethodsTest.cs

# · C# · 240 lines · 195 code · 11 blank · 34 comment · 12 complexity · 8da74b37b2759b04916a65dc67dd9bcb MD5 · raw file

  1. using ClusterAware;
  2. using ClusterAwareAdmin;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using ClusterAwareProperties;
  5. namespace ClusterAwareTest
  6. {
  7. /// <summary>
  8. ///This is a test class for NetworkMethodsTest and is intended
  9. ///to contain all NetworkMethodsTest Unit Tests
  10. ///</summary>
  11. [TestClass]
  12. public class NetworkMethodsTest
  13. {
  14. /// <summary>
  15. ///Gets or sets the test context which provides
  16. ///information about and functionality for the current test run.
  17. ///</summary>
  18. public TestContext TestContext { get; set; }
  19. private static ClusterManager _manager;
  20. #region Additional test attributes
  21. //
  22. //You can use the following additional attributes as you write your tests:
  23. //
  24. //Use ClassInitialize to run code before running the first test in the class
  25. [ClassInitialize]
  26. public static void ClassTestInitialize(TestContext testContext)
  27. {
  28. var managerInit = new ClusterManagerAdmin
  29. {
  30. Username = SharedConsts.User,
  31. Password = SharedConsts.Pwd,
  32. Domain = SharedConsts.Domain,
  33. Machine = SharedConsts.Machine,
  34. CacheInstances = false
  35. };
  36. managerInit.Connect();
  37. if (!managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
  38. {
  39. managerInit.Groups.Create(SharedConsts.NewGroupTest);
  40. }
  41. if (!managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
  42. {
  43. managerInit.Groups.Create(SharedConsts.GroupTest);
  44. }
  45. if (managerInit.OperationSystem == WindowsOperationSystem.Windows2008 || managerInit.OperationSystem == WindowsOperationSystem.Windows2008R2)
  46. {
  47. var node = managerInit.Groups.GetActiveNode(SharedConsts.GroupTest).Name;
  48. managerInit.Groups.SetPreferredOwners(SharedConsts.GroupTest, new[] { node });
  49. }
  50. if (!managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
  51. {
  52. managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceDep, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
  53. var res = managerInit.Resources.GetResource(SharedConsts.ResourceDep);
  54. res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
  55. res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
  56. res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
  57. managerInit.Resources.Save(res);
  58. }
  59. if (!managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
  60. {
  61. managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceTest, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
  62. var res = managerInit.Resources.GetResource(SharedConsts.ResourceTest);
  63. res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
  64. res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
  65. res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
  66. managerInit.Resources.Save(res);
  67. managerInit.Resources.AddDependency(SharedConsts.ResourceTest, SharedConsts.ResourceDep);
  68. }
  69. managerInit.Dispose();
  70. }
  71. //
  72. //Use ClassCleanup to run code after all tests in a class have run
  73. [ClassCleanup]
  74. public static void ClassTestCleanup()
  75. {
  76. var managerInit = new ClusterManagerAdmin
  77. {
  78. Username = SharedConsts.User,
  79. Password = SharedConsts.Pwd,
  80. Domain = SharedConsts.Domain,
  81. Machine = SharedConsts.Machine,
  82. CacheInstances = false
  83. };
  84. managerInit.Connect();
  85. if (managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
  86. {
  87. managerInit.Resources.Delete(SharedConsts.ResourceDep);
  88. }
  89. if (managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
  90. {
  91. managerInit.Resources.Delete(SharedConsts.ResourceTest);
  92. }
  93. if (managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
  94. {
  95. managerInit.Groups.Delete(SharedConsts.NewGroupTest);
  96. }
  97. if (managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
  98. {
  99. managerInit.Groups.Delete(SharedConsts.GroupTest);
  100. }
  101. managerInit.Dispose();
  102. }
  103. //
  104. //Use TestInitialize to run code before running each test
  105. [TestInitialize]
  106. public void Initialize()
  107. {
  108. _manager = new ClusterManager
  109. {
  110. Username = SharedConsts.User,
  111. Password = SharedConsts.Pwd,
  112. Domain = SharedConsts.Domain,
  113. Machine = SharedConsts.Machine,
  114. CacheInstances = false
  115. };
  116. _manager.Connect();
  117. }
  118. //
  119. //Use TestCleanup to run code after each test has run
  120. [TestCleanup]
  121. public void Cleanup()
  122. {
  123. _manager.Dispose();
  124. }
  125. //
  126. #endregion
  127. /// <summary>
  128. ///A test for Save
  129. ///</summary>
  130. [TestMethod]
  131. public void SaveTest()
  132. {
  133. _manager.CacheInstances = true;
  134. var target = new NetworkMethods(_manager);
  135. foreach (var net in target.GetNetworks())
  136. {
  137. var olddes = net.Description;
  138. net.Description = "TestSave";
  139. var actual = target.Save(net);
  140. Assert.AreEqual("TestSave", net.Description);
  141. actual.Description = olddes;
  142. actual = target.Save(actual);
  143. Assert.AreEqual(olddes, actual.Description);
  144. }
  145. _manager.CacheInstances = false;
  146. foreach (var net in target.GetNetworks())
  147. {
  148. var olddes = net.Description;
  149. net.Description = "TestSave";
  150. var actual = target.Save(net);
  151. Assert.AreEqual("TestSave", net.Description);
  152. actual.Description = olddes;
  153. actual = target.Save(actual);
  154. Assert.AreEqual(olddes, actual.Description);
  155. }
  156. }
  157. /// <summary>
  158. ///A test for GetNetworks
  159. ///</summary>
  160. [TestMethod]
  161. public void GetNetworksTest()
  162. {
  163. _manager.CacheInstances = true;
  164. var target = new NetworkMethods(_manager);
  165. var actual = target.GetNetworks();
  166. Assert.AreNotEqual(0, actual.Length);
  167. _manager.CacheInstances = false;
  168. actual = target.GetNetworks();
  169. Assert.AreNotEqual(0, actual.Length);
  170. }
  171. /// <summary>
  172. ///A test for GetNetwork
  173. ///</summary>
  174. [TestMethod]
  175. public void GetNetworkTest()
  176. {
  177. _manager.CacheInstances = true;
  178. var target = new NetworkMethods(_manager);
  179. foreach (var net in target.GetNetworks())
  180. {
  181. var actual = target.GetNetwork(net.Name);
  182. Assert.AreEqual(net.Name, actual.Name);
  183. }
  184. _manager.CacheInstances = false;
  185. foreach (var net in target.GetNetworks())
  186. {
  187. var actual = target.GetNetwork(net.Name);
  188. Assert.AreEqual(net.Name, actual.Name);
  189. }
  190. }
  191. /// <summary>
  192. ///A test for ExistNetwork
  193. ///</summary>
  194. [TestMethod]
  195. public void ExistNetworkTest()
  196. {
  197. _manager.CacheInstances = true;
  198. var target = new NetworkMethods(_manager);
  199. foreach (var net in target.GetNetworks())
  200. {
  201. var actual = target.ExistNetwork(net.Name);
  202. Assert.AreEqual(true, actual);
  203. }
  204. var result = target.ExistNetwork("Notfound");
  205. Assert.AreEqual(false, result);
  206. _manager.CacheInstances = false;
  207. foreach (var net in target.GetNetworks())
  208. {
  209. var actual = target.ExistNetwork(net.Name);
  210. Assert.AreEqual(true, actual);
  211. }
  212. result = target.ExistNetwork("Notfound");
  213. Assert.AreEqual(false, result);
  214. }
  215. /// <summary>
  216. ///A test for NetworkMethods Constructor
  217. ///</summary>
  218. [TestMethod]
  219. public void NetworkMethodsConstructorTest()
  220. {
  221. _manager.CacheInstances = true;
  222. var target = new NetworkMethods(_manager);
  223. Assert.AreNotEqual(null, target);
  224. _manager.CacheInstances = false;
  225. target = new NetworkMethods(_manager);
  226. Assert.AreNotEqual(null, target);
  227. }
  228. }
  229. }