Repository: jclouds Updated Branches: refs/heads/master 4d5f57a30 -> eb884e7c0
JCLOUDS-618: Allow servers without boot device in ElasticStack Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/eb884e7c Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/eb884e7c Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/eb884e7c Branch: refs/heads/master Commit: eb884e7c09be3d86195a997188a014f96ad14e73 Parents: 4d5f57a Author: Ignasi Barrera <[email protected]> Authored: Thu Jun 26 20:47:27 2014 +0200 Committer: Ignasi Barrera <[email protected]> Committed: Fri Jun 27 19:19:42 2014 +0200 ---------------------------------------------------------------------- .../functions/ServerInfoToNodeMetadata.java | 22 ++-- .../functions/GetImageIdFromServerTest.java | 104 +++++++++++++++++++ 2 files changed, 116 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds/blob/eb884e7c/apis/elasticstack/src/main/java/org/jclouds/elasticstack/compute/functions/ServerInfoToNodeMetadata.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 d1510e3..7b4f7d4 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<ServerInfo, NodeMetada @Override public String apply(Server from) { String imageId = null; - String bootDeviceId = Iterables.get(from.getBootDeviceIds(), 0); - Device bootDevice = from.getDevices().get(bootDeviceId); - if (bootDevice != null) { - try { - DriveInfo drive = cache.getUnchecked(bootDevice.getDriveUuid()); - imageId = drive.getName(); - } catch (NullPointerException e) { - logger.debug("drive %s not found", bootDevice.getDriveUuid()); - } catch (UncheckedExecutionException e) { - logger.warn(e, "error finding drive %s: %s", bootDevice.getDriveUuid(), e.getMessage()); + if (!from.getBootDeviceIds().isEmpty()) { + String bootDeviceId = Iterables.get(from.getBootDeviceIds(), 0); + Device bootDevice = from.getDevices().get(bootDeviceId); + if (bootDevice != null) { + try { + DriveInfo drive = cache.getUnchecked(bootDevice.getDriveUuid()); + imageId = drive.getName(); + } catch (NullPointerException e) { + logger.debug("drive %s not found", bootDevice.getDriveUuid()); + } catch (UncheckedExecutionException e) { + logger.warn(e, "error finding drive %s: %s", bootDevice.getDriveUuid(), e.getMessage()); + } } } return imageId; http://git-wip-us.apache.org/repos/asf/jclouds/blob/eb884e7c/apis/elasticstack/src/test/java/org/jclouds/elasticstack/compute/functions/GetImageIdFromServerTest.java ---------------------------------------------------------------------- diff --git a/apis/elasticstack/src/test/java/org/jclouds/elasticstack/compute/functions/GetImageIdFromServerTest.java b/apis/elasticstack/src/test/java/org/jclouds/elasticstack/compute/functions/GetImageIdFromServerTest.java new file mode 100644 index 0000000..fd34c2b --- /dev/null +++ b/apis/elasticstack/src/test/java/org/jclouds/elasticstack/compute/functions/GetImageIdFromServerTest.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.elasticstack.compute.functions; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; + +import java.util.Map; +import java.util.UUID; + +import org.jclouds.elasticstack.compute.functions.ServerInfoToNodeMetadata.GetImageIdFromServer; +import org.jclouds.elasticstack.domain.Device; +import org.jclouds.elasticstack.domain.DriveInfo; +import org.jclouds.elasticstack.domain.DriveMetrics; +import org.jclouds.elasticstack.domain.SCSIDevice; +import org.jclouds.elasticstack.domain.Server; +import org.jclouds.elasticstack.domain.VNC; +import org.testng.annotations.Test; + +import com.google.common.base.Function; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; + +/** + * Unit tests for the {@link GetImageIdFromServer} class. + */ +@Test(groups = "unit", testName = "GetImageIdFromServerTest") +public class GetImageIdFromServerTest { + + private static final String UNKNOWN_DRIVE_UUID = UUID.randomUUID().toString(); + + private LoadingCache<String, DriveInfo> knownDrives = CacheBuilder.newBuilder().build( + new CacheLoader<String, DriveInfo>() { + @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<String, Device> devices = deviceMapFor(UUID.randomUUID().toString()); + Server server = serverFor(devices, devices.keySet()); + assertEquals(function.apply(server), "foo"); + } + + public void testImageIdExistsAndUsesTheFirstDevice() { + Map<String, Device> devices = deviceMapFor(UUID.randomUUID().toString(), UNKNOWN_DRIVE_UUID); + Server server = serverFor(devices, devices.keySet()); + assertEquals(function.apply(server), "foo"); + } + + public void testImageIdIsNullWhenNoBootableDevices() { + Map<String, Device> devices = deviceMapFor(UUID.randomUUID().toString()); + Server server = serverFor(devices, ImmutableSet.<String> of()); + assertNull(function.apply(server)); + } + + public void testImageIdIsNullWhenNoDeviceWithGivenId() { + Map<String, Device> devices = deviceMapFor(UUID.randomUUID().toString()); + Server server = serverFor(ImmutableMap.<String, Device> of(), devices.keySet()); + assertNull(function.apply(server)); + } + + private static Map<String, Device> deviceMapFor(String... uuids) { + ImmutableSet.Builder<Device> 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<Device, String>() { + @Override + public String apply(Device input) { + return input.getId(); + } + }); + } + + private static Server serverFor(Map<String, Device> devices, Iterable<String> deviceIds) { + return new Server.Builder().name("test").vnc(new VNC(null, null, false)).devices(devices) + .bootDeviceIds(deviceIds).build(); + } +}
