changeset 1724:4f607fa1813d

Provide a better info message for 'non-alive' VMs when performing GC. Reviewed-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-August/015280.html PR2582
author Severin Gehwolf <sgehwolf@redhat.com>
date Mon, 17 Aug 2015 16:05:36 +0200
parents 9d2e605dbe6f
children 5bcf61ecacab
files vm-gc/command/src/main/java/com/redhat/thermostat/vm/gc/command/internal/GCCommand.java vm-gc/command/src/main/java/com/redhat/thermostat/vm/gc/command/locale/LocaleResources.java vm-gc/command/src/main/resources/com/redhat/thermostat/gc/command/locale/strings.properties vm-gc/command/src/test/java/com/redhat/thermostat/vm/gc/command/internal/GCCommandTest.java vm-gc/command/src/test/java/com/redhat/thermostat/vm/gc/command/internal/ShowGcNameCommandTest.java
diffstat 5 files changed, 44 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/vm-gc/command/src/main/java/com/redhat/thermostat/vm/gc/command/internal/GCCommand.java	Thu Aug 13 18:32:36 2015 +0200
+++ b/vm-gc/command/src/main/java/com/redhat/thermostat/vm/gc/command/internal/GCCommand.java	Mon Aug 17 16:05:36 2015 +0200
@@ -50,7 +50,9 @@
 import com.redhat.thermostat.storage.core.VmRef;
 import com.redhat.thermostat.storage.dao.AgentInfoDAO;
 import com.redhat.thermostat.storage.dao.VmInfoDAO;
+import com.redhat.thermostat.storage.model.AgentInformation;
 import com.redhat.thermostat.storage.model.VmInfo;
