NetworkStatusTest.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Net.NetworkInformation;
  5. using System.Net.Sockets;
  6. using Vinno.IUS.Common.Log;
  7. namespace Vinno.vCloud.Common.FIS.Test
  8. {
  9. public class NetworkStatusTest : VCloudTestItem
  10. {
  11. private readonly string _networkStatusTestError1 = "NetworkStatusTestError1";
  12. private readonly string _networkStatusTestError2 = "NetworkStatusTestError2";
  13. private readonly string _networkStatusTestError3 = "NetworkStatusTestError3";
  14. public NetworkStatusTest(VCloudServiceTest vCloudServiceTest) : base("NetworkStatus", vCloudServiceTest)
  15. {
  16. }
  17. public override VCloudServiceTestResult Execute()
  18. {
  19. //Check if network connected
  20. if (!CheckNetworkConnected())
  21. {
  22. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _networkStatusTestError1, "(101)");//ErrorCode=101
  23. }
  24. //Get ip from dns
  25. var result = TimeoutExecute(CheckIfvCloudHostIpAvailable);
  26. if (result == VCloudSubItemTestStatus.Timeout)
  27. {
  28. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(102)");//ErrorCode=102
  29. }
  30. if (result == VCloudSubItemTestStatus.Failed)
  31. {
  32. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _networkStatusTestError3, "(103)");//ErrorCode=103
  33. }
  34. //Check if can access vCloud host
  35. result = TimeoutExecute(CheckIfAccessvCloudHost);
  36. if (result == VCloudSubItemTestStatus.Timeout)
  37. {
  38. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(104)");//ErrorCode=104
  39. }
  40. if (result == VCloudSubItemTestStatus.Failed)
  41. {
  42. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _networkStatusTestError2, "(105)");//ErrorCode=105
  43. }
  44. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestDone);
  45. }
  46. private VCloudSubItemTestStatus CheckIfvCloudHostIpAvailable()
  47. {
  48. var ipAddress = GetIpAddress(VCloudServiceTest.VCloudHost);
  49. if (ipAddress == null)
  50. {
  51. return VCloudSubItemTestStatus.Failed;
  52. }
  53. return VCloudSubItemTestStatus.Successful;
  54. }
  55. private VCloudSubItemTestStatus CheckIfAccessvCloudHost()
  56. {
  57. VCloudSubItemTestStatus connnectvCloudHost = VCloudSubItemTestStatus.Failed;
  58. TcpClient tcp = null;
  59. try
  60. {
  61. tcp = new TcpClient();
  62. tcp.Connect(VCloudServiceTest.VCloudHost, 9090);
  63. if (tcp.Connected)
  64. {
  65. connnectvCloudHost = VCloudSubItemTestStatus.Successful;
  66. }
  67. }
  68. catch (Exception e)
  69. {
  70. Logger.WriteLineInfo($"TestNetworkStatus - access {VCloudServiceTest.VCloudHost}:9090 ex:{e}");
  71. }
  72. finally
  73. {
  74. tcp?.Close();
  75. }
  76. return connnectvCloudHost;
  77. }
  78. private bool CheckNetworkConnected()
  79. {
  80. var networkAvailable = NetworkInterface.GetIsNetworkAvailable();
  81. if (!networkAvailable)
  82. {
  83. return false;
  84. }
  85. return true;
  86. }
  87. private IPAddress GetIpAddress(string host)
  88. {
  89. IPAddress address = null;
  90. IPAddress[] ipAddresses = null;
  91. try
  92. {
  93. ipAddresses = Dns.GetHostAddresses(host);
  94. //This is for normal case to get the ipv4 address.
  95. address = ipAddresses?.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);
  96. }
  97. catch (Exception ex)
  98. {
  99. Logger.WriteLineError($"TestNetworkStatus - Get hosts address from dns failed.{ex}");
  100. }
  101. return address;
  102. }
  103. }
  104. }