123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using System;
- using System.Linq;
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Net.Sockets;
- using Vinno.IUS.Common.Log;
- namespace Vinno.vCloud.Common.FIS.Test
- {
- public class NetworkStatusTest : VCloudTestItem
- {
- private readonly string _networkStatusTestError1 = "NetworkStatusTestError1";
- private readonly string _networkStatusTestError2 = "NetworkStatusTestError2";
- private readonly string _networkStatusTestError3 = "NetworkStatusTestError3";
- public NetworkStatusTest(VCloudServiceTest vCloudServiceTest) : base("NetworkStatus", vCloudServiceTest)
- {
- }
- public override VCloudServiceTestResult Execute()
- {
- //Check if network connected
- if (!CheckNetworkConnected())
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _networkStatusTestError1, "(101)");//ErrorCode=101
- }
- //Get ip from dns
- var result = TimeoutExecute(CheckIfvCloudHostIpAvailable);
- if (result == VCloudSubItemTestStatus.Timeout)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(102)");//ErrorCode=102
- }
- if (result == VCloudSubItemTestStatus.Failed)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _networkStatusTestError3, "(103)");//ErrorCode=103
- }
- //Check if can access vCloud host
- result = TimeoutExecute(CheckIfAccessvCloudHost);
- if (result == VCloudSubItemTestStatus.Timeout)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(104)");//ErrorCode=104
- }
- if (result == VCloudSubItemTestStatus.Failed)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _networkStatusTestError2, "(105)");//ErrorCode=105
- }
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestDone);
- }
- private VCloudSubItemTestStatus CheckIfvCloudHostIpAvailable()
- {
- var ipAddress = GetIpAddress(VCloudServiceTest.VCloudHost);
- if (ipAddress == null)
- {
- return VCloudSubItemTestStatus.Failed;
- }
- return VCloudSubItemTestStatus.Successful;
- }
- private VCloudSubItemTestStatus CheckIfAccessvCloudHost()
- {
- VCloudSubItemTestStatus connnectvCloudHost = VCloudSubItemTestStatus.Failed;
- TcpClient tcp = null;
- try
- {
- tcp = new TcpClient();
- tcp.Connect(VCloudServiceTest.VCloudHost, 9090);
- if (tcp.Connected)
- {
- connnectvCloudHost = VCloudSubItemTestStatus.Successful;
- }
- }
- catch (Exception e)
- {
- Logger.WriteLineInfo($"TestNetworkStatus - access {VCloudServiceTest.VCloudHost}:9090 ex:{e}");
- }
- finally
- {
- tcp?.Close();
- }
- return connnectvCloudHost;
- }
- private bool CheckNetworkConnected()
- {
- var networkAvailable = NetworkInterface.GetIsNetworkAvailable();
- if (!networkAvailable)
- {
- return false;
- }
- return true;
- }
- private IPAddress GetIpAddress(string host)
- {
- IPAddress address = null;
- IPAddress[] ipAddresses = null;
- try
- {
- ipAddresses = Dns.GetHostAddresses(host);
- //This is for normal case to get the ipv4 address.
- address = ipAddresses?.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"TestNetworkStatus - Get hosts address from dns failed.{ex}");
- }
- return address;
- }
- }
- }
|