spec/unit/provider/ssh_authorized_key/parsed.rb in puppet-0.25.4 vs spec/unit/provider/ssh_authorized_key/parsed.rb in puppet-0.25.5
- old
+ new
@@ -13,34 +13,38 @@
include PuppetTest::FileParsing
before :each do
@sshauthkey_class = Puppet::Type.type(:ssh_authorized_key)
@provider = @sshauthkey_class.provider(:parsed)
+ @keyfile = File.join(tmpdir, 'authorized_keys')
+ @user = 'random_bob'
+ Puppet::Util.stubs(:uid).with(@user).returns 12345
end
after :each do
@provider.initvars
end
def mkkey(args)
fakeresource = fakeresource(:ssh_authorized_key, args[:name])
+ fakeresource.stubs(:should).with(:user).returns @user
+ fakeresource.stubs(:should).with(:target).returns @keyfile
key = @provider.new(fakeresource)
args.each do |p,v|
key.send(p.to_s + "=", v)
end
- return key
+ key
end
def genkey(key)
@provider.stubs(:filetype).returns(Puppet::Util::FileType::FileTypeRam)
- file = @provider.default_target
-
+ File.stubs(:chown)
+ File.stubs(:chmod)
key.flush
- text = @provider.target_object(file).read
- return text
+ @provider.target_object(@keyfile).read
end
PuppetTest.fakedata("data/providers/ssh_authorized_key/parsed").each { |file|
it "should be able to parse example data in #{file}" do
puts "Parsing %s" % file
@@ -71,11 +75,11 @@
genkey(key).should == "from=\"192.168.1.1\",no-pty,no-X11-forwarding ssh-rsa AAAAfsfddsjldjgksdflgkjsfdlgkj root@localhost\n"
end
it "'s parse_options method should be able to parse options containing commas" do
- options = %w{from="host1.reductlivelabs.com,host.reductivelabs.com" command="/usr/local/bin/run" ssh-pty}
+ options = %w{from="host1.reductlivelabs.com,host.puppetlabs.com" command="/usr/local/bin/run" ssh-pty}
optionstr = options.join(", ")
@provider.parse_options(optionstr).should == options
end
@@ -145,24 +149,39 @@
# File.stubs(:expand_path).with("~random_bob/.ssh").returns "/users/r/random_bob/.ssh"
#
# but mocha objects strenuously to stubbing File.expand_path
# so I'm left with using nobody.
@dir = File.expand_path("~nobody/.ssh")
- end
+ end
- it "should create the directory" do
+ it "should create the directory if it doesn't exist" do
File.stubs(:exist?).with(@dir).returns false
Dir.expects(:mkdir).with(@dir,0700)
@provider.flush
end
- it "should chown the directory to the user" do
+ it "should not create or chown the directory if it already exist" do
+ File.stubs(:exist?).with(@dir).returns false
+ Dir.expects(:mkdir).never
+ @provider.flush
+ end
+
+ it "should chown the directory to the user if it creates it" do
+ File.stubs(:exist?).with(@dir).returns false
+ Dir.stubs(:mkdir).with(@dir,0700)
uid = Puppet::Util.uid("nobody")
File.expects(:chown).with(uid, nil, @dir)
@provider.flush
end
+ it "should not create or chown the directory if it already exist" do
+ File.stubs(:exist?).with(@dir).returns false
+ Dir.expects(:mkdir).never
+ File.expects(:chown).never
+ @provider.flush
+ end
+
it "should chown the key file to the user" do
uid = Puppet::Util.uid("nobody")
File.expects(:chown).with(uid, nil, File.expand_path("~nobody/.ssh/authorized_keys"))
@provider.flush
end
@@ -177,19 +196,11 @@
before :each do
@resource.stubs(:should).with(:user).returns nil
@resource.stubs(:should).with(:target).returns("/tmp/.ssh_dir/place_to_put_authorized_keys")
end
- it "should make the directory" do
- File.stubs(:exist?).with("/tmp/.ssh_dir").returns false
- Dir.expects(:mkdir).with("/tmp/.ssh_dir", 0755)
- @provider.flush
+ it "should raise an error" do
+ proc { @provider.flush }.should raise_error
end
-
- it "should chmod the key file to 0644" do
- File.expects(:chmod).with(0644, "/tmp/.ssh_dir/place_to_put_authorized_keys")
- @provider.flush
- end
end
-
end
end