|
| 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.agent.resource.virtualnetwork; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertFalse; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.joda.time.Duration; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | +import org.mockito.ArgumentCaptor; |
| 31 | +import org.mockito.Mock; |
| 32 | +import org.mockito.Mockito; |
| 33 | +import org.mockito.junit.MockitoJUnitRunner; |
| 34 | + |
| 35 | +import com.cloud.agent.api.Answer; |
| 36 | +import com.cloud.agent.api.routing.AggregationControlCommand; |
| 37 | +import com.cloud.agent.api.routing.AggregationControlCommand.Action; |
| 38 | +import com.cloud.agent.api.routing.NetworkElementCommand; |
| 39 | +import com.cloud.agent.api.routing.SetPortForwardingRulesCommand; |
| 40 | +import com.cloud.agent.api.to.PortForwardingRuleTO; |
| 41 | +import com.cloud.utils.ExecutionResult; |
| 42 | + |
| 43 | +@RunWith(MockitoJUnitRunner.class) |
| 44 | +public class VirtualRoutingResourceAggregationTest { |
| 45 | + |
| 46 | + private static final String ROUTER_IP = "169.254.3.4"; |
| 47 | + private static final String ROUTER_GUEST_IP = "10.200.1.1"; |
| 48 | + private static final String ROUTER_NAME = "r-4-VM"; |
| 49 | + |
| 50 | + @Mock |
| 51 | + private VirtualRouterDeployer deployer; |
| 52 | + |
| 53 | + private VirtualRoutingResource resource; |
| 54 | + |
| 55 | + @Before |
| 56 | + public void setup() throws Exception { |
| 57 | + Mockito.when(deployer.prepareCommand(Mockito.any())).thenAnswer(invocation -> { |
| 58 | + NetworkElementCommand cmd = invocation.getArgument(0); |
| 59 | + cmd.setRouterAccessIp(ROUTER_IP); |
| 60 | + return new ExecutionResult(true, null); |
| 61 | + }); |
| 62 | + Mockito.lenient().when(deployer.cleanupCommand(Mockito.any())).thenReturn(new ExecutionResult(true, null)); |
| 63 | + Mockito.lenient().when(deployer.createFileInVR(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())) |
| 64 | + .thenReturn(new ExecutionResult(true, null)); |
| 65 | + Mockito.lenient().when(deployer.executeInVR(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(Duration.class))) |
| 66 | + .thenReturn(new ExecutionResult(true, null)); |
| 67 | + resource = new VirtualRoutingResource(deployer); |
| 68 | + resource.configure("VRResource", new HashMap<>()); |
| 69 | + } |
| 70 | + |
| 71 | + private Answer execute(NetworkElementCommand cmd) { |
| 72 | + return resource.executeRequest(cmd); |
| 73 | + } |
| 74 | + |
| 75 | + private AggregationControlCommand control(Action action) { |
| 76 | + return new AggregationControlCommand(action, ROUTER_NAME, ROUTER_IP, ROUTER_GUEST_IP); |
| 77 | + } |
| 78 | + |
| 79 | + private SetPortForwardingRulesCommand portForwarding(String publicIp) { |
| 80 | + SetPortForwardingRulesCommand cmd = new SetPortForwardingRulesCommand(List.of( |
| 81 | + new PortForwardingRuleTO(1, publicIp, 22, 22, "10.10.1.10", 22, 22, "TCP", false, false))); |
| 82 | + cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, ROUTER_NAME); |
| 83 | + return cmd; |
| 84 | + } |
| 85 | + |
| 86 | + private List<String> appliedConfigFiles() { |
| 87 | + ArgumentCaptor<String> content = ArgumentCaptor.forClass(String.class); |
| 88 | + Mockito.verify(deployer, Mockito.atLeast(0)).createFileInVR(Mockito.eq(ROUTER_IP), Mockito.anyString(), Mockito.anyString(), content.capture()); |
| 89 | + return content.getAllValues(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void overlappingAggregationsKeepCommandsOfBothJobs() { |
| 94 | + assertTrue(execute(control(Action.Start)).getResult()); |
| 95 | + assertTrue(execute(portForwarding("64.1.1.10")).getResult()); |
| 96 | + assertTrue(execute(control(Action.Start)).getResult()); |
| 97 | + assertTrue(execute(portForwarding("64.1.1.20")).getResult()); |
| 98 | + |
| 99 | + assertTrue(execute(control(Action.Finish)).getResult()); |
| 100 | + List<String> files = appliedConfigFiles(); |
| 101 | + assertEquals(1, files.size()); |
| 102 | + assertTrue(files.get(0).contains("64.1.1.10")); |
| 103 | + assertTrue(files.get(0).contains("64.1.1.20")); |
| 104 | + |
| 105 | + Answer second = execute(control(Action.Finish)); |
| 106 | + assertTrue(second.getResult()); |
| 107 | + assertEquals(1, appliedConfigFiles().size()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void finishOfJoinedAggregationReportsFailureOfTheApply() { |
| 112 | + Mockito.when(deployer.executeInVR(Mockito.anyString(), Mockito.eq(VRScripts.VR_CFG), Mockito.anyString(), Mockito.any(Duration.class))) |
| 113 | + .thenReturn(new ExecutionResult(false, "vr_cfg.sh failed")); |
| 114 | + |
| 115 | + execute(control(Action.Start)); |
| 116 | + execute(portForwarding("64.1.1.10")); |
| 117 | + execute(control(Action.Start)); |
| 118 | + execute(portForwarding("64.1.1.20")); |
| 119 | + |
| 120 | + Answer first = execute(control(Action.Finish)); |
| 121 | + assertFalse(first.getResult()); |
| 122 | + Answer second = execute(control(Action.Finish)); |
| 123 | + assertFalse(second.getResult()); |
| 124 | + assertEquals("vr_cfg.sh failed", second.getDetails()); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void laterSuccessfulApplyClearsRecordedFailure() { |
| 129 | + Mockito.when(deployer.executeInVR(Mockito.anyString(), Mockito.eq(VRScripts.VR_CFG), Mockito.anyString(), Mockito.any(Duration.class))) |
| 130 | + .thenReturn(new ExecutionResult(false, "vr_cfg.sh failed"), new ExecutionResult(true, null)); |
| 131 | + |
| 132 | + execute(control(Action.Start)); |
| 133 | + assertFalse(execute(control(Action.Finish)).getResult()); |
| 134 | + execute(control(Action.Start)); |
| 135 | + assertTrue(execute(control(Action.Finish)).getResult()); |
| 136 | + |
| 137 | + assertTrue(execute(control(Action.Finish)).getResult()); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void finishWithoutStartDoesNotFail() { |
| 142 | + Answer answer = execute(control(Action.Finish)); |
| 143 | + assertTrue(answer.getResult()); |
| 144 | + assertTrue(appliedConfigFiles().isEmpty()); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void commandsAfterFinishAreAppliedDirectly() { |
| 149 | + execute(control(Action.Start)); |
| 150 | + execute(control(Action.Finish)); |
| 151 | + |
| 152 | + assertTrue(execute(portForwarding("64.1.1.30")).getResult()); |
| 153 | + Mockito.verify(deployer, Mockito.atLeastOnce()).executeInVR(Mockito.eq(ROUTER_IP), Mockito.eq(VRScripts.UPDATE_CONFIG), Mockito.anyString(), Mockito.any(Duration.class)); |
| 154 | + } |
| 155 | +} |
0 commit comments