GatherLogsCommand implements Action { @Override public Object execute() throws Exception { RbcfInfo info = KarafDmCommandsUtils.findInstances( this.manager, this.applicationName, this.scopedInstancePath, this.out ); for( Instance inst : info.getScopedInstances()) { if( inst.getStatus() == InstanceStatus.NOT_DEPLOYED ) { StringBuilder sb = new StringBuilder( "No message will be sent to " ); sb.append( this.scopedInstancePath ); sb.append( ", the associated agent is not marked as deployed." ); this.out.println( sb.toString()); continue; } if( this.logger.isLoggable( Level.FINE )) { StringBuilder sb = new StringBuilder( "Sending a request to gather the logs from " ); sb.append( this.scopedInstancePath ); sb.append( " @ " ); sb.append( this.applicationName ); this.logger.fine( sb.toString()); } MsgCmdGatherLogs message = new MsgCmdGatherLogs(); this.manager.messagingMngr().sendMessageSafely( info.getManagedApplication(), inst, message ); } File dumpFile = new File( System.getProperty( "java.io.tmpdir" ), "roboconf-logs" ); this.out.println( "On reception, logs will be stored under " + dumpFile.getAbsolutePath() + "..." ); return null; }  @Override Object execute();  }
@Test public void testExecute_invalidApplicationName() throws Exception { this.gl.applicationName = "invalid"; this.gl.scopedInstancePath = null; ByteArrayOutputStream os = new ByteArrayOutputStream(); this.gl.out = new PrintStream( os, true, "UTF-8" ); this.gl.execute(); Assert.assertTrue( os.toString( "UTF-8" ).contains( "Unknown application" )); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.gl.applicationName ); Mockito.verifyZeroInteractions( this.messagingMngr ); }
@Test public void testExecute_invalidInstance() throws Exception { this.gl.applicationName = this.app.getName(); this.gl.scopedInstancePath = "not here"; ByteArrayOutputStream os = new ByteArrayOutputStream(); this.gl.out = new PrintStream( os, true, "UTF-8" ); this.gl.execute(); Assert.assertTrue( os.toString( "UTF-8" ).contains( "There is no" )); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.gl.applicationName ); Mockito.verifyZeroInteractions( this.messagingMngr ); }
@Test public void testExecute_notAScopedInstance() throws Exception { this.gl.applicationName = this.app.getName(); this.gl.scopedInstancePath = InstanceHelpers.computeInstancePath( this.app.getWar()); ByteArrayOutputStream os = new ByteArrayOutputStream(); this.gl.out = new PrintStream( os, true, "UTF-8" ); this.gl.execute(); Assert.assertTrue( os.toString( "UTF-8" ).contains( "is not a scoped instance" )); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.gl.applicationName ); Mockito.verifyZeroInteractions( this.messagingMngr ); }
@Test public void testExecute_valid_noSpecifiedInstance_notDeployed() throws Exception { this.gl.applicationName = this.app.getName(); this.gl.scopedInstancePath = null; ByteArrayOutputStream os = new ByteArrayOutputStream(); this.gl.out = new PrintStream( os, true, "UTF-8" ); this.gl.execute(); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.gl.applicationName ); Mockito.verifyZeroInteractions( this.messagingMngr ); }
@Test public void testExecute_valid_noSpecifiedInstance_allDeployed() throws Exception { this.gl.applicationName = this.app.getName(); this.gl.scopedInstancePath = null; for( Instance inst : InstanceHelpers.findAllScopedInstances( this.app )) inst.setStatus( InstanceStatus.DEPLOYED_STARTED ); ByteArrayOutputStream os = new ByteArrayOutputStream(); this.gl.out = new PrintStream( os, true, "UTF-8" ); this.gl.execute(); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.gl.applicationName ); ArgumentCaptor<Message> msg = ArgumentCaptor.forClass( Message.class ); Mockito.verify( this.messagingMngr, Mockito.times( 2 )).sendMessageSafely( Mockito.any( ManagedApplication.class ), Mockito.any( Instance.class ), msg.capture()); Assert.assertEquals( MsgCmdGatherLogs.class, msg.getValue().getClass()); String s = os.toString( "UTF-8" ).trim(); Assert.assertTrue( s.startsWith( "On reception" )); Assert.assertTrue( s.endsWith( "/roboconf-logs..." )); }
@Test public void testExecute_valid_withSpecifiedInstance_deployed() throws Exception { this.gl.applicationName = this.app.getName(); this.gl.scopedInstancePath = InstanceHelpers.computeInstancePath( this.app.getTomcatVm()); this.app.getTomcatVm().setStatus( InstanceStatus.DEPLOYED_STARTED ); this.gl.logger.setLevel( Level.FINE ); ByteArrayOutputStream os = new ByteArrayOutputStream(); this.gl.out = new PrintStream( os, true, "UTF-8" ); this.gl.execute(); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.gl.applicationName ); String s = os.toString( "UTF-8" ).trim(); Assert.assertTrue( s.startsWith( "On reception" )); Assert.assertTrue( s.endsWith( "/roboconf-logs..." )); ArgumentCaptor<Message> msg = ArgumentCaptor.forClass( Message.class ); Mockito.verify( this.messagingMngr, Mockito.times( 1 )).sendMessageSafely( Mockito.eq( this.ma ), Mockito.eq( this.app.getTomcatVm()), msg.capture()); Assert.assertEquals( MsgCmdGatherLogs.class, msg.getValue().getClass()); }
@Test public void testExecute_valid_withSpecifiedInstance_notDeployed() throws Exception { this.gl.applicationName = this.app.getName(); this.gl.scopedInstancePath = InstanceHelpers.computeInstancePath( this.app.getTomcatVm()); this.gl.logger.setLevel( Level.FINE ); ByteArrayOutputStream os = new ByteArrayOutputStream(); this.gl.out = new PrintStream( os, true, "UTF-8" ); this.gl.execute(); Mockito.verify( this.applicationMngr, Mockito.times( 1 )).findManagedApplicationByName( this.gl.applicationName ); Mockito.verifyZeroInteractions( this.messagingMngr ); String s = os.toString( "UTF-8" ).trim(); Assert.assertTrue( s.startsWith( "No message will be sent to " )); Assert.assertTrue( s.contains( "On reception" )); Assert.assertTrue( s.endsWith( "/roboconf-logs..." )); }