From 465c29d5f7f46f91a7803ef3856f5c2c87e99680 Mon Sep 17 00:00:00 2001 From: Jacob Zaval Date: Wed, 25 Apr 2018 10:32:20 -0700 Subject: [PATCH] Release/1.13 (#102) * bump version * New Keep Awake Tests (#99) * Add test case for portable bare metal * rename systemsetup to system * Fix spec_helper to use system * cookstyle changes * Add CONTRIBUTING.md and reference Contributing to Chef Managed Community Cookbooks (#97) * Add keychain guards (#100) * update keep_awake documentation regarding spec testing (#101) * refactor location of shared_examples * don't stub git * don't test library specific methods in recipe testing * remove nil and any references to node attributes in system library * add tests for when node attributes are nil * make variable names more descriptive --- CONTRIBUTING.md | 1 + README.md | 12 ++++ libraries/{systemsetup.rb => system.rb} | 25 +++----- metadata.rb | 2 +- recipes/keep_awake.rb | 12 ++-- resources/keychain.rb | 4 ++ spec/spec_helper.rb | 2 +- .../{systemsetup_spec.rb => system_spec.rb} | 14 +++++ spec/unit/recipes/keep_awake_spec.rb | 57 +++++++++++-------- 9 files changed, 79 insertions(+), 50 deletions(-) create mode 100644 CONTRIBUTING.md rename libraries/{systemsetup.rb => system.rb} (63%) rename spec/unit/libraries/{systemsetup_spec.rb => system_spec.rb} (91%) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..fc9342f --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1 @@ +Please refer to [Contributing to Chef Managed Community Cookbooks](https://github.com/chef-cookbooks/community_cookbook_documentation/blob/master/CONTRIBUTING.MD) for guidelines on contributing to this cookbook. diff --git a/README.md b/README.md index d8bed8a..697b8f1 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,18 @@ to always keep macOS on and available. | `node['macos']['network_time_server']` | `'time.windows.com'` | | `node['macos']['time_zone']` | `'America/Los_Angeles'` | +**N.b.** When ChefSpec testing implementations of this recipe, the `node['hardware']['machine_model']` +attribute needs to be set to a Mac model identifier, e.g. `MacMini6,2`, in order +for tests to pass: + +```ruby +let(:chef_run) do + ChefSpec::SoloRunner.new do |node| + node.normal['hardware']['machine_model'] = 'MacMini6,2' + end.converge(described_recipe) +end +``` + ### Mono Installs [Mono](http://www.mono-project.com/docs/about-mono/). Requires setting diff --git a/libraries/systemsetup.rb b/libraries/system.rb similarity index 63% rename from libraries/systemsetup.rb rename to libraries/system.rb index 631f795..ae36f57 100644 --- a/libraries/systemsetup.rb +++ b/libraries/system.rb @@ -3,12 +3,8 @@ module MacOS class FormFactor attr_reader :machine_model - def initialize(machine_model = nil) - @machine_model = if Chef.node.nil? - machine_model - else - Chef.node['hardware']['machine_model'] - end + def initialize(machine_model) + @machine_model = machine_model end def desktop? @@ -25,12 +21,8 @@ module MacOS class Environment attr_reader :virtualization_systems - def initialize(virtualization_systems = nil) - @virtualization_systems = if Chef.node.nil? - virtualization_systems - else - Chef.node['virtualization']['systems'] - end + def initialize(virtualization_systems) + @virtualization_systems = virtualization_systems end def vm? @@ -40,12 +32,9 @@ module MacOS class ScreenSaver attr_reader :user - def initialize(user = nil) - @user = if Chef.node.nil? - user - else - Chef.node['macos']['admin_user'] - end + + def initialize(user) + @user = user end def disabled? diff --git a/metadata.rb b/metadata.rb index 9ac4bc4..855e99f 100644 --- a/metadata.rb +++ b/metadata.rb @@ -5,7 +5,7 @@ license 'MIT' description 'Resources for configuring and provisioning macOS' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) chef_version '>= 13.0' if respond_to?(:chef_version) -version '1.12.0' +version '1.13.0' source_url 'https://github.com/Microsoft/macos-cookbook' issues_url 'https://github.com/Microsoft/macos-cookbook/issues' diff --git a/recipes/keep_awake.rb b/recipes/keep_awake.rb index 191dbbc..2725f94 100644 --- a/recipes/keep_awake.rb +++ b/recipes/keep_awake.rb @@ -1,6 +1,6 @@ -form_factor = MacOS::System::FormFactor.new() -env = MacOS::System::Environment.new() -scr_svr = MacOS::System::ScreenSaver.new() +form_factor = MacOS::System::FormFactor.new(node['hardware']['machine_model']) +environment = MacOS::System::Environment.new(node['virtualization']['systems']) +screensaver = MacOS::System::ScreenSaver.new(node['macos']['admin_user']) system_preference 'disable computer sleep' do preference :computersleep @@ -25,13 +25,13 @@ end system_preference 'wake the computer when accessed using a network connection' do preference :wakeonnetworkaccess setting 'On' - not_if { env.vm? } + not_if { environment.vm? } end system_preference 'restart after a power failure' do preference :restartpowerfailure setting 'On' - not_if { env.vm? } + not_if { environment.vm? } end system_preference 'pressing power button does not sleep computer' do @@ -67,6 +67,6 @@ end defaults 'com.apple.screensaver' do option '-currentHost write' settings 'idleTime' => 0 - not_if { scr_svr.disabled? } + not_if { screensaver.disabled? } user node['macos']['admin_user'] end diff --git a/resources/keychain.rb b/resources/keychain.rb index a42c424..7a4da4f 100644 --- a/resources/keychain.rb +++ b/resources/keychain.rb @@ -15,6 +15,7 @@ action :create do execute 'create a keychain' do command [*keyc.create_keychain(new_resource.kc_passwd)] + not_if { ::File.exist?(keychain) } end end @@ -22,6 +23,7 @@ action :delete do keyc = SecurityCommand.new('', keychain) execute 'delete selected keychain' do command [*keyc.delete_keychain] + only_if { ::File.exist?(keychain) } end end @@ -29,11 +31,13 @@ action :lock do keyc = SecurityCommand.new('', keychain) execute 'lock selected keychain' do command [*keyc.lock_keychain] + only_if { ::File.exist?(keychain) } end end action :unlock do keyc = SecurityCommand.new('', keychain) do command [*keyc.unlock_keychain(new_resource.kc_passwd)] + only_if { ::File.exist?(keychain) } end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 80cb1e5..e465856 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,7 +5,7 @@ require_relative '../libraries/macos_user' require_relative '../libraries/machine_name' require_relative '../libraries/metadata_util' require_relative '../libraries/plist' -require_relative '../libraries/systemsetup' +require_relative '../libraries/system' require_relative '../libraries/xcode' require_relative '../libraries/xcversion' require_relative '../libraries/command_line_tools' diff --git a/spec/unit/libraries/systemsetup_spec.rb b/spec/unit/libraries/system_spec.rb similarity index 91% rename from spec/unit/libraries/systemsetup_spec.rb rename to spec/unit/libraries/system_spec.rb index 8d78a70..2ed6ad9 100644 --- a/spec/unit/libraries/systemsetup_spec.rb +++ b/spec/unit/libraries/system_spec.rb @@ -72,6 +72,20 @@ describe MacOS::System::FormFactor do expect(ff.portable?).to eq false end end + + context 'when passed a machine model that is nil' do + it 'it does not register as form factor desktop' do + ff = MacOS::System::FormFactor.new(nil) + expect(ff.desktop?).to eq false + end + end + + context 'when passed a machine model that is nil' do + it 'it does not register as form factor portable' do + ff = MacOS::System::FormFactor.new(nil) + expect(ff.portable?).to eq false + end + end end describe MacOS::System::Environment do diff --git a/spec/unit/recipes/keep_awake_spec.rb b/spec/unit/recipes/keep_awake_spec.rb index 9f313f2..361c134 100644 --- a/spec/unit/recipes/keep_awake_spec.rb +++ b/spec/unit/recipes/keep_awake_spec.rb @@ -3,18 +3,12 @@ require 'spec_helper' include MacOS::System shared_context 'when running on bare metal macmini' do + before(:each) do + chef_run.node.normal['virtualization']['systems'] = { 'vbox' => 'host', 'parallels' => 'host' } + chef_run.node.normal['hardware']['machine_model'] = 'MacMini6,2' + end + shared_examples 'setting metal-specific power preferences' do - before(:each) do - chef_run.node.normal['hardware']['machine_model'] = 'MacMini6,2' - chef_run.node.normal['virtualization']['systems'] = { 'vbox' => 'host', 'parallels' => 'host' } - stub_command('which git').and_return('/usr/bin/git') - end - - it 'returns false' do - env = System::Environment.new('host') - expect(env.vm?).to be false - end - it 'sets wake on lan' do chef_run.converge(described_recipe) expect(chef_run).to set_system_preference('wake the computer when accessed using a network connection') @@ -36,19 +30,36 @@ shared_context 'when running on bare metal macmini' do end end +shared_context 'when running on bare metal macbook' do + before(:each) do + chef_run.node.normal['virtualization']['systems'] = { 'vbox' => 'host', 'parallels' => 'host' } + chef_run.node.normal['hardware']['machine_model'] = 'Macbook10,1' + end + + shared_examples 'setting portable metal-specific power preferences' do + it 'sets wake on lan' do + chef_run.converge(described_recipe) + expect(chef_run).to set_system_preference('wake the computer when accessed using a network connection') + end + + it 'sets restart after a power failure' do + chef_run.converge(described_recipe) + expect(chef_run).to set_system_preference('restart after a power failure') + end + + it 'converges successfully on bare metal' do + expect { chef_run }.to_not raise_error + end + end +end + shared_context 'running in a parallels virtual machine' do before(:each) do chef_run.node.normal['virtualization']['systems'] = { 'parallels' => 'guest' } chef_run.node.normal['hardware']['machine_model'] = 'Parallels13,1' - stub_command('which git').and_return('/usr/bin/git') end shared_examples 'not setting metal-specific power prefs' do - it 'confirms we are in a vm' do - env = System::Environment.new() - expect(env.vm?).to be true - end - it 'does not set wake on lan' do chef_run.converge(described_recipe) expect(chef_run).to_not set_system_preference('wake the computer when accessed using a network connection') @@ -63,7 +74,6 @@ shared_context 'running in a parallels virtual machine' do chef_run.converge(described_recipe) expect(chef_run).to_not set_system_preference('restart after a power failure') end - it 'converges successfully in a vm' do expect { chef_run }.to_not raise_error end @@ -74,15 +84,9 @@ shared_context 'running in an undetermined virtualization system' do before(:each) do chef_run.node.normal['virtualization']['systems'] = {} chef_run.node.normal['hardware']['machine_model'] = '' - stub_command('which git').and_return('/usr/bin/git') end shared_examples 'not setting metal-specific power prefs' do - it 'assumes we are in a vm' do - env = System::Environment.new('undetermined') - expect(env.vm?).to be true - end - it 'does not set wake on lan' do chef_run.converge(described_recipe) expect(chef_run).to_not set_system_preference('wake the computer when accessed using a network connection') @@ -121,4 +125,9 @@ describe 'macos::keep_awake' do include_context 'when running on bare metal macmini' it_behaves_like 'setting metal-specific power preferences' end + + describe 'keep_awake on portable bare metal' do + include_context 'when running on bare metal macbook' + it_behaves_like 'setting portable metal-specific power preferences' + end end