@Test
public void testRunScriptByRole() throws Exception {
ClusterControllerFactory factory = mock(ClusterControllerFactory.class);
ClusterController controller = mock(ClusterController.class);
when(factory.create((String)any())).thenReturn(controller);
ClusterStateStore memStore = new MemoryClusterStateStore();
memStore.save(createTestCluster(
new String[]{"reg/A", "reg/B"}, new String[]{"A", "B"}));
ClusterStateStoreFactory stateStoreFactory = mock(ClusterStateStoreFactory.class);
when(stateStoreFactory.create((ClusterSpec) any())).thenReturn(memStore);
RunScriptCommand command = new RunScriptCommand(factory, stateStoreFactory);
Map<String, File> keys = KeyPair.generateTemporaryFiles();
int rc = command.run(null, out, System.err, Lists.newArrayList(
"--instance-templates", "1 noop",
"--script", "/dev/null",
"--roles", "A",
"--cluster-name", "test-cluster",
"--provider", "provider",
"--identity", "myusername", "--credential", "mypassword",
"--private-key-file", keys.get("private").getAbsolutePath()
));
assertThat(rc, is(0));
ArgumentCaptor<Predicate> predicate = ArgumentCaptor.forClass(Predicate.class);
verify(controller).runScriptOnNodesMatching(
(ClusterSpec)any(), predicate.capture(), (Statement) any());
Predicate<NodeMetadata> expected = Predicates.and(
Predicates.<NodeMetadata>alwaysTrue(), withIds("reg/A"));
assertThat(predicate.getValue().toString(), is(expected.toString()));
}