From eb884e7c09be3d86195a997188a014f96ad14e73 Mon Sep 17 00:00:00 2001 From: Ignasi Barrera Date: Thu, 26 Jun 2014 20:47:27 +0200 Subject: [PATCH] JCLOUDS-618: Allow servers without boot device in ElasticStack --- .../functions/ServerInfoToNodeMetadata.java | 22 ++-- .../functions/GetImageIdFromServerTest.java | 104 ++++++++++++++++++ 2 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 apis/elasticstack/src/test/java/org/jclouds/elasticstack/compute/functions/GetImageIdFromServerTest.java diff --git a/apis/elasticstack/src/main/java/org/jclouds/elasticstack/compute/functions/ServerInfoToNodeMetadata.java b/apis/elasticstack/src/main/java/org/jclouds/elasticstack/compute/functions/ServerInfoToNodeMetadata.java index d1510e33e0..7b4f7d4a18 100644 --- a/apis/elasticstack/src/main/java/org/jclouds/elasticstack/compute/functions/ServerInfoToNodeMetadata.java +++ b/apis/elasticstack/src/main/java/org/jclouds/elasticstack/compute/functions/ServerInfoToNodeMetadata.java @@ -158,16 +158,18 @@ public class ServerInfoToNodeMetadata implements Function knownDrives = CacheBuilder.newBuilder().build( + new CacheLoader() { + @Override + public DriveInfo load(String key) throws Exception { + // Use a mock UUID to be able to simulate the unknown drives + return UNKNOWN_DRIVE_UUID.equals(key) ? null : new DriveInfo.Builder().name("foo") + .metrics(new DriveMetrics.Builder().build()).build(); + } + }); + + private GetImageIdFromServer function = new GetImageIdFromServer(knownDrives); + + public void testImageIdExists() { + Map devices = deviceMapFor(UUID.randomUUID().toString()); + Server server = serverFor(devices, devices.keySet()); + assertEquals(function.apply(server), "foo"); + } + + public void testImageIdExistsAndUsesTheFirstDevice() { + Map devices = deviceMapFor(UUID.randomUUID().toString(), UNKNOWN_DRIVE_UUID); + Server server = serverFor(devices, devices.keySet()); + assertEquals(function.apply(server), "foo"); + } + + public void testImageIdIsNullWhenNoBootableDevices() { + Map devices = deviceMapFor(UUID.randomUUID().toString()); + Server server = serverFor(devices, ImmutableSet. of()); + assertNull(function.apply(server)); + } + + public void testImageIdIsNullWhenNoDeviceWithGivenId() { + Map devices = deviceMapFor(UUID.randomUUID().toString()); + Server server = serverFor(ImmutableMap. of(), devices.keySet()); + assertNull(function.apply(server)); + } + + private static Map deviceMapFor(String... uuids) { + ImmutableSet.Builder devices = ImmutableSet.builder(); + for (int i = 0; i < uuids.length; i++) { + devices.add(new SCSIDevice.Builder(i).uuid(uuids[i]).build()); + } + + return Maps.uniqueIndex(devices.build(), new Function() { + @Override + public String apply(Device input) { + return input.getId(); + } + }); + } + + private static Server serverFor(Map devices, Iterable deviceIds) { + return new Server.Builder().name("test").vnc(new VNC(null, null, false)).devices(devices) + .bootDeviceIds(deviceIds).build(); + } +}