-

Transcripted Summary

Hi, everyone - welcome to this chapter.

I will show you how you can install InSpec on your workstation.

After InSpec is installed, you will write your first InSpec control to see how InSpec is working.

We will do this with a typical "Hello World" example.

Let's take a closer look at how you install InSpec on your workstation.

InSpec is a Ruby DSL and is available for MacOS, Windows, and Linux.



You can install it by a package manager installer or by a script, depending on your operating system.

You can find more details on the official download page.

Here, you can find all releases for mentioned operating systems and a bunch of other important stuff.

Due to the fact that I'm working on a Macbook, I will use Homebrew to install InSpec on my system.

Homebrew is a free and open source of the package management system that simplifies the installation of software on Apple's MacOS operating system and Linux.

InSpec is available as a standalone Homebrew package.

Open a terminal window and proceed with the installation.

Run the following command in your terminal.


brew cask install inspec

After successful installation, you can check the installation with the command.


inspec --version

Another helpful command is the help command to get information about all available InSpec commands and options.

If you want to know more about a specific command - here's an example for the archive command you can use.



inspec help archive


So, InSpec is installed.

Let us create our first control file.

After we successfully installed InSpec, we now take a closer look at the basic syntax of the InSpec DSL.

We will create our first control file which validates the Hello World program.

Imagine we have the following requirement - in the current directory, a file named helloworld.txt must exist, and this file must contain the string "Hello World."

To be honest, this requirement is not necessarily plausible, but it is a typical "Hello World" example as we know it from many other programming languages to see the syntax of the DSL.

Our control checks if a file named helloworld.txt includes the string "Hello World" - not more, not less.

Let's start with the implementation of this control, which verifies that our system is in the expected state.

We will do this in a test-driven design approach.

Go back through the terminal and navigate to a directory of your choice.

In my case, I created upfront the directory for this course named InspecTAU.

Now, create two files.

First, the file which will be our test - helloworld.rb.

Then the file which will be checked - helloworld.txt.

Now, launch your favorite IDE - in my case, I will use Visual Studio Code and go to the InSpecTAU directory.

Open the helloworld.rb and define the control as follows.


control "helloWorld" do                                          
    impact 1.0      

    title "Hello World"             

    desc "File should include the string Hello World." 

    ref "Hello World Requirement "

    describe file('helloworld.txt') do                       
     	its('content') { should include 'Hello World' } 
    end                                                 
end

You will see a lot of metadata in this control, like impact, title, description, and ref.

The real test, which verifies the expected behavior is in line 10 and 11.

I will explain the details in one of the next chapters.

Now, let us execute the control.

Go back to the terminal and run the following command to execute the control file.


inspec exec helloworld.rb

All right, the test is red.



The test failed because there is no string "Hello World" in the file that we verified.

Let's change the content in the file helloworld.txt and insert the string "Hello World".

Don't forget to save the file.

With this change, the system should be in the expected state.

Let's check this.



Great - our system is now in the expected state.

This was more or less test driven development of our first InSpec control.

So, we are at the end of this chapter.

In this chapter, we installed InSpec, and we created our first control file.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR