[jboss-cvs] JBossAS SVN: r112180 - branches/JBPAPP_5_1/testsuite/src/main/org/jboss/test/deployment.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Aug 31 01:45:35 EDT 2011


Author: bmaxwell
Date: 2011-08-31 01:45:35 -0400 (Wed, 31 Aug 2011)
New Revision: 112180

Added:
   branches/JBPAPP_5_1/testsuite/src/main/org/jboss/test/deployment/JBPAPP6716UnitTestCase.java
Log:
[JBPAPP-6716] add testcase to test deploy/undeploy/redeploy/isDeployed/listDeployed* methods on MainDeployer MBean via jmx and twiddle.sh 

Added: branches/JBPAPP_5_1/testsuite/src/main/org/jboss/test/deployment/JBPAPP6716UnitTestCase.java
===================================================================
--- branches/JBPAPP_5_1/testsuite/src/main/org/jboss/test/deployment/JBPAPP6716UnitTestCase.java	                        (rev 0)
+++ branches/JBPAPP_5_1/testsuite/src/main/org/jboss/test/deployment/JBPAPP6716UnitTestCase.java	2011-08-31 05:45:35 UTC (rev 112180)
@@ -0,0 +1,539 @@
+ /* distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.deployment;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.URLConnection;
+import java.security.Principal;
+import java.util.Collection;
+import java.util.Date;
+import java.util.Properties;
+
+import javax.management.JMX;
+import javax.management.MBeanServerConnection;
+import javax.management.ObjectName;
+
+import org.jboss.deployment.MainDeployerMBean;
+import org.jboss.mx.util.ObjectNameFactory;
+import org.jboss.security.SecurityAssociation;
+import org.jboss.security.SimplePrincipal;
+import org.jboss.system.server.ServerConfigImplMBean;
+import org.jboss.test.JBossTestCase;
+
+/**
+ * JBPAPP-6716 MainDeployer tests
+ * 
+ * @author Shaun Appleton
+ * @author bmaxwell
+ * @author klape
+ */
+public class JBPAPP6716UnitTestCase extends JBossTestCase
+{
+   private MainDeployerMBean deployer;
+   private ServerConfigImplMBean serverInfo;
+   private URL dummyUrl;
+   private URL dummyIndexUrl;
+   private String hostname;
+   private String JBOSS_HOME;
+   private String JBOSS_DEPLOY;
+   private Object[] previousSecurity = new Object[2];
+
+   public JBPAPP6716UnitTestCase(String name) throws Exception
+   {
+      super(name);
+   }
+
+   protected void setUp() throws Exception
+   {
+      super.setUp();
+      
+      setSecurity(new SimplePrincipal("admin"), "admin");      
+      
+      dummyUrl = getDeployURL("dummy.war");
+      
+      MBeanServerConnection server = getServer();
+      
+      ObjectName objectName = ObjectNameFactory.create("jboss.system:service=MainDeployer");
+      deployer = JMX.newMBeanProxy(server, objectName, MainDeployerMBean.class);
+      
+      objectName = ObjectNameFactory.create("jboss.system:type=ServerConfig");
+      serverInfo = JMX.newMBeanProxy(server, objectName, ServerConfigImplMBean.class);
+      
+      JBOSS_HOME = System.getProperty("jbosstest.dist");
+
+      // find the jboss config being run for this test      
+      File deployDir = new File(serverInfo.getServerHomeDir(), "deploy");
+      JBOSS_DEPLOY = deployDir.getAbsolutePath();
+      
+      hostname = System.getProperty("jbosstest.server.host");
+      dummyIndexUrl = new URL("http://" + hostname + ":8080/dummy/index.html");
+   }
+      
+   protected void tearDown() throws Exception
+   {   
+      super.tearDown();
+      setSecurity((Principal) previousSecurity[0], previousSecurity[1]);      
+   }
+           
+   //***** Deploy Tests *****//
+
+   /* this tests these methods:
+    * deploy(URL)
+    * deploy(String)
+    * undeploy(URL)
+    * undeploy(String)
+    * redeploy(URL)
+    * redeploy(String)
+    * isDeployed(URL)
+    *
+    */
+   public void testDeployUnDeployReDeployIsDeployed() throws Exception
+   {
+      String dummyUrlString = dummyUrl.toExternalForm();
+            
+      log.info("+++ testDeployUnDeployReDeployIsDeployed");      
+                    
+      try
+      {
+         //Deploy & undeploy with URL
+         deployer.deploy(dummyUrl);
+         assertTrue("Failed to deploy URL", isDeployed(dummyUrl, false));
+
+         deployer.undeploy(dummyUrl);
+         assertTrue(dummyUrlString + " is not deployed when it should be", !isDeployed(dummyUrl, false));
+
+         //Deploy & undeploy with String
+         deployer.deploy(dummyUrlString);
+         assertTrue(isDeployed(dummyUrl, true));
+
+         deployer.undeploy(dummyUrlString);
+         assertTrue(!isDeployed(dummyUrl, true));
+
+         //Redploy 
+         deployer.deploy(dummyUrl);
+         assertTrue(isDeployed(dummyUrl, false));
+
+         //With URL
+         deployer.redeploy(dummyUrl);
+         assertTrue(isDeployed(dummyUrl, false));
+
+         //With String
+         deployer.redeploy(dummyUrlString);
+         assertTrue(isDeployed(dummyUrl, true));
+      }
+      catch (Exception e)
+      {
+         e.printStackTrace();
+         super.fail("Caught exception, message: " + e.getMessage());
+      }
+      finally
+      {
+         try
+         {
+            //make sure it's undeployed before moving on
+            deployer.undeploy(dummyUrl);
+         }
+         catch(Exception ignore)
+         {
+            //ignore
+         }
+      }
+   }
+   
+   // run tests using $JBOSS_HOME/bin/twiddle.sh
+   public void testTwiddle()
+   {
+      // $JBOSS_HOME/bin/twiddle.sh -u admin -p admin invoke jboss.system:service=MainDeployer listDeployedAsString      
+      String invoke = "-u admin -p admin invoke jboss.system:service=MainDeployer ";
+      String[] deployments = new String[] 
+                                        { 
+            dummyUrl.toExternalForm(), 
+            dummyUrl.getPath() 
+            };
+      try
+      {
+         // test file:/path/to/dummy.war and /path/to/dummy.war
+         for(String deployment : deployments)
+         {
+            log.info("testing: " + deployment);
+            
+            // deploy(String)
+            twiddle(invoke + "deploy " + deployment, null);
+            
+            // isDeployed(String)
+            twiddle(invoke + "isDeployed " + deployment, "true");
+            
+            // listDeployedAsString()
+            twiddle(invoke + "listDeployedAsString", deployment);
+            
+            // listDeployed()
+            twiddle(invoke + "listDeployed", deployment);
+            
+            // listDeployedModules()
+            twiddle(invoke + "listDeployedModules", deployment);
+            
+            // undeploy(String)
+            twiddle(invoke + "undeploy " + deployment, null);
+      
+            // isDeployed(String)
+            twiddle(invoke + "isDeployed " + deployment, "false");
+            
+            // redeploy(String)
+            twiddle(invoke + "redeploy " + deployment, null);
+            
+            // isDeployed(String)
+            twiddle(invoke + "isDeployed " + deployment, "true");
+            
+            // redeploy(String)
+            twiddle(invoke + "redeploy " + deployment, null);
+   
+            // isDeployed(String)
+            twiddle(invoke + "isDeployed " + deployment, "true");
+         }
+      }
+      finally
+      {
+         cleanUp();
+      }      
+   }
+   
+   public void testDeploymentsInDeployDirectory()
+   {
+      File dummyWarFile = new File(JBOSS_DEPLOY,"dummy.war");
+      try
+      {
+         // copy dummy.war to deploy directory
+         copyUrlToFile(dummyUrl, dummyWarFile);
+         
+         // wait 1 second for deployment, max of 5 seconds, then fail         
+         int attempts = 0;
+         do
+         {
+            Thread.sleep(1000);
+            attempts++;
+         } while (deployer.isDeployed(dummyWarFile.getAbsolutePath()) == false && attempts < 5);
+         
+         if(attempts >= 5)
+         {
+            fail("Failed to deploy: " + dummyWarFile.getAbsolutePath() + " waited 5 seconds and it has not been deployed");
+         }
+            
+         // call undeploy, confirm dummy.war file is still in deploy dir and that it is not running, else fail
+         log.info("call undeploy, confirm dummy.war file is still in deploy dir and that it is not running, else fail");
+         deployer.undeploy(dummyWarFile.getAbsolutePath());
+         
+         if( ! dummyWarFile.exists() )
+         {
+            fail("The file: " + dummyWarFile.getAbsolutePath() + " should exist, undeploy should have not removed it from: " + JBOSS_DEPLOY);
+         }
+         if( isDummyRunning() )
+         {
+            fail("The url: " + dummyWarFile.getAbsolutePath() + " should not be accessible, because dummy.war should be stopped, after the previous undeploy call");
+         }
+         
+         // call deploy, confirm dummy.war is started again and is accessible
+         deployer.deploy(dummyWarFile.getAbsolutePath());
+         
+         if(deployer.isDeployed(dummyWarFile.getAbsolutePath()) == false)
+         {
+            fail("Failure: " + dummyWarFile.getAbsolutePath() + " should be deployed and started");
+         }
+         if( ! isDummyRunning() )
+         {
+            fail("The url: " + dummyWarFile.getAbsolutePath() + " should be accessible, because dummy.war should have been started again by the previous deploy call");
+         }       
+      }
+      catch(Exception e)
+      {
+         fail(e.getMessage());
+      }
+      finally
+      {
+         // remove dummy.war from deploy directory
+         if(dummyWarFile.delete() == false)
+         {
+            fail("Failed to remove: " + dummyWarFile.getAbsolutePath() + " test deployment from: " + JBOSS_DEPLOY);
+         }
+         int attempts = 0;
+         try
+         {
+            do
+            {
+               Thread.sleep(1000);
+               attempts++;
+            } while (deployer.isDeployed(dummyWarFile.getAbsolutePath()) == true && attempts < 5);
+            
+            if(attempts >= 5)
+            {
+               fail("Failed to undeploy: " + dummyWarFile.getAbsolutePath() + " waited 5 seconds and it has not been undeployed");
+            }
+         }
+         catch(Exception e)
+         {
+            
+         }                  
+      }
+   }      
+   
+   //***** Listing Methods *****//
+
+   public void testListDeployedAsString() throws Exception
+   {
+      log.info("+++ testListDeployedAsString");
+      
+      try
+      {
+         deployer.deploy(dummyUrl);
+         log.info("calling assert: " + dummyUrl.toExternalForm());
+         log.info(deployer.listDeployedAsString());
+         assertTrue("listDeployedAsString does not contain: " + dummyUrl, deployer.listDeployedAsString().contains(dummyUrl.toExternalForm()));
+      }
+      catch (Exception e)
+      {
+         super.fail("Caught exception, message: " + e.getMessage());
+      }
+      finally
+      {
+         cleanUp();
+      }
+   }
+      
+   // this tests methods: listDeployed, listDeployedModules
+   public void testListDeployedListDeployedModules() throws Exception
+   {
+      log.info("+++ testListDeployedListDeployedModules");
+      try
+      {
+         // deploy the dummy.war & make sure it is deployed
+         deployer.deploy(dummyUrl);
+         assertTrue("listDeployedAsString does not contain: " + dummyUrl, deployer.listDeployedAsString().contains(dummyUrl.toExternalForm()));
+                                                              
+         if(collectionContains(deployer.listDeployed(), dummyUrl.toExternalForm()) == false)
+            fail("listDeployed does not contain: " + dummyUrl);
+         
+         if(collectionContains(deployer.listDeployedModules(), dummyUrl.toExternalForm()) == false)
+            fail("listDeployedModules does not contain: " + dummyUrl);         
+      }
+      catch (Exception e)
+      {
+         super.fail("Caught exception, message: " + e.getMessage());
+      }
+      finally
+      {
+         cleanUp();
+      }
+   }
+   
+   /** Utility Methods **/
+   
+   private void cleanUp()
+   {
+      try
+      {
+         deployer.undeploy(dummyUrl);
+      }
+      catch(Exception e)
+      {
+         // eat it
+      }
+   }
+   
+   private void setSecurity(Principal username, Object password)
+   {
+      previousSecurity[0] = SecurityAssociation.getPrincipal();
+      previousSecurity[1] = SecurityAssociation.getCredential();
+      
+      SecurityAssociation.setPrincipal(username);
+      SecurityAssociation.setCredential(password);       
+   }
+   
+   /**
+    * Verifies that a given module is acutally deployed. Waits for a while,
+    * checking every few seconds, to see if the module has deployed yet.
+    * 
+    * @param url The URL for which we're testing deployment
+    * @param asString Should we use URL or URL.toExternalForm() (String)?
+    * @return
+    */
+   private boolean isDeployed(URL url, boolean isString)
+         throws Exception
+   {
+      boolean isDeployed = false;
+      for (int tries = 0; tries < 5; tries++)
+      {
+         // sleep for 3 secs
+//         Thread.sleep(3000);
+         if(isString)
+            isDeployed = deployer.isDeployed(url.toExternalForm());
+         else
+            isDeployed = deployer.isDeployed(url);
+
+         if(isDeployed)
+            break;
+      }
+      return isDeployed;
+   }
+   
+   private void twiddle(String args, String searchString)
+   {
+      String command = JBOSS_HOME + "/bin/twiddle.sh " + args;      
+      Process child = null;
+      
+      // default to true since searchString may not be checked 
+      boolean pass = true;
+      try
+      {
+         child = Runtime.getRuntime().exec(command, new String[]{"JBOSS_HOME="+JBOSS_HOME}, null);         
+
+         String st;
+         // get input stream from process
+         BufferedReader in = new BufferedReader(new InputStreamReader(child.getInputStream()));
+
+         String output = "";
+         while ((st = in.readLine()) != null)
+         {
+            output = output + st;
+         }
+         in.close();
+
+         // if a searchString is specified, then the output should contain it else pass will be false
+         if (searchString != null)
+         {
+            pass &= output.contains(searchString);
+         }
+
+         // if exit code is not 0, then we also fail
+         pass &= (child.waitFor() == 0);
+         //         pass &= (child.exitValue() == 0);
+
+         if (!pass)
+            fail(command + " failed, exitCode=" + child.exitValue() + " should be 0 and output should contain: "
+                  + searchString + " output:" + output);
+      }
+      catch (IOException ioe)
+      {
+         log.error("twiddle process failed to execute: " + command, ioe);
+         fail("twiddle process failed to execute: " + command);
+      }
+      catch (InterruptedException e)
+      {
+         log.error("twiddle process failed to finish: " + command, e);
+         fail("twiddle process failed to finish: " + command);
+      }
+      finally
+      {
+         if (child != null)
+            child.destroy();
+      }
+   }
+   
+   private void copyUrlToFile(URL url, File file) throws Exception
+   {  
+      log.info("Copying url: " + url + " to file: " + file);
+      InputStream is = url.openStream();            
+      FileOutputStream fos = new FileOutputStream(file);
+      
+      byte[] buf = new byte[1024];
+      int len;
+      while ((len = is.read(buf)) > 0)
+      {                       
+         fos.write(buf, 0, len);
+      }
+            
+      is.close();      
+      fos.close();      
+   }
+   
+   private boolean collectionContains(Collection collection, String string)
+   {      
+      for(Object o : collection)
+      {
+         log.info(o.toString());
+         if(o.toString().contains(string))
+         {
+            log.debug(o.toString() + " matched " + string);
+            return true;            
+         }
+      }
+      return false;
+   }
+   
+   private boolean isDummyRunning()
+   {
+      try
+      {
+         String content = readURL(dummyIndexUrl, false);
+         log.info("dummy.war content: " + content);
+         if(content != null && content.contains("Test html"))
+         {
+            log.info("dummy.war is running at: " + dummyIndexUrl);
+            return true;
+         }
+      }
+      catch(Exception e)
+      {
+         fail("Failed to test if dummy.war is running: " + e.getMessage());
+      }
+      
+      log.info("dummy.war is not running at: " + dummyIndexUrl);
+      return false;      
+   }
+   
+   private String readURL(URL url, boolean failOnException)
+   {                
+      BufferedReader in = null;
+      try
+      {
+         URLConnection uc = url.openConnection();  
+         in = new BufferedReader( new InputStreamReader(uc.getInputStream()));
+         
+         String inputLine = in.readLine();            
+         return inputLine;
+      }
+      catch (Exception e)
+      {         
+         if(failOnException)
+         {
+            log.error("Error occured trying to read: " + url, e);
+            fail("Unable to read url: " + url);
+         }
+         return null;
+      }
+      finally
+      {
+         if ( in != null )
+         {
+            try
+            {
+               in.close();   
+            }
+            catch (Exception e)
+            {
+               // eat it
+            }
+         }            
+      }
+   }
+}
\ No newline at end of file



More information about the jboss-cvs-commits mailing list