From 18470a48d0db69e4bd97d22c023f7a5ae3bcbd97 Mon Sep 17 00:00:00 2001 From: wilderrodrigues Date: Thu, 26 Mar 2015 11:10:35 +0100 Subject: [PATCH] Refactoring PingTestCommand to cope with new design - Basic tests added --- .../resource/CitrixResourceBase.java | 6 +-- .../wrapper/CitrixPingTestCommandWrapper.java | 47 +++++++++++++++++++ .../wrapper/CitrixRequestWrapper.java | 2 + .../wrapper/CitrixRequestWrapperTest.java | 14 ++++++ 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixPingTestCommandWrapper.java diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java index 8fa9e4c2bb0..50859afa66e 100644 --- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java +++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java @@ -453,8 +453,6 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (cmd instanceof NetworkElementCommand) { return _vrResource.executeRequest((NetworkElementCommand)cmd); - } else if (clazz == PingTestCommand.class) { - return execute((PingTestCommand)cmd); } else if (clazz == CheckOnHostCommand.class) { return execute((CheckOnHostCommand)cmd); } else if (clazz == ModifySshKeysCommand.class) { @@ -1945,7 +1943,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new Answer(cmd); } - private boolean doPingTest(final Connection conn, final String computingHostIp) { + public boolean doPingTest(final Connection conn, final String computingHostIp) { final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_host.getIp(), 22); try { sshConnection.connect(null, 60000, 60000); @@ -1970,7 +1968,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new CheckOnHostAnswer(cmd, null, "Not Implmeneted"); } - private boolean doPingTest(final Connection conn, final String domRIp, final String vmIp) { + public boolean doPingTest(final Connection conn, final String domRIp, final String vmIp) { final String args = "-i " + domRIp + " -p " + vmIp; final String result = callHostPlugin(conn, "vmops", "pingtest", "args", args); if (result == null || result.isEmpty()) { diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixPingTestCommandWrapper.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixPingTestCommandWrapper.java new file mode 100644 index 00000000000..8a7a01cf7b0 --- /dev/null +++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixPingTestCommandWrapper.java @@ -0,0 +1,47 @@ +// +// 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 com.cloud.hypervisor.xenserver.resource.wrapper; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.PingTestCommand; +import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase; +import com.cloud.resource.CommandWrapper; +import com.xensource.xenapi.Connection; + +public final class CitrixPingTestCommandWrapper extends CommandWrapper { + + @Override + public Answer execute(final PingTestCommand command, final CitrixResourceBase citrixResourceBase) { + final Connection conn = citrixResourceBase.getConnection(); + boolean result = false; + final String computingHostIp = command.getComputingHostIp(); + + if (computingHostIp != null) { + result = citrixResourceBase.doPingTest(conn, computingHostIp); + } else { + result = citrixResourceBase.doPingTest(conn, command.getRouterIp(), command.getPrivateIp()); + } + + if (!result) { + return new Answer(command, false, "PingTestCommand failed"); + } + return new Answer(command); + } +} \ No newline at end of file diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java index 6d262ee7725..badc892b2ee 100644 --- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java +++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java @@ -37,6 +37,7 @@ import com.cloud.agent.api.GetVncPortCommand; import com.cloud.agent.api.MaintainCommand; import com.cloud.agent.api.MigrateCommand; import com.cloud.agent.api.ModifyStoragePoolCommand; +import com.cloud.agent.api.PingTestCommand; import com.cloud.agent.api.PrepareForMigrationCommand; import com.cloud.agent.api.ReadyCommand; import com.cloud.agent.api.RebootCommand; @@ -99,6 +100,7 @@ public class CitrixRequestWrapper extends RequestWrapper { map.put(GetVncPortCommand.class, new CitrixGetVncPortCommandWrapper()); map.put(SetupCommand.class, new CitrixSetupCommandWrapper()); map.put(MaintainCommand.class, new CitrixMaintainCommandWrapper()); + map.put(PingTestCommand.class, new CitrixPingTestCommandWrapper()); } public static CitrixRequestWrapper getInstance() { diff --git a/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java b/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java index 45bdea1d6e5..0ba9d1a1f3b 100644 --- a/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java +++ b/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java @@ -38,6 +38,7 @@ import com.cloud.agent.api.GetVncPortCommand; import com.cloud.agent.api.MaintainCommand; import com.cloud.agent.api.MigrateCommand; import com.cloud.agent.api.ModifyStoragePoolCommand; +import com.cloud.agent.api.PingTestCommand; import com.cloud.agent.api.PrepareForMigrationCommand; import com.cloud.agent.api.ReadyCommand; import com.cloud.agent.api.RebootAnswer; @@ -554,6 +555,19 @@ public class CitrixRequestWrapperTest { assertFalse(answer.getResult()); } + + @Test + public void testPingTestCommandHostIp() { + final PingTestCommand storageDownloadCommand = new PingTestCommand("127.0.0.1"); + + final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(storageDownloadCommand, citrixResourceBase); + verify(citrixResourceBase, times(1)).getConnection(); + + assertFalse(answer.getResult()); + } } class NotAValidCommand extends Command {