CA-203431: Host name is repeated twice in multipathing alert message in XC (#2272)

* CA-203431: Removed prefix comma and space from the start of the list of hosts in the multipathing alert message.

* CA-203431: Removed duplicate Host names from multipathing alert message.

* CA-203431: Unit tests for MessageAlert.FindHostUuids with tweaks to allow it to handle a null input as if it was an empty enumeration.

Signed-off-by: Aaron Robson <aaron.robson@citrix.com>
This commit is contained in:
Aaron Robson 2018-10-23 14:50:28 +01:00 committed by Mihaela Stoica
parent 1ef23f393f
commit 97cd8286b9
3 changed files with 115 additions and 9 deletions

View File

@ -31,6 +31,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using XenAPI;
using XenAdmin.Actions;
using XenAdmin.Core;
@ -311,21 +312,28 @@ namespace XenAdmin.Alerts
else
{
// Several hosts in pool unhealthy, list their names as a summary
string output = "";
foreach (string s in currentState)
{
Match m = multipathRegex.Match(s);
if (m.Success)
{
output = string.Format("{0}, '{1}'", output, Message.Connection.Cache.Find_By_Uuid<Host>(m.Groups[1].Value));
}
}
var output = string.Join(", ",
FindHostUuids(currentState)
.Select(s => string.Format("'{0}'", Message.Connection.Cache.Find_By_Uuid<Host>(s)))
);
return string.Format(PropertyManager.GetFriendlyName("Message.body-multipath_periodic_alert_summary"),
Helpers.GetName(XenObject),
output);
}
}
public static IEnumerable<string> FindHostUuids(IEnumerable<string> lines)
{
if (lines == null)
return Enumerable.Empty<string>();
return lines
.Select(s => multipathRegex.Match(s))
.Where(m => m.Success)
.Select(m => m.Groups[1].Value)
.Distinct();
}
private string GetManagementBondName()
{
Bond bond = NetworkingHelper.GetMasterManagementBond(Connection);

View File

@ -0,0 +1,97 @@
/* Copyright (c) Citrix Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using XenAdmin.Alerts;
namespace XenAdminTests.UnitTests.AlertTests
{
[TestFixture, Category(TestCategories.Unit)]
public class MultipathAlertTest
{
[Test]
public void TestFindHostUuidsTestGivenNull()
{
FindHostUuidsTestHelper(null, Enumerable.Empty<string>());
}
[Test]
public void TestFindHostUuidsTestGivenMultipleWithoutDuplicates()
{
var given = new List<string>
{
"[20181016T09:55:52Z] host=0d5b647c-d913-40e0-a94f-4b9c94dd41eb; host-name=\"xrtuk-12-05\"; pbd=bf08dedc-2564-df8c-3f61-b07654cf824a; scsi_id=3600a098038303973743f486833396d41; current=2; max=4",
"[20181016T09:55:52Z] host=2e257cd5-b1cb-4f90-908b-e939e8730924; host-name=\"xrtuk-13-06\"; pbd=bf08dedc-2564-df8c-3f61-b07654cf824a; scsi_id=3600a098038303973743f486833396d41; current=2; max=4",
};
var expected = new List<string> { "0d5b647c-d913-40e0-a94f-4b9c94dd41eb", "2e257cd5-b1cb-4f90-908b-e939e8730924" };
FindHostUuidsTestHelper(given, expected);
}
[Test]
public void TestFindHostUuidsTestGivenMultipleWithDuplicates()
{
var given = new List<string>
{
"[20181016T09:55:52Z] host=2e257cd5-b1cb-4f90-908b-e939e8730924; host-name=\"xrtuk-13-06\"; pbd=bf08dedc-2564-df8c-3f61-b07654cf824a; scsi_id=3600a098038303973743f486833396d41; current=2; max=4",
"[20181016T09:55:52Z] host=2e257cd5-b1cb-4f90-908b-e939e8730924; host-name=\"xrtuk-13-06\"; root=true; current=2; max=4",
"[20181016T09:55:52Z] host=0d5b647c-d913-40e0-a94f-4b9c94dd41eb; host-name=\"xrtuk-12-05\"; pbd=bf08dedc-2564-df8c-3f61-b07654cf824a; scsi_id=3600a098038303973743f486833396d41; current=2; max=4"
};
var expected = new List<string> { "2e257cd5-b1cb-4f90-908b-e939e8730924", "0d5b647c-d913-40e0-a94f-4b9c94dd41eb" };
FindHostUuidsTestHelper(given, expected);
}
[Test]
public void TestFindHostUuidsTestGivenUnmatchedLinesShouldNotInterfere()
{
var given = new List<string>
{
"unmatched",
"[20181016T09:55:52Z] host=2e257cd5-b1cb-4f90-908b-e939e8730924; host-name=\"xrtuk-13-06\"; pbd=bf08dedc-2564-df8c-3f61-b07654cf824a; scsi_id=3600a098038303973743f486833396d41; current=2; max=4",
"unmatched",
"[20181016T09:55:52Z] host=2e257cd5-b1cb-4f90-908b-e939e8730924; host-name=\"xrtuk-13-06\"; root=true; current=2; max=4",
"unmatched",
"[20181016T09:55:52Z] host=0d5b647c-d913-40e0-a94f-4b9c94dd41eb; host-name=\"xrtuk-12-05\"; pbd=bf08dedc-2564-df8c-3f61-b07654cf824a; scsi_id=3600a098038303973743f486833396d41; current=2; max=4",
"unmatched"
};
var expected = new List<string> { "2e257cd5-b1cb-4f90-908b-e939e8730924", "0d5b647c-d913-40e0-a94f-4b9c94dd41eb" };
FindHostUuidsTestHelper(given, expected);
}
private static void FindHostUuidsTestHelper(IEnumerable<string> given, IEnumerable<string> expected)
{
var actual = MessageAlert.FindHostUuids(given);
Assert.IsNotNull(actual, "FindHostUuids should not return null.");
Assert.That(actual, Is.EquivalentTo(expected));
}
}
}

View File

@ -73,6 +73,7 @@
<Compile Include="HealthCheckTests\RequestUploadTaskTests.cs" />
<Compile Include="UnitTests\BatchUpdatesTests\BatchUpdatesTests.cs" />
<Compile Include="UnitTests\CPUFeaturesTest.cs" />
<Compile Include="UnitTests\AlertTests\MultipathAlertTest.cs" />
<Compile Include="UnitTests\HealthCheckAnalysisProgressTest.cs" />
<Compile Include="UnitTests\SubnetworkMaskValidatorTest.cs" />
<Compile Include="UnitTests\ExceptionSerializationTest.cs" />