mirror of
https://github.com/xcp-ng/xenadmin.git
synced 2025-01-20 15:29:26 +01:00
Extended product version checker to cope with RPM versions. Use xapi_build instead of xapi for version comparisons.
Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
This commit is contained in:
parent
df35684586
commit
cc4d3d9ee7
64
XenAdminTests/UnitTests/RpmVersionTests.cs
Normal file
64
XenAdminTests/UnitTests/RpmVersionTests.cs
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
/* 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 NUnit.Framework;
|
||||||
|
using XenAdmin.Core;
|
||||||
|
|
||||||
|
namespace XenAdminTests.UnitTests
|
||||||
|
{
|
||||||
|
[TestFixture, Category(TestCategories.Unit)]
|
||||||
|
class RpmVersionTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
[TestCase("0.1", "0.2", ExpectedResult = -1)]
|
||||||
|
[TestCase("0.1.2", "0.1.3", ExpectedResult = -1)]
|
||||||
|
[TestCase("0.1.2", "0.2", ExpectedResult = -1)]
|
||||||
|
[TestCase("0.1", "0.1.0", ExpectedResult = 0)]
|
||||||
|
[TestCase("0.1", "0.1.0.0.0", ExpectedResult = 0)]
|
||||||
|
[TestCase("7.1", "7.1.50", ExpectedResult = -1)]
|
||||||
|
[TestCase("7.1.6", "7.1.50", ExpectedResult = -1)]
|
||||||
|
[TestCase("22.19.0", "22.19.0-1.xs8", ExpectedResult = -1)]
|
||||||
|
[TestCase("22.19.0-1.xs8", "22.19.0-1.xs9", ExpectedResult = 0)]
|
||||||
|
[TestCase("22.19.0-1.xs8", "22.19.0-2.xs8", ExpectedResult = -1)]
|
||||||
|
[TestCase("22.19.0-1.xs8", "22.19.0-1.2.xs8", ExpectedResult = -1)]
|
||||||
|
[TestCase("22.19.0-1.xs8", "22.19.0-1.0.g1234abc.xs8", ExpectedResult = 0)]
|
||||||
|
[TestCase("22.19.0-1.0.xs8", "22.19.0-1.0.g1234abc.xs8", ExpectedResult = 0)]
|
||||||
|
[TestCase("22.19.0-1.1.g2234abc.xs8", "22.19.0-1.1.g1234abc.xs8", ExpectedResult = 0)]
|
||||||
|
[TestCase("1:22.19.0-1.xs8", "22.19.0-1.xs8", ExpectedResult = 1)]
|
||||||
|
[TestCase("1:22.19.0-1.xs8", "2:22.19.0-1.xs8", ExpectedResult = -1)]
|
||||||
|
[TestCase(null, "22.19.0-1.xs8", ExpectedResult = -1)]
|
||||||
|
[TestCase("22.19.0-1.xs8", null, ExpectedResult = 1)]
|
||||||
|
public int TestVersionComparer(string version1, string version2)
|
||||||
|
{
|
||||||
|
return Helpers.ProductVersionCompare(version1, version2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -112,6 +112,7 @@
|
|||||||
<Compile Include="UnitTests\HealthCheckAnalysisProgressTest.cs" />
|
<Compile Include="UnitTests\HealthCheckAnalysisProgressTest.cs" />
|
||||||
<Compile Include="UnitTests\NamesAndMessagesTests.cs" />
|
<Compile Include="UnitTests\NamesAndMessagesTests.cs" />
|
||||||
<Compile Include="UnitTests\NaturalCompareTest.cs" />
|
<Compile Include="UnitTests\NaturalCompareTest.cs" />
|
||||||
|
<Compile Include="UnitTests\RpmVersionTests.cs" />
|
||||||
<Compile Include="UnitTests\StreamUtilitiesTests.cs" />
|
<Compile Include="UnitTests\StreamUtilitiesTests.cs" />
|
||||||
<Compile Include="UnitTests\SubnetworkMaskValidatorTest.cs" />
|
<Compile Include="UnitTests\SubnetworkMaskValidatorTest.cs" />
|
||||||
<Compile Include="UnitTests\TimeUtilTests.cs" />
|
<Compile Include="UnitTests\TimeUtilTests.cs" />
|
||||||
|
@ -29,89 +29,144 @@
|
|||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using XenAdmin.Network;
|
using XenAdmin.Network;
|
||||||
using XenAPI;
|
using XenAPI;
|
||||||
|
using XenCenterLib;
|
||||||
|
|
||||||
|
|
||||||
namespace XenAdmin.Core
|
namespace XenAdmin.Core
|
||||||
{
|
{
|
||||||
public static partial class Helpers
|
public static partial class Helpers
|
||||||
{
|
{
|
||||||
/// <summary>
|
private static readonly Regex RpmVersionRegex = new Regex(@"^([0-9]+:)?([0-9a-zA-Z\.]+)(-[0-9a-zA-Z\.]+)?$");
|
||||||
/// Only log the unrecognised version message once (CA-11201).
|
|
||||||
/// </summary>
|
public class RpmVersion
|
||||||
private static bool _unrecognisedVersionWarned = false;
|
{
|
||||||
/// <summary>
|
public int Epoch { get; private set; }
|
||||||
/// Numbers should have three parts, i.e. be in the form a.b.c, otherwise they won't be parsed.
|
public List<int> Version { get; } = new List<int>();
|
||||||
/// </summary>
|
public List<int> Release { get; } = new List<int>();
|
||||||
/// <param name="version1">May be null.</param>
|
|
||||||
/// <param name="version2">May be null.</param>
|
/// <summary>
|
||||||
/// <returns></returns>
|
/// The xapi RPMs have versions like 1:22.13.0.1.g2226c2e3a-1.1.g4e82970.xs8.
|
||||||
|
/// The parts g* referring to git commits and the last part xs8 referring to
|
||||||
|
/// the distro are ignored by the parser.
|
||||||
|
/// </summary>
|
||||||
|
public static RpmVersion Parse(string versionString)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(versionString))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var match = RpmVersionRegex.Match(versionString);
|
||||||
|
if (!match.Success || match.Groups.Count < 4)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var rpmVersion = new RpmVersion();
|
||||||
|
|
||||||
|
var epoch = match.Groups[1].Value.TrimEnd(':');
|
||||||
|
if (int.TryParse(epoch, out var epochRes))
|
||||||
|
rpmVersion.Epoch = epochRes;
|
||||||
|
|
||||||
|
var version = match.Groups[2].Value.Split('.');
|
||||||
|
foreach (var v in version)
|
||||||
|
{
|
||||||
|
if (int.TryParse(v, out var result))
|
||||||
|
rpmVersion.Version.Add(result);
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var release = match.Groups[3].Value.TrimStart('-').Split('.');
|
||||||
|
foreach (var v in release)
|
||||||
|
{
|
||||||
|
if (int.TryParse(v, out var result))
|
||||||
|
rpmVersion.Release.Add(result);
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rpmVersion;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <remarks>Unlike the .NET Framework's Version.CompareTo() method, this method
|
||||||
|
/// considers 1.2.0 and 1.2 as equal.</remarks>
|
||||||
public static int ProductVersionCompare(string version1, string version2)
|
public static int ProductVersionCompare(string version1, string version2)
|
||||||
{
|
{
|
||||||
// Assume version numbers are of form 'a.b.c'
|
var v1 = RpmVersion.Parse(version1);
|
||||||
int a1 = 99, b1 = 99, c1 = 99, a2 = 99, b2 = 99, c2 = 99;
|
var v2 = RpmVersion.Parse(version2);
|
||||||
|
|
||||||
|
if (v1 == null && v2 == null)
|
||||||
string[] tokens = null;
|
return 0;
|
||||||
if (version1 != null)
|
if (v1 == null)
|
||||||
tokens = version1.Split('.');
|
|
||||||
if (tokens != null && tokens.Length == 3)
|
|
||||||
{
|
|
||||||
a1 = int.Parse(tokens[0]);
|
|
||||||
b1 = int.Parse(tokens[1]);
|
|
||||||
c1 = int.Parse(tokens[2]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!_unrecognisedVersionWarned)
|
|
||||||
{
|
|
||||||
log.DebugFormat("Unrecognised version format {0} - treating as developer version", version1);
|
|
||||||
_unrecognisedVersionWarned = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tokens = null;
|
|
||||||
if (version2 != null)
|
|
||||||
tokens = version2.Split('.');
|
|
||||||
if (tokens != null && tokens.Length == 3)
|
|
||||||
{
|
|
||||||
a2 = int.Parse(tokens[0]);
|
|
||||||
b2 = int.Parse(tokens[1]);
|
|
||||||
c2 = int.Parse(tokens[2]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!_unrecognisedVersionWarned)
|
|
||||||
{
|
|
||||||
log.DebugFormat("Unrecognised version format {0} - treating as developer version", version2);
|
|
||||||
_unrecognisedVersionWarned = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (a2 > a1)
|
|
||||||
{
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
if (v2 == null)
|
||||||
else if (a2 == a1)
|
return 1;
|
||||||
|
|
||||||
|
int result = v1.Epoch.CompareTo(v2.Epoch);
|
||||||
|
if (result != 0)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
var min = Math.Min(v1.Version.Count, v2.Version.Count);
|
||||||
|
|
||||||
|
while (i < min)
|
||||||
{
|
{
|
||||||
if (b2 > b1)
|
result = v1.Version[i].CompareTo(v2.Version[i]);
|
||||||
{
|
if (result != 0)
|
||||||
return -1;
|
return result;
|
||||||
}
|
i++;
|
||||||
else if (b2 == b1)
|
|
||||||
{
|
|
||||||
if (c2 > c1)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
else if (c1 == c2)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 1;
|
|
||||||
|
while (i < v1.Version.Count)
|
||||||
|
{
|
||||||
|
result = v1.Version[i].CompareTo(0);
|
||||||
|
if (result != 0)
|
||||||
|
return result;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i < v2.Version.Count)
|
||||||
|
{
|
||||||
|
result = 0.CompareTo(v2.Version[i]);
|
||||||
|
if (result != 0)
|
||||||
|
return result;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
min = Math.Min(v1.Release.Count, v2.Release.Count);
|
||||||
|
|
||||||
|
while (i < min)
|
||||||
|
{
|
||||||
|
result = v1.Release[i].CompareTo(v2.Release[i]);
|
||||||
|
if (result != 0)
|
||||||
|
return result;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i < v1.Release.Count)
|
||||||
|
{
|
||||||
|
result = v1.Release[i].CompareTo(0);
|
||||||
|
if (result != 0)
|
||||||
|
return result;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i < v2.Release.Count)
|
||||||
|
{
|
||||||
|
result = 0.CompareTo(v2.Release[i]);
|
||||||
|
if (result != 0)
|
||||||
|
return result;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#region Versions
|
#region Versions
|
||||||
|
|
||||||
/// <param name="conn">May be null, in which case true is returned.</param>
|
/// <param name="conn">May be null, in which case true is returned.</param>
|
||||||
|
@ -644,7 +644,7 @@ namespace XenAPI
|
|||||||
|
|
||||||
public string GetXapiVersion()
|
public string GetXapiVersion()
|
||||||
{
|
{
|
||||||
return Get(software_version, "xapi");
|
return Get(software_version, "xapi_build") ?? Get(software_version, "xapi");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user