tinc_keygen.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #! /usr/bin/env ruby
  2. require File.dirname(__FILE__) + '/../../../spec_helper'
  3. require 'mocha'
  4. require 'fileutils'
  5. describe "the tinc_keygen function" do
  6. before :each do
  7. @scope = Puppet::Parser::Scope.new
  8. end
  9. it "should exist" do
  10. Puppet::Parser::Functions.function("tinc_keygen").should == "function_tinc_keygen"
  11. end
  12. it "should raise a ParseError if no argument is passed" do
  13. lambda { @scope.function_tinc_keygen([]) }.should( raise_error(Puppet::ParseError))
  14. end
  15. it "should raise a ParseError if there is more than 2 arguments" do
  16. lambda { @scope.function_tinc_keygen(["foo", "bar", "foo"]) }.should( raise_error(Puppet::ParseError))
  17. end
  18. it "should raise a ParseError if the second argument is not fully qualified" do
  19. lambda { @scope.function_tinc_keygen(["foo","bar"]) }.should( raise_error(Puppet::ParseError))
  20. end
  21. it "should raise a ParseError if the private key path is a directory" do
  22. File.stubs(:directory?).with("/some_dir/rsa_key.priv").returns(true)
  23. lambda { @scope.function_tinc_keygen(['foo',"/some_dir"]) }.should( raise_error(Puppet::ParseError))
  24. end
  25. it "should raise a ParseError if the public key path is a directory" do
  26. File.stubs(:directory?).with("/some_dir/rsa_key.pub").returns(true)
  27. lambda { @scope.function_tinc_keygen(['foo',"/some_dir"]) }.should( raise_error(Puppet::ParseError))
  28. end
  29. describe "when executing properly" do
  30. before do
  31. File.stubs(:directory?).with('/tmp/a/b/rsa_key.priv').returns(false)
  32. File.stubs(:directory?).with('/tmp/a/b/rsa_key.pub').returns(false)
  33. File.stubs(:read).with('/tmp/a/b/rsa_key.priv').returns('privatekey')
  34. File.stubs(:read).with('/tmp/a/b/rsa_key.pub').returns('publickey')
  35. end
  36. it "should fail if the public but not the private key exists" do
  37. File.stubs(:exists?).with("/tmp/a/b/rsa_key.priv").returns(true)
  38. File.stubs(:exists?).with("/tmp/a/b/rsa_key.pub").returns(false)
  39. lambda { @scope.function_tinc_keygen(['foo',"/tmp/a/b"]) }.should( raise_error(Puppet::ParseError))
  40. end
  41. it "should fail if the private but not the public key exists" do
  42. File.stubs(:exists?).with("/tmp/a/b/rsa_key.priv").returns(true)
  43. File.stubs(:exists?).with("/tmp/a/b/rsa_key.pub").returns(false)
  44. lambda { @scope.function_tinc_keygen(['foo',"/tmp/a/b"]) }.should( raise_error(Puppet::ParseError))
  45. end
  46. it "should return an array of size 2 with the right content if the keyfiles exists" do
  47. File.stubs(:exists?).with("/tmp/a/b/rsa_key.priv").returns(true)
  48. File.stubs(:exists?).with("/tmp/a/b/rsa_key.pub").returns(true)
  49. File.stubs(:directory?).with('/tmp/a/b').returns(true)
  50. Puppet::Util.expects(:execute).never
  51. result = @scope.function_tinc_keygen(['foo','/tmp/a/b'])
  52. result.length.should == 2
  53. result[0].should == 'privatekey'
  54. result[1].should == 'publickey'
  55. end
  56. it "should create the directory path if it does not exist" do
  57. File.stubs(:exists?).with("/tmp/a/b/rsa_key.priv").returns(false)
  58. File.stubs(:exists?).with("/tmp/a/b/rsa_key.pub").returns(false)
  59. File.stubs(:directory?).with("/tmp/a/b").returns(false)
  60. FileUtils.expects(:mkdir_p).with("/tmp/a/b", :mode => 0700)
  61. Puppet::Util.expects(:execute).returns("foo\nbar\nGenerating 2048 bits keys\n++++\n---")
  62. result = @scope.function_tinc_keygen(['foo','/tmp/a/b'])
  63. result.length.should == 2
  64. result[0].should == 'privatekey'
  65. result[1].should == 'publickey'
  66. end
  67. it "should generate the key if the keyfiles do not exist" do
  68. File.stubs(:exists?).with("/tmp/a/b/rsa_key.priv").returns(false)
  69. File.stubs(:exists?).with("/tmp/a/b/rsa_key.pub").returns(false)
  70. File.stubs(:directory?).with("/tmp/a/b").returns(true)
  71. Puppet::Util.expects(:execute).with(['/usr/sbin/tincd','-c', '/tmp/a/b', '-n', 'foo', '-K']).returns("foo\nbar\nGenerating 2048 bits keys\n++++\n---")
  72. result = @scope.function_tinc_keygen(['foo','/tmp/a/b'])
  73. result.length.should == 2
  74. result[0].should == 'privatekey'
  75. result[1].should == 'publickey'
  76. end
  77. it "should fail if something goes wrong during generation" do
  78. File.stubs(:exists?).with("/tmp/a/b/rsa_key.priv").returns(false)
  79. File.stubs(:exists?).with("/tmp/a/b/rsa_key.pub").returns(false)
  80. File.stubs(:directory?).with("/tmp/a/b").returns(true)
  81. Puppet::Util.expects(:execute).with(['/usr/sbin/tincd','-c', '/tmp/a/b', '-n', 'foo', '-K']).returns("something is wrong")
  82. lambda { @scope.function_tinc_keygen(['foo',"/tmp/a/b"]) }.should( raise_error(Puppet::ParseError))
  83. end
  84. end
  85. end