The Usual Suspects Creating a Cloud Development Environment with Sonar, Selenium and JMeter on OpenShift Origin Jorge Hidalgo Senior Technology Architect Accenture Delivery Centre in Spain – Java Lead 1 The Usual Suspects – Jorge Hidalgo The Usual Suspects Creating a Cloud Development Environment with Sonar, Selenium and JMeter on OpenShift Origin Abstract: OpenShift has already established itself as a great platform to run Java applications (plus many other technologies) in the Cloud. But OpenShift also offers the foundation for a development environment, including source control with Git, build automation with Maven and continuous integration with Jenkins. In this session, we will learn how to extend the basic development environment and create a more powerful environment on OpenShift featuring “usual suspects” such as Sonar for continuous quality assurance, Selenium for functional testing, JMeter for performance/load testing as well as Arquillian for in-container testing. The session includes a live demo built on OpenShift Origin. Speaker: Jorge Hidalgo – Senior Technology Architect Accenture Delivery Centre in Spain – Java Lead Author of dr. macphail’s trance blog: http://deors.wordpress.com 2 The Usual Suspects – Jorge Hidalgo The Usual Suspects What is a Development Environment? 3 The Usual Suspects – Jorge Hidalgo The Usual Suspects How Cloud Platforms can be of help? 4 The Usual Suspects – Jorge Hidalgo The Usual Suspects • Agenda – – – – – – – 5 Understanding the Basics – Git and Apache Maven Continuous Integration with Jenkins Continuous Quality Assurance with Sonar Functional, Cross-browser Tests with Selenium WebDriver Performance/Load Tests with Apache JMeter In-Container Tests with Arquillian Q&A The Usual Suspects – Jorge Hidalgo 00: Understanding the Basics – Git and Apache Maven 6 The Usual Suspects – Jorge Hidalgo Understanding the Basics • Each application in OpenShift comes with a Git repository. – • Stores sources, configuration, binaries and scripts. Each push action to the repository triggers a new build and deploy process. – Details on what means “build and deploy” depends on the type of application, and can be customised with action hooks. git push origin master 7 The Usual Suspects – Jorge Hidalgo action hooks for a DIY app Understanding the Basics • For Java applications (jbossas-7 type) builds are managed with Apache Maven. – – • Isolated configuration. Isolated local repository. Lifecycle standardisation. validate package verify 8 compile test integration-test install deploy The Usual Suspects – Jorge Hidalgo action hooks for a jbossas-7 app Preparing Pet Clinic • We are using Spring Pet Clinic reference application as test subject. – – 9 Resource redirection fixed to work with Tomcat 7 and JBoss 7 Added some Selenium and Apache JMeter test scripts The Usual Suspects – Jorge Hidalgo Preparing Pet Clinic • Create a new JBoss application: Command line >rhc app create -a petclinic -t jbossas-7 UUID: a74be67eaafb4ced969a279ce3082e03 Git URL: ssh://a74be67eaafb4ced969a279ce3082e03@petclinicdemo.example.com/~/git/petclinic.git/ Public URL: http://petclinic-demo.example.com/ 10 The Usual Suspects – Jorge Hidalgo Preparing Pet Clinic • Add MySQL cartridge: Command line >rhc app cartridge add -a petclinic -c mysql-5.1 Root User: admin Root Password: Uvvp2ZZ_nBzn Database Name: petclinic Connection URL: mysql://127.0.250.129:3306/ 11 The Usual Suspects – Jorge Hidalgo Preparing Pet Clinic • Merge Pet Clinic sources and configuration. • Load schema and initial data: – Connect your favourite database client to the MySQL cartridge: • • – • 12 Run the scripts at: src/main/resources/db/mysql Alternatively: – • OpenShift: redirect ports with command rhc port-forward. Origin: use the client directly in the Origin box. Run scripts on app start and disable them next time here: src/main/resources/applicationContext-dataSource.xml Commit and push. The Usual Suspects – Jorge Hidalgo Preparing Pet Clinic 13 The Usual Suspects – Jorge Hidalgo 01: Continuous Integration with Jenkins 14 The Usual Suspects – Jorge Hidalgo Continuous Integration • OpenShift offers easy configuration of Jenkins, the popular Continuous Integration engine. Jenkins dashboard runs as another application within our domain. Jenkins consumes one gear. – For each application managed with Jenkins a especial type of Java application is created: the builder. Builders consume gears. – • Why add Jenkins? – – 15 Centralized build reports: status, error causes, which version is deployed, change log, etc. Better control to build process, e.g. add code profiling, add integration tests. The Usual Suspects – Jorge Hidalgo Jenkins Installation and Configuration • Create a new Jenkins application: Command line >rhc app create -a jenkins -t jenkins-1.4 UUID: 810197679f614bee8414b0fb701f50fc Git URL: ssh://810197679f614bee8414b0fb701f50fc@jenkinsdemo.example.com/~/git/jenkins.git/ Public URL: http://jenkins-demo.example.com/ 16 The Usual Suspects – Jorge Hidalgo Jenkins Installation and Configuration • Add build job (and builder app) for Pet Clinic: Command line >rhc app cartridge add -a petclinic –c jenkins-client-1.4 Job URL: https://jenkins-demo.example.com/job/petclinicbuild/ 17 The Usual Suspects – Jorge Hidalgo Jenkins Installation and Configuration 18 The Usual Suspects – Jorge Hidalgo 02: Continuous Quality Assurance with Sonar 19 The Usual Suspects – Jorge Hidalgo Continuous Quality Assurance • Sonar is a popular code profiler and dashboard that excels when used along a Continuous Integration engine: Seamless integration with Maven. – Leverages best-of-breed tools as Checkstyle, PMD or FindBugs. – Configurable quality profiles. – Re-execution of tests and test code coverage (UT, IT). – Design Structure Matrix analysis. – Flexible and highly customisable dashboard. – Actions plans / peer reviews. – Historic views / run charts. – Can be used with Java, .Net, C/C++, Groovy, PHP,... – 20 The Usual Suspects – Jorge Hidalgo Sonar Installation and Configuration • Create a new DIY application: Command line >rhc app create -a sonar -t diy-0.1 UUID: 4cd9eacb71b34401b81ea712352ffccb Git URL: ssh://4cd9eacb71b34401b81ea712352ffccb@sonardemo.example.com/~/git/sonar.git/ Public URL: http://sonar-demo.example.com/ 21 The Usual Suspects – Jorge Hidalgo Sonar Installation and Configuration • Add MySQL cartridge: Command line >rhc app cartridge add -a sonar -c mysql-5.1 Root User: admin Root Password: VyikSHzlLvjx Database Name: sonar Connection URL: mysql://127.0.251.129:3306/ 22 The Usual Suspects – Jorge Hidalgo Sonar Installation and Configuration • Unzip Sonar distribution to app root folder. • Update conf/sonar.properties with settings for MySQL cartridge and bind to OpenShift internal IP and port: File: /sonar/sonar-3.1.1/conf/sonar.properties sonar.web.host: 127.0.251.129 sonar.web.port: 8080 sonar.web.context: / sonar.jdbc.username: admin sonar.jdbc.password: VyikSHzlLvjx 23 sonar.jdbc.driver: com.mysql.jdbc.Driver sonar.jdbc.url: jdbc:mysql://127.0.251.129:3306/sonar ?useUnicode=true&characterEncoding=utf8 &rewriteBatchedStatements=true The Usual Suspects – Jorge Hidalgo Sonar Installation and Configuration • Update action hooks with the appropriate commands to start and stop Sonar: File: /sonar/.openshift/action_hooks/start #!/bin/bash cd $OPENSHIFT_REPO_DIR/sonar-3.1.1/bin/linux-x86-64 chmod 755 wrapper sh ./sonar.sh start File: /sonar/.openshift/action_hooks/stop #!/bin/bash cd $OPENSHIFT_REPO_DIR/sonar-3.1.1/bin/linux-x86-64 sh ./sonar.sh stop 24 The Usual Suspects – Jorge Hidalgo Sonar Installation and Configuration • Add this to Pet Clinic build job to trigger the Sonar analysis before packaging and deploying the application: Jenkins job: petclinic-build mvn --global-settings $OPENSHIFT_MAVEN_MIRROR clean test export SONAR_JDBC="-Dsonar.jdbc.url=jdbc:mysql://127.0.251.129:3306/sonar ?useUnicode=true&characterEncoding=utf8" export SONAR_DRIVER=-Dsonar.jdbc.driver=com.mysql.jdbc.Driver export SONAR_DBUSER=-Dsonar.jdbc.username=admin export SONAR_DBPASS=-Dsonar.jdbc.password=VyikSHzlLvjx export SONAR_URL=-Dsonar.host.url=http://sonar-demo.example.com export SONAR_OPTS="$SONAR_JDBC $SONAR_DRIVER $SONAR_DBUSER $SONAR_DBPASS $SONAR_URL" mvn --global-settings $OPENSHIFT_MAVEN_MIRROR sonar:sonar $SONAR_OPTS 25 The Usual Suspects – Jorge Hidalgo Sonar Installation and Configuration 26 The Usual Suspects – Jorge Hidalgo 03: Functional, Cross-browser Tests with Selenium WebDriver 27 The Usual Suspects – Jorge Hidalgo Functional Tests • Selenium is a popular framework / tool that allows, for web based applications, to record, re-execute and distribute tests across multiple browsers and computers. Because Java-only tests may not be enough. – Can run with HtmlUnit to simulate a real browser (useful to gather code coverage metrics). – Can run with local browsers or distributed across computers in a grid. – The grid can be hosted in OpenShift, but browsers cannot – need dedicated machines with a desktop session that will act as test nodes. – You can use your Origin box as both hub and test node. – 28 The Usual Suspects – Jorge Hidalgo Selenium Grid Hub Installation and Configuration • Create a new DIY application: Command line >rhc app create -a seleniumhub -t diy-0.1 UUID: ddd8d0a2182b4b6799ec7c7ce8ae62b8 Git URL: ssh://ddd8d0a2182b4b6799ec7c7ce8ae62b8@ seleniumhub-demo.example.com/~/git/seleniumhub.git/ Public URL: http://seleniumhub-demo.example.com 29 The Usual Suspects – Jorge Hidalgo Selenium Grid Hub Installation and Configuration • Copy selenium-server-standalone-2.25.0.jar (or other version) to folder selenium-2.25.0. • Update action hooks with appropriate commands to start and stop the hub: File: /seleniumhub/.openshift/action_hooks/start #!/bin/bash nohup java -jar $OPENSHIFT_REPO_DIR/selenium-2.25.0/ selenium-server-standalone-2.25.0.jar -role hub -host $OPENSHIFT_INTERNAL_IP -port $OPENSHIFT_INTERNAL_PORT >>$OPENSHIFT_LOG_DIR/seleniumhub.log 2>&1 & File: /seleniumhub/.openshift/action_hooks/stop #!/bin/bash kill `ps -ef | grep selenium | grep -v grep | awk '{ print $2 }'` > /dev/null 2>&1 exit 0 30 The Usual Suspects – Jorge Hidalgo Selenium Grid Hub Installation and Configuration • Update Maven target in Pet Clinic build job to launch Selenium tests with HtmlUnit to gather code coverage: Jenkins job: petclinic-build BEFORE mvn --global-settings $OPENSHIFT_MAVEN_MIRROR clean test ... Sonar settings ... mvn --global-settings $OPENSHIFT_MAVEN_MIRROR sonar:sonar $SONAR_OPTS Jenkins job: petclinic-build AFTER mvn --global-settings $OPENSHIFT_MAVEN_MIRROR clean verify –Pcargo-tomcat,selenium-tests ... Sonar settings ... mvn --global-settings $OPENSHIFT_MAVEN_MIRROR sonar:sonar $SONAR_OPTS 31 The Usual Suspects – Jorge Hidalgo Selenium Grid Hub Installation and Configuration • Add this at the end of Pet Clinic build job to launch Selenium tests with real browsers once the app is deployed: Jenkins job: petclinic-build export SELENIUM_BROWSERS="-Dtest.run.htmlunit=false -Dtest.run.firefox=true -Dtest.run.chrome=true" export SELENIUM_HUB="-Dtest.selenium.hub.url= http://seleniumhub-demo.example.com/wd/hub" export SELENIUM_TARGET="-Dtest.target.server.url= http://petclinic-demo.example.com" export SELENIUM_OPTS="$SELENIUM_BROWSERS $SELENIUM_HUB $SELENIUM_TARGET" mvn --global-settings $OPENSHIFT_MAVEN_MIRROR failsafe:integration-test –Pselenium-tests $SELENIUM_OPTS 32 The Usual Suspects – Jorge Hidalgo Pet Clinic Advanced Settings • What is the cargo-tomcat Maven profile doing? – Provisions a Tomcat 7 container before ITs are executed. Tomcat runs with JaCoCo agent to gather coverage metrics. Stops the Tomcat 7 container after ITs are executed. – Instead of Tomcat, we could use Glassfish, JBoss, Jetty... – – 33 The Usual Suspects – Jorge Hidalgo Pet Clinic Advanced Settings • What is the selenium-tests Maven profile doing? – • 34 Configures Maven’s Failsafe plug-in to execute Selenium integration tests during Maven’s integration-test phase. Tell Sonar where to find JaCoCo report. The Usual Suspects – Jorge Hidalgo Pet Clinic Advanced Settings • How are we dealing with different configuration for local and cloud execution? By default, assumes local execution (so developers can test within their IDEs). Local settings also work in CI server. – Leverage openshift profile to copy configuration files over default ones when packaging the app for OpenShift. – 35 The Usual Suspects – Jorge Hidalgo Selenium in Action 36 The Usual Suspects – Jorge Hidalgo Integration Tests Coverage as Seen by Sonar 37 The Usual Suspects – Jorge Hidalgo 04: Performance/Load Tests with Apache JMeter 38 The Usual Suspects – Jorge Hidalgo Performance/Load Tests • Performance/Load Tests purpose is to verify application performance under stress: – – 39 Capable of handling multiple requests at the same time. Well designed and implemented for concurrency. • Adding Load Tests to continuous integration allows for early detection of concurrency and performance issues. • Engineers/Architects may define thresholds to prevent deployments of non performant builds. The Usual Suspects – Jorge Hidalgo Performance/Load Tests • Apache JMeter is a veteran framework / tool that allows to record, define and re-execute a wide variety of integration tests: HTTP requests. – Web Services (SOAP, JAX-RPC) requests. – And many more. – • JMeter is capable of running tests concurrently using multiple threads: – – 40 Locally, limited due to the amount of available resources. Remotely, using JMeter Server on multiple test nodes (load injectors). The Usual Suspects – Jorge Hidalgo Adding JMeter Tests to Jenkins and Sonar • Update Maven target in Pet Clinic build job to launch JMeter tests and save them in a known place for Sonar: Jenkins job: petclinic-build BEFORE mvn --global-settings $OPENSHIFT_MAVEN_MIRROR clean verify -Pcargo-tomcat,selenium-tests Jenkins job: petclinic-build AFTER mvn --global-settings $OPENSHIFT_MAVEN_MIRROR clean verify -Pcargo-tomcat,selenium-tests,jmeter-tests 41 The Usual Suspects – Jorge Hidalgo Pet Clinic Advanced Settings • What is the jmeter-tests Maven profile doing? – • 42 Configures Maven’s JMeter plug-in to execute JMeter tests during Maven’s integration-test phase. Tell Sonar where to find JMeter report. The Usual Suspects – Jorge Hidalgo JMeter Execution Report as Seen by Sonar 43 The Usual Suspects – Jorge Hidalgo 05: In-Container Tests with Arquillian 44 The Usual Suspects – Jorge Hidalgo In-Container Tests • There is a gap between ‘strict’ unit tests (method or class level, in isolation) and functional integration tests (focused on user interactions). • Another type of tests, called unit-integration tests or incontainer tests, bridge that gap and help testing application components: – – 45 Within a container, using the services the container provides. Key to properly test features like: AOP transactions, AOP error handling, CDI bean injection, etc. The Usual Suspects – Jorge Hidalgo In-Container Tests • Arquillian is a popular in-container test framework, recently born but with great traction in the community. • Key features of Arquillian include: – – – 46 It’s open and modular – very easy to add extensions and new features. Allows for cross-container tests with the help of Maven profiles – very convenient if you want to test your components in multiple containers (Weld, JBoss, Glassfish...). Makes all the plumbing for you – prepares deployable units and puts them into the container of choice, either embedded (within the same JVM) or not (local / remote containers). The Usual Suspects – Jorge Hidalgo Preparing Arquillian Test Application • Create a new JBoss application: Command line >rhc app create -a arqtest -t jbossas-7 UUID: 0690fabf2e3549abb746a911d8087227 Git URL: ssh://0690fabf2e3549abb746a911d8087227@arqtestdemo.example.com/~/git/arqtest.git/ Public URL: http://arqtest-demo.example.com/ 47 • Merge sources and configuration from test application. • Using some examples from Arquillian web site. The Usual Suspects – Jorge Hidalgo Preparing Arquillian Test Application • Add build job (and builder app) for Arqtest: Command line >rhc app cartridge add -a arqtest -c jenkinsclient-1.4 Job URL: https://jenkins-demo.example.com/job/arqtestbuild/ 48 The Usual Suspects – Jorge Hidalgo Preparing Arquillian Test Application • Add this to Arqtest build job to launch Arquillian tests: Jenkins job: arqtest-build BEFORE mvn --global-settings $OPENSHIFT_MAVEN_MIRROR --version mvn --global-settings $OPENSHIFT_MAVEN_MIRROR clean package –Popenshift -DskipTests Jenkins job: arqtest-build AFTER mvn --global-settings $OPENSHIFT_MAVEN_MIRROR --version mvn --global-settings $OPENSHIFT_MAVEN_MIRROR clean test -Parquillian-glassfish-embedded mvn --global-settings $OPENSHIFT_MAVEN_MIRROR clean package –Popenshift -DskipTests • 49 Commit and push. The Usual Suspects – Jorge Hidalgo Running Arqtest Build Job • 50 Arqtest build job will execute tests with a Glassfish embedded container: The Usual Suspects – Jorge Hidalgo The Usual Suspects – Wrap up 51 • Platforms like OpenShift enable development teams to build and use powerful, best-of-breed development tools. • Easy access to the best software engineering practices. • With Origin, you get all the tools you need in the same box. Deploy it locally or in your in-house infrastructure. • Excellent for individuals and small development teams... • ...But if you work in a larger team and want more power, why not deploy the same in OpenShift? The Usual Suspects – Jorge Hidalgo Q&A 52 The Usual Suspects – Jorge Hidalgo