Go back to blog listing

Deploying Static Analysis and Unit Testing in Containerized Environments, Part 1

How to Configure Parasoft C/C++test 2020.1 for a Containerized Development Environment

How to Configure Parasoft C/C++test 2020.1 for Containerized Development Environments

The release of Parasoft C/C++test 2020.1 brings a collection of enhancements that make it easy to use with Docker containers. I’ll walk you through the steps to configure C/C++test 2020.1 to work in containerized development environments.

Embedded Development Teams Love Docker Containers

Containerized deployments of development tools are becoming the bread and butter of embedded development teams. Even though containers were initially developed to solve problems with the deployment of microservices and web-based applications, they recently gained popularity among embedded teams — especially big teams that use containers to manage complex toolchains.

When it comes to managing complex development environments, especially in the safety-critical space, teams usually struggle with the following challenges:

  • Synchronizing upgrades for the entire team to a new version of a tool like a compiler, build toolchain, and so on.
  • Dynamically reacting to a new security patch for the library or software development kit (SDK), and the like.
  • Assuring consistency of the toolchain for all team members and the automated infrastructure (CI/CD).
  • Ability to version the development environment and restoring it to service the older version of the product that was certified with the specific toolchain.
  • Onboarding and setting up new developers.

All these problems are easy to solve with containers. No wonder teams love containers! Let’s have a look at how to configure C/C++test for use in a containerized environment.

Using Parasoft C/C++test Professional With a Containerized Compilation Toolchain

It’s easy to configure Parasoft C/C++test Professional to work with a compilation toolchain and the execution environment deployed in containers. We support deployments that are based on Linux and Docker containers. Here’s a high-level overview of how that type of setup looks:

Containerized-Compilation-Toolchain-Image-1

As you can see, C/C++test accesses the containerized compiler (GNU GCC) and the runtime environments. In this specific setup, there are two separate Docker containers:

  • One for the compiler and build tools
  • Another for the execution environment (for example, stripped version of embedded Linux)
A compilation toolchain is used for static analysis to pre-process the code before parsing and for unit testing to compile the instrumented source. An execution environment is used only for the unit testing to run the created test binary with unit test cases.

Configuring Parasoft C++test 2020.1

Let’s see how to configure C/C++test Professional to work with an in-container compilation toolchain and execution environment. In the example, we’ll use the latest GCC container image available via the Docker hub.

The following instructions assume you have a Docker engine correctly installed and configured in your environment. And the same for C/C++test Professional 2020.1.

Step 1: Start Parasoft C/C++test
1. Make sure your Docker works. Let’s start with a quick test and print the version number of the GCC compiler included in the “latest” image of the GCC container:

Docker-Test-Screen-Capture-Image-2

2. Before we start Parasoft C/C++test, set a dedicated environmental variable CPPTEST_CONTAINER_NAME to the name of the container. This is an easy way to tell C/C++test which container should be used to invoke compiler or linker. It works nicely with Jenkins jobs.

3. In addition, I recommend that you set the CPPTEST_INSTALL variable to point to the C/C++test installation directory. It makes for a simple setup. Once the variables are set, we can start C/C++test.

4. For your workspace, select a location somewhere in the HOME directory (more on this later).

Start-CPPtest-Screen-Capture-Image-3

Step 2: Create a Demo Project

To make creating a demo project super simple, let’s create the default “HelloWorld” project using the standard “New Project” wizard.

1. Choose File Menu> New> C++ Project and then pick “Hello World C++ Project”.

2. Enter the name of choice for your project. It should look more or less like the screenshot below.

3. Click Finish to create the default project for our example.

Create-Project-Screen-Capture-Image-4

Step 3: Modify Compiler Settings

Once the project is created, modify a couple of items in the C/C++test Build Settings.

1. Right click on the project and select Properties and then Parasoft>C/C++test>Build Settings.

2. In the Build Settings panel, modify two items in the Compiler settings section:

  • Family. Change to match the compiler installed in the container. At the time of writing this blog, the latest “gcc” image contained the GNU GCC 9.2 compiler, which prompted selecting “GNU GCC 9.x (x86_64)” compiler configuration.
  • Default compiler executables. The default executables for the compiler have to be replaced with special wrappers to invoke the in-container compiler instead of the regular compiler that’s installed directly in the host operating system. These wrappers, remote-gcc and remote-g++, are shipped in the C/C++test 2020.1 distribution. In the screen shot below, I’m using a helper environmental variable for the C/C++test installation directory. You can either set a similar variable for your environment or use full paths to locate the wrapping scripts.

Build-Settings-Screen-Capture-Image-5

Step 4: Verify the Container Is Running

Make sure that the container with your compilation toolchain is up and running before using Parasoft C/C++test.

The compiler wrappers that we set up in the previous step are designed to execute the compiler commands in the running container, which is faster than starting the container for every invocation.

For CI/CD deployments, starting the container before C/C++test runs and stopping it after can be a part of the Jenkins job. In this example, we assume GUI base is used. We can create eclipse Run Configurations for this purpose. I suggest creating two configurations of “C/C++ Application” type and naming them as follows:

  • “Start gcc container”
  • “Stop gcc container”

1. Select Run menu > Run Configurations …

2. Create two Run Configurations named “Start gcc container” and “Stop gcc container”.

3. In the Main tab for both configurations, enter your path to Docker as the “C/C++ Application” like shown below:

Run-Configurations-Main-Stop-ScrCap-Image-6

4. For “Start gcc container”, in the arguments tab enter the following command:

run --rm -d -it --name gcc -v ${env_var:HOME}:${env_var:HOME}
-v
${env_var:CPPTEST_INSTALL}:${env_var:CPPTEST_INSTALL} gcc

As shown in the screen shot below:

Run-Configurations-Arguments-Start-ScrCap-Image-8

The interesting part of this command are -v options, which are used for mapping volumes.

The first -v option maps the local HOME directory to the container file system. The intention here is to enable access to the source code for the compiler operating inside the container and also for Parasoft C/C++test, which runs in the host operating system.

I’m using HOME in my example for convenience because I placed my workspace directly in HOME. 

For real setups, you can strictly map only the part of the file system that contains the source tree and the eclipse workspace with project.

The second -v option maps the location of C/C++test installation directory to enable access to C/C++test header files inside the container.

5. For “Stop gcc container”, in the arguments tab enter the following command as shown in the image below:

stop gcc

Run-Configurations-Arguments-Stop-ScrCap-Image-7

6. Let’s use the “Start gcc container” run configuration to run the container.

Eclipse console should show you the hash of the container. To make sure everything is all right, you can switch to the command line and invoke the following command:

Run-Container-Screen-Capture-Image-9

If you see our “gcc” container on the list, you're good to start using C/C++test for testing your code.

7. Let’s select the project in the navigator. Using the Parasoft menu, select Test Using> Builtin> Unit Testing> Generate Unit Tests.

You should see a new “tests” directory created inside your project with a single unit test created for the “main” function.

8. Once the test cases are generated, you're ready to execute them. For the execution, select Test Using> Builtin> Unit Testing> Run Unit Tests in the container.

C/C++test should invoke a collection of in-container compilations and linking command lines, ending with a test binary creation and execution in the container.

That’s it! With these steps, you prepared Parasoft C/C++test to perform testing activities using the in-container compilation toolchain.

If you need to change the compiler for a project or modify something in the runtime execution environment, you can either update the container image or change it to a different version. (Remember to update the CPPTEST_CONTAINER_NAME variable.)

Stay tuned for Part 2 coming soon. I will explain how to create a container image with Parasoft C/C++test Standard.

Unified development testing for C and C++ applications

Stay up to date