SOC Lab Manual
SOC Lab Manual
Faster deployments are being done but the quality of code has taken a
backseat
Several issues are reported and the team is doing a lot of re-work.
It is increasingly becoming difficult to bring in changes to the design and
code
The coach suggests the usage of a code analysis tool (static and dynamic) to
ensure that developers check in only quality code which ensure which will help
mitigate the mentioned challenges. Let us understand the static code analysis,
how it helps ensure quality of code with the help of a static analysis tool like
Sonarqube.
Need
In the name of fast deployment, code quality should not be compromised. This
will have long lasting effects on speed and will bring the productivity to zero.
Hence only quality code needs to be pushed to the version control system. This
applies to all code – source code, test code, automated scripts etc.
The development team needs to fix the issues to ensure the technical debt is
removed. This is usually performed after coding, compilation and build of
source code. CheckStyle, FindBugs, Sonar Qube, PMD are examples of static
code analysis.
Technical debt
Sonarqube features
Sonarqube is a web based open-source tool to manage code quality. It has the
following features -
Working of sonarqube
6. Quality gates can be applied to ensure that code that does not pass the quality
conditions do not move forward to the next stage.
Each rule is associated with rework effort. If the rule is violated it adds to
the rework effort.
Total original effort:
Practical tips
Create profiles with increasing number of rules so that teams are not
overwhelmed with too many rules in the beginning
A mix of tools can be used to check quality
It is important for the teams to fix issues at the earliest and improve the
quality
Issues can be resolved based on priority. The categories of issues can be
seen in sonarqube dashboard
The development team at "Pura Vida" will have their challenges mitigated with
Sonarqube for the following reasons:
Code quality will be ensured from design and clean coding perspectives
which will go a long way in ensuring that code is maintainable and able
to adapt to changes quickly
This will go a long way in ensuring code quality with speed
Exercise-6
Implementation of CICD with java & open-source stack.
-Write a build script to build the application using a build automation tool
loke maven. Create a folder structure that will run the build script &
invoke the various software development build stages. This script should
invoke the static analysis tool & unit test cases and deploy the application
to a web application server like Tomcat.
Build Automation:
Developers are paid to invest their effort in solving business problems by
developing software programs to build a software system. In the process of
building software system, developers have to do following technical activities
periodically several times in a day/week.
Getting latest version of program files from configuration server.
Compiling the programs.
Performing static analysis on program/code to get feedback on design.
All the above steps are a consolidation of Build Process of a software system.
Instead, by automating above activities, developers can effort on solving
business problems by devising best in class algorithm. In order to achieve the
above automation, we adopt a process called build-automation. Primary goal of
Build Automation is to automate manual operations in the build process.
Compilation of code
Testing and integrating the changes
Packaging the binaries
Deployment to test server
Maven-a build automation tool:-
Maven features:
Workflow of Maven:
Maven has few built-in lifecycle goals like clean, compile etc. The other user
defined targets and project related configurations are specified in the pom.xml
in the project. When a build is run, Maven executes a set of goals.
Maven can be considered as a tool that can be used for building and managing
any java-based project.
Explanation for important tags in the below POM.xml are given with ‘->’
symbol.
Maven architecture is basically based on assembly of various Plug-Ins
thus making Maven having a modular architecture.
Maven benefits:
Maven is intended to make the day-to-day work of Java developers easier
and generally help with the comprehension of any java-based project.
Maven has many in-built templates and using Maven has multi-fold
benefits and some of them are:
Creates Project Source code template that defines a Project Structure that
brings in the following advantages:
Clear separation of concerns in development
Discipline in coding
Import all the necessary libraries that are dependencies to create an
application
Build and package the application by compiling the source code and
package the code into a .jar file.
Practical tips:
Build every time a change is checked in
Ensure that the steps of static/dynamic analysis, unit testing, code coverage
etc are also included
If the build is broken, the development team needs to fix it immediately
You can see the demonstration of usage of Maven for orchestration of build
lifecycle activities in the subsequent section.
Test automation is the use of special software (separate from the software
being tested) to control the execution of tests and the comparison of actual
outcomes with predicted outcomes. Automated code coverage ensures -
JUnit
NUnit
MSTest
Cobertura
Emma
JaCoCo
JUnit features:
Is an open source Java testing framework used to write and run repeatable
tests
Is an instance of the xUnit architecture for unit testing frameworks
Features-
o Assertions for testing expected results
o Test fixtures for sharing common test data
o Test suites for easily organizing and running tests
o Graphical and textual test runners
JUnit is an open-source Java testing framework used to write and run repeatable
automated test cases. It is the Java version of xUnit architecture for unit and
regression testing frameworks written by Erich Gamma and Kent Beck. Here
are the common terms used -
JUnit benefits:
JUnit runs a suite of tests and reports results. For each test in the test
suite:
o Calls setup(). This method should create any object that you may
need for testing
o Calls tearDown(). This method should remove any objects you
created
JUnit calls one test method. This method may comprise multiple tests. i.e,
it may make multiple calls to the method you are testing.
Each test method checks a condition (assertion) and returns back to the
test runner whether the test failed or succeeded.
The test runner uses the result to report to the user. The report indicates
the tests which passed(green), tests which failed(red) and overall status.
JaCoCo is an open source code coverage tool which can be plugged into
Eclipse. JaCoCo is invoked through build scripts.
JaCoCo features:
Free Java tool that calculates the percentage of code accessed by tests
Useful to identify parts of the Java code lacking testing
Can be called using Ant and Maven scripts
JaCoCo will generate the detailed reports that shows different coverage related
parameters like line coverage, method coverage, class coverage and so on. Here
are the snapshots of the reports generated by JaCoCo.