|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.hypervisor.kvm.resource.wrapper; |
| 18 | + |
| 19 | +import com.cloud.hypervisor.kvm.resource.LibvirtVMDef; |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.mockito.Mockito; |
| 24 | +import org.mockito.Spy; |
| 25 | +import org.mockito.junit.MockitoJUnitRunner; |
| 26 | + |
| 27 | +import java.util.UUID; |
| 28 | + |
| 29 | +@RunWith(MockitoJUnitRunner.class) |
| 30 | +public class LibvirtGetUnmanagedInstancesCommandWrapperTest { |
| 31 | + |
| 32 | + @Spy |
| 33 | + private LibvirtGetUnmanagedInstancesCommandWrapper wrapper = new LibvirtGetUnmanagedInstancesCommandWrapper(); |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testGetDiskRelativePathNullDisk() { |
| 37 | + Assert.assertNull(wrapper.getDiskRelativePath(null)); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testGetDiskRelativePathBlockType() { |
| 42 | + LibvirtVMDef.DiskDef diskDef = Mockito.mock(LibvirtVMDef.DiskDef.class); |
| 43 | + Mockito.when(diskDef.getDiskType()).thenReturn(LibvirtVMDef.DiskDef.DiskType.BLOCK); |
| 44 | + Assert.assertNull(wrapper.getDiskRelativePath(diskDef)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testGetDiskRelativePathNullPath() { |
| 49 | + LibvirtVMDef.DiskDef diskDef = Mockito.mock(LibvirtVMDef.DiskDef.class); |
| 50 | + Mockito.when(diskDef.getDiskType()).thenReturn(LibvirtVMDef.DiskDef.DiskType.FILE); |
| 51 | + Mockito.when(diskDef.getSourcePath()).thenReturn(null); |
| 52 | + Assert.assertNull(wrapper.getDiskRelativePath(diskDef)); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testGetDiskRelativePathWithoutSlashes() { |
| 57 | + LibvirtVMDef.DiskDef diskDef = Mockito.mock(LibvirtVMDef.DiskDef.class); |
| 58 | + Mockito.when(diskDef.getDiskType()).thenReturn(LibvirtVMDef.DiskDef.DiskType.FILE); |
| 59 | + String imagePath = UUID.randomUUID().toString(); |
| 60 | + Mockito.when(diskDef.getSourcePath()).thenReturn(imagePath); |
| 61 | + Assert.assertEquals(imagePath, wrapper.getDiskRelativePath(diskDef)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testGetDiskRelativePathFullPath() { |
| 66 | + LibvirtVMDef.DiskDef diskDef = Mockito.mock(LibvirtVMDef.DiskDef.class); |
| 67 | + Mockito.when(diskDef.getDiskType()).thenReturn(LibvirtVMDef.DiskDef.DiskType.FILE); |
| 68 | + String relativePath = "ea4b2296-d349-4968-ab72-c8eb523b556e"; |
| 69 | + String imagePath = String.format("/mnt/97e4c9ed-e3bc-3e26-b103-7967fc9feae1/%s", relativePath); |
| 70 | + Mockito.when(diskDef.getSourcePath()).thenReturn(imagePath); |
| 71 | + Assert.assertEquals(relativePath, wrapper.getDiskRelativePath(diskDef)); |
| 72 | + } |
| 73 | +} |
0 commit comments