Skip to content

Commit

Permalink
Merge pull request #272 from dylanratcliffe/name_to_principal
Browse files Browse the repository at this point in the history
More Windows mocking!!!
  • Loading branch information
Dylan authored Jul 1, 2020
2 parents 2414269 + b6a2d86 commit 9b4373d
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 6 deletions.
16 changes: 13 additions & 3 deletions features/windows.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@ Feature: Run onceover with windows

Scenario: Run with common Windows code
Given control repo "windows"
When I run onceover command "run spec"
When I run onceover command "run spec" with class "role::users"
Then I should not see any errors

Scenario: Run with common Windows code without workarounds
Given existing control repo "windows"
When I run onceover command "run spec --no_workarounds"
When I run onceover command "run spec --no_workarounds" with class "role::users"
And test osfamily is not "windows"
Then I should see error with message pattern "uninitialized constant"
Then Onceover should exit 1

Scenario: Compiling a windows role with groups that is valid should compile
Given control repo "windows"
When I run onceover command "run spec" with class "role::groups"
Then I should not see any errors

Scenario: Compiling a windows role with users that is valid should compile
Given control repo "windows"
When I run onceover command "run spec" with class "role::users"
Then I should not see any errors

16 changes: 13 additions & 3 deletions lib/onceover/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ def run_spec!
#`bin/rake spec_standalone`
if @config.opts[:parallel]
logger.debug "Running #{@command_prefix}rake parallel_spec from #{@repo.tempdir}"
result = Backticks::Runner.new(interactive:true).run(@command_prefix.strip.split, 'rake', 'parallel_spec').join
result = run_command(@command_prefix.strip.split, 'rake', 'parallel_spec')
else
require 'io/console'
logger.debug "Running #{@command_prefix}rake spec_standalone from #{@repo.tempdir}"
result = Backticks::Runner.new(interactive:true).run(@command_prefix.strip.split, 'rake', 'spec_standalone').join
result = run_command(@command_prefix.strip.split, 'rake', 'spec_standalone')
end

# Reset env to previous state if we modified it
Expand All @@ -117,11 +118,20 @@ def run_acceptance!
#`bundle install --binstubs`
#`bin/rake spec_standalone`
logger.debug "Running #{@command_prefix}rake acceptance from #{@repo.tempdir}"
result = Backticks::Runner.new(interactive:true).run(@command_prefix.strip.split, 'rake', 'acceptance').join
result = run_command(@command_prefix.strip.split, 'rake', 'acceptance')
end

# Finally exit and preserve the exit code
exit result.status.exitstatus
end

def run_command(*args)
begin
STDERR.raw! if STDERR.isatty
result = Backticks::Runner.new(interactive: true).run(args.flatten).join
ensure
STDERR.cooked! if STDERR.isatty
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Class: role::groups
#
#
class role::groups {
group {'Administrators':
members => ['foo'],
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Class: role::users
#
#
class role::users {
user { 'dylan':
ensure => 'present',
groups => ['Administrators'],
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Class: role::groups
#
#
class role::groups {
group {'Administrators':
members => ['foo'],
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Class: role::users
#
#
class role::users {
user { 'dylan':
ensure => 'present',
groups => ['Administrators'],
}
}
46 changes: 46 additions & 0 deletions templates/test_spec.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,52 @@ describe "<%= cls.name %>" do
allow_any_instance_of(Puppet::Type.type(:acl).provider(:windows)).to receive(:respond_to?).with(:get_group_name).and_return(false)
allow_any_instance_of(Puppet::Type.type(:acl).provider(:windows)).to receive(:respond_to?).with(:validate).and_return(true)
end

# The windows_adsi provider also has an issue where the contructor takes
# a different number of arguments depending on whether the ADSI
# underlying connectivity exists. This causes the following error:
#
# wrong number of arguments (given 1, expected 0)
#
# This fixes that if we aren't using Windows
begin
# Test to see if this works without modification
require 'puppet/util/windows'
require 'puppet/util/windows/adsi'
Puppet::Util::Windows::ADSI.computer_name
rescue LoadError, StandardError
# Declare an entire mocked module because for some reason we can't load it unless we are on Windows
module Puppet::Util::Windows
module ADSI
class ADSIObject; end
class User < ADSIObject; end
class UserProfile; end
class Group < ADSIObject; end
end
module File; end
module Registry
end
module SID
class Principal; end
end
class EventLog; end
end

# Mock commonly used things
allow(Puppet::Util::Windows::SID).to receive(:name_to_sid).and_return('S-1-5-32-544')
allow(Puppet::Util::Windows::SID).to receive(:name_to_principal).and_return(nil)

allow_any_instance_of(Puppet::Util::Windows::ADSI::User).to receive(:initialize)
allow_any_instance_of(Puppet::Util::Windows::ADSI::User).to receive(:groups).and_return([])
allow_any_instance_of(Puppet::Util::Windows::ADSI::User).to receive(:name_sid_hash).and_return({})

# Group instance methods
allow_any_instance_of(Puppet::Util::Windows::ADSI::Group).to receive(:members).and_return([])
allow_any_instance_of(Puppet::Util::Windows::ADSI::Group).to receive(:members_sids).and_return([])

# Class methods
allow(Puppet::Util::Windows::ADSI::Group).to receive(:name_sid_hash).and_return({})
end
end
<% end -%>

Expand Down

0 comments on commit 9b4373d

Please sign in to comment.