changeset 1964:cc1a683958ca

Change default find-vm behaviour to search all agents Search all agents rather than only those living. Add --alive-agents-only flag to instead search only alive agents. --agentId flag unchanged. Reviewed-by: jerboaa, jkang Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-August/015077.html Review-thread: PR3040
author Andrew Azores <aazores@redhat.com>
date Fri, 14 Aug 2015 13:38:40 -0400
parents 91224918b9be
children 180cf94d4b41
files vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/FindVmCommand.java vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/HostCriterion.java vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/VmCriterion.java vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/locale/LocaleResources.java vm-find/command/src/main/resources/com/redhat/thermostat/vm/find/command/locale/strings.properties vm-find/command/src/test/java/com/redhat/thermostat/vm/find/command/internal/FindVmCommandTest.java vm-find/distribution/thermostat-plugin.xml
diffstat 7 files changed, 183 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/FindVmCommand.java	Tue Aug 11 15:13:23 2015 -0400
+++ b/vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/FindVmCommand.java	Fri Aug 14 13:38:40 2015 -0400
@@ -66,6 +66,9 @@
 
     private static final Translate<LocaleResources> translator = LocaleResources.createTranslator();
 
+    static final String ALIVE_AGENTS_ONLY_ARGUMENT = "alive-agents-only";
+    static final String AGENT_ID_ARGUMENT = "agentId";
+
     private AgentInfoDAO agentInfoDAO;
     private HostInfoDAO hostInfoDAO;
     private VmInfoDAO vmInfoDAO;
@@ -83,36 +86,45 @@
         requireNonNull(hostInfoDAO, translator.localize(LocaleResources.HOST_SERVICE_UNAVAILABLE));
         requireNonNull(vmInfoDAO, translator.localize(LocaleResources.VM_SERVICE_UNAVAILABLE));
 
-        List<AgentInformation> agentsToSearch = getAgentsToSearch(ctx.getArguments());
+        Arguments arguments = ctx.getArguments();
+        List<AgentInformation> agentsToSearch = getAgentsToSearch(arguments, agentInfoDAO);
 
-        Map<String, String> hostCriteria = getHostCriteria(ctx.getArguments());
-        Map<String, String> vmCriteria = getVmCriteria(ctx.getArguments());
-
-        if (hostCriteria.isEmpty() && vmCriteria.isEmpty()) {
-            throw new CommandException(translator.localize(LocaleResources.NO_CRITERIA_GIVEN));
-        }
+        Map<String, String> hostCriteria = getHostCriteria(arguments);
+        Map<String, String> vmCriteria = getVmCriteria(arguments);
+        assertCriteriaGiven(hostCriteria, vmCriteria);
 
         HostMatcher hostMatcher = new HostMatcher(hostCriteria);
         VmMatcher vmMatcher = new VmMatcher(vmCriteria);
 
         List<Pair<HostInfo, VmInfo>> results = performSearch(agentsToSearch, hostMatcher, vmMatcher);
 
-        ResultsRenderer resultsRenderer = new ResultsRenderer(ctx.getArguments());
+        ResultsRenderer resultsRenderer = new ResultsRenderer(arguments);
         resultsRenderer.print(ctx.getConsole().getOutput(), results);
     }
 