+import com.redhat.thermostat.storage.model.VmInfo.AliveStatus;
 import com.redhat.thermostat.vm.gc.command.locale.LocaleResources;
 
 public class GCCommand extends AbstractCommand {
@@ -99,10 +101,15 @@
 
         if (result == null) {
             throw new CommandException(translator.localize(LocaleResources.VM_NOT_FOUND, vmId.get()));
-        } else {
-            HostRef dummyRef = new HostRef(result.getAgentId(), "dummy");
-            sendGCRequest(new VmRef(dummyRef, result.getVmId(), result.getVmPid(), result.getVmName()));
         }
+        HostRef dummyRef = new HostRef(result.getAgentId(), "dummy");
+        AgentInformation agentInfo = agentInfoDAO.getAgentInformation(dummyRef);
+        AliveStatus status = result.isAlive(agentInfo);
+        if (status != AliveStatus.RUNNING) {
+            throw new CommandException(translator.localize(LocaleResources.VM_NOT_ALIVE, vmId.get()));
+        }
+        
+        sendGCRequest(new VmRef(dummyRef, result.getVmId(), result.getVmPid(), result.getVmName()));
     }
 
     private void sendGCRequest(VmRef vmRef) throws CommandException {
--- a/vm-gc/command/src/main/java/com/redhat/thermostat/vm/gc/command/locale/LocaleResources.java	Thu Aug 13 18:32:36 2015 +0200
+++ b/vm-gc/command/src/main/java/com/redhat/thermostat/vm/gc/command/locale/LocaleResources.java	Mon Aug 17 16:05:36 2015 +0200
@@ -46,6 +46,7 @@
     VM_SERVICE_UNAVAILABLE,
     LISTENER_UNAVAILABLE,
     VM_NOT_FOUND,
+    VM_NOT_ALIVE,
     COMMAND_INTERRUPTED,
     GC_ERROR_RESULT,
     GC_SUCCESS_RESULT,
--- a/vm-gc/command/src/main/resources/com/redhat/thermostat/gc/command/locale/strings.properties	Thu Aug 13 18:32:36 2015 +0200
+++ b/vm-gc/command/src/main/resources/com/redhat/thermostat/gc/command/locale/strings.properties	Mon Aug 17 16:05:36 2015 +0200
@@ -3,7 +3,8 @@
 VM_SERVICE_UNAVAILABLE = Unable to get vm information (VmInfoDAO is unavailable)
 AGENT_SERVICE_UNAVAILABLE = Unable to get agent information (AgentInfoDAO is unavailable)
 LISTENER_UNAVAILABLE = Unable to get listener
-VM_NOT_FOUND = VM with ID: {0} not found
+VM_NOT_FOUND = VM with ID {0} not found
+VM_NOT_ALIVE = VM with ID {0} is not running.
 COMMAND_INTERRUPTED = Command interrupted while waiting for services.
 GC_ERROR_RESULT = GC Request Error for VM with PID: {0}
 GC_SUCCESS_RESULT = GC Successful for VM with PID: {0}
--- a/vm-gc/command/src/test/java/com/redhat/thermostat/vm/gc/command/internal/GCCommandTest.java	Thu Aug 13 18:32:36 2015 +0200
+++ b/vm-gc/command/src/test/java/com/redhat/thermostat/vm/gc/command/internal/GCCommandTest.java	Mon Aug 17 16:05:36 2015 +0200
@@ -38,6 +38,7 @@
 
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -56,6 +57,7 @@
 import com.redhat.thermostat.storage.core.VmRef;
 import com.redhat.thermostat.storage.dao.AgentInfoDAO;
 import com.redhat.thermostat.storage.dao.VmInfoDAO;
+import com.redhat.thermostat.storage.model.AgentInformation;
 import com.redhat.thermostat.storage.model.VmInfo;
 import com.redhat.thermostat.test.TestCommandContextFactory;
 
@@ -86,12 +88,16 @@
     public void testPerformGC() throws Exception {
         String vmId = "liveVM";
 
-        VmRef vmRef = new VmRef(new HostRef(null, "dummy"), "liveVM", -1, "dummy");
+        VmRef vmRef = new VmRef(new HostRef("dummy", "dummy"), "liveVM", -1, "dummy");
 
-        VmInfo vmInfo = mock(VmInfo.class);
-        when(vmInfo.getVmPid()).thenReturn(-1);
-        when(vmInfo.getVmId()).thenReturn("liveVM");
-        when(vmInfo.getVmName()).thenReturn("dummy");
+        VmInfo vmInfo = new VmInfo("dummy", "liveVM", -1, 0l, 0l, null, null, null, null, "dummy", "dummy", null, null, null, null, null, 0l, null) {
+            
+            @Override
+            public AliveStatus isAlive(AgentInformation agentInfo) {
+                return AliveStatus.RUNNING;
+            }
+            
+        };
 
         when(vmInfoDAO.getVmInfo(any(VmId.class))).thenReturn(vmInfo);
 
@@ -123,6 +129,25 @@
 
         command.run(context);
     }
+    
+    @Test(expected = CommandException.class)
+    public void testPerformGCOnNotLivingVM() throws Exception {
+        VmInfo vmInfo = new VmInfo("foo-agent", "my-vm-id", -1, 0l, 0l, null, null, null, null, "dummy", null, null, null, null, null, null, 0l, null) {
+            @Override
+            public AliveStatus isAlive(AgentInformation agentInfo) {
+                return AliveStatus.EXITED;
+            }
+        };
+        String vmId = "my-vm-id";
+        VmId myId = new VmId(vmId);
+        when(vmInfoDAO.getVmInfo(eq(myId))).thenReturn(vmInfo);
+        
+        CommandContext context = createVmIdArgs(vmId);
+
+        setServices();
+
+        command.run(context);
+    }
 
     @Test(expected = CommandException.class)
     public void testGCWithoutServices() throws Exception {
--- a/vm-gc/command/src/test/java/com/redhat/thermostat/vm/gc/command/internal/ShowGcNameCommandTest.java	Thu Aug 13 18:32:36 2015 +0200
+++ b/vm-gc/command/src/test/java/com/redhat/thermostat/vm/gc/command/internal/ShowGcNameCommandTest.java	Mon Aug 17 16:05:36 2015 +0200
@@ -98,7 +98,7 @@
         try {
             command.run(new TestCommandContextFactory().createContext(args));
         } catch (CommandException e) {
-            assertTrue(e.getMessage().startsWith("VM with ID:"));
+            assertTrue(e.getMessage().startsWith("VM with ID"));
             assertTrue(e.getMessage().endsWith("not found"));
         }
     }