Rick
Rick

Reputation: 255

OS Specific Puppet Unit Test

I'm still getting up to speed on Puppet and rspec and all that, but...

We've currently got a CI runner that tests our Puppet module code using a Docker container running on Linux. Well, we're now delving into using Windows-specific features of Puppet, and our tests are failing. So we're wondering if there's any way to have the tests get ignored if the platform running them is Linux?

For example, if we need to run our unit tests against our code that manages the local groups (https://puppet.com/docs/pe/2018.1/managing_windows_configurations.html#manage_local_groups), is there a way so that when we run it locally on our Windows Dev boxes, it works, but when it runs on our (Linux-based) CI runner, it skips that particular test?

Per request, here is an example of the code we're looking to use to manage a local group:

class my_repo::profile::windows::remote_desktop_users (
  Array $members = ['MyDomain\MyUserAccount', 'MyDomain\ARandomDomainGroup'],
) {
  group{'Set local Remote Desktop Users memberships':
    ensure          => present,
    name            => 'Remote Desktop Users',
    members         => $members,
    auth_membership => false
  }
}

Note: We're using the Role-Profile pattern

The above code seems to work. It just bombs out when our unit tests run via our CI:

describe 'my_repo::profile::windows::remote_desktop_users' do
  on_supported_os.select { |_, f| f[:os]['family'] == 'windows' }.each do |os, os_facts|
    context "on #{os}" do
      let(:facts) { os_facts }

      it { is_expected.to compile }
    end
  end
end

Thanks

Upvotes: 0

Views: 566

Answers (1)

John Bollinger
John Bollinger

Reputation: 180998

As I remarked in a comment, I was able to reproduce a test failure using the manifest and Spec code (now) presented in the question. If I understand correctly that the failure is observed only when running the unit tests and not when serving up catalogs for real, then it follows that the problem is in the test environment's configuration, which may or may not be on you.

But as for the actual question:

So we're wondering if there's any way to have the tests get ignored if the platform running them is Linux?

Sure you can. Rspec tests are written in Ruby, and you can use substantially all standard Ruby features in them, including control flow statements and mechanisms for executing system commands. Thus, as a temporary workaround, you can put your breaks-when-not-running-on-Windows tests into a conditional statement, like this:

describe 'my_repo::profile::windows::remote_desktop_users' do
  on_supported_os.select { |_, f| f[:os]['family'] == 'windows' }.each do |os, os_facts|
    if %x{facter os.family}.chomp == 'windows'
      context "on #{os}" do
        let(:facts) { os_facts }

        it { is_expected.to compile }
      end
    end
  end
end

Note in particular the use of a %x expression to execute a system command on the host where the test runs, and in it use of facter to request the specific system fact that tells you whether the test is running on Windows. That resolves the issue for me. Note that this particular implementation will require facter to be installed on your CI machine.

Upvotes: 0

Related Questions