-    List<AgentInformation> getAgentsToSearch(Arguments arguments) {
+    static List<AgentInformation> getAgentsToSearch(Arguments arguments, AgentInfoDAO agentInfoDAO) throws CommandException {
+        validateAgentStatusArguments(arguments);
         List<AgentInformation> aliveAgents;
-        if (arguments.hasArgument("agentId")) {
-            String agentId = arguments.getArgument("agentId");
+        if (arguments.hasArgument(AGENT_ID_ARGUMENT)) {
+            String agentId = arguments.getArgument(AGENT_ID_ARGUMENT);
 
             aliveAgents = Collections.singletonList(agentInfoDAO.getAgentInformation(new HostRef(agentId, "dummy")));
+        } else if (arguments.hasArgument(ALIVE_AGENTS_ONLY_ARGUMENT)) {
+            aliveAgents = agentInfoDAO.getAliveAgents();
         } else {
-            aliveAgents = agentInfoDAO.getAliveAgents();
+            aliveAgents = agentInfoDAO.getAllAgentInformation();
         }
         return aliveAgents;
     }
 
+    static void validateAgentStatusArguments(Arguments arguments) throws CommandException {
+        boolean hasAgentIdArgument = arguments.hasArgument(AGENT_ID_ARGUMENT);
+        boolean hasAliveAgentsOnlyArgument = arguments.hasArgument(ALIVE_AGENTS_ONLY_ARGUMENT);
+        if (hasAgentIdArgument && hasAliveAgentsOnlyArgument) {
+            throw new CommandException(translator.localize(LocaleResources.AGENT_FLAGS_CLASH, AGENT_ID_ARGUMENT, ALIVE_AGENTS_ONLY_ARGUMENT));
+        }
+    }
+
     static Map<String, String> getHostCriteria(Arguments arguments) {
         Map<String, String> hostCriteria = new HashMap<>();
         for (HostCriterion criterion : HostCriterion.values()) {
@@ -123,6 +135,12 @@
         return hostCriteria;
     }
 
+    static void assertCriteriaGiven(Map<String, String> hostCriteria, Map<String, String> vmCriteria) throws CommandException {
+        if (hostCriteria.isEmpty() && vmCriteria.isEmpty()) {
+            throw new CommandException(translator.localize(LocaleResources.NO_CRITERIA_GIVEN));
+        }
+    }
+
     static Map<String, String> getVmCriteria(Arguments arguments) {
         Map<String, String> vmCriteria = new HashMap<>();
         for (VmCriterion criterion : VmCriterion.values()) {
--- a/vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/HostCriterion.java	Tue Aug 11 15:13:23 2015 -0400
+++ b/vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/HostCriterion.java	Fri Aug 14 13:38:40 2015 -0400
@@ -38,8 +38,6 @@
 
 import com.redhat.thermostat.storage.model.HostInfo;
 
-import java.util.NoSuchElementException;
-
 enum HostCriterion implements CriterionMatcher<HostInfo, String> {
     HOSTNAME("hostname", new HostnameMatcher()),
     OS_KERNEL("oskernel", new OsKernelMatcher()),
--- a/vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/VmCriterion.java	Tue Aug 11 15:13:23 2015 -0400
+++ b/vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/VmCriterion.java	Fri Aug 14 13:38:40 2015 -0400
@@ -41,7 +41,6 @@
 import java.nio.file.InvalidPathException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.util.NoSuchElementException;
 import java.util.regex.PatternSyntaxException;
 
 enum VmCriterion implements CriterionMatcher<VmInfo, String> {
--- a/vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/locale/LocaleResources.java	Tue Aug 11 15:13:23 2015 -0400
+++ b/vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/locale/LocaleResources.java	Fri Aug 14 13:38:40 2015 -0400
@@ -45,6 +45,7 @@
     HOST_SERVICE_UNAVAILABLE,
     VM_SERVICE_UNAVAILABLE,
     NO_CRITERIA_GIVEN,
+    AGENT_FLAGS_CLASH,
     ;
 
     static final String RESOURCE_BUNDLE =
--- a/vm-find/command/src/main/resources/com/redhat/thermostat/vm/find/command/locale/strings.properties	Tue Aug 11 15:13:23 2015 -0400
+++ b/vm-find/command/src/main/resources/com/redhat/thermostat/vm/find/command/locale/strings.properties	Fri Aug 14 13:38:40 2015 -0400
@@ -3,3 +3,4 @@
 HOST_SERVICE_UNAVAILABLE = Unable to get host information (HostInfoDAO is unavailable)
 VM_SERVICE_UNAVAILABLE = Unable to get vm information (VmInfoDAO is unavailable)
 NO_CRITERIA_GIVEN = No filtering criteria were specified
+AGENT_FLAGS_CLASH = --{0} and --{1} cannot be used in conjunction
\ No newline at end of file
--- a/vm-find/command/src/test/java/com/redhat/thermostat/vm/find/command/internal/FindVmCommandTest.java	Tue Aug 11 15:13:23 2015 -0400
+++ b/vm-find/command/src/test/java/com/redhat/thermostat/vm/find/command/internal/FindVmCommandTest.java	Fri Aug 14 13:38:40 2015 -0400
@@ -40,7 +40,6 @@
 import com.redhat.thermostat.common.cli.Arguments;
 import com.redhat.thermostat.common.cli.CommandException;
 import com.redhat.thermostat.storage.core.HostRef;
-import com.redhat.thermostat.storage.core.VmId;
 import com.redhat.thermostat.storage.core.VmRef;
 import com.redhat.thermostat.storage.dao.AgentInfoDAO;
 import com.redhat.thermostat.storage.dao.HostInfoDAO;
@@ -51,6 +50,9 @@
 import com.redhat.thermostat.test.TestCommandContextFactory;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.internal.runners.statements.InvokeMethod;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
 
 import java.util.Arrays;
 import java.util.Collections;
@@ -67,6 +69,7 @@
 import static org.junit.Assert.fail;
 import static org.junit.matchers.JUnitMatchers.containsString;
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.isA;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -167,7 +170,7 @@
         when(hostInfoDAO.getHostInfo(any(HostRef.class))).thenReturn(hostInfo);
 
         AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class);
-        when(agentInfoDAO.getAliveAgents()).thenReturn(Collections.singletonList(agent));
+        when(agentInfoDAO.getAllAgentInformation()).thenReturn(Collections.singletonList(agent));
         when(agentInfoDAO.getAgentInformation(any(HostRef.class))).thenReturn(agent);
 
         TestCommandContextFactory testCommandContextFactory = new TestCommandContextFactory();
@@ -203,7 +206,7 @@
         when(hostInfoDAO.getHostInfo(any(HostRef.class))).thenReturn(hostInfo);
 
         AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class);
-        when(agentInfoDAO.getAliveAgents()).thenReturn(Collections.singletonList(agent));
+        when(agentInfoDAO.getAllAgentInformation()).thenReturn(Collections.singletonList(agent));
         when(agentInfoDAO.getAgentInformation(any(HostRef.class))).thenReturn(agent);
 
         TestCommandContextFactory testCommandContextFactory = new TestCommandContextFactory();
@@ -218,30 +221,156 @@
     }
 
     @Test
-    public void testGetAgentsToSearchWithoutAgentIdArg() {
+    public void testGetAgentsToSearchWithoutAgentArgs() throws CommandException {
         List<AgentInformation> list = Arrays.asList(mock(AgentInformation.class), mock(AgentInformation.class));
-        FindVmCommand command = new FindVmCommand();
         AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class);
-        when(agentInfoDAO.getAliveAgents()).thenReturn(list);
-        command.setAgentInfoDAO(agentInfoDAO);
-        List<AgentInformation> agentsToSearch = command.getAgentsToSearch(args);
+        when(agentInfoDAO.getAllAgentInformation()).thenReturn(list);
+        List<AgentInformation> agentsToSearch = FindVmCommand.getAgentsToSearch(args, agentInfoDAO);
+
         assertThat(agentsToSearch, is(equalTo(list)));
     }
 
     @Test
-    public void testGetAgentsToSearchWithAgentIdArg() {
-        AgentInformation foo = new AgentInformation("foo");
-        AgentInformation bar = new AgentInformation("bar");
+    public void testGetAgentsToSearchWithAgentIdArg() throws CommandException {
+        final AgentInformation foo = new AgentInformation("foo");
+        foo.setAlive(false);
+        final AgentInformation bar = new AgentInformation("bar");
+        bar.setAlive(true);
         List<AgentInformation> list = Arrays.asList(foo, bar);
         AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class);
         when(agentInfoDAO.getAliveAgents()).thenReturn(list);
-        when(agentInfoDAO.getAgentInformation(any(HostRef.class))).thenReturn(foo);
+        when(agentInfoDAO.getAgentInformation(isA(HostRef.class))).thenAnswer(new Answer<AgentInformation>() {
+            @Override
+            public AgentInformation answer(InvocationOnMock invocationOnMock) throws Throwable {
+                String id = ((HostRef) invocationOnMock.getArguments()[0]).getAgentId();
+                if (id.equals("foo")) {
+                    return foo;
+                } else {
+                    return bar;
+                }
+            }
+        });
+        when(args.hasArgument(FindVmCommand.AGENT_ID_ARGUMENT)).thenReturn(true);
+        when(args.getArgument(FindVmCommand.AGENT_ID_ARGUMENT)).thenReturn(foo.getAgentId());
+        List<AgentInformation> agentsToSearch = FindVmCommand.getAgentsToSearch(args, agentInfoDAO);
+
+        assertThat(agentsToSearch, is(equalTo(Collections.singletonList(foo))));
+    }
+
+    @Test
+    public void testGetAgentsToSearchWithAliveAgentsOnlyArg() throws CommandException {
+        AgentInformation foo = new AgentInformation("foo");
+        foo.setAlive(false);
+        AgentInformation bar = new AgentInformation("bar");
+        bar.setAlive(true);
+        AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class);
+        when(agentInfoDAO.getAliveAgents()).thenReturn(Collections.singletonList(bar));
+        when(agentInfoDAO.getAgentInformation(new HostRef("foo", "dummy"))).thenReturn(foo);
+        when(agentInfoDAO.getAgentInformation(new HostRef("bar", "dummy"))).thenReturn(bar);
+        when(args.hasArgument(FindVmCommand.ALIVE_AGENTS_ONLY_ARGUMENT)).thenReturn(true);
+        List<AgentInformation> agentsToSearch = FindVmCommand.getAgentsToSearch(args, agentInfoDAO);
+        assertThat(agentsToSearch, is(equalTo(Collections.singletonList(bar))));
+    }
+
+    @Test(expected = CommandException.class)
+    public void testBothAgentIdAndAliveAgentsOnlyArgs() throws CommandException {
+        when(args.hasArgument(FindVmCommand.AGENT_ID_ARGUMENT)).thenReturn(true);
+        when(args.hasArgument(FindVmCommand.ALIVE_AGENTS_ONLY_ARGUMENT)).thenReturn(true);
+
+        VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class);
+        HostInfoDAO hostInfoDAO = mock(HostInfoDAO.class);
+        AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class);
+
+        TestCommandContextFactory testCommandContextFactory = new TestCommandContextFactory();
         FindVmCommand command = new FindVmCommand();
+        command.setVmInfoDAO(vmInfoDAO);
         command.setAgentInfoDAO(agentInfoDAO);
-        when(args.hasArgument("agentId")).thenReturn(true);
-        when(args.getArgument("agentId")).thenReturn(foo.getAgentId());
-        List<AgentInformation> agentsToSearch = command.getAgentsToSearch(args);
-        assertThat(agentsToSearch, is(equalTo(Collections.singletonList(foo))));
+        command.setHostInfoDAO(hostInfoDAO);
+        command.run(testCommandContextFactory.createContext(args));
+    }
+
+    @Test(expected = CommandException.class)
+    public void testValidateAgentStatusArgumentsThrowsExceptionWhenBothFlagsGiven() throws CommandException {
+        when(args.hasArgument(FindVmCommand.AGENT_ID_ARGUMENT)).thenReturn(true);
+        when(args.hasArgument(FindVmCommand.ALIVE_AGENTS_ONLY_ARGUMENT)).thenReturn(true);
+        FindVmCommand.validateAgentStatusArguments(args);
+    }
+
+    @Test
+    public void testValidateAgentStatusArgumentsShouldNotThrowExceptionOnValidInput() {
+        when(args.hasArgument(FindVmCommand.AGENT_ID_ARGUMENT)).thenReturn(false);
+        when(args.hasArgument(FindVmCommand.ALIVE_AGENTS_ONLY_ARGUMENT)).thenReturn(true);
+        try {
+            FindVmCommand.validateAgentStatusArguments(args);
+        } catch (CommandException ce) {
+            fail("Exception should not be thrown when only " + FindVmCommand.ALIVE_AGENTS_ONLY_ARGUMENT + " is given. Exception: " + ce.getLocalizedMessage());
+        }
+    }
+
+    @Test
+    public void testValidateAgentStatusArgumentsShouldNotThrowExceptionOnValidInput2() {
+        when(args.hasArgument(FindVmCommand.AGENT_ID_ARGUMENT)).thenReturn(true);
+        when(args.hasArgument(FindVmCommand.ALIVE_AGENTS_ONLY_ARGUMENT)).thenReturn(false);
+        try {
+            FindVmCommand.validateAgentStatusArguments(args);
+        } catch (CommandException ce) {
+            fail("Exception should not be thrown when only " + FindVmCommand.AGENT_ID_ARGUMENT + " is given. Exception: " + ce.getLocalizedMessage());
+        }
+    }
+
+    @Test
+    public void testValidateAgentStatusArgumentsShouldNotThrowExceptionOnValidInput3() {
+        when(args.hasArgument(FindVmCommand.AGENT_ID_ARGUMENT)).thenReturn(false);
+        when(args.hasArgument(FindVmCommand.ALIVE_AGENTS_ONLY_ARGUMENT)).thenReturn(false);
+        try {
+            FindVmCommand.validateAgentStatusArguments(args);
+        } catch (CommandException ce) {
+            fail("Exception should not be thrown when neither agent flag is given. Exception: " + ce.getLocalizedMessage());
+        }
+    }
+
+    @Test(expected = CommandException.class)
+    public void testAssertCriteriaGivenThrowsExceptionWhenInputsEmpty() throws CommandException {
+        Map<String, String> hostCriteria = new HashMap<>();
+        Map<String, String> vmCriteria = new HashMap<>();
+        FindVmCommand.assertCriteriaGiven(hostCriteria, vmCriteria);
+    }
+
+    @Test
+    public void testAssertCriteriaGivenShouldNotThrowExceptionOnValidInput() {
+        Map<String, String> hostCriteria = new HashMap<>();
+        hostCriteria.put("foo", "bar");
+        Map<String, String> vmCriteria = new HashMap<>();
+        try {
+            FindVmCommand.assertCriteriaGiven(hostCriteria, vmCriteria);
+        } catch (CommandException ce) {
+            fail("Exception should not be thrown when host criteria are given. Exception: " + ce.getLocalizedMessage());
+        }
+    }
+
+    @Test
+    public void testAssertCriteriaGivenShouldNotThrowExceptionOnValidInput2() {
+        Map<String, String> hostCriteria = new HashMap<>();
+        Map<String, String> vmCriteria = new HashMap<>();
+        vmCriteria.put("foo", "bar");
+        try {
+            FindVmCommand.assertCriteriaGiven(hostCriteria, vmCriteria);
+        } catch (CommandException ce) {
+            fail("Exception should not be thrown when vm criteria are given. Exception: " + ce.getLocalizedMessage());
+        }
+    }
+
+    @Test
+    public void testAssertCriteriaGivenShouldNotThrowExceptionOnValidInput3() {
+        Map<String, String> hostCriteria = new HashMap<>();
+        hostCriteria.put("foo", "bar");
+        Map<String, String> vmCriteria = new HashMap<>();
+        vmCriteria.put("foo2", "bar2");
+        try {
+            FindVmCommand.assertCriteriaGiven(hostCriteria, vmCriteria);
+        } catch (CommandException ce) {
+            fail("Exception should not be thrown when host and vm criteria are given. Exception: " + ce.getLocalizedMessage());
+        }
     }
 
     @Test
--- a/vm-find/distribution/thermostat-plugin.xml	Tue Aug 11 15:13:23 2015 -0400
+++ b/vm-find/distribution/thermostat-plugin.xml	Fri Aug 14 13:38:40 2015 -0400
@@ -59,7 +59,12 @@
           <short>a</short>
           <argument>id</argument>
           <required>false</required>
-          <description>specify the agent to query. If none specified, then all living agents are queried</description>
+          <description>specify the agent to query. If none specified, then all agents are queried</description>
+        </option>
+        <option>
+          <long>alive-agents-only</long>
+          <required>false</required>
+          <description>specify that only currently alive agents should be queried</description>
         </option>
 
         <!-- Display Options -->