PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToImageTest.java

https://github.com/alasdairhodge/jclouds
Java | 139 lines | 89 code | 32 blank | 18 comment | 0 complexity | 5776ed71d86d0485c45afadcae5d8e2e MD5 | raw file
  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.virtualbox.functions;
  20. import static org.easymock.EasyMock.eq;
  21. import static org.easymock.EasyMock.expect;
  22. import static org.easymock.classextension.EasyMock.createNiceMock;
  23. import static org.easymock.classextension.EasyMock.replay;
  24. import static org.testng.Assert.assertEquals;
  25. import static org.testng.AssertJUnit.assertTrue;
  26. import java.util.Map;
  27. import org.jclouds.compute.config.BaseComputeServiceContextModule;
  28. import org.jclouds.compute.domain.Image;
  29. import org.jclouds.compute.domain.OsFamily;
  30. import org.jclouds.compute.reference.ComputeServiceConstants;
  31. import org.jclouds.json.Json;
  32. import org.jclouds.json.config.GsonModule;
  33. import org.testng.annotations.Test;
  34. import org.virtualbox_4_1.IGuestOSType;
  35. import org.virtualbox_4_1.IMachine;
  36. import org.virtualbox_4_1.IVirtualBox;
  37. import org.virtualbox_4_1.VirtualBoxManager;
  38. import com.google.inject.Guice;
  39. @Test(groups = "unit")
  40. public class IMachineToImageTest {
  41. Map<OsFamily, Map<String, String>> map = new BaseComputeServiceContextModule() {
  42. }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule())
  43. .getInstance(Json.class));
  44. @Test
  45. public void testConvert() throws Exception {
  46. VirtualBoxManager vbm = createNiceMock(VirtualBoxManager.class);
  47. IVirtualBox vBox = createNiceMock(IVirtualBox.class);
  48. IMachine vm = createNiceMock(IMachine.class);
  49. IGuestOSType guestOsType = createNiceMock(IGuestOSType.class);
  50. String linuxDescription = "Ubuntu 10.04";
  51. expect(vbm.getVBox()).andReturn(vBox).anyTimes();
  52. expect(vm.getOSTypeId()).andReturn("os-type").anyTimes();
  53. expect(vBox.getGuestOSType(eq("os-type"))).andReturn(guestOsType);
  54. expect(vm.getDescription()).andReturn("my-ubuntu-machine").anyTimes();
  55. expect(guestOsType.getDescription()).andReturn(linuxDescription).anyTimes();
  56. expect(guestOsType.getIs64Bit()).andReturn(true);
  57. replay(vbm, vBox, vm, guestOsType);
  58. IMachineToImage fn = new IMachineToImage(vbm, map);
  59. Image image = fn.apply(vm);
  60. assertEquals(image.getDescription(), "my-ubuntu-machine");
  61. assertEquals(image.getOperatingSystem().getDescription(), linuxDescription);
  62. assertTrue(image.getOperatingSystem().is64Bit());
  63. assertEquals(image.getOperatingSystem().getFamily(), OsFamily.UBUNTU);
  64. assertEquals(image.getOperatingSystem().getVersion(), "10.04");
  65. }
  66. @Test
  67. public void testConvert1() throws Exception {
  68. VirtualBoxManager vbm = createNiceMock(VirtualBoxManager.class);
  69. IVirtualBox vBox = createNiceMock(IVirtualBox.class);
  70. IMachine vm = createNiceMock(IMachine.class);
  71. IGuestOSType guestOsType = createNiceMock(IGuestOSType.class);
  72. String guestOsDescription = "ubuntu 11.04 server (i386)";
  73. String vmDescription = "ubuntu-11.04-server-i386";
  74. expect(vbm.getVBox()).andReturn(vBox).anyTimes();
  75. expect(vm.getOSTypeId()).andReturn("os-type").anyTimes();
  76. expect(vBox.getGuestOSType(eq("os-type"))).andReturn(guestOsType);
  77. expect(vm.getDescription()).andReturn(vmDescription).anyTimes();
  78. expect(guestOsType.getDescription()).andReturn(guestOsDescription).anyTimes();
  79. expect(guestOsType.getIs64Bit()).andReturn(true);
  80. replay(vbm, vBox, vm, guestOsType);
  81. IMachineToImage fn = new IMachineToImage(vbm, map);
  82. Image image = fn.apply(vm);
  83. assertEquals(image.getDescription(), vmDescription);
  84. assertEquals(image.getOperatingSystem().getDescription(), guestOsDescription);
  85. assertTrue(image.getOperatingSystem().is64Bit());
  86. assertEquals(image.getOperatingSystem().getFamily(), OsFamily.UBUNTU);
  87. assertEquals(image.getOperatingSystem().getVersion(), "11.04");
  88. }
  89. @Test
  90. public void testUnparseableOsString() throws Exception {
  91. VirtualBoxManager vbm = createNiceMock(VirtualBoxManager.class);
  92. IVirtualBox vBox = createNiceMock(IVirtualBox.class);
  93. IMachine vm = createNiceMock(IMachine.class);
  94. IGuestOSType guestOsType = createNiceMock(IGuestOSType.class);
  95. expect(vbm.getVBox()).andReturn(vBox).anyTimes();
  96. String unknownOsDescription = "SomeOtherOs 2.04";
  97. expect(vm.getOSTypeId()).andReturn("os-type").anyTimes();
  98. expect(vm.getDescription()).andReturn("my-unknown-machine").anyTimes();
  99. expect(guestOsType.getDescription()).andReturn(unknownOsDescription).anyTimes();
  100. expect(guestOsType.getIs64Bit()).andReturn(true);
  101. expect(vBox.getGuestOSType(eq("os-type"))).andReturn(guestOsType);
  102. replay(vbm, vBox, vm, guestOsType);
  103. Image image = new IMachineToImage(vbm, map).apply(vm);
  104. assertEquals(image.getOperatingSystem().getDescription(), "SomeOtherOs 2.04");
  105. assertEquals(image.getOperatingSystem().getVersion(), "");
  106. }
  107. }