<commit_before>---
title: Writing a Server Test
prev:
  text: "Add a Suite"
  url: "adding-suite"
next:
  text: "Writing a Failing Recipe"
  url: "writing-failing-recipe"
---

Now to write a test or two. Previously we've seen the bats testing framework in action but this isn't always a viable option. For example if you needed to verify that a package was installed and you needed to test that on Ubuntu and CentOS platforms, then what would you do? You need to bust out some platform detection in order to run a Debian or RPM-based command. Feels like Chef would help us here since it's good at that sort of thing. On the other hand there are advantages to treating our Chef run as a black box, an configuration-management implementation detail, if you will. So what to do?

A nice solution to the platform-agnostic test issue exists called [Serverspec](http://serverspec.org/). It is a set of RSpec matchers that can assert things about servers like packages installed, services enabled, ports listening, etc. Let's see what this looks like for our Git Daemon tests.

First we're going to create a directory for our test file:

~~~
mkdir -p test/integration/server/serverspec
~~~

Next, create a file called `test/integration/server/serverspec/git_daemon_spec.rb` with the following:

~~~ruby
require 'serverspec'

include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS

RSpec.configure do |c|
  c.before :all do
    c.path = '/sbin:/usr/sbin'
  end
end<commit_msg>Fix typo and change comma to dash<commit_after>---
title: Writing a Server Test
prev:
  text: "Add a Suite"
  url: "adding-suite"
next:
  text: "Writing a Failing Recipe"
  url: "writing-failing-recipe"
---

Now to write a test or two. Previously we've seen the bats testing framework in action but this isn't always a viable option. For example if you needed to verify that a package was installed and you needed to test that on Ubuntu and CentOS platforms, then what would you do? You need to bust out some platform detection in order to run a Debian or RPM-based command. Feels like Chef would help us here since it's good at that sort of thing. On the other hand there are advantages to treating our Chef run as a black box - a configuration-management implementation detail, if you will. So what to do?

A nice solution to the platform-agnostic test issue exists called [Serverspec](http://serverspec.org/). It is a set of RSpec matchers that can assert things about servers like packages installed, services enabled, ports listening, etc. Let's see what this looks like for our Git Daemon tests.

First we're going to create a directory for our test file:

~~~
mkdir -p test/integration/server/serverspec
~~~

Next, create a file called `test/integration/server/serverspec/git_daemon_spec.rb` with the following:

~~~ruby
require 'serverspec'

include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS

RSpec.configure do |c|
  c.before :all do
    c.path = '/sbin:/usr/sbin'
  end
end
{'fraction_non_alphanumeric': 0.0709979906229069, 'fraction_numerical': 0.0, 'mean_word_length': 4.249560632688928, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3012481', 'n_tokens_mistral': 851, 'n_tokens_neox': 813, 'n_words': 433}