Showing posts with label Code quality. Show all posts
Showing posts with label Code quality. Show all posts

Saturday, September 14, 2024

What is the difference between Code Coverage and Code Analysis

Both code coverage and code analysis are methods used to improve software quality, but they differ in their approach, goals, and implementation. Here's a detailed comparison:

Code Coverage:

  • Definition: Code coverage is a metric that measures the amount of code that is executed during automated tests.
  • Purpose: The main goal is to ensure that the test suite exercises as much of the codebase as possible, helping to identify untested areas.
  • How it Works:
    • Tools run the tests and track which parts of the code (lines, branches, functions) are executed.
    • Coverage is expressed as a percentage indicating how much of the code has been executed during tests.
  • Metrics Tracked:
    • Line Coverage: How many lines of code are executed.
    • Branch Coverage: How many decision points (e.g., if statements) are tested.
    • Function Coverage: How many functions or methods are invoked during tests.
  • Example Tools: JaCoCo, Coverage.py, Istanbul.
  • Output: Reports showing which portions of the code are covered by tests and which are not.
  • Use Case: Primarily used during the testing phase to gauge the extent to which the code is exercised by tests.

Code Analysis:

  • Definition: Code analysis is a technique used to evaluate the quality, structure, and potential errors in the codebase. It can be performed either statically (without executing the code) or dynamically (during runtime).
  • Types of Code Analysis:
    • Static Code Analysis: Inspects the source code for issues such as coding standards violations, potential bugs, or security vulnerabilities without running the code.
    • Dynamic Code Analysis: Involves analyzing the behavior of the code during execution, often looking for performance issues, memory leaks, or runtime errors.
  • Purpose: The goal is to find potential problems or improve code quality by identifying security risks, performance bottlenecks, or areas that violate best practices.
  • How it Works:
    • Static analysis tools scan the code for patterns that match predefined rules (e.g., code smells, unused variables).
    • Dynamic analysis tools monitor the code while it's running to observe its actual behavior.
  • Example Tools:
    • Static Analysis: SonarQube, Pylint, ESLint.
    • Dynamic Analysis: Valgrind, Dynatrace.
  • Output: Reports showing potential bugs, security vulnerabilities, code smells, or violations of coding standards.
  • Use Case: Can be performed during development to ensure code quality and prevent issues from entering production.

Wednesday, April 5, 2023

SonarQube Server Cannot be reached Error Resolution | SonarQube Troubleshooting | Fix for max virtual memory areas vm.max_map_count [65530] is too low

 How to Resolve SonarQube Server cannot be reached Error?

Pre-requisites:

refer this page for pre-requisites to install SonarQube


Resolution:

Make sure EC2 instance or server where you are installing SonarQube has enough virtual memory and open file limits for  Linux OS.

How to change default value for vm.max_map_count temporarily
sudo sysctl vm.max_map_count=262144
sudo sysctl fs.file-max=65536
The above command will load the max_map_count values and open file limits till the next system restart.

How to update vm.max_map_count directly in sysctl.conf permanently
Login to instance where you will be installing SonarQube, perform the below command to configure virtual memory permanently for SonarQube to function:
sudo vi /etc/sysctl.conf

Add the following lines to the bottom of that file:

vm.max_map_count=262144
fs.file-max=65536

To make sure changes are getting into effect:
sudo sysctl -p

Make sure SonarQube is up and running by checking the logs
sudo docker-compose logs --follow


Once you see the message, that's it. SonarQube have been configured successfully. press control C and enter.

Watch Steps in YouTube channel: 

Monday, January 3, 2022

Install SonarQube using Docker | Install SonarQube using Docker on Ubuntu 18.0.4 | Install SonarQube using Docker-Compose

How to setup SonarQube using Docker and Docker compose?

SonarQube is static code analyis tool. It can be installed quickly using Docker with less manual steps.

Pre-requistes:

  • Ubuntu EC2 up and running with at least t2.small
  • Port 9000 is opened in security firewall rule
  • Make sure maximum number of 

sudo vi /etc/sysctl.conf

Add the following lines to the bottom of that file:

vm.max_map_count=262144
fs.file-max=65536

To make sure changes are getting into effect:

sudo sysctl -p

Install Docker
sudo apt-get install docker -y

