Signed-off-by: Brice Figureau <brice-pup...@daysofwonder.com>
---
 bin/puppetca                       |  103 +--------------------------
 lib/puppet/application/puppetca.rb |   81 +++++++++++++++++++++
 spec/unit/application/puppetca.rb  |  138 ++++++++++++++++++++++++++++++++++++
 3 files changed, 221 insertions(+), 101 deletions(-)
 create mode 100644 lib/puppet/application/puppetca.rb
 create mode 100644 spec/unit/application/puppetca.rb

diff --git a/bin/puppetca b/bin/puppetca
index 81e1439..771e31b 100755
--- a/bin/puppetca
+++ b/bin/puppetca
@@ -96,104 +96,5 @@
 # Copyright (c) 2005 Reductive Labs, LLC
 # Licensed under the GNU Public License
 
-require 'puppet'
-require 'puppet/ssl/certificate_authority'
-require 'getoptlong'
-
-options = [
-    [ "--all",      "-a",  GetoptLong::NO_ARGUMENT ],
-    [ "--clean",    "-c",  GetoptLong::NO_ARGUMENT ],
-    [ "--debug",    "-d",  GetoptLong::NO_ARGUMENT ],
-    [ "--generate", "-g",  GetoptLong::NO_ARGUMENT ],
-    [ "--help",     "-h",  GetoptLong::NO_ARGUMENT ],
-    [ "--list",     "-l",  GetoptLong::NO_ARGUMENT ],
-    [ "--print",    "-p",  GetoptLong::NO_ARGUMENT ],
-    [ "--revoke",   "-r",  GetoptLong::NO_ARGUMENT ],
-    [ "--sign",     "-s",  GetoptLong::NO_ARGUMENT ],
-    [ "--verify",          GetoptLong::NO_ARGUMENT ],
-       [ "--version",  "-V",  GetoptLong::NO_ARGUMENT ],
-    [ "--verbose",  "-v",  GetoptLong::NO_ARGUMENT ]
-]
-
-# Add all of the config parameters as valid options.
-Puppet.settings.addargs(options)
-
-result = GetoptLong.new(*options)
-
-modes = Puppet::SSL::CertificateAuthority::Interface::INTERFACE_METHODS
-
-all = false
-mode = nil
-
-begin
-    result.each { |opt,arg|
-        case opt
-            when "--clean"
-                mode = :destroy
-            when "--all"
-                all = true
-            when "--debug"
-                Puppet::Util::Log.level = :debug
-            when "--help"
-                if Puppet.features.usage?
-                    RDoc::usage && exit
-                else
-                    puts "No help available unless you have RDoc::usage 
installed"
-                    exit
-                end
-            when "--version"
-                puts "%s" % Puppet.version
-                exit
-            when "--verbose"
-                Puppet::Util::Log.level = :info
-            else
-                tmp = opt.sub("--", '').to_sym
-                if modes.include?(tmp)
-                    mode = tmp
-                else
-                    Puppet.settings.handlearg(opt, arg)
-                end
-        end
-    }
-rescue GetoptLong::InvalidOption => detail
-    $stderr.puts "Try '#{$0} --help'"
-    exit(1)
-end
-
-# Now parse the config
-Puppet.parse_config
-
-if Puppet.settings.print_configs?
-    exit(Puppet.settings.print_configs ? 0 : 1)
-end
-
-Puppet::Util::Log.newdestination :console
-
-Puppet::SSL::Host.ca_location = :local
-
-begin
-    ca = Puppet::SSL::CertificateAuthority.new
-rescue => detail
-    puts detail.backtrace if Puppet[:trace]
-    puts detail.to_s
-    exit(23)
-end
-
-unless mode
-    $stderr.puts "You must specify a mode; see the output from --help"
-    exit(12)
-end
-
-if all
-    hosts = :all
-else
-    hosts = ARGV.collect { |h| h.downcase }
-end
-
-begin
-    ca.apply(mode, :to => hosts)
-rescue => detail
-    puts detail.backtrace if Puppet[:trace]
-    puts detail.to_s
-    exit(24)
-end
+require 'puppet/application/puppetca'
+Puppet::Application[:puppetca].run
\ No newline at end of file
diff --git a/lib/puppet/application/puppetca.rb 
b/lib/puppet/application/puppetca.rb
new file mode 100644
index 0000000..2e91b9f
--- /dev/null
+++ b/lib/puppet/application/puppetca.rb
@@ -0,0 +1,81 @@
+require 'puppet'
+require 'puppet/application'
+require 'puppet/ssl/certificate_authority'
+
+puppetca_options = [
+    [ "--all",      "-a",  GetoptLong::NO_ARGUMENT ],
+    [ "--clean",    "-c",  GetoptLong::NO_ARGUMENT ],
+    [ "--debug",    "-d",  GetoptLong::NO_ARGUMENT ],
+    [ "--generate", "-g",  GetoptLong::NO_ARGUMENT ],
+    [ "--help",     "-h",  GetoptLong::NO_ARGUMENT ],
+    [ "--list",     "-l",  GetoptLong::NO_ARGUMENT ],
+    [ "--print",    "-p",  GetoptLong::NO_ARGUMENT ],
+    [ "--revoke",   "-r",  GetoptLong::NO_ARGUMENT ],
+    [ "--sign",     "-s",  GetoptLong::NO_ARGUMENT ],
+    [ "--verify",          GetoptLong::NO_ARGUMENT ],
+       [ "--version",  "-V",  GetoptLong::NO_ARGUMENT ],
+    [ "--verbose",  "-v",  GetoptLong::NO_ARGUMENT ]
+]
+
+Puppet::Application.new(:puppetca, puppetca_options) do
+
+    should_parse_config
+
+    attr_accessor :mode, :all, :ca
+
+    command(:main) do
+        if @all
+            hosts = :all
+        else
+            hosts = ARGV.collect { |h| puts h; h.downcase }
+        end
+        begin
+            @ca.apply(@mode, :to => hosts)
+        rescue => detail
+            puts detail.backtrace if Puppet[:trace]
+            puts detail.to_s
+            exit(24)
+        end
+    end
+
+    setup do
+        if Puppet.settings.print_configs?
+            exit(Puppet.settings.print_configs ? 0 : 1)
+        end
+
+        Puppet::Util::Log.newdestination :console
+
+        Puppet::SSL::Host.ca_location = :local
+
+        begin
+            @ca = Puppet::SSL::CertificateAuthority.new
+        rescue => detail
+            puts detail.backtrace if Puppet[:trace]
+            puts detail.to_s
+            exit(23)
+        end
+    end
+
+    option(:unknown) do |opt, arg|
+        modes = Puppet::SSL::CertificateAuthority::Interface::INTERFACE_METHODS
+        tmp = opt.sub("--", '').to_sym
+        @mode = modes.include?(tmp) ? tmp : nil
+        true
+    end
+
+    option(:clean) do |arg|
+        @mode = :destroy
+    end
+
+    option(:all) do |arg|
+        @all = true
+    end
+
+    option(:verbose) do |arg|
+        Puppet::Util::Log.level = :info
+    end
+
+    option(:debug) do |arg|
+        Puppet::Util::Log.level = :debug
+    end
+end
\ No newline at end of file
diff --git a/spec/unit/application/puppetca.rb 
b/spec/unit/application/puppetca.rb
new file mode 100644
index 0000000..22f1674
--- /dev/null
+++ b/spec/unit/application/puppetca.rb
@@ -0,0 +1,138 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'puppet/application/puppetca'
+
+describe "PuppetCA" do
+    before :each do
+        @puppetca = Puppet::Application[:puppetca]
+    end
+
+    it "should ask Puppet::Application to parse Puppet configuration file" do
+        @puppetca.should_parse_config?.should be_true
+    end
+
+    it "should declare a main command" do
+        @puppetca.should respond_to(:main)
+    end
+
+    it "should declare a fallback for unknown options" do
+        @puppetca.should respond_to(:handle_unknown)
+    end
+
+    it "should set log level to info with the --verbose option" do
+
+        Puppet::Log.expects(:level=).with(:info)
+
+        @puppetca.handle_verbose(0)
+    end
+
+    it "should set log level to debug with the --debug option" do
+
+        Puppet::Log.expects(:level=).with(:debug)
+
+        @puppetca.handle_debug(0)
+    end
+
+    it "should set mode to :destroy for --clean" do
+        @puppetca.handle_clean(0)
+        @puppetca.mode.should == :destroy
+    end
+
+    it "should set all to true for --all" do
+        @puppetca.handle_all(0)
+        @puppetca.all.should be_true
+    end
+
+    Puppet::SSL::CertificateAuthority::Interface::INTERFACE_METHODS.each do 
|method|
+        it "should set mode to #{method} with option --#{method}" do
+            @puppetca.handle_unknown("--#{method}", nil)
+
+            @puppetca.mode.should == method
+        end
+    end
+
+    it "should set mode to nil for an option not in the list of known 
CertificateAuthority option" do
+        @puppetca.handle_unknown("--dontknowme", nil)
+
+        @puppetca.mode.should be_nil
+    end
+
+    describe "during setup" do
+
+        before :each do
+            Puppet::Log.stubs(:newdestination)
+            Puppet::SSL::Host.stubs(:ca_location=)
+            Puppet::SSL::CertificateAuthority.stubs(:new)
+        end
+
+        it "should set console as the log destination" do
+            Puppet::Log.expects(:newdestination).with(:console)
+
+            @puppetca.run_setup
+        end
+
+        it "should print puppet config if asked to in Puppet config" do
+            @puppetca.stubs(:exit)
+            Puppet.settings.stubs(:print_configs?).returns(true)
+
+            Puppet.settings.expects(:print_configs)
+
+            @puppetca.run_setup
+        end
+
+        it "should exit after printing puppet config if asked to in Puppet 
config" do
+            Puppet.settings.stubs(:print_configs?).returns(true)
+
+            lambda { @puppetca.run_setup }.should raise_error(SystemExit)
+        end
+
+        it "should create a new certificate authority" do
+            Puppet::SSL::CertificateAuthority.expects(:new)
+
+            @puppetca.run_setup
+        end
+    end
+
+    describe "when running" do
+        before :each do
+            @puppetca.all = false
+            @ca = stub_everything 'ca'
+            @puppetca.ca = @ca
+            ARGV.stubs(:collect).returns([])
+        end
+
+        it "should delegate to the CertificateAuthority" do
+            @ca.expects(:apply)
+
+            @puppetca.main
+        end
+
+        it "should delegate with :all if option --all was given" do
+            @puppetca.handle_all(0)
+
+            @ca.expects(:apply).with { |mode,to| to[:to] == :all }
+
+            @puppetca.main
+        end
+
+        it "should delegate to ca.apply with the hosts given on command line" 
do
+            ARGV.stubs(:collect).returns(["host"])
+
+            @ca.expects(:apply).with { |mode,to| to[:to] == ["host"]}
+
+            @puppetca.main
+        end
+
+        it "should delegate to ca.apply with current set mode" do
+            @puppetca.mode = "currentmode"
+            ARGV.stubs(:collect).returns(["host"])
+
+            @ca.expects(:apply).with { |mode,to| mode == "currentmode" }
+
+            @puppetca.main
+        end
+
+    end
+end
\ No newline at end of file
-- 
1.6.0.2


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to puppet-dev@googlegroups.com
To unsubscribe from this group, send email to 
puppet-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to