Skip to content

Commit

Permalink
Merge branch 'main' into rhel8to9
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronlippold authored Dec 12, 2024
2 parents 201029d + 897af46 commit 6fe4099
Showing 1 changed file with 50 additions and 50 deletions.
100 changes: 50 additions & 50 deletions src/courses/advanced/05.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ Let's start by creating a new profile:
@tab Command

```bash
inspec init profile git
inspec init profile my_git
```

@tab Output

```bash
─────────────────────────── InSpec Code Generator ───────────────────────────

Creating new profile at /workspaces/saf-training-lab-environment/git
Creating new profile at /workspaces/saf-training-lab-environment/my_git
• Creating file inspec.yml
• Creating directory /workspaces/saf-training-lab-environment/git/controls
• Creating directory /workspaces/saf-training-lab-environment/my_git/controls
• Creating file controls/example.rb
• Creating file README.md
```
Expand All @@ -39,20 +39,20 @@ Creating new profile at /workspaces/saf-training-lab-environment/git
To write resources, we first need to know what we are testing! In your Codespaces environment, there is a git repository that we will test under the `resources` folder. The git repository will be the test target, similarly to how the docker containers acted as test targets in previous sections. Unzip the target git repository using the following command:

```sh
unzip ./resources/git_test.zip
tar xzvf resources/git_test_target.tar.gz
```

This will generate a `git_test` repository which we will use for these examples.

Now let's write some tests and confirm that they run. You can put these tests in the `example.rb` file generated in the `controls` folder of your `git` InSpec profile. These tests are written using the `command` resource which is provided by InSpec. We will write a `git` resource in this section to improve this test. **Note that you will need to put the full directory path of the `.git` file from your `git_test` repository as the `git_dir` value on line 4 of `example.rb`. To get the full path of your current location in the terminal, use `pwd`.**
Now let's write some tests and confirm that they run. You can put these tests in the `example.rb` file generated in the `controls` folder of your `my_git` InSpec profile. These tests are written using the `command` resource which is provided by InSpec. We will write a `git` resource in this section to improve this test. **Note that you will need to put the full directory path of the `.git` file from your `git_test_target` repository as the `git_dir` value on line 4 of `example.rb`. To get the full path of your current location in the terminal, use `pwd`.**

```ruby
# encoding: utf-8
# copyright: 2018, The Authors

git_dir = "/workspaces/saf-training-lab-environment/git_test/.git"
git_dir = "/workspaces/saf-training-lab-environment/git_test_target_target/.git"

# The following banches should exist
# The following branches should exist
describe command("git --git-dir #{git_dir} branch") do
its('stdout') { should match /main/ }
end
Expand All @@ -68,12 +68,12 @@ end

# What is the latest commit
describe command("git --git-dir #{git_dir} log -1 --pretty=format:'%h'") do
its('stdout') { should match /edc207f/ }
its('stdout') { should match /7a748c6/ }
end

# What is the second to last commit
describe command("git --git-dir #{git_dir} log --skip=1 -1 --pretty=format:'%h'") do
its('stdout') { should match /8c30bff/ }
its('stdout') { should match /edc207f/ }
end
```

Expand All @@ -83,29 +83,29 @@ Run the profile.
@tab Command

```bash
inspec exec git
inspec exec my_git
```

@tab Output

```bash
Redirecting to cinc-auditor...

Profile: InSpec Profile (git)
Profile: InSpec Profile (my_git)
Version: 0.1.0
Target: local://
Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce

Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch`
✔ stdout is expected to match /main/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch`
✔ stdout is expected to match /testBranch/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch`
✔ stdout is expected to match /^\* main/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log -1 --pretty=format:'%h'`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log -1 --pretty=format:'%h'`
✔ stdout is expected to match /7a748c6/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log --skip=1 -1 --pretty=format:'%h'`
✔ stdout is expected to match /edc207f/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log --skip=1 -1 --pretty=format:'%h'`
✔ stdout is expected to match /8c30bff/

Test Summary: 5 successful, 0 failures, 0 skipped
```
Expand All @@ -128,7 +128,7 @@ Let's rewrite these tests in a way that abstracts away the complexity of working
Let's rewrite the first test in our example file to make it more readable by inventing a `git` resource that can simplify our test as follows:

```ruby
# The following banches should exist
# The following branches should exist
describe git(git_dir) do
its('branches') { should include 'main' }
end
Expand All @@ -141,7 +141,7 @@ Now let's run the profile.
@tab Command

```bash
inspec exec git
inspec exec my_git
```

@tab Output
Expand All @@ -151,7 +151,7 @@ Redirecting to cinc-auditor...

[2023-02-22T03:21:41+00:00] ERROR: Failed to load profile git: Failed to load source for controls/example.rb: undefined method `git' for #<Inspec::ControlEvalContext:0x000000000540af38>
Profile: InSpec Profile (git)
Profile: InSpec Profile (my_git)
Version: 0.1.0
Failure Message: Failed to load source for controls/example.rb: undefined method `git' for #<Inspec::ControlEvalContext:0x000000000540af38>
Target: local://
Expand All @@ -168,7 +168,7 @@ We should get an error because the git method and resource are not defined yet.

### Develop the git resource

