Mercurial > hg > release > thermostat-1.0
changeset 396:776aa41e0dba
PR999: "vm-info --hostId <hostId>" should show all vms on host
Reviewed-by: rkennke
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-June/001909.html
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Tue, 19 Jun 2012 19:28:13 -0400 |
parents | 053bc97253ad |
children | 14c11d63e75c |
files | tools/src/main/java/com/redhat/thermostat/tools/cli/HostVMArguments.java tools/src/main/java/com/redhat/thermostat/tools/cli/VMInfoCommand.java tools/src/test/java/com/redhat/thermostat/tools/cli/HostVMArgumentsTest.java tools/src/test/java/com/redhat/thermostat/tools/cli/VMInfoCommandTest.java |
diffstat | 4 files changed, 125 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/tools/src/main/java/com/redhat/thermostat/tools/cli/HostVMArguments.java Tue Jun 19 15:37:59 2012 -0400 +++ b/tools/src/main/java/com/redhat/thermostat/tools/cli/HostVMArguments.java Tue Jun 19 19:28:13 2012 -0400 @@ -55,11 +55,22 @@ private VmRef vm; HostVMArguments(Arguments args) throws CommandException { + this(args, true); + } + + HostVMArguments(Arguments args, boolean vmRequired) throws CommandException { String hostId = args.getArgument(HOST_ID_ARGUMENT); String vmId = args.getArgument(VM_ID_ARGUMENT); host = new HostRef(hostId, "dummy"); - int parsedVmId = parseVmId(vmId); - vm = new VmRef(host, parsedVmId, "dummy"); + try { + int parsedVmId = parseVmId(vmId); + vm = new VmRef(host, parsedVmId, "dummy"); + } catch (CommandException ce) { + if (vmRequired) { + throw ce; + } + vm = null; + } } private int parseVmId(String vmId) throws CommandException { @@ -78,8 +89,20 @@ return vm; } + /** + * @return a collection of arguments for accepting hosts and vms (where both + * are required) + */ static Collection<ArgumentSpec> getArgumentSpecs() { - ArgumentSpec vmId = new SimpleArgumentSpec(VM_ID_ARGUMENT, "the ID of the VM to monitor", true, true); + return getArgumentSpecs(true); + } + + /** + * @return a collection of arguments for accepting hosts and vms (where the + * vm is optional) + */ + static Collection<ArgumentSpec> getArgumentSpecs(boolean vmRequired) { + ArgumentSpec vmId = new SimpleArgumentSpec(VM_ID_ARGUMENT, "the ID of the VM to monitor", vmRequired, true); ArgumentSpec hostId = new SimpleArgumentSpec(HOST_ID_ARGUMENT, "the ID of the host to monitor", true, true); return Arrays.asList(vmId, hostId); }
--- a/tools/src/main/java/com/redhat/thermostat/tools/cli/VMInfoCommand.java Tue Jun 19 15:37:59 2012 -0400 +++ b/tools/src/main/java/com/redhat/thermostat/tools/cli/VMInfoCommand.java Tue Jun 19 19:28:13 2012 -0400 @@ -47,6 +47,7 @@ import com.redhat.thermostat.common.cli.CommandException; import com.redhat.thermostat.common.dao.DAOException; import com.redhat.thermostat.common.dao.DAOFactory; +import com.redhat.thermostat.common.dao.HostRef; import com.redhat.thermostat.common.dao.VmInfoDAO; import com.redhat.thermostat.common.dao.VmRef; import com.redhat.thermostat.common.model.VmInfo; @@ -62,15 +63,28 @@ public void run(CommandContext ctx) throws CommandException { DAOFactory daoFactory = ApplicationContext.getInstance().getDAOFactory(); VmInfoDAO vmsDAO = daoFactory.getVmInfoDAO(); - HostVMArguments hostVMArgs = new HostVMArguments(ctx.getArguments()); + HostVMArguments hostVMArgs = new HostVMArguments(ctx.getArguments(), false); + HostRef host = hostVMArgs.getHost(); VmRef vm = hostVMArgs.getVM(); try { - getAndPrintVMInfo(ctx, vmsDAO, vm); + if (vm != null) { + getAndPrintVMInfo(ctx, vmsDAO, vm); + } else { + getAndPrintAllVMInfo(ctx, vmsDAO, host); + + } } catch (DAOException ex) { ctx.getConsole().getError().println(ex.getMessage()); } } + private void getAndPrintAllVMInfo(CommandContext ctx, VmInfoDAO vmsDAO, HostRef host) { + Collection<VmRef> vms = vmsDAO.getVMs(host); + for (VmRef vm : vms) { + getAndPrintVMInfo(ctx, vmsDAO, vm); + } + } + private void getAndPrintVMInfo(CommandContext ctx, VmInfoDAO vmsDAO, VmRef vm) { VmInfo vmInfo = vmsDAO.getVmInfo(vm); @@ -113,7 +127,7 @@ @Override public Collection<ArgumentSpec> getAcceptedArguments() { - return HostVMArguments.getArgumentSpecs(); + return HostVMArguments.getArgumentSpecs(false); } @Override
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/src/test/java/com/redhat/thermostat/tools/cli/HostVMArgumentsTest.java Tue Jun 19 19:28:13 2012 -0400 @@ -0,0 +1,63 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.tools.cli; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; + +import org.junit.Test; + +import com.redhat.thermostat.common.cli.ArgumentSpec; + +public class HostVMArgumentsTest { + + @Test + public void testArgumentSpecification() { + ArrayList<ArgumentSpec> args = new ArrayList<>(HostVMArguments.getArgumentSpecs(false)); + assertEquals(2, args.size()); + + // TODO different order should be okay + ArgumentSpec vmIdArg = args.get(0); + assertEquals("vmId", vmIdArg.getName()); + assertEquals(false, vmIdArg.isRequired()); + + ArgumentSpec hostIdArg = args.get(1); + assertEquals("hostId", hostIdArg.getName()); + assertEquals(true, hostIdArg.isRequired()); + } +}
--- a/tools/src/test/java/com/redhat/thermostat/tools/cli/VMInfoCommandTest.java Tue Jun 19 15:37:59 2012 -0400 +++ b/tools/src/test/java/com/redhat/thermostat/tools/cli/VMInfoCommandTest.java Tue Jun 19 19:28:13 2012 -0400 @@ -43,6 +43,7 @@ import static org.mockito.Mockito.when; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Collection; import java.util.HashMap; @@ -122,6 +123,7 @@ VmInfo vmInfo = new VmInfo(234, start.getTimeInMillis(), end.getTimeInMillis(), "vmVersion", "javaHome", "mainClass", "commandLine", "vmName", "vmInfo", "vmVersion", "vmArguments", new HashMap<String,String>(), new HashMap<String,String>(), new ArrayList<String>()); when(vmsDAO.getVmInfo(vm)).thenReturn(vmInfo); when(vmsDAO.getVmInfo(new VmRef(host, 9876, "dummy"))).thenThrow(new DAOException("Unknown VM ID: 9876")); + when(vmsDAO.getVMs(host)).thenReturn(Arrays.asList(vm)); DAOFactory daoFactory = mock(DAOFactory.class); when(daoFactory.getVmInfoDAO()).thenReturn(vmsDAO); ApplicationContext.getInstance().setDAOFactory(daoFactory); @@ -146,6 +148,22 @@ } @Test + public void testAllVmInfoForHost() throws CommandException { + SimpleArguments args = new SimpleArguments(); + args.addArgument("hostId", "123"); + cmd.run(cmdCtxFactory.createContext(args)); + String expected = "Process ID: 234\n" + + "Start time: Thu Jun 07 15:32:00 UTC 2012\n" + + "Stop time: Fri Nov 01 01:22:00 UTC 2013\n" + + "Main class: mainClass\n" + + "Command line: commandLine\n" + + "Java version: vmVersion\n" + + "Virtual machine: vmName\n" + + "VM arguments: vmArguments\n"; + assertEquals(expected, cmdCtxFactory.getOutput()); + } + + @Test public void testVmInfoUnknownVM() throws CommandException { SimpleArguments args = new SimpleArguments(); args.addArgument("vmId", "9876"); @@ -216,7 +234,7 @@ Collection<ArgumentSpec> args = cmd.getAcceptedArguments(); assertNotNull(args); assertEquals(2, args.size()); - assertTrue(args.contains(new SimpleArgumentSpec("vmId", "the ID of the VM to monitor", true, true))); + assertTrue(args.contains(new SimpleArgumentSpec("vmId", "the ID of the VM to monitor", false, true))); assertTrue(args.contains(new SimpleArgumentSpec("hostId", "the ID of the host to monitor", true, true))); }