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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
require 'spec_helper'
require 'puppet/settings'
require 'puppet/settings/file_setting'
describe Puppet::Settings::FileSetting do
FileSetting = Puppet::Settings::FileSetting
include PuppetSpec::Files
describe "when controlling permissions" do
def settings(wanted_values = {})
real_values = {
:user => 'root',
:group => 'root',
:mkusers => false,
:service_user_available? => false,
:service_group_available? => false
}.merge(wanted_values)
settings = double("settings")
allow(settings).to receive(:[]).with(:user).and_return(real_values[:user])
allow(settings).to receive(:[]).with(:group).and_return(real_values[:group])
allow(settings).to receive(:[]).with(:mkusers).and_return(real_values[:mkusers])
allow(settings).to receive(:service_user_available?).and_return(real_values[:service_user_available?])
allow(settings).to receive(:service_group_available?).and_return(real_values[:service_group_available?])
settings
end
context "owner" do
it "can always be root" do
settings = settings(:user => "the_service", :mkusers => true)
setting = FileSetting.new(:settings => settings, :owner => "root", :desc => "a setting")
expect(setting.owner).to eq("root")
end
it "is the service user if we are making users" do
settings = settings(:user => "the_service", :mkusers => true, :service_user_available? => false)
setting = FileSetting.new(:settings => settings, :owner => "service", :desc => "a setting")
expect(setting.owner).to eq("the_service")
end
it "is the service user if the user is available on the system" do
settings = settings(:user => "the_service", :mkusers => false, :service_user_available? => true)
setting = FileSetting.new(:settings => settings, :owner => "service", :desc => "a setting")
expect(setting.owner).to eq("the_service")
end
it "is root when the setting specifies service and the user is not available on the system" do
settings = settings(:user => "the_service", :mkusers => false, :service_user_available? => false)
setting = FileSetting.new(:settings => settings, :owner => "service", :desc => "a setting")
expect(setting.owner).to eq("root")
end
it "is unspecified when no specific owner is wanted" do
expect(FileSetting.new(:settings => settings(), :desc => "a setting").owner).to be_nil
end
it "does not allow other owners" do
expect { FileSetting.new(:settings => settings(), :desc => "a setting", :name => "testing", :default => "the default", :owner => "invalid") }.
to raise_error(FileSetting::SettingError, /The :owner parameter for the setting 'testing' must be either 'root' or 'service'/)
end
end
context "group" do
it "is unspecified when no specific group is wanted" do
setting = FileSetting.new(:settings => settings(), :desc => "a setting")
expect(setting.group).to be_nil
end
it "is root if root is requested" do
settings = settings(:group => "the_group")
setting = FileSetting.new(:settings => settings, :group => "root", :desc => "a setting")
expect(setting.group).to eq("root")
end
it "is the service group if we are making users" do
settings = settings(:group => "the_service", :mkusers => true)
setting = FileSetting.new(:settings => settings, :group => "service", :desc => "a setting")
expect(setting.group).to eq("the_service")
end
it "is the service user if the group is available on the system" do
settings = settings(:group => "the_service", :mkusers => false, :service_group_available? => true)
setting = FileSetting.new(:settings => settings, :group => "service", :desc => "a setting")
expect(setting.group).to eq("the_service")
end
it "is unspecified when the setting specifies service and the group is not available on the system" do
settings = settings(:group => "the_service", :mkusers => false, :service_group_available? => false)
setting = FileSetting.new(:settings => settings, :group => "service", :desc => "a setting")
expect(setting.group).to be_nil
end
it "does not allow other groups" do
expect { FileSetting.new(:settings => settings(), :group => "invalid", :name => 'testing', :desc => "a setting") }.
to raise_error(FileSetting::SettingError, /The :group parameter for the setting 'testing' must be either 'root' or 'service'/)
end
end
end
it "should be able to be converted into a resource" do
expect(FileSetting.new(:settings => double("settings"), :desc => "eh")).to respond_to(:to_resource)
end
describe "when being converted to a resource" do
before do
@basepath = make_absolute("/somepath")
allow(Puppet::FileSystem).to receive(:exist?).and_call_original
allow(Puppet::FileSystem).to receive(:exist?).with(@basepath).and_return(true)
@settings = double('settings')
@file = Puppet::Settings::FileSetting.new(:settings => @settings, :desc => "eh", :name => :myfile, :section => "mysect")
allow(@settings).to receive(:value).with(:myfile, nil, false).and_return(@basepath)
end
it "should return :file as its type" do
expect(@file.type).to eq(:file)
end
it "skips non-existent files" do
expect(@file).to receive(:type).and_return(:file)
expect(Puppet::FileSystem).to receive(:exist?).with(@basepath).and_return(false)
expect(@file.to_resource).to be_nil
end
it "manages existing files" do
expect(@file).to receive(:type).and_return(:file)
expect(@file.to_resource).to be_instance_of(Puppet::Resource)
end
it "always manages directories" do
expect(@file).to receive(:type).and_return(:directory)
expect(@file.to_resource).to be_instance_of(Puppet::Resource)
end
describe "on POSIX systems", :if => Puppet.features.posix? do
it "should skip files in /dev" do
allow(@settings).to receive(:value).with(:myfile, nil, false).and_return("/dev/file")
expect(@file.to_resource).to be_nil
end
end
it "should skip files whose paths are not strings" do
allow(@settings).to receive(:value).with(:myfile, nil, false).and_return(:foo)
expect(@file.to_resource).to be_nil
end
it "should return a file resource with the path set appropriately" do
resource = @file.to_resource
expect(resource.type).to eq("File")
expect(resource.title).to eq(@basepath)
end
it "should have a working directory with a root directory not called dev", :if => Puppet::Util::Platform.windows? do
# Although C:\Dev\.... is a valid path on Windows, some other code may regard it as a path to be ignored. e.g. /dev/null resolves to C:\dev\null on Windows.
path = File.expand_path('somefile')
expect(path).to_not match(/^[A-Z]:\/dev/i)
end
it "should fully qualified returned files if necessary (#795)" do
allow(@settings).to receive(:value).with(:myfile, nil, false).and_return("myfile")
path = File.expand_path('myfile')
allow(Puppet::FileSystem).to receive(:exist?).with(path).and_return(true)
expect(@file.to_resource.title).to eq(path)
end
it "should set the mode on the file if a mode is provided as an octal number" do
Puppet[:manage_internal_file_permissions] = true
@file.mode = 0755
expect(@file.to_resource[:mode]).to eq('755')
end
it "should set the mode on the file if a mode is provided as a string" do
Puppet[:manage_internal_file_permissions] = true
@file.mode = '0755'
expect(@file.to_resource[:mode]).to eq('755')
end
it "should not set the mode on a the file if manage_internal_file_permissions is disabled" do
Puppet[:manage_internal_file_permissions] = false
allow(@file).to receive(:mode).and_return(0755)
expect(@file.to_resource[:mode]).to eq(nil)
end
it "should set the owner if running as root and the owner is provided" do
Puppet[:manage_internal_file_permissions] = true
expect(Puppet.features).to receive(:root?).and_return(true)
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
allow(@file).to receive(:owner).and_return("foo")
expect(@file.to_resource[:owner]).to eq("foo")
end
it "should not set the owner if manage_internal_file_permissions is disabled" do
Puppet[:manage_internal_file_permissions] = false
allow(Puppet.features).to receive(:root?).and_return(true)
allow(@file).to receive(:owner).and_return("foo")
expect(@file.to_resource[:owner]).to eq(nil)
end
it "should set the group if running as root and the group is provided" do
Puppet[:manage_internal_file_permissions] = true
expect(Puppet.features).to receive(:root?).and_return(true)
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
allow(@file).to receive(:group).and_return("foo")
expect(@file.to_resource[:group]).to eq("foo")
end
it "should not set the group if manage_internal_file_permissions is disabled" do
Puppet[:manage_internal_file_permissions] = false
allow(Puppet.features).to receive(:root?).and_return(true)
allow(@file).to receive(:group).and_return("foo")
expect(@file.to_resource[:group]).to eq(nil)
end
it "should not set owner if not running as root" do
Puppet[:manage_internal_file_permissions] = true
expect(Puppet.features).to receive(:root?).and_return(false)
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
allow(@file).to receive(:owner).and_return("foo")
expect(@file.to_resource[:owner]).to be_nil
end
it "should not set group if not running as root" do
Puppet[:manage_internal_file_permissions] = true
expect(Puppet.features).to receive(:root?).and_return(false)
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
allow(@file).to receive(:group).and_return("foo")
expect(@file.to_resource[:group]).to be_nil
end
describe "on Microsoft Windows systems" do
before :each do
allow(Puppet::Util::Platform).to receive(:windows?).and_return(true)
end
it "should not set owner" do
allow(@file).to receive(:owner).and_return("foo")
expect(@file.to_resource[:owner]).to be_nil
end
it "should not set group" do
allow(@file).to receive(:group).and_return("foo")
expect(@file.to_resource[:group]).to be_nil
end
end
it "should set :ensure to the file type" do
expect(@file).to receive(:type).and_return(:directory)
expect(@file.to_resource[:ensure]).to eq(:directory)
end
it "should set the loglevel to :debug" do
expect(@file.to_resource[:loglevel]).to eq(:debug)
end
it "should set the backup to false" do
expect(@file.to_resource[:backup]).to be_falsey
end
it "should tag the resource with the settings section" do
expect(@file).to receive(:section).and_return("mysect")
expect(@file.to_resource).to be_tagged("mysect")
end
it "should tag the resource with the setting name" do
expect(@file.to_resource).to be_tagged("myfile")
end
it "should tag the resource with 'settings'" do
expect(@file.to_resource).to be_tagged("settings")
end
it "should set links to 'follow'" do
expect(@file.to_resource[:links]).to eq(:follow)
end
end
describe "#munge" do
it 'does not expand the path of the special value :memory: so we can set dblocation to an in-memory database' do
filesetting = FileSetting.new(:settings => double("settings"), :desc => "eh")
expect(filesetting.munge(':memory:')).to eq(':memory:')
end
end
context "when opening", :unless => Puppet::Util::Platform.windows? do
let(:path) do
tmpfile('file_setting_spec')
end
let(:setting) do
settings = double("settings", :value => path)
FileSetting.new(:name => :mysetting, :desc => "creates a file", :settings => settings)
end
it "creates a file with mode 0640" do
setting.mode = '0640'
expect(File).to_not be_exist(path)
setting.open('w')
expect(File).to be_exist(path)
expect(Puppet::FileSystem.stat(path).mode & 0777).to eq(0640)
end
it "preserves the mode of an existing file" do
setting.mode = '0640'
Puppet::FileSystem.touch(path)
Puppet::FileSystem.chmod(0644, path)
setting.open('w')
expect(Puppet::FileSystem.stat(path).mode & 0777).to eq(0644)
end
end
end
|