Install Docker-Compose
sudo apt-get install docker-compose -y

What is Docker Compose?
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
 
The purpose of docker-compose is to function as docker cli but to issue multiple commands much more quickly. To make use of docker-compose, you need to encode the commands you were running before into a docker-compose.yml file
 
Run docker-compose up and Compose starts and runs your entire app.

Create docker-compose.yml
this yml has all configuration for installing both SonarQube and Postgresql:
sudo vi docker-compose.yml 

(Copy the below code high-lighted in yellow color)
version: "3"

services:
  sonarqube:
    image: sonarqube:lts-community
    container_name: sonarqube
    restart: unless-stopped
    environment:
      - SONARQUBE_JDBC_USERNAME=sonar
      - SONARQUBE_JDBC_PASSWORD=password123
      - SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonarqube
    ports:
      - "9000:9000"
      - "9092:9092"
    volumes:
      - sonarqube_conf:/opt/sonarqube/conf
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_bundled-plugins:/opt/sonarqube/lib/bundled-plugins

  db:
    image: postgres:12
    container_name: db
    restart: unless-stopped
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=password123
      - POSTGRES_DB=sonarqube
    volumes:
      - sonarqube_db:/var/lib/postgresql10
      - postgresql_data:/var/lib/postgresql10/data

volumes:
  postgresql_data:
  sonarqube_bundled-plugins:
  sonarqube_conf:
  sonarqube_data:
  sonarqube_db:
  sonarqube_extensions:

Save the file by entering :wq!

Now execute the compose file using Docker compose command:
sudo docker-compose up -d 


If you are getting any errors like this, make sure you exit below commands to adding Docker group to current user.

sudo usermod -aG docker $USER

Make sure SonarQube is up and running
sudo docker-compose logs --follow



Once you see the message, that's it. SonarQube is been installed successfully. press control C and enter.
Now access sonarQube UI by going to browser and enter public dns name with port 9000

Please follow steps for integrating SonarQube with Jenkins
https://www.coachdevops.com/2020/04/how-to-integrate-sonarqube-with-jenkins.html

Wednesday, January 20, 2021

Install SonarQube 8 on Ubuntu | How to setup SonarQube 8 on Ubuntu 18.0.4?

SonarQube is one of the popular static code analysis tools. SonarQube enables developers to write cleaner, safer code. SonarQube is open-source, Java based tool. SonarQube uses database for storing analysis results. Database can be MS SQL, Oracle or PostgreSQL.  We will use PostgreSQL as it is open source as well.

Please find steps for installing SonarQube on Ubuntu 18.0.4 in AWS Cloud. Make sure port 9000 is opened in security group(firewall rule).

Pre-requisites:
Instance should have at least 2 GB RAM. For AWS, instance should be atleast t2.small

Watch the steps in YouTube:
 
SonarQube Architecture

SonarQube have three components namely
1. Scanner - This contains scanner and analyser to scan application code.
2. SonarQube server - contains Webserver(UI) and search server 
3. DB server - used for storing the analysis reports.

Let us start with java install (skip java install if you already have it installed)

Install Open JDK 11
sudo apt-get update && sudo apt-get install default-jdk -y

Postgres Installation

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
 
 
 
sudo wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -

 

sudo apt-get -y install postgresql postgresql-contrib


Ignore the message in red color below:

sudo systemctl start postgresql
sudo systemctl enable postgresql

Login as postgres user
sudo su - postgres

Now create a user below by executing below command
createuser sonar

9. Switch to sql shell by entering
psql







Execute the below three lines (one by one)

ALTER USER sonar WITH ENCRYPTED password 'password';

CREATE DATABASE sonarqube OWNER sonar;

 GRANT ALL PRIVILEGES ON DATABASE sonarqube to sonar;

\q





type exit to come out of postgres user.




3. Download SonarQube and Install

sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.6.0.39681.zip

sudo apt-get -y install unzip
sudo unzip sonarqube*.zip -d /opt





sudo mv /opt/sonarqube-8.6.0.39681 /opt/sonarqube -v



Create Group and User:
sudo groupadd sonarGroup

Now add the user with directory access
sudo useradd -c "user to run SonarQube" -d /opt/sonarqube -g sonarGroup sonar 
sudo chown sonar:sonarGroup /opt/sonarqube -R

