0

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

Rick
  • 245
  • 3
  • 13
  • 1
    Unit tests are unaffected by the underlying platform by definition, and OS specifics are simulated during unit testing. Can we see the code to identify the root cause here? – Matthew Schuchard Mar 13 '20 at 20:00
  • When the unit test bombs out, what error message(s) is emitted? Surely your CI makes that information available to you. – John Bollinger Mar 16 '20 at 19:34
  • Somewhat to my surprise, I was able to use the example code to produce a broken test in PDK 1.16 running on Linux. It fails with a compilation error centered around this: "Validate method failed for class members: undefined method `name_to_principal' for Puppet::Util::Windows::SID:Module". I do not know whether this is reflective of the issue you see in your CI (but not on your live puppetserver?), but I am inclined to attribute it to a flaw in PDK. If you are seeing something similar then I encourage you to [file a ticket](https://tickets.puppetlabs.com/). – John Bollinger Mar 16 '20 at 20:24

1 Answers1

0

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157