diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/functional/resource/macos_userdefaults_spec.rb | 119 | ||||
-rw-r--r-- | spec/unit/resource/macos_user_defaults_spec.rb | 130 |
2 files changed, 154 insertions, 95 deletions
diff --git a/spec/functional/resource/macos_userdefaults_spec.rb b/spec/functional/resource/macos_userdefaults_spec.rb new file mode 100644 index 0000000000..a9ed206019 --- /dev/null +++ b/spec/functional/resource/macos_userdefaults_spec.rb @@ -0,0 +1,119 @@ +# +# Copyright:: Copyright (c) Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +require "spec_helper" + +describe Chef::Resource::MacosUserDefaults do + def create_resource + node = Chef::Node.new + events = Chef::EventDispatch::Dispatcher.new + run_context = Chef::RunContext.new(node, {}, events) + resource = Chef::Resource::MacosUserDefaults.new("test", run_context) + resource + end + + let(:resource) do + create_resource + end + + context "has a default value" do + it ":macos_userdefaults for resource name" do + expect(resource.name).to eq("test") + end + + it "NSGlobalDomain for the domain property" do + expect(resource.domain).to eq("NSGlobalDomain") + end + + it "nil for the host property" do + expect(resource.host).to be_nil + end + + it "nil for the user property" do + expect(resource.user).to be_nil + end + + it ":write for resource action" do + expect(resource.action).to eq([:write]) + end + end + + it "supports :write, :delete actions" do + expect { resource.action :write }.not_to raise_error + expect { resource.action :delete }.not_to raise_error + end + + context "can process expected data" do + it "set array values" do + resource.domain "/Library/Preferences/ManagedInstalls" + resource.key "TestArrayValues" + resource.value [ "/Library/Managed Installs/fake.log", "/Library/Managed Installs/also_fake.log"] + resource.run_action(:write) + expect(resource.get_preference resource).to eq([ "/Library/Managed Installs/fake.log", "/Library/Managed Installs/also_fake.log"]) + end + + it "set dictionary value" do + resource.domain "/Library/Preferences/ManagedInstalls" + resource.key "TestDictionaryValues" + resource.value "User": "/Library/Managed Installs/way_fake.log" + resource.run_action(:write) + expect(resource.get_preference resource).to eq("User" => "/Library/Managed Installs/way_fake.log") + end + + it "set array of dictionaries" do + resource.domain "/Library/Preferences/ManagedInstalls" + resource.key "TestArrayWithDictionary" + resource.value [ { "User": "/Library/Managed Installs/way_fake.log" } ] + resource.run_action(:write) + expect(resource.get_preference resource).to eq([ { "User" => "/Library/Managed Installs/way_fake.log" } ]) + end + + it "set boolean for preference value" do + resource.domain "/Library/Preferences/ManagedInstalls" + resource.key "TestBooleanValue" + resource.value true + resource.run_action(:write) + expect(resource.get_preference resource).to eq(true) + end + + it "sets value to global domain when domain is not passed" do + resource.key "TestKey" + resource.value 1 + resource.run_action(:write) + expect(resource.get_preference resource).to eq(1) + end + + it "short domain names" do + resource.domain "com.apple.dock" + resource.key "titlesize" + resource.value "20" + resource.run_action(:write) + expect(resource.get_preference resource).to eq("20") + end + end + + it "we can delete a preference with full path" do + resource.domain "/Library/Preferences/ManagedInstalls" + resource.key "TestKey" + expect { resource.run_action(:delete) }. to_not raise_error + end + + it "we can delete a preference with short name" do + resource.domain "com.apple.dock" + resource.key "titlesize" + expect { resource.run_action(:delete) }. to_not raise_error + end +end diff --git a/spec/unit/resource/macos_user_defaults_spec.rb b/spec/unit/resource/macos_user_defaults_spec.rb index 2c643ab266..19ff31c430 100644 --- a/spec/unit/resource/macos_user_defaults_spec.rb +++ b/spec/unit/resource/macos_user_defaults_spec.rb @@ -18,119 +18,59 @@ require "spec_helper" describe Chef::Resource::MacosUserDefaults do - - let(:resource) { Chef::Resource::MacosUserDefaults.new("foo") } - let(:provider) { resource.provider_for_action(:write) } - - it "has a resource name of :macos_userdefaults" do - expect(resource.resource_name).to eq(:macos_userdefaults) - end - - it "the domain property defaults to NSGlobalDomain" do - expect(resource.domain).to eq("NSGlobalDomain") - end - - it "the value property coerces keys in hashes to strings so we can compare them with plist data" do - resource.value "User": "/Library/Managed Installs/way_fake.log" - expect(resource.value).to eq({ "User" => "/Library/Managed Installs/way_fake.log" }) - end - - it "the host property defaults to nil" do - expect(resource.host).to be_nil - end - - it "the sudo property defaults to false" do - expect(resource.sudo).to be false - end - - it "sets the default action as :write" do - expect(resource.action).to eq([:write]) - end - - it "supports :write action" do - expect { resource.action :write }.not_to raise_error - end - - describe "#defaults_export_cmd" do - it "exports NSGlobalDomain if no domain is set" do - expect(provider.defaults_export_cmd(resource)).to eq(["/usr/bin/defaults", "export", "NSGlobalDomain", "-"]) + let(:test_value) { "fakest_key_value" } + let(:test_key) { "fakest_key" } + let(:node) { Chef::Node.new } + let(:events) { Chef::EventDispatch::Dispatcher.new } + let(:run_context) { Chef::RunContext.new(node, {}, events) } + let(:resource) { + Chef::Resource::MacosUserDefaults.new("foo", run_context).tap do |r| + r.value test_value + r.key test_key end + } - it "exports a provided domain" do - resource.domain "com.tim" - expect(provider.defaults_export_cmd(resource)).to eq(["/usr/bin/defaults", "export", "com.tim", "-"]) + context "has a default value" do + it ":macos_userdefaults for resource name" do + expect(resource.resource_name).to eq(:macos_userdefaults) end - it "sets -currentHost if host is 'current'" do - resource.host "current" - expect(provider.defaults_export_cmd(resource)).to eq(["/usr/bin/defaults", "-currentHost", "export", "NSGlobalDomain", "-"]) + it "NSGlobalDomain for the domain property" do + expect(resource.domain).to eq("NSGlobalDomain") end - it "sets -host 'tim-laptop if host is 'tim-laptop'" do - resource.host "tim-laptop" - expect(provider.defaults_export_cmd(resource)).to eq(["/usr/bin/defaults", "-host", "tim-laptop", "export", "NSGlobalDomain", "-"]) - end - end - - describe "#defaults_modify_cmd" do - # avoid needing to set these required values over and over. We'll overwrite them where necessary - before do - resource.key = "foo" - resource.value = "bar" - end - - it "writes to NSGlobalDomain if domain isn't specified" do - expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-string", "bar"]) - end - - it "uses the domain property if set" do - resource.domain = "MyCustomDomain" - expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "MyCustomDomain", "foo", "-string", "bar"]) + it "nil for the host property" do + expect(resource.host).to be_nil end - it "sets host specific values using host property" do - resource.host = "tims_laptop" - expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "-host", "tims_laptop", "write", "NSGlobalDomain", "foo", "-string", "bar"]) + it "nil for the user property" do + expect(resource.user).to be_nil end - it "if host is set to :current it passes CurrentHost" do - resource.host = :current - expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "-currentHost", "write", "NSGlobalDomain", "foo", "-string", "bar"]) - end - - it "raises ArgumentError if bool is specified, but the value can't be made into a bool" do - resource.type "bool" - expect { provider.defaults_modify_cmd }.to raise_error(ArgumentError) - end - - it "autodetects array type and passes individual values" do - resource.value = %w{one two three} - expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-array", "one", "two", "three"]) - end - - it "autodetects string type and passes a single value" do - resource.value = "one" - expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-string", "one"]) + it ":write for resource action" do + expect(resource.action).to eq([:write]) end + end - it "autodetects integer type and passes a single value" do - resource.value = 1 - expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-int", 1]) + context ":write" do + it "is a supported action" do + expect { resource.action :write }.not_to raise_error end - it "autodetects boolean type from TrueClass value and passes a 'TRUE' string" do - resource.value = true - expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-bool", "TRUE"]) + it "successfully updates the preference" do + resource.run_action(:write) + expect(resource.get_preference resource).eql? test_value end + end - it "autodetects boolean type from FalseClass value and passes a 'FALSE' string" do - resource.value = false - expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-bool", "FALSE"]) + context ":delete" do + it "is a supported action" do + expect { resource.action :delete }.not_to raise_error end - it "autodetects dict type from Hash value and flattens keys & values" do - resource.value = { "foo" => "bar" } - expect(provider.defaults_modify_cmd).to eq(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-dict", "foo", "bar"]) + it "successfully deletes the preference" do + resource.run_action(:delete) + expect(resource.get_preference resource).to be_nil end end end |