Modify sonar.properties file
sudo vi /opt/sonarqube/conf/sonar.properties
uncomment the below lines by removing # and add values highlighted yellow
sonar.jdbc.username=sonar
sonar.jdbc.password=password





Next, Add the below line
sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube

 
 
 
Press escape, and enter :wq! to come out of the above screen.

Edit the sonar script file and set RUN_AS_USER
sudo vi /opt/sonarqube/bin/linux-x86-64/sonar.sh
Add enable the below line 
RUN_AS_USER=sonar







Create Sonar as a service(this will enable to start automatically when you restart the server)

Execute the below command:

sudo vi /etc/systemd/system/sonar.service











add the below code in green color:
[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=forking

ExecStart=/opt/sonarqube/bin/l
inux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/li
nux-x86-64/sonar.sh stop
LimitNOFILE=131072
LimitNPROC=8192
User=sonar
Group=sonarGroup
Restart=always


[Install]
WantedBy=multi-user.target

Save the file by entering :wq!
 
Kernel System changes
we must make a few modifications to a couple of kernel system limits files for sonarqube to work.
sudo vi /etc/sysctl.conf

Add the following lines to the bottom of that file:

vm.max_map_count=262144
fs.file-max=65536
 

Next, we're going to edit limits.conf. Open that file with the command:

sudo vi /etc/security/limits.conf
At the end of this file, add the following: 

sonar   -   nofile   65536
sonar   -   nproc    4096


Reload system level changes without server boot
sudo sysctl -p

Start SonarQube Now
sudo systemctl start sonar

sudo systemctl enable sonar

sudo systemctl status sonar
type q now to come out of this mode.
Now execute the below command to see if Sonarqube is up and running. This may take a few minutes.
 
check the Sonar logs to make sure there is no error:

tail -f /opt/sonarqube/logs/sonar*.log

Make sure you get the below message that says sonarqube is up..

Now access sonarQube UI by going to browser and enter public dns name with port 9000

Please follow steps for integrating SonarQube with Jenkins

https://www.coachdevops.com/2020/04/how-to-integrate-sonarqube-with-jenkins.html

Thursday, May 7, 2020

Install SonarQube using Docker | Install SonarQube using Docker on Ubuntu 18.0.4 | Install SonarQube using Docker-Compose

SonarQube is code quality tool. It can be installed quickly using Docker with less manual steps.

How to setup Sonarqube using Docker and Docker compose?

Pre-requisites:
9000 is opened security firewall rules

Install Docker
sudo apt-get install docker -y

Install Docker-Compose
sudo apt-get install docker-compose -y

What is Docker Compose?
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
 
The purpose of docker-compose is to function as docker cli but to issue multiple commands much more quickly. To make use of docker-compose, you need to encode the commands you were running before into a docker-compose.yml file
 
Run docker-compose up and Compose starts and runs your entire app.

Create docker-compose.yml
this yml has all configuration for installing both SonarQube and Postgresql:
sudo vi docker-compose.yml 

(Copy the below code high-lighted in yellow color)
version: "3"

services:
  sonarqube:
    image: sonarqube:6.7.1
    container_name: sonarqube
    restart: unless-stopped
    environment:
      - SONARQUBE_JDBC_USERNAME=sonar
      - SONARQUBE_JDBC_PASSWORD=password123
      - SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonarqube
    ports:
      - "9000:9000"
      - "9092:9092"
    volumes:
      - sonarqube_conf:/opt/sonarqube/conf
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_bundled-plugins:/opt/sonarqube/lib/bundled-plugins

  db:
    image: postgres:10.1
    container_name: db
    restart: unless-stopped
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=password123
      - POSTGRES_DB=sonarqube
    volumes:
      - sonarqube_db:/var/lib/postgresql10
      - postgresql_data:/var/lib/postgresql10/data

volumes:
  postgresql_data:
  sonarqube_bundled-plugins:
  sonarqube_conf:
  sonarqube_data:
  sonarqube_db:
  sonarqube_extensions:

Save the file by entering :wq!

Now execute the compose file using Docker compose command:
sudo docker-compose up -d 


If you are getting any errors like this, make sure you exit below commands to adding Docker group to current user.

sudo usermod -aG docker $USER

Make sure SonarQube is up and running
sudo docker-compose logs

Once you see the message, that's it. SonarQube is been installed successfully.
Now access sonarQube UI by going to browser and enter public dns name with port 9000

Please follow steps for integrating SonarQube with Jenkins
https://www.coachdevops.com/2020/04/how-to-integrate-sonarqube-with-jenkins.html

Tuesday, May 5, 2020

How to Deactivate a Rule in SonarQube | Deactivate a rule in Sonarqube community edition

How to deactivate a rule in SonarQube Ruleset in Sonarqube 7.7 version. You can not deactivate a rule in built-in profile. You need to have your own profile by copying from built-in profile and then you can activate/deactive/customize rules at will.



Create a custom profile by copying from built-in profile


Give some name

 Once you created click on Total rules 379 under Active

Now you can deactivate rule in SonarQube



Once you created custom profile, make sure you associate profile with your project or set as default.

How to install SonarQube on Ubuntu | Install Sonarqube 7.7 on Ubuntu 18.0.4 with PostgreSQL

SonarQube is one of the popular static code analysis tools. SonarQube is open-source, java based tool It also needs database as well - Database can be MySQL, Oracle or PostgreSQL.  We will use PostgreSQL as it is open source as well.

Please find steps for installing SonarQube 7.7 on Ubuntu 18.0.4. Make sure port 9000 is opened in security group(firewall rule).

Pre-requistes:
  • Instance should have at least 2 GB RAM. For AWS, instance should new and type should be t2.small
  • Make sure port 9000 is opened in security group(firewall rule).
Java 11 installation steps
sudo apt-get update && sudo apt-get install default-jdk -y

2. PostgreSQL Installation

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

sudo wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -


sudo apt-get -y install postgresql postgresql-contrib
Ignore the message in red color below:

Start PostGresSQL

sudo systemctl start postgresql
sudo systemctl enable postgresql

Login as postgres user

sudo su - postgres


Now create a sonar user below

createuser sonar


9. Switch to sql shell by entering
psql








Setup user name and password in Postgresql

Execute the below three lines (one by one)
ALTER USER sonar WITH ENCRYPTED password 'password';
CREATE DATABASE sonarqube OWNER sonar;
\q







type exit to come out of postgres user.




3. Now install SonarQube Web App
sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.7.zip


sudo apt-get -y install unzip
sudo unzip sonarqube-7.7.zip -d /opt





sudo mv /opt/sonarqube-7.7 /opt/sonarqube -v



Create Group and User:
sudo groupadd sonarGroup

Now add the user with directory access
sudo useradd -c "user to run SonarQube" -d /opt/sonarqube -g sonarGroup sonar
sudo chown sonar:sonarGroup /opt/sonarqube -R

Modify sonar.properties file
sudo vi /opt/sonarqube/conf/sonar.properties
uncomment the below lines by removing # and add values highlighted yellow
sonar.jdbc.username=sonar
sonar.jdbc.password=password





Next, add the below line:
sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube

Press escape, and enter :wq! to come out of the above screen.

Edit the sonar script file and set RUN_AS_USER
sudo vi /opt/sonarqube/bin/linux-x86-64/sonar.sh
(Scroll down by using down arrow button)

#enable the below line  by removing #

RUN_AS_USER=sonar

 



Create Sonar as a service
Execute the below command:
sudo vi /etc/systemd/system/sonar.service











add the below code in green color, please press insert button before you copy and paste.
[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=forking

ExecStart=/opt/sonarqube/bin/l
inux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/li
nux-x86-64/sonar.sh stop

User=sonar
Group=sonarGroup
Restart=always

[Install]
WantedBy=multi-user.target

# once you add press :wq! to come out of the above editor
sudo systemctl start sonar

sudo systemctl enable sonar

sudo systemctl status sonar
type q now to come out of this mode.
Now execute the below command to see if Sonarqube is up and running. This may take a few minutes.

Check Sonar logs by executing tail command:
tail -f /opt/sonarqube/logs/sonar.log

Make sure you get the below message that says sonarqube is up..


Now access SonarQube UI by going to browser and enter public dns name with port 9000
Login as admin/admin

Please follow steps for integrating SonarQube with Jenkins

https://www.coachdevops.com/2020/04/how-to-integrate-sonarqube-with-jenkins.html

Please watch the above steps as a demo in YouTube as well: