1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
require 'spec_helper'
if Puppet.features.microsoft_windows?
require 'puppet/util/windows'
class WindowsSecurity
extend Puppet::Util::Windows::Security
end
end
describe Puppet::Type.type(:file).provider(:windows), :if => Puppet.features.microsoft_windows? do
include PuppetSpec::Files
let(:path) { tmpfile('windows_file_spec') }
let(:resource) { Puppet::Type.type(:file).new :path => path, :mode => '0777', :provider => described_class.name }
let(:provider) { resource.provider }
let(:sid) { 'S-1-1-50' }
let(:account) { 'quinn' }
describe "#mode" do
it "should return a string representing the mode in 4-digit octal notation" do
FileUtils.touch(path)
WindowsSecurity.set_mode(0644, path)
expect(provider.mode).to eq('0644')
end
it "should return absent if the file doesn't exist" do
expect(provider.mode).to eq(:absent)
end
end
describe "#mode=" do
it "should chmod the file to the specified value" do
FileUtils.touch(path)
WindowsSecurity.set_mode(0644, path)
provider.mode = '0755'
expect(provider.mode).to eq('0755')
end
it "should pass along any errors encountered" do
expect do
provider.mode = '0644'
end.to raise_error(Puppet::Error, /failed to set mode/)
end
end
describe "#id2name" do
it "should return the name of the user identified by the sid" do
expect(Puppet::Util::Windows::SID).to receive(:valid_sid?).with(sid).and_return(true)
expect(Puppet::Util::Windows::SID).to receive(:sid_to_name).with(sid).and_return(account)
expect(provider.id2name(sid)).to eq(account)
end
it "should return the argument if it's already a name" do
expect(Puppet::Util::Windows::SID).to receive(:valid_sid?).with(account).and_return(false)
expect(Puppet::Util::Windows::SID).not_to receive(:sid_to_name)
expect(provider.id2name(account)).to eq(account)
end
it "should return nil if the user doesn't exist" do
expect(Puppet::Util::Windows::SID).to receive(:valid_sid?).with(sid).and_return(true)
expect(Puppet::Util::Windows::SID).to receive(:sid_to_name).with(sid).and_return(nil)
expect(provider.id2name(sid)).to eq(nil)
end
end
describe "#name2id" do
it "should delegate to name_to_sid" do
expect(Puppet::Util::Windows::SID).to receive(:name_to_sid).with(account).and_return(sid)
expect(provider.name2id(account)).to eq(sid)
end
end
describe "#owner" do
it "should return the sid of the owner if the file does exist" do
FileUtils.touch(resource[:path])
allow(provider).to receive(:get_owner).with(resource[:path]).and_return(sid)
expect(provider.owner).to eq(sid)
end
it "should return absent if the file doesn't exist" do
expect(provider.owner).to eq(:absent)
end
end
describe "#owner=" do
it "should set the owner to the specified value" do
expect(provider).to receive(:set_owner).with(sid, resource[:path])
provider.owner = sid
end
it "should propagate any errors encountered when setting the owner" do
allow(provider).to receive(:set_owner).and_raise(ArgumentError)
expect {
provider.owner = sid
}.to raise_error(Puppet::Error, /Failed to set owner/)
end
end
describe "#group" do
it "should return the sid of the group if the file does exist" do
FileUtils.touch(resource[:path])
allow(provider).to receive(:get_group).with(resource[:path]).and_return(sid)
expect(provider.group).to eq(sid)
end
it "should return absent if the file doesn't exist" do
expect(provider.group).to eq(:absent)
end
end
describe "#group=" do
it "should set the group to the specified value" do
expect(provider).to receive(:set_group).with(sid, resource[:path])
provider.group = sid
end
it "should propagate any errors encountered when setting the group" do
allow(provider).to receive(:set_group).and_raise(ArgumentError)
expect {
provider.group = sid
}.to raise_error(Puppet::Error, /Failed to set group/)
end
end
describe "when validating" do
{:owner => 'foo', :group => 'foo', :mode => '0777'}.each do |k,v|
it "should fail if the filesystem doesn't support ACLs and we're managing #{k}" do
allow_any_instance_of(described_class).to receive(:supports_acl?).and_return(false)
expect {
Puppet::Type.type(:file).new :path => path, k => v
}.to raise_error(Puppet::Error, /Can only manage owner, group, and mode on filesystems that support Windows ACLs, such as NTFS/)
end
end
it "should not fail if the filesystem doesn't support ACLs and we're not managing permissions" do
allow_any_instance_of(described_class).to receive(:supports_acl?).and_return(false)
Puppet::Type.type(:file).new :path => path
end
end
end
|