2023-01-24 15:29:31 +01:00
|
|
|
|
/* Copyright (c) Cloud Software Group, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
*
|
|
|
|
|
* 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;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using XenAdmin.Core;
|
2019-06-03 19:04:52 +02:00
|
|
|
|
using XenAdmin.Model;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAPI;
|
2019-06-03 19:04:52 +02:00
|
|
|
|
using CacheEntry = System.Collections.Generic.KeyValuePair<XenAdmin.Core.ObjectChange, System.Func<XenAdmin.Network.ICache, bool>>;
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
namespace XenAdminTests.XenModelTests
|
|
|
|
|
{
|
2019-06-03 19:04:52 +02:00
|
|
|
|
[TestFixture, Category(TestCategories.Unit)]
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public class CacheTests
|
|
|
|
|
{
|
2019-06-03 19:04:52 +02:00
|
|
|
|
private static IEnumerable<CacheEntry> ObjectChanges
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-06-03 19:04:52 +02:00
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(VM), "1", new VM()), cache => cache.VMs.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(Folder), "1", new Folder(null, "folder")), cache => cache.Folders.Length == 0);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(Host), "1", new Host()), cache => cache.Hosts.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(Pool), "1", new Pool()), cache => cache.Pools.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(VMSS), "1", new VMSS()), cache => cache.VMSSs.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(Network), "1", new Network()), cache => cache.Networks.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(VBD), "1", new VBD()), cache => cache.VBDs.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(Bond), "1", new Bond()), cache => cache.Bonds.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(VDI), "1", new VDI()), cache => cache.VDIs.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(PBD), "1", new PBD()), cache => cache.PBDs.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(Tunnel), "1", new Tunnel()), cache => cache.Tunnels.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(VIF), "1", new VIF()), cache => cache.VIFs.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(SM), "1", new SM()), cache => cache.SMs.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(Pool_patch), "1", new Pool_patch()), cache => cache.Pool_patches.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(SR), "1", new SR()), cache => cache.SRs.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(Message), "1", new Message()), cache => cache.Messages.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(Host_cpu), "1", new Host_cpu()), cache => cache.Host_cpus.Length == 1);
|
|
|
|
|
yield return new CacheEntry(new ObjectChange(typeof(Role), "1", new Role()), cache => cache.Roles.Length == 1);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-03 19:04:52 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
[Test]
|
2019-06-03 19:04:52 +02:00
|
|
|
|
public void TestsCacheCollectionsAfterUpdateFrom([ValueSource(nameof(ObjectChanges))] CacheEntry test)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
var changes = test.Key;
|
|
|
|
|
Cache cache = new Cache();
|
|
|
|
|
if (changes.type == typeof(Folder))
|
2019-06-03 19:04:52 +02:00
|
|
|
|
Assert.Throws<ArgumentException>(() => cache.UpdateFrom(new XenConnection(), new List<ObjectChange> { changes }));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
else
|
2019-06-03 19:04:52 +02:00
|
|
|
|
cache.UpdateFrom(new XenConnection(), new List<ObjectChange> { changes });
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-06-03 19:04:52 +02:00
|
|
|
|
Assert.IsTrue(test.Value(cache));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void FolderTest()
|
|
|
|
|
{
|
|
|
|
|
Cache cache = new Cache();
|
|
|
|
|
Folder folder = new Folder(null, "folder") {opaque_ref = "1"};
|
2019-06-03 19:04:52 +02:00
|
|
|
|
cache.AddFolder(new XenRef<Folder>(folder.opaque_ref), folder);
|
|
|
|
|
Assert.IsTrue(cache.Folders.Length == 1);
|
|
|
|
|
Assert.AreEqual(folder, cache.Resolve(new XenRef<Folder>(folder.opaque_ref)));
|
|
|
|
|
Assert.AreEqual(folder, cache.Folders[0]);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
//Check tryresolve
|
2019-06-03 19:04:52 +02:00
|
|
|
|
cache.TryResolve(new XenRef<Folder>(folder.opaque_ref), out var result);
|
|
|
|
|
Assert.AreEqual(folder, result);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
//Check Clear
|
|
|
|
|
cache.Clear();
|
2019-06-03 19:04:52 +02:00
|
|
|
|
Assert.IsTrue(cache.Folders.Length == 0);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|