Let's start by creating a new file called `git.rb` in the `libraries` directory. If you do not already have a `libraries` directory, you can make one in the `git` InSpec profile directory. The content of the file should look like this:
Let's start by creating a new file called `git.rb` in the `libraries` directory. If you do not already have a `libraries` directory, you can make one in the `my_git` InSpec profile directory. The content of the file should look like this:
```ruby
# encoding: utf-8
Expand All @@ -191,15 +191,15 @@ Run the profile again.
@tab Command

```bash
inspec exec git
inspec exec my_git
```

@tab Output

```bash
[2023-02-22T03:25:57+00:00] ERROR: Failed to load profile git: Failed to load source for controls/example.rb: wrong number of arguments (given 1, expected 0)
Profile: InSpec Profile (git)
Profile: InSpec Profile (my_git)
Version: 0.1.0
Failure Message: Failed to load source for controls/example.rb: wrong number of arguments (given 1, expected 0)
Target: local://
Expand Down Expand Up @@ -241,27 +241,27 @@ Now when we run the profile:
@tab Command
```bash
inspec exec git
inspec exec my_git
```
@tab Output
```bash
Profile: InSpec Profile (git)
Profile: InSpec Profile (my_git)
Version: 0.1.0
Target: local://
Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce
git
× branches
undefined method `branches' for #<#<Class:0x00000000041485a8>:0x00000000043620c8>
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch`
✔ stdout is expected to match /testBranch/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch`
✔ stdout is expected to match /^\* main/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log -1 --pretty=format:'%h'`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log -1 --pretty=format:'%h'`
✔ stdout is expected to match /edc207f/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log --skip=1 -1 --pretty=format:'%h'`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log --skip=1 -1 --pretty=format:'%h'`
✔ stdout is expected to match /8c30bff/

Test Summary: 4 successful, 1 failure, 0 skipped
Expand Down Expand Up @@ -298,28 +298,28 @@ We have now defined the branches method. Let's see what the test output shows us
@tab Command

```bash
inspec exec git
inspec exec my_git
```

@tab Output

```bash
Profile: InSpec Profile (git)
Profile: InSpec Profile (my_git)
Version: 0.1.0
Target: local://
Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce

git
× branches is expected to include "main"
expected nil to include "main", but it does not respond to `include?`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch`
✔ stdout is expected to match /testBranch/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch`
✔ stdout is expected to match /^\* main/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log -1 --pretty=format:'%h'`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log -1 --pretty=format:'%h'`
✔ stdout is expected to match /7a748c6/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log --skip=1 -1 --pretty=format:'%h'`
✔ stdout is expected to match /edc207f/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log --skip=1 -1 --pretty=format:'%h'`
✔ stdout is expected to match /8c30bff/

Test Summary: 4 successful, 1 failure, 0 skipped
```
Expand Down Expand Up @@ -355,7 +355,7 @@ Now we see that we get a passing test!
Now let's adjust our test to also check for our second branch that we created earlier as well as check our current branch:

```ruby
# The following banches should exist
# The following branches should exist
describe git(git_dir) do
its('branches') { should include 'main' }
its('branches') { should include 'testBranch' }
Expand All @@ -368,13 +368,13 @@ end
@tab Command

```bash
inspec exec git
inspec exec my_git
```

@tab Output

```bash
Profile: InSpec Profile (git)
Profile: InSpec Profile (my_git)
Version: 0.1.0
Target: local://
Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce
Expand All @@ -384,10 +384,10 @@ Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce
✔ branches is expected to include "testBranch"
× current_branch
undefined method `current_branch' for #<#<Class:0x0000000005400588>:0x00000000053fd0b8>
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log -1 --pretty=format:'%h'`
✔ stdout is expected to match /edc207f/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log --skip=1 -1 --pretty=format:'%h'`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log -1 --pretty=format:'%h'`
✔ stdout is expected to match /8c30bff/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log --skip=1 -1 --pretty=format:'%h'`
✔ stdout is expected to match /7a748c6/
Test Summary: 6 successful, 1 failure, 0 skipped
```
Expand Down Expand Up @@ -428,13 +428,13 @@ Now we can run the profile again.
@tab Command

```bash
inspec exec git
inspec exec my_git
```

@tab Output

```bash
Profile: InSpec Profile (git)
Profile: InSpec Profile (my_git)
Version: 0.1.0
Target: local://
Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce
Expand All @@ -443,9 +443,9 @@ Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce
✔ branches is expected to include "main"
✔ branches is expected to include "testBranch"
✔ current_branch is expected to cmp == "main"
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log -1 --pretty=format:'%h'`
✔ stdout is expected to match /edc207f/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log --skip=1 -1 --pretty=format:'%h'`
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log -1 --pretty=format:'%h'`
✔ stdout is expected to match /7a748c6/
Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log --skip=1 -1 --pretty=format:'%h'`
✔ stdout is expected to match /8c30bff/

Test Summary: 7 successful, 0 failures, 0 skipped
Expand Down Expand Up @@ -487,7 +487,7 @@ You are currently running on:
Release: 20.04
Arch: x86_64

inspec> git('/workspaces/saf-training-lab-environment/git_test/.git').current_branch
inspec> git('/workspaces/saf-training-lab-environment/git_test_target/.git').current_branch
=> "main"
```

Expand All @@ -496,11 +496,11 @@ inspec> git('/workspaces/saf-training-lab-environment/git_test/.git').current_br
::: warning
Note that we are passing in the _profile_ directory to the `--depends` flag, and not the profile's `libraries` directory. In our example, it's
``` sh
inspec shell --depends git
inspec shell --depends my_git
```
and not
``` sh
inspec shell --depends git/libraries
inspec shell --depends my_git/libraries
```
:::

Expand Down

0 comments on commit 6fe4099

Please sign in to comment.