ssh_keygen_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #! /usr/bin/env ruby -S rspec
  2. require 'spec_helper'
  3. require 'rspec-puppet'
  4. require 'mocha'
  5. require 'fileutils'
  6. describe 'ssh_keygen' do
  7. let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
  8. it 'should exist' do
  9. Puppet::Parser::Functions.function("ssh_keygen").should == "function_ssh_keygen"
  10. end
  11. it 'should raise a ParseError if no argument is passed' do
  12. lambda {
  13. scope.function_ssh_keygen([])
  14. }.should(raise_error(Puppet::ParseError))
  15. end
  16. it 'should raise a ParseError if there is more than 1 arguments' do
  17. lambda {
  18. scope.function_ssh_keygen(["foo", "bar"])
  19. }.should( raise_error(Puppet::ParseError))
  20. end
  21. it 'should raise a ParseError if the argument is not fully qualified' do
  22. lambda {
  23. scope.function_ssh_keygen(["foo"])
  24. }.should( raise_error(Puppet::ParseError))
  25. end
  26. it "should raise a ParseError if the private key path is a directory" do
  27. File.stubs(:directory?).with("/some_dir").returns(true)
  28. lambda {
  29. scope.function_ssh_keygen(["/some_dir"])
  30. }.should( raise_error(Puppet::ParseError))
  31. end
  32. it "should raise a ParseError if the public key path is a directory" do
  33. File.stubs(:directory?).with("/some_dir.pub").returns(true)
  34. lambda {
  35. scope.function_ssh_keygen(["/some_dir.pub"])
  36. }.should( raise_error(Puppet::ParseError))
  37. end
  38. describe 'when executing properly' do
  39. before do
  40. File.stubs(:directory?).with('/tmp/a/b/c').returns(false)
  41. File.stubs(:directory?).with('/tmp/a/b/c.pub').returns(false)
  42. File.stubs(:read).with('/tmp/a/b/c').returns('privatekey')
  43. File.stubs(:read).with('/tmp/a/b/c.pub').returns('publickey')
  44. end
  45. it 'should fail if the public but not the private key exists' do
  46. File.stubs(:exists?).with('/tmp/a/b/c').returns(true)
  47. File.stubs(:exists?).with('/tmp/a/b/c.pub').returns(false)
  48. lambda {
  49. scope.function_ssh_keygen(['/tmp/a/b/c'])
  50. }.should( raise_error(Puppet::ParseError))
  51. end
  52. it "should fail if the private but not the public key exists" do
  53. File.stubs(:exists?).with("/tmp/a/b/c").returns(false)
  54. File.stubs(:exists?).with("/tmp/a/b/c.pub").returns(true)
  55. lambda {
  56. scope.function_ssh_keygen(["/tmp/a/b/c"])
  57. }.should( raise_error(Puppet::ParseError))
  58. end
  59. it "should return an array of size 2 with the right conent if the keyfiles exists" do
  60. File.stubs(:exists?).with("/tmp/a/b/c").returns(true)
  61. File.stubs(:exists?).with("/tmp/a/b/c.pub").returns(true)
  62. File.stubs(:directory?).with('/tmp/a/b').returns(true)
  63. Puppet::Util.expects(:execute).never
  64. result = scope.function_ssh_keygen(['/tmp/a/b/c'])
  65. result.length.should == 2
  66. result[0].should == 'privatekey'
  67. result[1].should == 'publickey'
  68. end
  69. it "should create the directory path if it does not exist" do
  70. File.stubs(:exists?).with("/tmp/a/b/c").returns(false)
  71. File.stubs(:exists?).with("/tmp/a/b/c.pub").returns(false)
  72. File.stubs(:directory?).with("/tmp/a/b").returns(false)
  73. FileUtils.expects(:mkdir_p).with("/tmp/a/b", :mode => 0700)
  74. Puppet::Util::Execution.expects(:execute).returns("")
  75. result = scope.function_ssh_keygen(['/tmp/a/b/c'])
  76. result.length.should == 2
  77. result[0].should == 'privatekey'
  78. result[1].should == 'publickey'
  79. end
  80. it "should generate the key if the keyfiles do not exist" do
  81. File.stubs(:exists?).with("/tmp/a/b/c").returns(false)
  82. File.stubs(:exists?).with("/tmp/a/b/c.pub").returns(false)
  83. File.stubs(:directory?).with("/tmp/a/b").returns(true)
  84. Puppet::Util::Execution.expects(:execute).with(['/usr/bin/ssh-keygen','-t', 'rsa', '-b', '4096', '-f', '/tmp/a/b/c', '-P', '', '-q']).returns("")
  85. result = scope.function_ssh_keygen(['/tmp/a/b/c'])
  86. result.length.should == 2
  87. result[0].should == 'privatekey'
  88. result[1].should == 'publickey'
  89. end
  90. it "should fail if something goes wrong during generation" do
  91. File.stubs(:exists?).with("/tmp/a/b/c").returns(false)
  92. File.stubs(:exists?).with("/tmp/a/b/c.pub").returns(false)
  93. File.stubs(:directory?).with("/tmp/a/b").returns(true)
  94. Puppet::Util::Execution.expects(:execute).with(['/usr/bin/ssh-keygen','-t', 'rsa', '-b', '4096', '-f', '/tmp/a/b/c', '-P', '', '-q']).returns("something is wrong")
  95. lambda {
  96. scope.function_ssh_keygen(["/tmp/a/b/c"])
  97. }.should( raise_error(Puppet::ParseError))
  98. end
  99. end
  100. end