|
| 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 static org.junit.Assert.assertTrue; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.times; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +import org.apache.cloudstack.ca.PostCertificateRenewalCommand; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | +import org.libvirt.Connect; |
| 31 | +import org.libvirt.Domain; |
| 32 | +import org.libvirt.LibvirtException; |
| 33 | +import org.mockito.ArgumentCaptor; |
| 34 | +import org.mockito.Mock; |
| 35 | +import org.mockito.Mockito; |
| 36 | +import org.mockito.MockedConstruction; |
| 37 | +import org.mockito.junit.MockitoJUnitRunner; |
| 38 | + |
| 39 | +import com.cloud.agent.api.Answer; |
| 40 | +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; |
| 41 | +import com.cloud.utils.script.Script; |
| 42 | + |
| 43 | +@RunWith(MockitoJUnitRunner.class) |
| 44 | +public class LibvirtPostCertificateRenewalCommandWrapperTest { |
| 45 | + |
| 46 | + private static final long SUPPORTED_QEMU_VERSION = 6000000L; |
| 47 | + |
| 48 | + @Mock |
| 49 | + private LibvirtComputingResource libvirtComputingResource; |
| 50 | + @Mock |
| 51 | + private LibvirtUtilitiesHelper libvirtUtilitiesHelper; |
| 52 | + @Mock |
| 53 | + private Connect connect; |
| 54 | + |
| 55 | + private final LibvirtPostCertificateRenewalCommandWrapper wrapper = new LibvirtPostCertificateRenewalCommandWrapper(); |
| 56 | + |
| 57 | + @Before |
| 58 | + public void setUp() { |
| 59 | + when(libvirtComputingResource.getHypervisorQemuVersion()).thenReturn(SUPPORTED_QEMU_VERSION); |
| 60 | + } |
| 61 | + |
| 62 | + private Answer executeWithScriptMocked() { |
| 63 | + try (MockedConstruction<Script> ignored = Mockito.mockConstruction(Script.class)) { |
| 64 | + return wrapper.execute(new PostCertificateRenewalCommand(), libvirtComputingResource); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testExecuteReloadsVncTlsCertificateForEachRunningVm() throws Exception { |
| 70 | + final Domain vm1 = mock(Domain.class); |
| 71 | + when(vm1.getName()).thenReturn("i-2-3-VM"); |
| 72 | + final Domain vm2 = mock(Domain.class); |
| 73 | + when(vm2.getName()).thenReturn("i-4-5-VM"); |
| 74 | + |
| 75 | + when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); |
| 76 | + when(libvirtUtilitiesHelper.getConnection()).thenReturn(connect); |
| 77 | + when(connect.listDomains()).thenReturn(new int[]{1, 2}); |
| 78 | + when(connect.domainLookupByID(1)).thenReturn(vm1); |
| 79 | + when(connect.domainLookupByID(2)).thenReturn(vm2); |
| 80 | + |
| 81 | + final Answer answer = executeWithScriptMocked(); |
| 82 | + |
| 83 | + assertTrue(answer.getResult()); |
| 84 | + final ArgumentCaptor<String> monitorCommandCaptor = ArgumentCaptor.forClass(String.class); |
| 85 | + verify(vm1, times(1)).qemuMonitorCommand(monitorCommandCaptor.capture(), Mockito.eq(0)); |
| 86 | + verify(vm2, times(1)).qemuMonitorCommand(Mockito.anyString(), Mockito.eq(0)); |
| 87 | + final String capturedCommand = monitorCommandCaptor.getValue(); |
| 88 | + assertTrue(capturedCommand.contains("display-reload")); |
| 89 | + assertTrue(capturedCommand.contains("\"type\":\"vnc\"")); |
| 90 | + assertTrue(capturedCommand.contains("\"tls-certs\":true")); |
| 91 | + verify(vm1, times(1)).free(); |
| 92 | + verify(vm2, times(1)).free(); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testExecuteContinuesWithOtherVmsWhenOneReloadFails() throws Exception { |
| 97 | + final Domain failingVm = mock(Domain.class); |
| 98 | + when(failingVm.getName()).thenReturn("i-2-3-VM"); |
| 99 | + when(failingVm.qemuMonitorCommand(Mockito.anyString(), Mockito.eq(0))).thenThrow(mock(LibvirtException.class)); |
| 100 | + final Domain workingVm = mock(Domain.class); |
| 101 | + when(workingVm.getName()).thenReturn("i-4-5-VM"); |
| 102 | + |
| 103 | + when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); |
| 104 | + when(libvirtUtilitiesHelper.getConnection()).thenReturn(connect); |
| 105 | + when(connect.listDomains()).thenReturn(new int[]{1, 2}); |
| 106 | + when(connect.domainLookupByID(1)).thenReturn(failingVm); |
| 107 | + when(connect.domainLookupByID(2)).thenReturn(workingVm); |
| 108 | + |
| 109 | + final Answer answer = executeWithScriptMocked(); |
| 110 | + |
| 111 | + assertTrue(answer.getResult()); |
| 112 | + verify(workingVm, times(1)).qemuMonitorCommand(Mockito.anyString(), Mockito.eq(0)); |
| 113 | + verify(failingVm, times(1)).free(); |
| 114 | + verify(workingVm, times(1)).free(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testExecuteContinuesWhenNoRunningVms() throws Exception { |
| 119 | + when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); |
| 120 | + when(libvirtUtilitiesHelper.getConnection()).thenReturn(connect); |
| 121 | + when(connect.listDomains()).thenReturn(new int[]{}); |
| 122 | + |
| 123 | + final Answer answer = executeWithScriptMocked(); |
| 124 | + |
| 125 | + assertTrue(answer.getResult()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testExecuteHandlesUnableToListRunningVms() throws Exception { |
| 130 | + when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); |
| 131 | + when(libvirtUtilitiesHelper.getConnection()).thenThrow(mock(LibvirtException.class)); |
| 132 | + |
| 133 | + final Answer answer = executeWithScriptMocked(); |
| 134 | + |
| 135 | + assertTrue(answer.getResult()); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testExecuteSkipsVncCertReloadWhenQemuVersionTooOld() throws Exception { |
| 140 | + when(libvirtComputingResource.getHypervisorQemuVersion()).thenReturn(5002000L); |
| 141 | + |
| 142 | + final Answer answer = executeWithScriptMocked(); |
| 143 | + |
| 144 | + assertTrue(answer.getResult()); |
| 145 | + verifyNoInteractions(libvirtUtilitiesHelper); |
| 146 | + } |
| 147 | +} |
0 commit comments