From b859baa04737644e40002f511c5941d002a956e3 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Sat, 26 Mar 2011 00:12:17 -0700 Subject: MAINT: the API is officially named "string" as of this moment. Now that we have settled on the final public name for the API, "Puppet::String", mass-rename and mass-edit all the files to follow. Reviewed-By: Randall Hansen --- lib/puppet/application/config.rb | 4 +- lib/puppet/application/configurer.rb | 6 +- lib/puppet/application/indirection_base.rb | 10 +- lib/puppet/application/interface.rb | 95 -------- lib/puppet/application/interface_base.rb | 97 -------- lib/puppet/application/string.rb | 95 ++++++++ lib/puppet/application/string_base.rb | 97 ++++++++ lib/puppet/interface.rb | 98 -------- lib/puppet/interface/action.rb | 26 --- lib/puppet/interface/action_builder.rb | 27 --- lib/puppet/interface/action_manager.rb | 45 ---- lib/puppet/interface/indirector.rb | 79 ------- lib/puppet/interface/interface_collection.rb | 98 -------- lib/puppet/interface/v0.0.1/catalog.rb | 40 ---- lib/puppet/interface/v0.0.1/catalog/select.rb | 10 - lib/puppet/interface/v0.0.1/certificate.rb | 28 --- lib/puppet/interface/v0.0.1/certificate_request.rb | 4 - .../v0.0.1/certificate_revocation_list.rb | 4 - lib/puppet/interface/v0.0.1/config.rb | 11 - lib/puppet/interface/v0.0.1/configurer.rb | 12 - lib/puppet/interface/v0.0.1/facts.rb | 18 -- lib/puppet/interface/v0.0.1/file.rb | 5 - lib/puppet/interface/v0.0.1/key.rb | 4 - lib/puppet/interface/v0.0.1/node.rb | 5 - lib/puppet/interface/v0.0.1/report.rb | 15 -- lib/puppet/interface/v0.0.1/resource.rb | 4 - lib/puppet/interface/v0.0.1/resource_type.rb | 4 - lib/puppet/interface/v0.0.1/status.rb | 4 - lib/puppet/string.rb | 98 ++++++++ lib/puppet/string/action.rb | 26 +++ lib/puppet/string/action_builder.rb | 27 +++ lib/puppet/string/action_manager.rb | 45 ++++ lib/puppet/string/indirector.rb | 79 +++++++ lib/puppet/string/string_collection.rb | 98 ++++++++ lib/puppet/string/v0.0.1/catalog.rb | 40 ++++ lib/puppet/string/v0.0.1/catalog/select.rb | 10 + lib/puppet/string/v0.0.1/certificate.rb | 28 +++ lib/puppet/string/v0.0.1/certificate_request.rb | 4 + .../string/v0.0.1/certificate_revocation_list.rb | 4 + lib/puppet/string/v0.0.1/config.rb | 11 + lib/puppet/string/v0.0.1/configurer.rb | 12 + lib/puppet/string/v0.0.1/facts.rb | 18 ++ lib/puppet/string/v0.0.1/file.rb | 5 + lib/puppet/string/v0.0.1/key.rb | 4 + lib/puppet/string/v0.0.1/node.rb | 5 + lib/puppet/string/v0.0.1/report.rb | 15 ++ lib/puppet/string/v0.0.1/resource.rb | 4 + lib/puppet/string/v0.0.1/resource_type.rb | 4 + lib/puppet/string/v0.0.1/status.rb | 4 + spec/spec_helper.rb | 2 +- spec/unit/application/config_spec.rb | 4 +- spec/unit/application/indirection_base_spec.rb | 2 +- spec/unit/application/interface_base_spec.rb | 73 ------ spec/unit/application/interface_spec.rb | 10 - spec/unit/application/string_base_spec.rb | 73 ++++++ spec/unit/application/string_spec.rb | 10 + spec/unit/interface/action_builder_spec.rb | 30 --- spec/unit/interface/action_manager_spec.rb | 216 ------------------ spec/unit/interface/action_spec.rb | 75 ------- spec/unit/interface/catalog_spec.rb | 6 - spec/unit/interface/certificate_request_spec.rb | 6 - .../interface/certificate_revocation_list_spec.rb | 6 - spec/unit/interface/certificate_spec.rb | 6 - spec/unit/interface/config_spec.rb | 24 -- spec/unit/interface/configurer_spec.rb | 24 -- spec/unit/interface/facts_spec.rb | 21 -- spec/unit/interface/file_spec.rb | 6 - spec/unit/interface/indirector_spec.rb | 55 ----- spec/unit/interface/interface_collection_spec.rb | 249 --------------------- spec/unit/interface/key_spec.rb | 6 - spec/unit/interface/node_spec.rb | 9 - spec/unit/interface/report_spec.rb | 6 - spec/unit/interface/resource_spec.rb | 6 - spec/unit/interface/resource_type_spec.rb | 6 - spec/unit/interface_spec.rb | 83 ------- spec/unit/string/action_builder_spec.rb | 30 +++ spec/unit/string/action_manager_spec.rb | 216 ++++++++++++++++++ spec/unit/string/action_spec.rb | 75 +++++++ spec/unit/string/catalog_spec.rb | 6 + spec/unit/string/certificate_request_spec.rb | 6 + .../string/certificate_revocation_list_spec.rb | 6 + spec/unit/string/certificate_spec.rb | 6 + spec/unit/string/config_spec.rb | 24 ++ spec/unit/string/configurer_spec.rb | 24 ++ spec/unit/string/facts_spec.rb | 21 ++ spec/unit/string/file_spec.rb | 6 + spec/unit/string/indirector_spec.rb | 55 +++++ spec/unit/string/key_spec.rb | 6 + spec/unit/string/node_spec.rb | 9 + spec/unit/string/report_spec.rb | 6 + spec/unit/string/resource_spec.rb | 6 + spec/unit/string/resource_type_spec.rb | 6 + spec/unit/string/string_collection_spec.rb | 249 +++++++++++++++++++++ spec/unit/string_spec.rb | 83 +++++++ 94 files changed, 1670 insertions(+), 1670 deletions(-) delete mode 100644 lib/puppet/application/interface.rb delete mode 100644 lib/puppet/application/interface_base.rb create mode 100644 lib/puppet/application/string.rb create mode 100644 lib/puppet/application/string_base.rb delete mode 100644 lib/puppet/interface.rb delete mode 100644 lib/puppet/interface/action.rb delete mode 100644 lib/puppet/interface/action_builder.rb delete mode 100644 lib/puppet/interface/action_manager.rb delete mode 100644 lib/puppet/interface/indirector.rb delete mode 100644 lib/puppet/interface/interface_collection.rb delete mode 100644 lib/puppet/interface/v0.0.1/catalog.rb delete mode 100644 lib/puppet/interface/v0.0.1/catalog/select.rb delete mode 100644 lib/puppet/interface/v0.0.1/certificate.rb delete mode 100644 lib/puppet/interface/v0.0.1/certificate_request.rb delete mode 100644 lib/puppet/interface/v0.0.1/certificate_revocation_list.rb delete mode 100644 lib/puppet/interface/v0.0.1/config.rb delete mode 100644 lib/puppet/interface/v0.0.1/configurer.rb delete mode 100644 lib/puppet/interface/v0.0.1/facts.rb delete mode 100644 lib/puppet/interface/v0.0.1/file.rb delete mode 100644 lib/puppet/interface/v0.0.1/key.rb delete mode 100644 lib/puppet/interface/v0.0.1/node.rb delete mode 100644 lib/puppet/interface/v0.0.1/report.rb delete mode 100644 lib/puppet/interface/v0.0.1/resource.rb delete mode 100644 lib/puppet/interface/v0.0.1/resource_type.rb delete mode 100644 lib/puppet/interface/v0.0.1/status.rb create mode 100644 lib/puppet/string.rb create mode 100644 lib/puppet/string/action.rb create mode 100644 lib/puppet/string/action_builder.rb create mode 100644 lib/puppet/string/action_manager.rb create mode 100644 lib/puppet/string/indirector.rb create mode 100644 lib/puppet/string/string_collection.rb create mode 100644 lib/puppet/string/v0.0.1/catalog.rb create mode 100644 lib/puppet/string/v0.0.1/catalog/select.rb create mode 100644 lib/puppet/string/v0.0.1/certificate.rb create mode 100644 lib/puppet/string/v0.0.1/certificate_request.rb create mode 100644 lib/puppet/string/v0.0.1/certificate_revocation_list.rb create mode 100644 lib/puppet/string/v0.0.1/config.rb create mode 100644 lib/puppet/string/v0.0.1/configurer.rb create mode 100644 lib/puppet/string/v0.0.1/facts.rb create mode 100644 lib/puppet/string/v0.0.1/file.rb create mode 100644 lib/puppet/string/v0.0.1/key.rb create mode 100644 lib/puppet/string/v0.0.1/node.rb create mode 100644 lib/puppet/string/v0.0.1/report.rb create mode 100644 lib/puppet/string/v0.0.1/resource.rb create mode 100644 lib/puppet/string/v0.0.1/resource_type.rb create mode 100644 lib/puppet/string/v0.0.1/status.rb mode change 100644 => 100755 spec/unit/application/config_spec.rb mode change 100644 => 100755 spec/unit/application/indirection_base_spec.rb delete mode 100644 spec/unit/application/interface_base_spec.rb delete mode 100644 spec/unit/application/interface_spec.rb create mode 100755 spec/unit/application/string_base_spec.rb create mode 100755 spec/unit/application/string_spec.rb delete mode 100644 spec/unit/interface/action_builder_spec.rb delete mode 100755 spec/unit/interface/action_manager_spec.rb delete mode 100644 spec/unit/interface/action_spec.rb delete mode 100644 spec/unit/interface/catalog_spec.rb delete mode 100644 spec/unit/interface/certificate_request_spec.rb delete mode 100644 spec/unit/interface/certificate_revocation_list_spec.rb delete mode 100644 spec/unit/interface/certificate_spec.rb delete mode 100644 spec/unit/interface/config_spec.rb delete mode 100644 spec/unit/interface/configurer_spec.rb delete mode 100644 spec/unit/interface/facts_spec.rb delete mode 100644 spec/unit/interface/file_spec.rb delete mode 100644 spec/unit/interface/indirector_spec.rb delete mode 100644 spec/unit/interface/interface_collection_spec.rb delete mode 100644 spec/unit/interface/key_spec.rb delete mode 100644 spec/unit/interface/node_spec.rb delete mode 100644 spec/unit/interface/report_spec.rb delete mode 100644 spec/unit/interface/resource_spec.rb delete mode 100644 spec/unit/interface/resource_type_spec.rb delete mode 100755 spec/unit/interface_spec.rb create mode 100755 spec/unit/string/action_builder_spec.rb create mode 100755 spec/unit/string/action_manager_spec.rb create mode 100755 spec/unit/string/action_spec.rb create mode 100755 spec/unit/string/catalog_spec.rb create mode 100755 spec/unit/string/certificate_request_spec.rb create mode 100755 spec/unit/string/certificate_revocation_list_spec.rb create mode 100755 spec/unit/string/certificate_spec.rb create mode 100755 spec/unit/string/config_spec.rb create mode 100755 spec/unit/string/configurer_spec.rb create mode 100755 spec/unit/string/facts_spec.rb create mode 100755 spec/unit/string/file_spec.rb create mode 100755 spec/unit/string/indirector_spec.rb create mode 100755 spec/unit/string/key_spec.rb create mode 100755 spec/unit/string/node_spec.rb create mode 100755 spec/unit/string/report_spec.rb create mode 100755 spec/unit/string/resource_spec.rb create mode 100755 spec/unit/string/resource_type_spec.rb create mode 100755 spec/unit/string/string_collection_spec.rb create mode 100755 spec/unit/string_spec.rb diff --git a/lib/puppet/application/config.rb b/lib/puppet/application/config.rb index 90c5f53c4..f6559277b 100644 --- a/lib/puppet/application/config.rb +++ b/lib/puppet/application/config.rb @@ -1,4 +1,4 @@ -require 'puppet/application/interface_base' +require 'puppet/application/string_base' -class Puppet::Application::Config < Puppet::Application::InterfaceBase +class Puppet::Application::Config < Puppet::Application::StringBase end diff --git a/lib/puppet/application/configurer.rb b/lib/puppet/application/configurer.rb index 5c9af37d7..b440098ee 100644 --- a/lib/puppet/application/configurer.rb +++ b/lib/puppet/application/configurer.rb @@ -1,5 +1,5 @@ require 'puppet/application' -require 'puppet/interface' +require 'puppet/string' class Puppet::Application::Configurer < Puppet::Application should_parse_config @@ -17,7 +17,7 @@ class Puppet::Application::Configurer < Puppet::Application end def run_command - report = Puppet::Interface[:configurer, '0.0.1'].synchronize(Puppet[:certname]) - Puppet::Interface[:report, '0.0.1'].submit(report) + report = Puppet::String[:configurer, '0.0.1'].synchronize(Puppet[:certname]) + Puppet::String[:report, '0.0.1'].submit(report) end end diff --git a/lib/puppet/application/indirection_base.rb b/lib/puppet/application/indirection_base.rb index 7d1c851cf..da61f408d 100644 --- a/lib/puppet/application/indirection_base.rb +++ b/lib/puppet/application/indirection_base.rb @@ -1,6 +1,6 @@ -require 'puppet/application/interface_base' +require 'puppet/application/string_base' -class Puppet::Application::IndirectionBase < Puppet::Application::InterfaceBase +class Puppet::Application::IndirectionBase < Puppet::Application::StringBase option("--terminus TERMINUS") do |arg| @terminus = arg end @@ -10,10 +10,10 @@ class Puppet::Application::IndirectionBase < Puppet::Application::InterfaceBase def setup super - if interface.respond_to?(:indirection) - raise "Could not find data type #{type} for application #{self.class.name}" unless interface.indirection + if string.respond_to?(:indirection) + raise "Could not find data type #{type} for application #{self.class.name}" unless string.indirection - interface.set_terminus(terminus) if terminus + string.set_terminus(terminus) if terminus end end end diff --git a/lib/puppet/application/interface.rb b/lib/puppet/application/interface.rb deleted file mode 100644 index f447dc30d..000000000 --- a/lib/puppet/application/interface.rb +++ /dev/null @@ -1,95 +0,0 @@ -require 'puppet/application' -require 'puppet/interface' - -class Puppet::Application::Interface < Puppet::Application - - should_parse_config - run_mode :agent - - option("--debug", "-d") do |arg| - Puppet::Util::Log.level = :debug - end - - option("--verbose", "-v") do - Puppet::Util::Log.level = :info - end - - def list(*arguments) - if arguments.empty? - arguments = %w{terminuses actions} - end - interfaces.each do |name| - str = "#{name}:\n" - if arguments.include?("terminuses") - begin - terms = terminus_classes(name.to_sym) - str << "\tTerminuses: #{terms.join(", ")}\n" - rescue => detail - puts detail.backtrace if Puppet[:trace] - $stderr.puts "Could not load terminuses for #{name}: #{detail}" - end - end - - if arguments.include?("actions") - begin - actions = actions(name.to_sym) - str << "\tActions: #{actions.join(", ")}\n" - rescue => detail - puts detail.backtrace if Puppet[:trace] - $stderr.puts "Could not load actions for #{name}: #{detail}" - end - end - - print str - end - end - - attr_accessor :verb, :name, :arguments - - def main - # Call the method associated with the provided action (e.g., 'find'). - send(verb, *arguments) - end - - def setup - Puppet::Util::Log.newdestination :console - - load_applications # Call this to load all of the apps - - @verb, @arguments = command_line.args - @arguments ||= [] - - validate - end - - def validate - unless verb - raise "You must specify 'find', 'search', 'save', or 'destroy' as a verb; 'save' probably does not work right now" - end - - unless respond_to?(verb) - raise "Command '#{verb}' not found for 'interface'" - end - end - - def interfaces - Puppet::Interface.interfaces - end - - def terminus_classes(indirection) - Puppet::Indirector::Terminus.terminus_classes(indirection).collect { |t| t.to_s }.sort - end - - def actions(indirection) - return [] unless interface = Puppet::Interface[indirection, '0.0.1'] - interface.load_actions - return interface.actions.sort { |a,b| a.to_s <=> b.to_s } - end - - def load_applications - command_line.available_subcommands.each do |app| - command_line.require_application app - end - end -end - diff --git a/lib/puppet/application/interface_base.rb b/lib/puppet/application/interface_base.rb deleted file mode 100644 index 841f3ca12..000000000 --- a/lib/puppet/application/interface_base.rb +++ /dev/null @@ -1,97 +0,0 @@ -require 'puppet/application' -require 'puppet/interface' - -class Puppet::Application::InterfaceBase < Puppet::Application - should_parse_config - run_mode :agent - - def preinit - super - trap(:INT) do - $stderr.puts "Cancelling Interface" - exit(0) - end - end - - option("--debug", "-d") do |arg| - Puppet::Util::Log.level = :debug - end - - option("--verbose", "-v") do - Puppet::Util::Log.level = :info - end - - option("--format FORMAT") do |arg| - @format = arg.to_sym - end - - option("--mode RUNMODE", "-r") do |arg| - raise "Invalid run mode #{arg}; supported modes are user, agent, master" unless %w{user agent master}.include?(arg) - self.class.run_mode(arg.to_sym) - set_run_mode self.class.run_mode - end - - - attr_accessor :interface, :type, :verb, :arguments, :format - attr_writer :exit_code - - # This allows you to set the exit code if you don't want to just exit - # immediately but you need to indicate a failure. - def exit_code - @exit_code || 0 - end - - def main - # Call the method associated with the provided action (e.g., 'find'). - if result = interface.send(verb, *arguments) - puts render(result) - end - exit(exit_code) - end - - # Override this if you need custom rendering. - def render(result) - render_method = Puppet::Network::FormatHandler.format(format).render_method - if render_method == "to_pson" - jj result - exit(0) - else - result.send(render_method) - end - end - - def setup - Puppet::Util::Log.newdestination :console - - @verb = command_line.args.shift - @arguments = command_line.args - @arguments ||= [] - - @arguments = Array(@arguments) - - @type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym - - # TODO: These should be configurable versions. - unless Puppet::Interface.interface?(@type, :latest) - raise "Could not find any version of interface '#{@type}'" - end - @interface = Puppet::Interface[@type, :latest] - @format ||= @interface.default_format - - # We copy all of the app options to the interface. - # This allows each action to read in the options. - @interface.options = options - - validate - end - - def validate - unless verb - raise "You must specify #{interface.actions.join(", ")} as a verb; 'save' probably does not work right now" - end - - unless interface.action?(verb) - raise "Command '#{verb}' not found for #{type}" - end - end -end diff --git a/lib/puppet/application/string.rb b/lib/puppet/application/string.rb new file mode 100644 index 000000000..aa369e669 --- /dev/null +++ b/lib/puppet/application/string.rb @@ -0,0 +1,95 @@ +require 'puppet/application' +require 'puppet/string' + +class Puppet::Application::String < Puppet::Application + + should_parse_config + run_mode :agent + + option("--debug", "-d") do |arg| + Puppet::Util::Log.level = :debug + end + + option("--verbose", "-v") do + Puppet::Util::Log.level = :info + end + + def list(*arguments) + if arguments.empty? + arguments = %w{terminuses actions} + end + strings.each do |name| + str = "#{name}:\n" + if arguments.include?("terminuses") + begin + terms = terminus_classes(name.to_sym) + str << "\tTerminuses: #{terms.join(", ")}\n" + rescue => detail + puts detail.backtrace if Puppet[:trace] + $stderr.puts "Could not load terminuses for #{name}: #{detail}" + end + end + + if arguments.include?("actions") + begin + actions = actions(name.to_sym) + str << "\tActions: #{actions.join(", ")}\n" + rescue => detail + puts detail.backtrace if Puppet[:trace] + $stderr.puts "Could not load actions for #{name}: #{detail}" + end + end + + print str + end + end + + attr_accessor :verb, :name, :arguments + + def main + # Call the method associated with the provided action (e.g., 'find'). + send(verb, *arguments) + end + + def setup + Puppet::Util::Log.newdestination :console + + load_applications # Call this to load all of the apps + + @verb, @arguments = command_line.args + @arguments ||= [] + + validate + end + + def validate + unless verb + raise "You must specify 'find', 'search', 'save', or 'destroy' as a verb; 'save' probably does not work right now" + end + + unless respond_to?(verb) + raise "Command '#{verb}' not found for 'string'" + end + end + + def strings + Puppet::String.strings + end + + def terminus_classes(indirection) + Puppet::Indirector::Terminus.terminus_classes(indirection).collect { |t| t.to_s }.sort + end + + def actions(indirection) + return [] unless string = Puppet::String[indirection, '0.0.1'] + string.load_actions + return string.actions.sort { |a,b| a.to_s <=> b.to_s } + end + + def load_applications + command_line.available_subcommands.each do |app| + command_line.require_application app + end + end +end + diff --git a/lib/puppet/application/string_base.rb b/lib/puppet/application/string_base.rb new file mode 100644 index 000000000..5b701597d --- /dev/null +++ b/lib/puppet/application/string_base.rb @@ -0,0 +1,97 @@ +require 'puppet/application' +require 'puppet/string' + +class Puppet::Application::StringBase < Puppet::Application + should_parse_config + run_mode :agent + + def preinit + super + trap(:INT) do + $stderr.puts "Cancelling String" + exit(0) + end + end + + option("--debug", "-d") do |arg| + Puppet::Util::Log.level = :debug + end + + option("--verbose", "-v") do + Puppet::Util::Log.level = :info + end + + option("--format FORMAT") do |arg| + @format = arg.to_sym + end + + option("--mode RUNMODE", "-r") do |arg| + raise "Invalid run mode #{arg}; supported modes are user, agent, master" unless %w{user agent master}.include?(arg) + self.class.run_mode(arg.to_sym) + set_run_mode self.class.run_mode + end + + + attr_accessor :string, :type, :verb, :arguments, :format + attr_writer :exit_code + + # This allows you to set the exit code if you don't want to just exit + # immediately but you need to indicate a failure. + def exit_code + @exit_code || 0 + end + + def main + # Call the method associated with the provided action (e.g., 'find'). + if result = string.send(verb, *arguments) + puts render(result) + end + exit(exit_code) + end + + # Override this if you need custom rendering. + def render(result) + render_method = Puppet::Network::FormatHandler.format(format).render_method + if render_method == "to_pson" + jj result + exit(0) + else + result.send(render_method) + end + end + + def setup + Puppet::Util::Log.newdestination :console + + @verb = command_line.args.shift + @arguments = command_line.args + @arguments ||= [] + + @arguments = Array(@arguments) + + @type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym + + # TODO: These should be configurable versions. + unless Puppet::String.string?(@type, :latest) + raise "Could not find any version of string '#{@type}'" + end + @string = Puppet::String[@type, :latest] + @format ||= @string.default_format + + # We copy all of the app options to the string. + # This allows each action to read in the options. + @string.options = options + + validate + end + + def validate + unless verb + raise "You must specify #{string.actions.join(", ")} as a verb; 'save' probably does not work right now" + end + + unless string.action?(verb) + raise "Command '#{verb}' not found for #{type}" + end + end +end diff --git a/lib/puppet/interface.rb b/lib/puppet/interface.rb deleted file mode 100644 index a667c6b75..000000000 --- a/lib/puppet/interface.rb +++ /dev/null @@ -1,98 +0,0 @@ -require 'puppet' -require 'puppet/util/autoload' - -class Puppet::Interface - require 'puppet/interface/action_manager' - require 'puppet/interface/interface_collection' - - include Puppet::Interface::ActionManager - extend Puppet::Interface::ActionManager - - include Puppet::Util - - class << self - # This is just so we can search for actions. We only use its - # list of directories to search. - # Can't we utilize an external autoloader, or simply use the $LOAD_PATH? -pvb - def autoloader - @autoloader ||= Puppet::Util::Autoload.new(:application, "puppet/interface") - end - - def interfaces - Puppet::Interface::InterfaceCollection.interfaces - end - - def interface?(name, version) - Puppet::Interface::InterfaceCollection.interface?(name, version) - end - - def register(instance) - Puppet::Interface::InterfaceCollection.register(instance) - end - - def define(name, version, &block) - if interface?(name, version) - interface = Puppet::Interface::InterfaceCollection[name, version] - else - interface = self.new(name, version) - Puppet::Interface::InterfaceCollection.register(interface) - interface.load_actions - end - - interface.instance_eval(&block) if block_given? - - return interface - end - - alias :[] :define - end - - attr_accessor :default_format - - def set_default_format(format) - self.default_format = format.to_sym - end - - attr_accessor :type, :verb, :version, :arguments, :options - attr_reader :name - - def initialize(name, version, &block) - unless Puppet::Interface::InterfaceCollection.validate_version(version) - raise ArgumentError, "Cannot create interface with invalid version number '#{version}'!" - end - - @name = Puppet::Interface::InterfaceCollection.underscorize(name) - @version = version - @default_format = :pson - - instance_eval(&block) if block_given? - end - - # Try to find actions defined in other files. - def load_actions - path = "puppet/interface/v#{version}/#{name}" - - loaded = [] - Puppet::Interface.autoloader.search_directories.each do |dir| - fdir = ::File.join(dir, path) - next unless FileTest.directory?(fdir) - - Dir.chdir(fdir) do - Dir.glob("*.rb").each do |file| - aname = file.sub(/\.rb/, '') - if loaded.include?(aname) - Puppet.debug "Not loading duplicate action '#{aname}' for '#{name}' from '#{fdir}/#{file}'" - next - end - loaded << aname - Puppet.debug "Loading action '#{aname}' for '#{name}' from '#{fdir}/#{file}'" - require "#{path}/#{aname}" - end - end - end - end - - def to_s - "Puppet::Interface[#{name.inspect}, #{version.inspect}]" - end -end diff --git a/lib/puppet/interface/action.rb b/lib/puppet/interface/action.rb deleted file mode 100644 index 1a5730d1b..000000000 --- a/lib/puppet/interface/action.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'puppet/interface' - -class Puppet::Interface::Action - attr_reader :name - - def initialize(interface, name, attrs = {}) - name = name.to_s - raise "'#{name}' is an invalid action name" unless name =~ /^[a-z]\w*$/ - - @interface = interface - @name = name - attrs.each do |k,v| send("#{k}=", v) end - end - - def invoke(*args, &block) - @interface.method(name).call(*args,&block) - end - - def invoke=(block) - if @interface.is_a?(Class) - @interface.define_method(@name, &block) - else - @interface.meta_def(@name, &block) - end - end -end diff --git a/lib/puppet/interface/action_builder.rb b/lib/puppet/interface/action_builder.rb deleted file mode 100644 index e389ea3ea..000000000 --- a/lib/puppet/interface/action_builder.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'puppet/interface' -require 'puppet/interface/action' - -class Puppet::Interface::ActionBuilder - attr_reader :action - - def self.build(interface, name, &block) - name = name.to_s - raise "Action '#{name}' must specify a block" unless block - builder = new(interface, name, &block) - builder.action - end - - def initialize(interface, name, &block) - @interface = interface - @action = Puppet::Interface::Action.new(interface, name) - instance_eval(&block) - end - - # Ideally the method we're defining here would be added to the action, and a - # method on the interface would defer to it, but we can't get scope correct, - # so we stick with this. --daniel 2011-03-24 - def invoke(&block) - raise "Invoke called on an ActionBuilder with no corresponding Action" unless @action - @action.invoke = block - end -end diff --git a/lib/puppet/interface/action_manager.rb b/lib/puppet/interface/action_manager.rb deleted file mode 100644 index 8b2944bb1..000000000 --- a/lib/puppet/interface/action_manager.rb +++ /dev/null @@ -1,45 +0,0 @@ -require 'puppet/interface/action_builder' - -module Puppet::Interface::ActionManager - # Declare that this app can take a specific action, and provide - # the code to do so. - def action(name, &block) - @actions ||= {} - name = name.to_s.downcase.to_sym - - raise "Action #{name} already defined for #{self}" if action?(name) - - action = Puppet::Interface::ActionBuilder.build(self, name, &block) - - @actions[name] = action - end - - # This is the short-form of an action definition; it doesn't use the - # builder, just creates the action directly from the block. - def script(name, &block) - @actions ||= {} - name = name.to_s.downcase.to_sym - raise "Action #{name} already defined for #{self}" if action?(name) - @actions[name] = Puppet::Interface::Action.new(self, name, :invoke => block) - end - - def actions - @actions ||= {} - result = @actions.keys - - if self.is_a?(Class) and superclass.respond_to?(:actions) - result += superclass.actions - elsif self.class.respond_to?(:actions) - result += self.class.actions - end - result.sort - end - - def get_action(name) - @actions[name].dup - end - - def action?(name) - actions.include?(name.to_sym) - end -end diff --git a/lib/puppet/interface/indirector.rb b/lib/puppet/interface/indirector.rb deleted file mode 100644 index 485af4779..000000000 --- a/lib/puppet/interface/indirector.rb +++ /dev/null @@ -1,79 +0,0 @@ -require 'puppet' -require 'puppet/interface' - -class Puppet::Interface::Indirector < Puppet::Interface - def self.indirections - Puppet::Indirector::Indirection.instances.collect { |t| t.to_s }.sort - end - - def self.terminus_classes(indirection) - Puppet::Indirector::Terminus.terminus_classes(indirection.to_sym).collect { |t| t.to_s }.sort - end - - action :destroy do - invoke { |*args| call_indirection_method(:destroy, *args) } - end - - action :find do - invoke { |*args| call_indirection_method(:find, *args) } - end - - action :save do - invoke { |*args| call_indirection_method(:save, *args) } - end - - action :search do - invoke { |*args| call_indirection_method(:search, *args) } - end - - # Print the configuration for the current terminus class - action :info do - invoke do |*args| - if t = indirection.terminus_class - puts "Run mode '#{Puppet.run_mode.name}': #{t}" - else - $stderr.puts "No default terminus class for run mode '#{Puppet.run_mode.name}'" - end - end - end - - attr_accessor :from - - def indirection_name - @indirection_name || name.to_sym - end - - # Here's your opportunity to override the indirection name. By default - # it will be the same name as the interface. - def set_indirection_name(name) - @indirection_name = name - end - - # Return an indirection associated with an interface, if one exists - # One usually does. - def indirection - unless @indirection - Puppet.info("Could not find terminus for #{indirection_name}") unless @indirection = Puppet::Indirector::Indirection.instance(indirection_name) - end - @indirection - end - - def set_terminus(from) - begin - indirection.terminus_class = from - rescue => detail - raise "Could not set '#{indirection.name}' terminus to '#{from}' (#{detail}); valid terminus types are #{terminus_classes(indirection.name).join(", ") }" - end - end - - def call_indirection_method(method, *args) - begin - result = indirection.send(method, *args) - rescue => detail - puts detail.backtrace if Puppet[:trace] - raise "Could not call '#{method}' on '#{indirection_name}': #{detail}" - end - - result - end -end diff --git a/lib/puppet/interface/interface_collection.rb b/lib/puppet/interface/interface_collection.rb deleted file mode 100644 index 92e2933fe..000000000 --- a/lib/puppet/interface/interface_collection.rb +++ /dev/null @@ -1,98 +0,0 @@ -require 'puppet/interface' - -module Puppet::Interface::InterfaceCollection - SEMVER_VERSION = /^(\d+)\.(\d+)\.(\d+)([A-Za-z][0-9A-Za-z-]*|)$/ - - @interfaces = Hash.new { |hash, key| hash[key] = {} } - - def self.interfaces - unless @loaded - @loaded = true - $LOAD_PATH.each do |dir| - next unless FileTest.directory?(dir) - Dir.chdir(dir) do - Dir.glob("puppet/interface/v*/*.rb").collect { |f| f.sub(/\.rb/, '') }.each do |file| - iname = file.sub(/\.rb/, '') - begin - require iname - rescue Exception => detail - puts detail.backtrace if Puppet[:trace] - raise "Could not load #{iname} from #{dir}/#{file}: #{detail}" - end - end - end - end - end - return @interfaces.keys - end - - def self.versions(name) - versions = [] - $LOAD_PATH.each do |dir| - next unless FileTest.directory?(dir) - v_dir = File.join dir, %w[puppet interface v*] - Dir.glob(File.join v_dir, "#{name}{.rb,/*.rb}").each do |f| - v = f.sub(%r[.*/v([^/]+?)/#{name}(?:(?:/[^/]+)?.rb)$], '\1') - if validate_version(v) - versions << v - else - warn "'#{v}' (#{f}) is not a valid version string; skipping" - end - end - end - return versions.uniq.sort { |a, b| compare_versions(a, b) } - end - - def self.validate_version(version) - !!(SEMVER_VERSION =~ version.to_s) - end - - def self.compare_versions(a, b) - a, b = [a, b].map do |x| - parts = SEMVER_VERSION.match(x).to_a[1..4] - parts[0..2] = parts[0..2].map { |e| e.to_i } - parts - end - - cmp = a[0..2] <=> b[0..2] - if cmp == 0 - cmp = a[3] <=> b[3] - cmp = +1 if a[3].empty? && !b[3].empty? - cmp = -1 if b[3].empty? && !a[3].empty? - end - cmp - end - - def self.[](name, version) - version = versions(name).last if version == :latest - unless version.nil? - @interfaces[underscorize(name)][version] if interface?(name, version) - end - end - - def self.interface?(name, version) - version = versions(name).last if version == :latest - return false if version.nil? - - name = underscorize(name) - - unless @interfaces.has_key?(name) && @interfaces[name].has_key?(version) - require "puppet/interface/v#{version}/#{name}" - end - return @interfaces.has_key?(name) && @interfaces[name].has_key?(version) - rescue LoadError - return false - end - - def self.register(interface) - @interfaces[underscorize(interface.name)][interface.version] = interface - end - - def self.underscorize(name) - unless name.to_s =~ /^[-_a-z]+$/i then - raise ArgumentError, "#{name.inspect} (#{name.class}) is not a valid interface name" - end - - name.to_s.downcase.split(/[-_]/).join('_').to_sym - end -end diff --git a/lib/puppet/interface/v0.0.1/catalog.rb b/lib/puppet/interface/v0.0.1/catalog.rb deleted file mode 100644 index 7d61528bc..000000000 --- a/lib/puppet/interface/v0.0.1/catalog.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:catalog, '0.0.1') do - action(:apply) do - invoke do |catalog| - report = Puppet::Transaction::Report.new("apply") - report.configuration_version = catalog.version - - Puppet::Util::Log.newdestination(report) - - begin - benchmark(:notice, "Finished catalog run") do - catalog.apply(:report => report) - end - rescue => detail - puts detail.backtrace if Puppet[:trace] - Puppet.err "Failed to apply catalog: #{detail}" - end - - report.finalize_report - report - end - end - - action(:download) do - invoke do |certname,facts| - Puppet::Resource::Catalog.terminus_class = :rest - facts_to_upload = {:facts_format => :b64_zlib_yaml, :facts => CGI.escape(facts.render(:b64_zlib_yaml))} - catalog = nil - retrieval_duration = thinmark do - catalog = Puppet::Interface[:catalog, '0.0.1'].find(certname, facts_to_upload) - end - catalog = catalog.to_ral - catalog.finalize - catalog.retrieval_duration = retrieval_duration - catalog.write_class_file - catalog - end - end -end diff --git a/lib/puppet/interface/v0.0.1/catalog/select.rb b/lib/puppet/interface/v0.0.1/catalog/select.rb deleted file mode 100644 index 35f1a1e0b..000000000 --- a/lib/puppet/interface/v0.0.1/catalog/select.rb +++ /dev/null @@ -1,10 +0,0 @@ -# Select and show a list of resources of a given type. -Puppet::Interface.define(:catalog, '0.0.1') do - action :select do - invoke do |host,type| - catalog = Puppet::Resource::Catalog.indirection.find(host) - - catalog.resources.reject { |res| res.type != type }.each { |res| puts res } - end - end -end diff --git a/lib/puppet/interface/v0.0.1/certificate.rb b/lib/puppet/interface/v0.0.1/certificate.rb deleted file mode 100644 index 2615e3d86..000000000 --- a/lib/puppet/interface/v0.0.1/certificate.rb +++ /dev/null @@ -1,28 +0,0 @@ -require 'puppet/interface/indirector' -require 'puppet/ssl/host' - -Puppet::Interface::Indirector.define(:certificate, '0.0.1') do - - action :generate do - invoke do |name| - host = Puppet::SSL::Host.new(name) - host.generate_certificate_request - host.certificate_request.class.indirection.save(host.certificate_request) - end - end - - action :list do - invoke do - Puppet::SSL::Host.indirection.search("*", { - :for => :certificate_request, - }).map { |h| h.inspect } - end - end - - action :sign do - invoke do |name| - Puppet::SSL::Host.indirection.save(Puppet::SSL::Host.new(name)) - end - end - -end diff --git a/lib/puppet/interface/v0.0.1/certificate_request.rb b/lib/puppet/interface/v0.0.1/certificate_request.rb deleted file mode 100644 index e5ed1b51e..000000000 --- a/lib/puppet/interface/v0.0.1/certificate_request.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:certificate_request, '0.0.1') do -end diff --git a/lib/puppet/interface/v0.0.1/certificate_revocation_list.rb b/lib/puppet/interface/v0.0.1/certificate_revocation_list.rb deleted file mode 100644 index f6d8a3d6d..000000000 --- a/lib/puppet/interface/v0.0.1/certificate_revocation_list.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:certificate_revocation_list, '0.0.1') do -end diff --git a/lib/puppet/interface/v0.0.1/config.rb b/lib/puppet/interface/v0.0.1/config.rb deleted file mode 100644 index 7b74ce542..000000000 --- a/lib/puppet/interface/v0.0.1/config.rb +++ /dev/null @@ -1,11 +0,0 @@ -require 'puppet/interface' - -Puppet::Interface.define(:config, '0.0.1') do - action(:print) do - invoke do |*args| - Puppet.settings[:configprint] = args.join(",") - Puppet.settings.print_config_options - nil - end - end -end diff --git a/lib/puppet/interface/v0.0.1/configurer.rb b/lib/puppet/interface/v0.0.1/configurer.rb deleted file mode 100644 index 38536b684..000000000 --- a/lib/puppet/interface/v0.0.1/configurer.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'puppet/interface' - -Puppet::Interface.define(:configurer, '0.0.1') do - action(:synchronize) do - invoke do |certname| - facts = Puppet::Interface[:facts, '0.0.1'].find(certname) - catalog = Puppet::Interface[:catalog, '0.0.1'].download(certname, facts) - report = Puppet::Interface[:catalog, '0.0.1'].apply(catalog) - report - end - end -end diff --git a/lib/puppet/interface/v0.0.1/facts.rb b/lib/puppet/interface/v0.0.1/facts.rb deleted file mode 100644 index c4bbad845..000000000 --- a/lib/puppet/interface/v0.0.1/facts.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'puppet/interface/indirector' -require 'puppet/node/facts' - -Puppet::Interface::Indirector.define(:facts, '0.0.1') do - set_default_format :yaml - - # Upload our facts to the server - action(:upload) do - invoke do |*args| - Puppet::Node::Facts.indirection.terminus_class = :facter - facts = Puppet::Node::Facts.indirection.find(Puppet[:certname]) - Puppet::Node::Facts.indirection.terminus_class = :rest - Puppet::Node::Facts.indirection.save(facts) - Puppet.notice "Uploaded facts for '#{Puppet[:certname]}'" - nil - end - end -end diff --git a/lib/puppet/interface/v0.0.1/file.rb b/lib/puppet/interface/v0.0.1/file.rb deleted file mode 100644 index 91904e8e0..000000000 --- a/lib/puppet/interface/v0.0.1/file.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:file, '0.0.1') do - set_indirection_name :file_bucket_file -end diff --git a/lib/puppet/interface/v0.0.1/key.rb b/lib/puppet/interface/v0.0.1/key.rb deleted file mode 100644 index fbc9b67b1..000000000 --- a/lib/puppet/interface/v0.0.1/key.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:key, '0.0.1') do -end diff --git a/lib/puppet/interface/v0.0.1/node.rb b/lib/puppet/interface/v0.0.1/node.rb deleted file mode 100644 index 4ecec1478..000000000 --- a/lib/puppet/interface/v0.0.1/node.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:node, '0.0.1') do - set_default_format :yaml -end diff --git a/lib/puppet/interface/v0.0.1/report.rb b/lib/puppet/interface/v0.0.1/report.rb deleted file mode 100644 index bacb46e70..000000000 --- a/lib/puppet/interface/v0.0.1/report.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:report, '0.0.1') do - action(:submit) do - invoke do |report| - begin - Puppet::Transaction::Report.terminus_class = :rest - report.save - rescue => detail - puts detail.backtrace if Puppet[:trace] - Puppet.err "Could not send report: #{detail}" - end - end - end -end diff --git a/lib/puppet/interface/v0.0.1/resource.rb b/lib/puppet/interface/v0.0.1/resource.rb deleted file mode 100644 index 1a6f3b69d..000000000 --- a/lib/puppet/interface/v0.0.1/resource.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:resource, '0.0.1') do -end diff --git a/lib/puppet/interface/v0.0.1/resource_type.rb b/lib/puppet/interface/v0.0.1/resource_type.rb deleted file mode 100644 index 6f5547c4d..000000000 --- a/lib/puppet/interface/v0.0.1/resource_type.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:resource_type, '0.0.1') do -end diff --git a/lib/puppet/interface/v0.0.1/status.rb b/lib/puppet/interface/v0.0.1/status.rb deleted file mode 100644 index 7f4b56a2b..000000000 --- a/lib/puppet/interface/v0.0.1/status.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:status, '0.0.1') do -end diff --git a/lib/puppet/string.rb b/lib/puppet/string.rb new file mode 100644 index 000000000..b5f7b9048 --- /dev/null +++ b/lib/puppet/string.rb @@ -0,0 +1,98 @@ +require 'puppet' +require 'puppet/util/autoload' + +class Puppet::String + require 'puppet/string/action_manager' + require 'puppet/string/string_collection' + + include Puppet::String::ActionManager + extend Puppet::String::ActionManager + + include Puppet::Util + + class << self + # This is just so we can search for actions. We only use its + # list of directories to search. + # Can't we utilize an external autoloader, or simply use the $LOAD_PATH? -pvb + def autoloader + @autoloader ||= Puppet::Util::Autoload.new(:application, "puppet/string") + end + + def strings + Puppet::String::StringCollection.strings + end + + def string?(name, version) + Puppet::String::StringCollection.string?(name, version) + end + + def register(instance) + Puppet::String::StringCollection.register(instance) + end + + def define(name, version, &block) + if string?(name, version) + string = Puppet::String::StringCollection[name, version] + else + string = self.new(name, version) + Puppet::String::StringCollection.register(string) + string.load_actions + end + + string.instance_eval(&block) if block_given? + + return string + end + + alias :[] :define + end + + attr_accessor :default_format + + def set_default_format(format) + self.default_format = format.to_sym + end + + attr_accessor :type, :verb, :version, :arguments, :options + attr_reader :name + + def initialize(name, version, &block) + unless Puppet::String::StringCollection.validate_version(version) + raise ArgumentError, "Cannot create string with invalid version number '#{version}'!" + end + + @name = Puppet::String::StringCollection.underscorize(name) + @version = version + @default_format = :pson + + instance_eval(&block) if block_given? + end + + # Try to find actions defined in other files. + def load_actions + path = "puppet/string/v#{version}/#{name}" + + loaded = [] + Puppet::String.autoloader.search_directories.each do |dir| + fdir = ::File.join(dir, path) + next unless FileTest.directory?(fdir) + + Dir.chdir(fdir) do + Dir.glob("*.rb").each do |file| + aname = file.sub(/\.rb/, '') + if loaded.include?(aname) + Puppet.debug "Not loading duplicate action '#{aname}' for '#{name}' from '#{fdir}/#{file}'" + next + end + loaded << aname + Puppet.debug "Loading action '#{aname}' for '#{name}' from '#{fdir}/#{file}'" + require "#{path}/#{aname}" + end + end + end + end + + def to_s + "Puppet::String[#{name.inspect}, #{version.inspect}]" + end +end diff --git a/lib/puppet/string/action.rb b/lib/puppet/string/action.rb new file mode 100644 index 000000000..4db9e97e2 --- /dev/null +++ b/lib/puppet/string/action.rb @@ -0,0 +1,26 @@ +require 'puppet/string' + +class Puppet::String::Action + attr_reader :name + + def initialize(string, name, attrs = {}) + name = name.to_s + raise "'#{name}' is an invalid action name" unless name =~ /^[a-z]\w*$/ + + @string = string + @name = name + attrs.each do |k,v| send("#{k}=", v) end + end + + def invoke(*args, &block) + @string.method(name).call(*args,&block) + end + + def invoke=(block) + if @string.is_a?(Class) + @string.define_method(@name, &block) + else + @string.meta_def(@name, &block) + end + end +end diff --git a/lib/puppet/string/action_builder.rb b/lib/puppet/string/action_builder.rb new file mode 100644 index 000000000..b3db51104 --- /dev/null +++ b/lib/puppet/string/action_builder.rb @@ -0,0 +1,27 @@ +require 'puppet/string' +require 'puppet/string/action' + +class Puppet::String::ActionBuilder + attr_reader :action + + def self.build(string, name, &block) + name = name.to_s + raise "Action '#{name}' must specify a block" unless block + builder = new(string, name, &block) + builder.action + end + + def initialize(string, name, &block) + @string = string + @action = Puppet::String::Action.new(string, name) + instance_eval(&block) + end + + # Ideally the method we're defining here would be added to the action, and a + # method on the string would defer to it, but we can't get scope correct, + # so we stick with this. --daniel 2011-03-24 + def invoke(&block) + raise "Invoke called on an ActionBuilder with no corresponding Action" unless @action + @action.invoke = block + end +end diff --git a/lib/puppet/string/action_manager.rb b/lib/puppet/string/action_manager.rb new file mode 100644 index 000000000..c29dbf454 --- /dev/null +++ b/lib/puppet/string/action_manager.rb @@ -0,0 +1,45 @@ +require 'puppet/string/action_builder' + +module Puppet::String::ActionManager + # Declare that this app can take a specific action, and provide + # the code to do so. + def action(name, &block) + @actions ||= {} + name = name.to_s.downcase.to_sym + + raise "Action #{name} already defined for #{self}" if action?(name) + + action = Puppet::String::ActionBuilder.build(self, name, &block) + + @actions[name] = action + end + + # This is the short-form of an action definition; it doesn't use the + # builder, just creates the action directly from the block. + def script(name, &block) + @actions ||= {} + name = name.to_s.downcase.to_sym + raise "Action #{name} already defined for #{self}" if action?(name) + @actions[name] = Puppet::String::Action.new(self, name, :invoke => block) + end + + def actions + @actions ||= {} + result = @actions.keys + + if self.is_a?(Class) and superclass.respond_to?(:actions) + result += superclass.actions + elsif self.class.respond_to?(:actions) + result += self.class.actions + end + result.sort + end + + def get_action(name) + @actions[name].dup + end + + def action?(name) + actions.include?(name.to_sym) + end +end diff --git a/lib/puppet/string/indirector.rb b/lib/puppet/string/indirector.rb new file mode 100644 index 000000000..15984e39e --- /dev/null +++ b/lib/puppet/string/indirector.rb @@ -0,0 +1,79 @@ +require 'puppet' +require 'puppet/string' + +class Puppet::String::Indirector < Puppet::String + def self.indirections + Puppet::Indirector::Indirection.instances.collect { |t| t.to_s }.sort + end + + def self.terminus_classes(indirection) + Puppet::Indirector::Terminus.terminus_classes(indirection.to_sym).collect { |t| t.to_s }.sort + end + + action :destroy do + invoke { |*args| call_indirection_method(:destroy, *args) } + end + + action :find do + invoke { |*args| call_indirection_method(:find, *args) } + end + + action :save do + invoke { |*args| call_indirection_method(:save, *args) } + end + + action :search do + invoke { |*args| call_indirection_method(:search, *args) } + end + + # Print the configuration for the current terminus class + action :info do + invoke do |*args| + if t = indirection.terminus_class + puts "Run mode '#{Puppet.run_mode.name}': #{t}" + else + $stderr.puts "No default terminus class for run mode '#{Puppet.run_mode.name}'" + end + end + end + + attr_accessor :from + + def indirection_name + @indirection_name || name.to_sym + end + + # Here's your opportunity to override the indirection name. By default + # it will be the same name as the string. + def set_indirection_name(name) + @indirection_name = name + end + + # Return an indirection associated with an string, if one exists + # One usually does. + def indirection + unless @indirection + Puppet.info("Could not find terminus for #{indirection_name}") unless @indirection = Puppet::Indirector::Indirection.instance(indirection_name) + end + @indirection + end + + def set_terminus(from) + begin + indirection.terminus_class = from + rescue => detail + raise "Could not set '#{indirection.name}' terminus to '#{from}' (#{detail}); valid terminus types are #{terminus_classes(indirection.name).join(", ") }" + end + end + + def call_indirection_method(method, *args) + begin + result = indirection.send(method, *args) + rescue => detail + puts detail.backtrace if Puppet[:trace] + raise "Could not call '#{method}' on '#{indirection_name}': #{detail}" + end + + result + end +end diff --git a/lib/puppet/string/string_collection.rb b/lib/puppet/string/string_collection.rb new file mode 100644 index 000000000..e9cba7f55 --- /dev/null +++ b/lib/puppet/string/string_collection.rb @@ -0,0 +1,98 @@ +require 'puppet/string' + +module Puppet::String::StringCollection + SEMVER_VERSION = /^(\d+)\.(\d+)\.(\d+)([A-Za-z][0-9A-Za-z-]*|)$/ + + @strings = Hash.new { |hash, key| hash[key] = {} } + + def self.strings + unless @loaded + @loaded = true + $LOAD_PATH.each do |dir| + next unless FileTest.directory?(dir) + Dir.chdir(dir) do + Dir.glob("puppet/string/v*/*.rb").collect { |f| f.sub(/\.rb/, '') }.each do |file| + iname = file.sub(/\.rb/, '') + begin + require iname + rescue Exception => detail + puts detail.backtrace if Puppet[:trace] + raise "Could not load #{iname} from #{dir}/#{file}: #{detail}" + end + end + end + end + end + return @strings.keys + end + + def self.versions(name) + versions = [] + $LOAD_PATH.each do |dir| + next unless FileTest.directory?(dir) + v_dir = File.join dir, %w[puppet string v*] + Dir.glob(File.join v_dir, "#{name}{.rb,/*.rb}").each do |f| + v = f.sub(%r[.*/v([^/]+?)/#{name}(?:(?:/[^/]+)?.rb)$], '\1') + if validate_version(v) + versions << v + else + warn "'#{v}' (#{f}) is not a valid version string; skipping" + end + end + end + return versions.uniq.sort { |a, b| compare_versions(a, b) } + end + + def self.validate_version(version) + !!(SEMVER_VERSION =~ version.to_s) + end + + def self.compare_versions(a, b) + a, b = [a, b].map do |x| + parts = SEMVER_VERSION.match(x).to_a[1..4] + parts[0..2] = parts[0..2].map { |e| e.to_i } + parts + end + + cmp = a[0..2] <=> b[0..2] + if cmp == 0 + cmp = a[3] <=> b[3] + cmp = +1 if a[3].empty? && !b[3].empty? + cmp = -1 if b[3].empty? && !a[3].empty? + end + cmp + end + + def self.[](name, version) + version = versions(name).last if version == :latest + unless version.nil? + @strings[underscorize(name)][version] if string?(name, version) + end + end + + def self.string?(name, version) + version = versions(name).last if version == :latest + return false if version.nil? + + name = underscorize(name) + + unless @strings.has_key?(name) && @strings[name].has_key?(version) + require "puppet/string/v#{version}/#{name}" + end + return @strings.has_key?(name) && @strings[name].has_key?(version) + rescue LoadError + return false + end + + def self.register(string) + @strings[underscorize(string.name)][string.version] = string + end + + def self.underscorize(name) + unless name.to_s =~ /^[-_a-z]+$/i then + raise ArgumentError, "#{name.inspect} (#{name.class}) is not a valid string name" + end + + name.to_s.downcase.split(/[-_]/).join('_').to_sym + end +end diff --git a/lib/puppet/string/v0.0.1/catalog.rb b/lib/puppet/string/v0.0.1/catalog.rb new file mode 100644 index 000000000..0ddd83176 --- /dev/null +++ b/lib/puppet/string/v0.0.1/catalog.rb @@ -0,0 +1,40 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:catalog, '0.0.1') do + action(:apply) do + invoke do |catalog| + report = Puppet::Transaction::Report.new("apply") + report.configuration_version = catalog.version + + Puppet::Util::Log.newdestination(report) + + begin + benchmark(:notice, "Finished catalog run") do + catalog.apply(:report => report) + end + rescue => detail + puts detail.backtrace if Puppet[:trace] + Puppet.err "Failed to apply catalog: #{detail}" + end + + report.finalize_report + report + end + end + + action(:download) do + invoke do |certname,facts| + Puppet::Resource::Catalog.terminus_class = :rest + facts_to_upload = {:facts_format => :b64_zlib_yaml, :facts => CGI.escape(facts.render(:b64_zlib_yaml))} + catalog = nil + retrieval_duration = thinmark do + catalog = Puppet::String[:catalog, '0.0.1'].find(certname, facts_to_upload) + end + catalog = catalog.to_ral + catalog.finalize + catalog.retrieval_duration = retrieval_duration + catalog.write_class_file + catalog + end + end +end diff --git a/lib/puppet/string/v0.0.1/catalog/select.rb b/lib/puppet/string/v0.0.1/catalog/select.rb new file mode 100644 index 000000000..52c77d3ce --- /dev/null +++ b/lib/puppet/string/v0.0.1/catalog/select.rb @@ -0,0 +1,10 @@ +# Select and show a list of resources of a given type. +Puppet::String.define(:catalog, '0.0.1') do + action :select do + invoke do |host,type| + catalog = Puppet::Resource::Catalog.indirection.find(host) + + catalog.resources.reject { |res| res.type != type }.each { |res| puts res } + end + end +end diff --git a/lib/puppet/string/v0.0.1/certificate.rb b/lib/puppet/string/v0.0.1/certificate.rb new file mode 100644 index 000000000..7b2e5f397 --- /dev/null +++ b/lib/puppet/string/v0.0.1/certificate.rb @@ -0,0 +1,28 @@ +require 'puppet/string/indirector' +require 'puppet/ssl/host' + +Puppet::String::Indirector.define(:certificate, '0.0.1') do + + action :generate do + invoke do |name| + host = Puppet::SSL::Host.new(name) + host.generate_certificate_request + host.certificate_request.class.indirection.save(host.certificate_request) + end + end + + action :list do + invoke do + Puppet::SSL::Host.indirection.search("*", { + :for => :certificate_request, + }).map { |h| h.inspect } + end + end + + action :sign do + invoke do |name| + Puppet::SSL::Host.indirection.save(Puppet::SSL::Host.new(name)) + end + end + +end diff --git a/lib/puppet/string/v0.0.1/certificate_request.rb b/lib/puppet/string/v0.0.1/certificate_request.rb new file mode 100644 index 000000000..218b40b98 --- /dev/null +++ b/lib/puppet/string/v0.0.1/certificate_request.rb @@ -0,0 +1,4 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:certificate_request, '0.0.1') do +end diff --git a/lib/puppet/string/v0.0.1/certificate_revocation_list.rb b/lib/puppet/string/v0.0.1/certificate_revocation_list.rb new file mode 100644 index 000000000..9731b4f2d --- /dev/null +++ b/lib/puppet/string/v0.0.1/certificate_revocation_list.rb @@ -0,0 +1,4 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:certificate_revocation_list, '0.0.1') do +end diff --git a/lib/puppet/string/v0.0.1/config.rb b/lib/puppet/string/v0.0.1/config.rb new file mode 100644 index 000000000..ae1a408cf --- /dev/null +++ b/lib/puppet/string/v0.0.1/config.rb @@ -0,0 +1,11 @@ +require 'puppet/string' + +Puppet::String.define(:config, '0.0.1') do + action(:print) do + invoke do |*args| + Puppet.settings[:configprint] = args.join(",") + Puppet.settings.print_config_options + nil + end + end +end diff --git a/lib/puppet/string/v0.0.1/configurer.rb b/lib/puppet/string/v0.0.1/configurer.rb new file mode 100644 index 000000000..a6ea74b6a --- /dev/null +++ b/lib/puppet/string/v0.0.1/configurer.rb @@ -0,0 +1,12 @@ +require 'puppet/string' + +Puppet::String.define(:configurer, '0.0.1') do + action(:synchronize) do + invoke do |certname| + facts = Puppet::String[:facts, '0.0.1'].find(certname) + catalog = Puppet::String[:catalog, '0.0.1'].download(certname, facts) + report = Puppet::String[:catalog, '0.0.1'].apply(catalog) + report + end + end +end diff --git a/lib/puppet/string/v0.0.1/facts.rb b/lib/puppet/string/v0.0.1/facts.rb new file mode 100644 index 000000000..73acb0df6 --- /dev/null +++ b/lib/puppet/string/v0.0.1/facts.rb @@ -0,0 +1,18 @@ +require 'puppet/string/indirector' +require 'puppet/node/facts' + +Puppet::String::Indirector.define(:facts, '0.0.1') do + set_default_format :yaml + + # Upload our facts to the server + action(:upload) do + invoke do |*args| + Puppet::Node::Facts.indirection.terminus_class = :facter + facts = Puppet::Node::Facts.indirection.find(Puppet[:certname]) + Puppet::Node::Facts.indirection.terminus_class = :rest + Puppet::Node::Facts.indirection.save(facts) + Puppet.notice "Uploaded facts for '#{Puppet[:certname]}'" + nil + end + end +end diff --git a/lib/puppet/string/v0.0.1/file.rb b/lib/puppet/string/v0.0.1/file.rb new file mode 100644 index 000000000..cc5737f28 --- /dev/null +++ b/lib/puppet/string/v0.0.1/file.rb @@ -0,0 +1,5 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:file, '0.0.1') do + set_indirection_name :file_bucket_file +end diff --git a/lib/puppet/string/v0.0.1/key.rb b/lib/puppet/string/v0.0.1/key.rb new file mode 100644 index 000000000..95aceade5 --- /dev/null +++ b/lib/puppet/string/v0.0.1/key.rb @@ -0,0 +1,4 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:key, '0.0.1') do +end diff --git a/lib/puppet/string/v0.0.1/node.rb b/lib/puppet/string/v0.0.1/node.rb new file mode 100644 index 000000000..bc31a2cf3 --- /dev/null +++ b/lib/puppet/string/v0.0.1/node.rb @@ -0,0 +1,5 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:node, '0.0.1') do + set_default_format :yaml +end diff --git a/lib/puppet/string/v0.0.1/report.rb b/lib/puppet/string/v0.0.1/report.rb new file mode 100644 index 000000000..55a008533 --- /dev/null +++ b/lib/puppet/string/v0.0.1/report.rb @@ -0,0 +1,15 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:report, '0.0.1') do + action(:submit) do + invoke do |report| + begin + Puppet::Transaction::Report.terminus_class = :rest + report.save + rescue => detail + puts detail.backtrace if Puppet[:trace] + Puppet.err "Could not send report: #{detail}" + end + end + end +end diff --git a/lib/puppet/string/v0.0.1/resource.rb b/lib/puppet/string/v0.0.1/resource.rb new file mode 100644 index 000000000..9838be0fa --- /dev/null +++ b/lib/puppet/string/v0.0.1/resource.rb @@ -0,0 +1,4 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:resource, '0.0.1') do +end diff --git a/lib/puppet/string/v0.0.1/resource_type.rb b/lib/puppet/string/v0.0.1/resource_type.rb new file mode 100644 index 000000000..8ca31ea6c --- /dev/null +++ b/lib/puppet/string/v0.0.1/resource_type.rb @@ -0,0 +1,4 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:resource_type, '0.0.1') do +end diff --git a/lib/puppet/string/v0.0.1/status.rb b/lib/puppet/string/v0.0.1/status.rb new file mode 100644 index 000000000..41de2bb99 --- /dev/null +++ b/lib/puppet/string/v0.0.1/status.rb @@ -0,0 +1,4 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:status, '0.0.1') do +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c329c12e7..4e54d7235 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,7 +4,7 @@ $LOAD_PATH.unshift(dir, dir + 'lib', dir + '../lib') require 'mocha' require 'puppet' -require 'puppet/interface' +require 'puppet/string' require 'rspec' RSpec.configure do |config| diff --git a/spec/unit/application/config_spec.rb b/spec/unit/application/config_spec.rb old mode 100644 new mode 100755 index 3d894a89c..a45adc8d3 --- a/spec/unit/application/config_spec.rb +++ b/spec/unit/application/config_spec.rb @@ -4,7 +4,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') require 'puppet/application/config' describe Puppet::Application::Config do - it "should be a subclass of Puppet::Application::InterfaceBase" do - Puppet::Application::Config.superclass.should equal(Puppet::Application::InterfaceBase) + it "should be a subclass of Puppet::Application::StringBase" do + Puppet::Application::Config.superclass.should equal(Puppet::Application::StringBase) end end diff --git a/spec/unit/application/indirection_base_spec.rb b/spec/unit/application/indirection_base_spec.rb old mode 100644 new mode 100755 index 2e7bd65a9..ecc49d9a9 --- a/spec/unit/application/indirection_base_spec.rb +++ b/spec/unit/application/indirection_base_spec.rb @@ -7,6 +7,6 @@ describe Puppet::Application::IndirectionBase do it "should support a 'from' terminus" describe "setup" do - it "should fail if its interface does not support an indirection" + it "should fail if its string does not support an indirection" end end diff --git a/spec/unit/application/interface_base_spec.rb b/spec/unit/application/interface_base_spec.rb deleted file mode 100644 index d82325bfd..000000000 --- a/spec/unit/application/interface_base_spec.rb +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') -require 'puppet/application/interface_base' - -describe Puppet::Application::InterfaceBase do - before :all do - @dir = Dir.mktmpdir - $LOAD_PATH.push(@dir) - FileUtils.mkdir_p(File.join @dir, 'puppet', 'interface', 'v0.0.1') - FileUtils.touch(File.join @dir, 'puppet', 'interface', 'v0.0.1', 'basetest.rb') - end - - after :all do - FileUtils.remove_entry_secure @dir - $LOAD_PATH.pop - end - - base_interface = Puppet::Interface.define(:basetest, '0.0.1') - class Puppet::Application::InterfaceBase::Basetest < Puppet::Application::InterfaceBase - end - - before do - @app = Puppet::Application::InterfaceBase::Basetest.new - @app.stubs(:interface).returns base_interface - @app.stubs(:exit) - @app.stubs(:puts) - Puppet::Util::Log.stubs(:newdestination) - end - - describe "when calling main" do - before do - @app.verb = :find - @app.arguments = ["myname", "myarg"] - @app.interface.stubs(:find) - end - - it "should send the specified verb and name to the interface" do - @app.interface.expects(:find).with("myname", "myarg") - - @app.main - end - - it "should use its render method to render any result" - - it "should exit with the current exit code" - end - - describe "during setup" do - before do - @app.command_line.stubs(:args).returns(["find", "myname", "myarg"]) - @app.stubs(:validate) - end - - it "should set the verb from the command line arguments" do - @app.setup - @app.verb.should == "find" - end - - it "should make sure arguments are an array" do - @app.command_line.stubs(:args).returns(["find", "myname", "myarg"]) - @app.setup - @app.arguments.should == ["myname", "myarg"] - end - - it "should set the options on the interface" do - @app.options[:foo] = "bar" - @app.setup - - @app.interface.options.should == @app.options - end - end -end diff --git a/spec/unit/application/interface_spec.rb b/spec/unit/application/interface_spec.rb deleted file mode 100644 index 153e9bdc1..000000000 --- a/spec/unit/application/interface_spec.rb +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') -require 'puppet/application/interface' - -describe Puppet::Application::Interface do - it "should be an application" do - Puppet::Application::Interface.superclass.should equal(Puppet::Application) - end -end diff --git a/spec/unit/application/string_base_spec.rb b/spec/unit/application/string_base_spec.rb new file mode 100755 index 000000000..bc563e11d --- /dev/null +++ b/spec/unit/application/string_base_spec.rb @@ -0,0 +1,73 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') +require 'puppet/application/string_base' + +describe Puppet::Application::StringBase do + before :all do + @dir = Dir.mktmpdir + $LOAD_PATH.push(@dir) + FileUtils.mkdir_p(File.join @dir, 'puppet', 'string', 'v0.0.1') + FileUtils.touch(File.join @dir, 'puppet', 'string', 'v0.0.1', 'basetest.rb') + end + + after :all do + FileUtils.remove_entry_secure @dir + $LOAD_PATH.pop + end + + base_string = Puppet::String.define(:basetest, '0.0.1') + class Puppet::Application::StringBase::Basetest < Puppet::Application::StringBase + end + + before do + @app = Puppet::Application::StringBase::Basetest.new + @app.stubs(:string).returns base_string + @app.stubs(:exit) + @app.stubs(:puts) + Puppet::Util::Log.stubs(:newdestination) + end + + describe "when calling main" do + before do + @app.verb = :find + @app.arguments = ["myname", "myarg"] + @app.string.stubs(:find) + end + + it "should send the specified verb and name to the string" do + @app.string.expects(:find).with("myname", "myarg") + + @app.main + end + + it "should use its render method to render any result" + + it "should exit with the current exit code" + end + + describe "during setup" do + before do + @app.command_line.stubs(:args).returns(["find", "myname", "myarg"]) + @app.stubs(:validate) + end + + it "should set the verb from the command line arguments" do + @app.setup + @app.verb.should == "find" + end + + it "should make sure arguments are an array" do + @app.command_line.stubs(:args).returns(["find", "myname", "myarg"]) + @app.setup + @app.arguments.should == ["myname", "myarg"] + end + + it "should set the options on the string" do + @app.options[:foo] = "bar" + @app.setup + + @app.string.options.should == @app.options + end + end +end diff --git a/spec/unit/application/string_spec.rb b/spec/unit/application/string_spec.rb new file mode 100755 index 000000000..13af0a546 --- /dev/null +++ b/spec/unit/application/string_spec.rb @@ -0,0 +1,10 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') +require 'puppet/application/string' + +describe Puppet::Application::String do + it "should be an application" do + Puppet::Application::String.superclass.should equal(Puppet::Application) + end +end diff --git a/spec/unit/interface/action_builder_spec.rb b/spec/unit/interface/action_builder_spec.rb deleted file mode 100644 index 27e817fe9..000000000 --- a/spec/unit/interface/action_builder_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') -require 'puppet/interface/action_builder' - -describe Puppet::Interface::ActionBuilder do - describe "::build" do - it "should build an action" do - action = Puppet::Interface::ActionBuilder.build(nil,:foo) do - end - action.should be_a(Puppet::Interface::Action) - action.name.should == "foo" - end - - it "should define a method on the interface which invokes the action" do - interface = Puppet::Interface.new(:action_builder_test_interface, '0.0.1') - action = Puppet::Interface::ActionBuilder.build(interface, :foo) do - invoke do - "invoked the method" - end - end - - interface.foo.should == "invoked the method" - end - - it "should require a block" do - lambda { Puppet::Interface::ActionBuilder.build(nil,:foo) }.should raise_error("Action 'foo' must specify a block") - end - end -end diff --git a/spec/unit/interface/action_manager_spec.rb b/spec/unit/interface/action_manager_spec.rb deleted file mode 100755 index 3aff7ac11..000000000 --- a/spec/unit/interface/action_manager_spec.rb +++ /dev/null @@ -1,216 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') - -# This is entirely an internal class for Interface, so we have to load it instead of our class. -require 'puppet/interface' - -class ActionManagerTester - include Puppet::Interface::ActionManager -end - -describe Puppet::Interface::ActionManager do - subject { ActionManagerTester.new } - - describe "when included in a class" do - it "should be able to define an action" do - subject.action(:foo) do - invoke { "something "} - end - end - - it "should be able to define a 'script' style action" do - subject.script :bar do - "a bar is where beer is found" - end - end - - it "should be able to list defined actions" do - subject.action(:foo) do - invoke { "something" } - end - subject.action(:bar) do - invoke { "something" } - end - - subject.actions.should =~ [:foo, :bar] - end - - it "should list 'script' actions" do - subject.script :foo do "foo" end - subject.actions.should =~ [:foo] - end - - it "should list both script and normal actions" do - subject.action :foo do - invoke do "foo" end - end - subject.script :bar do "a bar is where beer is found" end - - subject.actions.should =~ [:foo, :bar] - end - - it "should be able to indicate when an action is defined" do - subject.action(:foo) do - invoke { "something" } - end - - subject.should be_action(:foo) - end - - it "should indicate an action is defined for script actions" do - subject.script :foo do "foo" end - subject.should be_action :foo - end - - it "should correctly treat action names specified as strings" do - subject.action(:foo) do - invoke { "something" } - end - - subject.should be_action("foo") - end - end - - describe "when used to extend a class" do - subject { Class.new.extend(Puppet::Interface::ActionManager) } - - it "should be able to define an action" do - subject.action(:foo) do - invoke { "something "} - end - end - - it "should be able to list defined actions" do - subject.action(:foo) do - invoke { "something" } - end - subject.action(:bar) do - invoke { "something" } - end - - subject.actions.should include(:bar) - subject.actions.should include(:foo) - end - - it "should be able to indicate when an action is defined" do - subject.action(:foo) { "something" } - subject.should be_action(:foo) - end - end - - describe "when used both at the class and instance level" do - before do - @klass = Class.new do - include Puppet::Interface::ActionManager - extend Puppet::Interface::ActionManager - end - @instance = @klass.new - end - - it "should be able to define an action at the class level" do - @klass.action(:foo) do - invoke { "something "} - end - end - - it "should create an instance method when an action is defined at the class level" do - @klass.action(:foo) do - invoke { "something" } - end - @instance.foo.should == "something" - end - - it "should be able to define an action at the instance level" do - @instance.action(:foo) do - invoke { "something "} - end - end - - it "should create an instance method when an action is defined at the instance level" do - @instance.action(:foo) do - invoke { "something" } - end - @instance.foo.should == "something" - end - - it "should be able to list actions defined at the class level" do - @klass.action(:foo) do - invoke { "something" } - end - @klass.action(:bar) do - invoke { "something" } - end - - @klass.actions.should include(:bar) - @klass.actions.should include(:foo) - end - - it "should be able to list actions defined at the instance level" do - @instance.action(:foo) do - invoke { "something" } - end - @instance.action(:bar) do - invoke { "something" } - end - - @instance.actions.should include(:bar) - @instance.actions.should include(:foo) - end - - it "should be able to list actions defined at both instance and class level" do - @klass.action(:foo) do - invoke { "something" } - end - @instance.action(:bar) do - invoke { "something" } - end - - @instance.actions.should include(:bar) - @instance.actions.should include(:foo) - end - - it "should be able to indicate when an action is defined at the class level" do - @klass.action(:foo) do - invoke { "something" } - end - @instance.should be_action(:foo) - end - - it "should be able to indicate when an action is defined at the instance level" do - @klass.action(:foo) do - invoke { "something" } - end - @instance.should be_action(:foo) - end - - it "should list actions defined in superclasses" do - @subclass = Class.new(@klass) - @instance = @subclass.new - - @klass.action(:parent) do - invoke { "a" } - end - @subclass.action(:sub) do - invoke { "a" } - end - @instance.action(:instance) do - invoke { "a" } - end - - @instance.should be_action(:parent) - @instance.should be_action(:sub) - @instance.should be_action(:instance) - end - - it "should create an instance method when an action is defined in a superclass" do - @subclass = Class.new(@klass) - @instance = @subclass.new - - @klass.action(:foo) do - invoke { "something" } - end - @instance.foo.should == "something" - end - end -end diff --git a/spec/unit/interface/action_spec.rb b/spec/unit/interface/action_spec.rb deleted file mode 100644 index 292caabb9..000000000 --- a/spec/unit/interface/action_spec.rb +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') -require 'puppet/interface/action' - -describe Puppet::Interface::Action do - describe "when validating the action name" do - it "should require a name" do - lambda { Puppet::Interface::Action.new(nil,nil) }.should raise_error("'' is an invalid action name") - end - - it "should not allow empty names" do - lambda { Puppet::Interface::Action.new(nil,'') }.should raise_error("'' is an invalid action name") - end - - it "should not allow names with whitespace" do - lambda { Puppet::Interface::Action.new(nil,'foo bar') }.should raise_error("'foo bar' is an invalid action name") - end - - it "should not allow names beginning with dashes" do - lambda { Puppet::Interface::Action.new(nil,'-foobar') }.should raise_error("'-foobar' is an invalid action name") - end - end - - describe "when invoking" do - it "should be able to call other actions on the same object" do - interface = Puppet::Interface.new(:my_interface, '0.0.1') do - action(:foo) do - invoke { 25 } - end - - action(:bar) do - invoke { "the value of foo is '#{foo}'" } - end - end - interface.foo.should == 25 - interface.bar.should == "the value of foo is '25'" - end - - # bar is a class action calling a class action - # quux is a class action calling an instance action - # baz is an instance action calling a class action - # qux is an instance action calling an instance action - it "should be able to call other actions on the same object when defined on a class" do - class Puppet::Interface::MyInterfaceBaseClass < Puppet::Interface - action(:foo) do - invoke { 25 } - end - - action(:bar) do - invoke { "the value of foo is '#{foo}'" } - end - - action(:quux) do - invoke { "qux told me #{qux}" } - end - end - - interface = Puppet::Interface::MyInterfaceBaseClass.new(:my_inherited_interface, '0.0.1') do - action(:baz) do - invoke { "the value of foo in baz is '#{foo}'" } - end - - action(:qux) do - invoke { baz } - end - end - interface.foo.should == 25 - interface.bar.should == "the value of foo is '25'" - interface.quux.should == "qux told me the value of foo in baz is '25'" - interface.baz.should == "the value of foo in baz is '25'" - interface.qux.should == "the value of foo in baz is '25'" - end - end -end diff --git a/spec/unit/interface/catalog_spec.rb b/spec/unit/interface/catalog_spec.rb deleted file mode 100644 index c615181ed..000000000 --- a/spec/unit/interface/catalog_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') - -describe Puppet::Interface.define(:catalog, '0.0.1') do -end diff --git a/spec/unit/interface/certificate_request_spec.rb b/spec/unit/interface/certificate_request_spec.rb deleted file mode 100644 index 0143ebc85..000000000 --- a/spec/unit/interface/certificate_request_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') - -describe Puppet::Interface.define(:certificate_request, '0.0.1') do -end diff --git a/spec/unit/interface/certificate_revocation_list_spec.rb b/spec/unit/interface/certificate_revocation_list_spec.rb deleted file mode 100644 index 9655dd3fa..000000000 --- a/spec/unit/interface/certificate_revocation_list_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') - -describe Puppet::Interface.define(:certificate_revocation_list, '0.0.1') do -end diff --git a/spec/unit/interface/certificate_spec.rb b/spec/unit/interface/certificate_spec.rb deleted file mode 100644 index e5c63e456..000000000 --- a/spec/unit/interface/certificate_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') - -describe Puppet::Interface.define(:certificate, '0.0.1') do -end diff --git a/spec/unit/interface/config_spec.rb b/spec/unit/interface/config_spec.rb deleted file mode 100644 index 2e82b0b08..000000000 --- a/spec/unit/interface/config_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') - -describe Puppet::Interface.define(:config, '0.0.1') do - it "should use Settings#print_config_options when asked to print" do - Puppet.settings.stubs(:puts) - Puppet.settings.expects(:print_config_options) - subject.print - end - - it "should set 'configprint' to all desired values and call print_config_options when a specific value is provided" do - Puppet.settings.stubs(:puts) - Puppet.settings.expects(:print_config_options) - subject.print("libdir", "ssldir") - Puppet.settings[:configprint].should == "libdir,ssldir" - end - - it "should always return nil" do - Puppet.settings.stubs(:puts) - Puppet.settings.expects(:print_config_options) - subject.print("libdir").should be_nil - end -end diff --git a/spec/unit/interface/configurer_spec.rb b/spec/unit/interface/configurer_spec.rb deleted file mode 100644 index e97e63b65..000000000 --- a/spec/unit/interface/configurer_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') -require 'puppet/indirector/catalog/rest' -require 'tempfile' - -describe Puppet::Interface.define(:configurer, '0.0.1') do - describe "#synchronize" do - it "should retrieve and apply a catalog and return a report" do - dirname = Dir.mktmpdir("puppetdir") - Puppet[:vardir] = dirname - Puppet[:confdir] = dirname - @catalog = Puppet::Resource::Catalog.new - @file = Puppet::Resource.new(:file, File.join(dirname, "tmp_dir_resource"), :parameters => {:ensure => :present}) - @catalog.add_resource(@file) - Puppet::Resource::Catalog::Rest.any_instance.stubs(:find).returns(@catalog) - - report = subject.synchronize("foo") - - report.kind.should == "apply" - report.status.should == "changed" - end - end -end diff --git a/spec/unit/interface/facts_spec.rb b/spec/unit/interface/facts_spec.rb deleted file mode 100644 index 5f0214fd5..000000000 --- a/spec/unit/interface/facts_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') - -describe Puppet::Interface.define(:facts, '0.0.1') do - it "should define an 'upload' fact" do - subject.should be_action(:upload) - end - - it "should set its default format to :yaml" do - subject.default_format.should == :yaml - end - - describe "when uploading" do - it "should set the terminus_class to :facter" - - it "should set the cach_eclass to :rest" - - it "should find the current certname" - end -end diff --git a/spec/unit/interface/file_spec.rb b/spec/unit/interface/file_spec.rb deleted file mode 100644 index bd6e31c0e..000000000 --- a/spec/unit/interface/file_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') - -describe Puppet::Interface.define(:file, '0.0.1') do -end diff --git a/spec/unit/interface/indirector_spec.rb b/spec/unit/interface/indirector_spec.rb deleted file mode 100644 index 4b2beaefc..000000000 --- a/spec/unit/interface/indirector_spec.rb +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') -require 'puppet/interface/indirector' - -describe Puppet::Interface::Indirector do - before do - @instance = Puppet::Interface::Indirector.new(:test, '0.0.1') - - @indirection = stub 'indirection', :name => :stub_indirection - - @instance.stubs(:indirection).returns @indirection - end - - it "should be able to return a list of indirections" do - Puppet::Interface::Indirector.indirections.should be_include("catalog") - end - - it "should be able to return a list of terminuses for a given indirection" do - Puppet::Interface::Indirector.terminus_classes(:catalog).should be_include("compiler") - end - - describe "as an instance" do - it "should be able to determine its indirection" do - # Loading actions here an get, um, complicated - Puppet::Interface.stubs(:load_actions) - Puppet::Interface::Indirector.new(:catalog, '0.0.1').indirection.should equal(Puppet::Resource::Catalog.indirection) - end - end - - [:find, :search, :save, :destroy].each do |method| - it "should define a '#{method}' action" do - Puppet::Interface::Indirector.should be_action(method) - end - - it "should just call the indirection method when the '#{method}' action is invoked" do - @instance.indirection.expects(method).with(:test, "myargs") - @instance.send(method, :test, "myargs") - end - end - - it "should be able to override its indirection name" do - @instance.set_indirection_name :foo - @instance.indirection_name.should == :foo - end - - it "should be able to set its terminus class" do - @instance.indirection.expects(:terminus_class=).with(:myterm) - @instance.set_terminus(:myterm) - end - - it "should define a class-level 'info' action" do - Puppet::Interface::Indirector.should be_action(:info) - end -end diff --git a/spec/unit/interface/interface_collection_spec.rb b/spec/unit/interface/interface_collection_spec.rb deleted file mode 100644 index 3e4b9d624..000000000 --- a/spec/unit/interface/interface_collection_spec.rb +++ /dev/null @@ -1,249 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') -require 'tmpdir' - -describe Puppet::Interface::InterfaceCollection do - before :all do - @interfaces = subject.instance_variable_get("@interfaces").dup - end - - before :each do - subject.instance_variable_get("@interfaces").clear - end - - after :all do - subject.instance_variable_set("@interfaces", @interfaces) - end - - describe "::interfaces" do - end - - describe "::versions" do - before :each do - @dir = Dir.mktmpdir - @lib = FileUtils.mkdir_p(File.join @dir, 'puppet', 'interface') - $LOAD_PATH.push(@dir) - end - - after :each do - FileUtils.remove_entry_secure @dir - $LOAD_PATH.pop - end - - it "should return an empty array when no versions are loadable" do - subject.versions(:fozzie).should == [] - end - - it "should return versions loadable as puppet/interface/v{version}/{name}" do - FileUtils.mkdir_p(File.join @lib, 'v1.0.0') - FileUtils.touch(File.join @lib, 'v1.0.0', 'fozzie.rb') - subject.versions(:fozzie).should == ['1.0.0'] - end - - it "should an ordered list of all versions loadable as puppet/interface/v{version}/{name}" do - %w[ 1.2.1rc2 1.2.1beta1 1.2.1rc1 1.2.1 1.2.2 ].each do |version| - FileUtils.mkdir_p(File.join @lib, "v#{version}") - FileUtils.touch(File.join @lib, "v#{version}", 'fozzie.rb') - end - subject.versions(:fozzie).should == %w[ 1.2.1beta1 1.2.1rc1 1.2.1rc2 1.2.1 1.2.2 ] - end - - it "should not return a version for an empty puppet/interface/v{version}/{name}" do - FileUtils.mkdir_p(File.join @lib, 'v1.0.0', 'fozzie') - subject.versions(:fozzie).should == [] - end - - it "should an ordered list of all versions loadable as puppet/interface/v{version}/{name}/*.rb" do - %w[ 1.2.1rc2 1.2.1beta1 1.2.1rc1 1.2.1 1.2.2 ].each do |version| - FileUtils.mkdir_p(File.join @lib, "v#{version}", "fozzie") - FileUtils.touch(File.join @lib, "v#{version}", 'fozzie', 'action.rb') - end - subject.versions(:fozzie).should == %w[ 1.2.1beta1 1.2.1rc1 1.2.1rc2 1.2.1 1.2.2 ] - end - end - - describe "::validate_version" do - it 'should permit three number versions' do - subject.validate_version('10.10.10').should == true - end - - it 'should permit versions with appended descriptions' do - subject.validate_version('10.10.10beta').should == true - end - - it 'should not permit versions with more than three numbers' do - subject.validate_version('1.2.3.4').should == false - end - - it 'should not permit versions with only two numbers' do - subject.validate_version('10.10').should == false - end - - it 'should not permit versions with only one number' do - subject.validate_version('123').should == false - end - - it 'should not permit versions with text in any position but at the end' do - subject.validate_version('v1.1.1').should == false - end - end - - describe "::compare_versions" do - # (a <=> b) should be: - # -1 if a < b - # 0 if a == b - # 1 if a > b - it 'should sort major version numbers numerically' do - subject.compare_versions('1.0.0', '2.0.0').should == -1 - subject.compare_versions('2.0.0', '1.1.1').should == 1 - subject.compare_versions('2.0.0', '10.0.0').should == -1 - end - - it 'should sort minor version numbers numerically' do - subject.compare_versions('0.1.0', '0.2.0').should == -1 - subject.compare_versions('0.2.0', '0.1.1').should == 1 - subject.compare_versions('0.2.0', '0.10.0').should == -1 - end - - it 'should sort tiny version numbers numerically' do - subject.compare_versions('0.0.1', '0.0.2').should == -1 - subject.compare_versions('0.0.2', '0.0.1').should == 1 - subject.compare_versions('0.0.2', '0.0.10').should == -1 - end - - it 'should sort major version before minor version' do - subject.compare_versions('1.1.0', '1.2.0').should == -1 - subject.compare_versions('1.2.0', '1.1.1').should == 1 - subject.compare_versions('1.2.0', '1.10.0').should == -1 - - subject.compare_versions('1.1.0', '2.2.0').should == -1 - subject.compare_versions('2.2.0', '1.1.1').should == 1 - subject.compare_versions('2.2.0', '1.10.0').should == 1 - end - - it 'should sort minor version before tiny version' do - subject.compare_versions('0.1.1', '0.1.2').should == -1 - subject.compare_versions('0.1.2', '0.1.1').should == 1 - subject.compare_versions('0.1.2', '0.1.10').should == -1 - - subject.compare_versions('0.1.1', '0.2.2').should == -1 - subject.compare_versions('0.2.2', '0.1.1').should == 1 - subject.compare_versions('0.2.2', '0.1.10').should == 1 - end - - it 'should sort appended strings asciibetically' do - subject.compare_versions('0.0.0a', '0.0.0b').should == -1 - subject.compare_versions('0.0.0beta1', '0.0.0beta2').should == -1 - subject.compare_versions('0.0.0beta1', '0.0.0rc1').should == -1 - subject.compare_versions('0.0.0beta1', '0.0.0alpha1').should == 1 - subject.compare_versions('0.0.0beta1', '0.0.0beta1').should == 0 - end - - it "should sort appended strings before 'whole' versions" do - subject.compare_versions('0.0.1a', '0.0.1').should == -1 - subject.compare_versions('0.0.1', '0.0.1beta').should == 1 - end - end - - describe "::[]" do - before :each do - subject.instance_variable_get("@interfaces")[:foo]['0.0.1'] = 10 - end - - before :each do - @dir = Dir.mktmpdir - @lib = FileUtils.mkdir_p(File.join @dir, 'puppet', 'interface') - $LOAD_PATH.push(@dir) - end - - after :each do - FileUtils.remove_entry_secure @dir - $LOAD_PATH.pop - end - - it "should return the interface with the given name" do - subject["foo", '0.0.1'].should == 10 - end - - it "should attempt to load the interface if it isn't found" do - subject.expects(:require).with('puppet/interface/v0.0.1/bar') - subject["bar", '0.0.1'] - end - - it "should attempt to load the interface with the greatest version for specified version :latest" do - %w[ 1.2.1 1.2.2 ].each do |version| - FileUtils.mkdir_p(File.join @lib, "v#{version}") - FileUtils.touch(File.join @lib, "v#{version}", 'fozzie.rb') - end - subject.expects(:require).with('puppet/interface/v1.2.2/fozzie') - subject['fozzie', :latest] - end - end - - describe "::interface?" do - before :each do - subject.instance_variable_get("@interfaces")[:foo]['0.0.1'] = 10 - end - - it "should return true if the interface specified is registered" do - subject.interface?("foo", '0.0.1').should == true - end - - it "should attempt to require the interface if it is not registered" do - subject.expects(:require).with('puppet/interface/v0.0.1/bar') - subject.interface?("bar", '0.0.1') - end - - it "should return true if requiring the interface registered it" do - subject.stubs(:require).with do - subject.instance_variable_get("@interfaces")[:bar]['0.0.1'] = 20 - end - subject.interface?("bar", '0.0.1').should == true - end - - it "should return false if the interface is not registered" do - subject.stubs(:require).returns(true) - subject.interface?("bar", '0.0.1').should == false - end - - it "should return false if there is a LoadError requiring the interface" do - subject.stubs(:require).raises(LoadError) - subject.interface?("bar", '0.0.1').should == false - end - end - - describe "::register" do - it "should store the interface by name" do - interface = Puppet::Interface.new(:my_interface, '0.0.1') - subject.register(interface) - subject.instance_variable_get("@interfaces").should == {:my_interface => {'0.0.1' => interface}} - end - end - - describe "::underscorize" do - faulty = [1, "#foo", "$bar", "sturm und drang", :"sturm und drang"] - valid = { - "Foo" => :foo, - :Foo => :foo, - "foo_bar" => :foo_bar, - :foo_bar => :foo_bar, - "foo-bar" => :foo_bar, - :"foo-bar" => :foo_bar, - } - - valid.each do |input, expect| - it "should map #{input.inspect} to #{expect.inspect}" do - result = subject.underscorize(input) - result.should == expect - end - end - - faulty.each do |input| - it "should fail when presented with #{input.inspect} (#{input.class})" do - expect { subject.underscorize(input) }. - should raise_error ArgumentError, /not a valid interface name/ - end - end - end -end diff --git a/spec/unit/interface/key_spec.rb b/spec/unit/interface/key_spec.rb deleted file mode 100644 index 519497808..000000000 --- a/spec/unit/interface/key_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') - -describe Puppet::Interface.define(:key, '0.0.1') do -end diff --git a/spec/unit/interface/node_spec.rb b/spec/unit/interface/node_spec.rb deleted file mode 100644 index 91914c3ae..000000000 --- a/spec/unit/interface/node_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') - -describe Puppet::Interface.define(:node, '0.0.1') do - it "should set its default format to :yaml" do - subject.default_format.should == :yaml - end -end diff --git a/spec/unit/interface/report_spec.rb b/spec/unit/interface/report_spec.rb deleted file mode 100644 index 23855db09..000000000 --- a/spec/unit/interface/report_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') - -describe Puppet::Interface.define(:report, '0.0.1') do -end diff --git a/spec/unit/interface/resource_spec.rb b/spec/unit/interface/resource_spec.rb deleted file mode 100644 index 408be250c..000000000 --- a/spec/unit/interface/resource_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') - -describe Puppet::Interface.define(:resource, '0.0.1') do -end diff --git a/spec/unit/interface/resource_type_spec.rb b/spec/unit/interface/resource_type_spec.rb deleted file mode 100644 index 860be282c..000000000 --- a/spec/unit/interface/resource_type_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') - -describe Puppet::Interface.define(:resource_type, '0.0.1') do -end diff --git a/spec/unit/interface_spec.rb b/spec/unit/interface_spec.rb deleted file mode 100755 index cf7d209da..000000000 --- a/spec/unit/interface_spec.rb +++ /dev/null @@ -1,83 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb') - -describe Puppet::Interface do - before :all do - @interfaces = Puppet::Interface::InterfaceCollection.instance_variable_get("@interfaces").dup - end - - before :each do - Puppet::Interface::InterfaceCollection.instance_variable_get("@interfaces").clear - end - - after :all do - Puppet::Interface::InterfaceCollection.instance_variable_set("@interfaces", @interfaces) - end - - describe "#define" do - it "should register the interface" do - interface = Puppet::Interface.define(:interface_test_register, '0.0.1') - interface.should == Puppet::Interface[:interface_test_register, '0.0.1'] - end - - it "should load actions" do - Puppet::Interface.any_instance.expects(:load_actions) - Puppet::Interface.define(:interface_test_load_actions, '0.0.1') - end - - it "should require a version number" do - proc { Puppet::Interface.define(:no_version) }.should raise_error(ArgumentError) - end - end - - describe "#initialize" do - it "should require a version number" do - proc { Puppet::Interface.new(:no_version) }.should raise_error(ArgumentError) - end - - it "should require a valid version number" do - proc { Puppet::Interface.new(:bad_version, 'Rasins') }.should raise_error(ArgumentError) - end - - it "should instance-eval any provided block" do - face = Puppet::Interface.new(:interface_test_block,'0.0.1') do - action(:something) do - invoke { "foo" } - end - end - - face.something.should == "foo" - end - end - - it "should have a name" do - Puppet::Interface.new(:me,'0.0.1').name.should == :me - end - - it "should stringify with its own name" do - Puppet::Interface.new(:me,'0.0.1').to_s.should =~ /\bme\b/ - end - - it "should allow overriding of the default format" do - face = Puppet::Interface.new(:me,'0.0.1') - face.set_default_format :foo - face.default_format.should == :foo - end - - it "should default to :pson for its format" do - Puppet::Interface.new(:me, '0.0.1').default_format.should == :pson - end - - # Why? - it "should create a class-level autoloader" do - Puppet::Interface.autoloader.should be_instance_of(Puppet::Util::Autoload) - end - - it "should try to require interfaces that are not known" do - Puppet::Interface::InterfaceCollection.expects(:require).with "puppet/interface/v0.0.1/foo" - Puppet::Interface[:foo, '0.0.1'] - end - - it "should be able to load all actions in all search paths" -end diff --git a/spec/unit/string/action_builder_spec.rb b/spec/unit/string/action_builder_spec.rb new file mode 100755 index 000000000..c3395cf6a --- /dev/null +++ b/spec/unit/string/action_builder_spec.rb @@ -0,0 +1,30 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') +require 'puppet/string/action_builder' + +describe Puppet::String::ActionBuilder do + describe "::build" do + it "should build an action" do + action = Puppet::String::ActionBuilder.build(nil,:foo) do + end + action.should be_a(Puppet::String::Action) + action.name.should == "foo" + end + + it "should define a method on the string which invokes the action" do + string = Puppet::String.new(:action_builder_test_string, '0.0.1') + action = Puppet::String::ActionBuilder.build(string, :foo) do + invoke do + "invoked the method" + end + end + + string.foo.should == "invoked the method" + end + + it "should require a block" do + lambda { Puppet::String::ActionBuilder.build(nil,:foo) }.should raise_error("Action 'foo' must specify a block") + end + end +end diff --git a/spec/unit/string/action_manager_spec.rb b/spec/unit/string/action_manager_spec.rb new file mode 100755 index 000000000..3921f02c0 --- /dev/null +++ b/spec/unit/string/action_manager_spec.rb @@ -0,0 +1,216 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +# This is entirely an internal class for String, so we have to load it instead of our class. +require 'puppet/string' + +class ActionManagerTester + include Puppet::String::ActionManager +end + +describe Puppet::String::ActionManager do + subject { ActionManagerTester.new } + + describe "when included in a class" do + it "should be able to define an action" do + subject.action(:foo) do + invoke { "something "} + end + end + + it "should be able to define a 'script' style action" do + subject.script :bar do + "a bar is where beer is found" + end + end + + it "should be able to list defined actions" do + subject.action(:foo) do + invoke { "something" } + end + subject.action(:bar) do + invoke { "something" } + end + + subject.actions.should =~ [:foo, :bar] + end + + it "should list 'script' actions" do + subject.script :foo do "foo" end + subject.actions.should =~ [:foo] + end + + it "should list both script and normal actions" do + subject.action :foo do + invoke do "foo" end + end + subject.script :bar do "a bar is where beer is found" end + + subject.actions.should =~ [:foo, :bar] + end + + it "should be able to indicate when an action is defined" do + subject.action(:foo) do + invoke { "something" } + end + + subject.should be_action(:foo) + end + + it "should indicate an action is defined for script actions" do + subject.script :foo do "foo" end + subject.should be_action :foo + end + + it "should correctly treat action names specified as strings" do + subject.action(:foo) do + invoke { "something" } + end + + subject.should be_action("foo") + end + end + + describe "when used to extend a class" do + subject { Class.new.extend(Puppet::String::ActionManager) } + + it "should be able to define an action" do + subject.action(:foo) do + invoke { "something "} + end + end + + it "should be able to list defined actions" do + subject.action(:foo) do + invoke { "something" } + end + subject.action(:bar) do + invoke { "something" } + end + + subject.actions.should include(:bar) + subject.actions.should include(:foo) + end + + it "should be able to indicate when an action is defined" do + subject.action(:foo) { "something" } + subject.should be_action(:foo) + end + end + + describe "when used both at the class and instance level" do + before do + @klass = Class.new do + include Puppet::String::ActionManager + extend Puppet::String::ActionManager + end + @instance = @klass.new + end + + it "should be able to define an action at the class level" do + @klass.action(:foo) do + invoke { "something "} + end + end + + it "should create an instance method when an action is defined at the class level" do + @klass.action(:foo) do + invoke { "something" } + end + @instance.foo.should == "something" + end + + it "should be able to define an action at the instance level" do + @instance.action(:foo) do + invoke { "something "} + end + end + + it "should create an instance method when an action is defined at the instance level" do + @instance.action(:foo) do + invoke { "something" } + end + @instance.foo.should == "something" + end + + it "should be able to list actions defined at the class level" do + @klass.action(:foo) do + invoke { "something" } + end + @klass.action(:bar) do + invoke { "something" } + end + + @klass.actions.should include(:bar) + @klass.actions.should include(:foo) + end + + it "should be able to list actions defined at the instance level" do + @instance.action(:foo) do + invoke { "something" } + end + @instance.action(:bar) do + invoke { "something" } + end + + @instance.actions.should include(:bar) + @instance.actions.should include(:foo) + end + + it "should be able to list actions defined at both instance and class level" do + @klass.action(:foo) do + invoke { "something" } + end + @instance.action(:bar) do + invoke { "something" } + end + + @instance.actions.should include(:bar) + @instance.actions.should include(:foo) + end + + it "should be able to indicate when an action is defined at the class level" do + @klass.action(:foo) do + invoke { "something" } + end + @instance.should be_action(:foo) + end + + it "should be able to indicate when an action is defined at the instance level" do + @klass.action(:foo) do + invoke { "something" } + end + @instance.should be_action(:foo) + end + + it "should list actions defined in superclasses" do + @subclass = Class.new(@klass) + @instance = @subclass.new + + @klass.action(:parent) do + invoke { "a" } + end + @subclass.action(:sub) do + invoke { "a" } + end + @instance.action(:instance) do + invoke { "a" } + end + + @instance.should be_action(:parent) + @instance.should be_action(:sub) + @instance.should be_action(:instance) + end + + it "should create an instance method when an action is defined in a superclass" do + @subclass = Class.new(@klass) + @instance = @subclass.new + + @klass.action(:foo) do + invoke { "something" } + end + @instance.foo.should == "something" + end + end +end diff --git a/spec/unit/string/action_spec.rb b/spec/unit/string/action_spec.rb new file mode 100755 index 000000000..4026c9a58 --- /dev/null +++ b/spec/unit/string/action_spec.rb @@ -0,0 +1,75 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') +require 'puppet/string/action' + +describe Puppet::String::Action do + describe "when validating the action name" do + it "should require a name" do + lambda { Puppet::String::Action.new(nil,nil) }.should raise_error("'' is an invalid action name") + end + + it "should not allow empty names" do + lambda { Puppet::String::Action.new(nil,'') }.should raise_error("'' is an invalid action name") + end + + it "should not allow names with whitespace" do + lambda { Puppet::String::Action.new(nil,'foo bar') }.should raise_error("'foo bar' is an invalid action name") + end + + it "should not allow names beginning with dashes" do + lambda { Puppet::String::Action.new(nil,'-foobar') }.should raise_error("'-foobar' is an invalid action name") + end + end + + describe "when invoking" do + it "should be able to call other actions on the same object" do + string = Puppet::String.new(:my_string, '0.0.1') do + action(:foo) do + invoke { 25 } + end + + action(:bar) do + invoke { "the value of foo is '#{foo}'" } + end + end + string.foo.should == 25 + string.bar.should == "the value of foo is '25'" + end + + # bar is a class action calling a class action + # quux is a class action calling an instance action + # baz is an instance action calling a class action + # qux is an instance action calling an instance action + it "should be able to call other actions on the same object when defined on a class" do + class Puppet::String::MyStringBaseClass < Puppet::String + action(:foo) do + invoke { 25 } + end + + action(:bar) do + invoke { "the value of foo is '#{foo}'" } + end + + action(:quux) do + invoke { "qux told me #{qux}" } + end + end + + string = Puppet::String::MyStringBaseClass.new(:my_inherited_string, '0.0.1') do + action(:baz) do + invoke { "the value of foo in baz is '#{foo}'" } + end + + action(:qux) do + invoke { baz } + end + end + string.foo.should == 25 + string.bar.should == "the value of foo is '25'" + string.quux.should == "qux told me the value of foo in baz is '25'" + string.baz.should == "the value of foo in baz is '25'" + string.qux.should == "the value of foo in baz is '25'" + end + end +end diff --git a/spec/unit/string/catalog_spec.rb b/spec/unit/string/catalog_spec.rb new file mode 100755 index 000000000..a11d29a04 --- /dev/null +++ b/spec/unit/string/catalog_spec.rb @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +describe Puppet::String.define(:catalog, '0.0.1') do +end diff --git a/spec/unit/string/certificate_request_spec.rb b/spec/unit/string/certificate_request_spec.rb new file mode 100755 index 000000000..96e1d8837 --- /dev/null +++ b/spec/unit/string/certificate_request_spec.rb @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +describe Puppet::String.define(:certificate_request, '0.0.1') do +end diff --git a/spec/unit/string/certificate_revocation_list_spec.rb b/spec/unit/string/certificate_revocation_list_spec.rb new file mode 100755 index 000000000..cf50471c8 --- /dev/null +++ b/spec/unit/string/certificate_revocation_list_spec.rb @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +describe Puppet::String.define(:certificate_revocation_list, '0.0.1') do +end diff --git a/spec/unit/string/certificate_spec.rb b/spec/unit/string/certificate_spec.rb new file mode 100755 index 000000000..719ee6b06 --- /dev/null +++ b/spec/unit/string/certificate_spec.rb @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +describe Puppet::String.define(:certificate, '0.0.1') do +end diff --git a/spec/unit/string/config_spec.rb b/spec/unit/string/config_spec.rb new file mode 100755 index 000000000..562265287 --- /dev/null +++ b/spec/unit/string/config_spec.rb @@ -0,0 +1,24 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +describe Puppet::String.define(:config, '0.0.1') do + it "should use Settings#print_config_options when asked to print" do + Puppet.settings.stubs(:puts) + Puppet.settings.expects(:print_config_options) + subject.print + end + + it "should set 'configprint' to all desired values and call print_config_options when a specific value is provided" do + Puppet.settings.stubs(:puts) + Puppet.settings.expects(:print_config_options) + subject.print("libdir", "ssldir") + Puppet.settings[:configprint].should == "libdir,ssldir" + end + + it "should always return nil" do + Puppet.settings.stubs(:puts) + Puppet.settings.expects(:print_config_options) + subject.print("libdir").should be_nil + end +end diff --git a/spec/unit/string/configurer_spec.rb b/spec/unit/string/configurer_spec.rb new file mode 100755 index 000000000..400bfb593 --- /dev/null +++ b/spec/unit/string/configurer_spec.rb @@ -0,0 +1,24 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') +require 'puppet/indirector/catalog/rest' +require 'tempfile' + +describe Puppet::String.define(:configurer, '0.0.1') do + describe "#synchronize" do + it "should retrieve and apply a catalog and return a report" do + dirname = Dir.mktmpdir("puppetdir") + Puppet[:vardir] = dirname + Puppet[:confdir] = dirname + @catalog = Puppet::Resource::Catalog.new + @file = Puppet::Resource.new(:file, File.join(dirname, "tmp_dir_resource"), :parameters => {:ensure => :present}) + @catalog.add_resource(@file) + Puppet::Resource::Catalog::Rest.any_instance.stubs(:find).returns(@catalog) + + report = subject.synchronize("foo") + + report.kind.should == "apply" + report.status.should == "changed" + end + end +end diff --git a/spec/unit/string/facts_spec.rb b/spec/unit/string/facts_spec.rb new file mode 100755 index 000000000..a537b7420 --- /dev/null +++ b/spec/unit/string/facts_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +describe Puppet::String.define(:facts, '0.0.1') do + it "should define an 'upload' fact" do + subject.should be_action(:upload) + end + + it "should set its default format to :yaml" do + subject.default_format.should == :yaml + end + + describe "when uploading" do + it "should set the terminus_class to :facter" + + it "should set the cach_eclass to :rest" + + it "should find the current certname" + end +end diff --git a/spec/unit/string/file_spec.rb b/spec/unit/string/file_spec.rb new file mode 100755 index 000000000..bbc8c7e09 --- /dev/null +++ b/spec/unit/string/file_spec.rb @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +describe Puppet::String.define(:file, '0.0.1') do +end diff --git a/spec/unit/string/indirector_spec.rb b/spec/unit/string/indirector_spec.rb new file mode 100755 index 000000000..89306c416 --- /dev/null +++ b/spec/unit/string/indirector_spec.rb @@ -0,0 +1,55 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') +require 'puppet/string/indirector' + +describe Puppet::String::Indirector do + before do + @instance = Puppet::String::Indirector.new(:test, '0.0.1') + + @indirection = stub 'indirection', :name => :stub_indirection + + @instance.stubs(:indirection).returns @indirection + end + + it "should be able to return a list of indirections" do + Puppet::String::Indirector.indirections.should be_include("catalog") + end + + it "should be able to return a list of terminuses for a given indirection" do + Puppet::String::Indirector.terminus_classes(:catalog).should be_include("compiler") + end + + describe "as an instance" do + it "should be able to determine its indirection" do + # Loading actions here an get, um, complicated + Puppet::String.stubs(:load_actions) + Puppet::String::Indirector.new(:catalog, '0.0.1').indirection.should equal(Puppet::Resource::Catalog.indirection) + end + end + + [:find, :search, :save, :destroy].each do |method| + it "should define a '#{method}' action" do + Puppet::String::Indirector.should be_action(method) + end + + it "should just call the indirection method when the '#{method}' action is invoked" do + @instance.indirection.expects(method).with(:test, "myargs") + @instance.send(method, :test, "myargs") + end + end + + it "should be able to override its indirection name" do + @instance.set_indirection_name :foo + @instance.indirection_name.should == :foo + end + + it "should be able to set its terminus class" do + @instance.indirection.expects(:terminus_class=).with(:myterm) + @instance.set_terminus(:myterm) + end + + it "should define a class-level 'info' action" do + Puppet::String::Indirector.should be_action(:info) + end +end diff --git a/spec/unit/string/key_spec.rb b/spec/unit/string/key_spec.rb new file mode 100755 index 000000000..d77f02ec4 --- /dev/null +++ b/spec/unit/string/key_spec.rb @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +describe Puppet::String.define(:key, '0.0.1') do +end diff --git a/spec/unit/string/node_spec.rb b/spec/unit/string/node_spec.rb new file mode 100755 index 000000000..7198efe76 --- /dev/null +++ b/spec/unit/string/node_spec.rb @@ -0,0 +1,9 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +describe Puppet::String.define(:node, '0.0.1') do + it "should set its default format to :yaml" do + subject.default_format.should == :yaml + end +end diff --git a/spec/unit/string/report_spec.rb b/spec/unit/string/report_spec.rb new file mode 100755 index 000000000..51342c2fa --- /dev/null +++ b/spec/unit/string/report_spec.rb @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +describe Puppet::String.define(:report, '0.0.1') do +end diff --git a/spec/unit/string/resource_spec.rb b/spec/unit/string/resource_spec.rb new file mode 100755 index 000000000..de7e747ed --- /dev/null +++ b/spec/unit/string/resource_spec.rb @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +describe Puppet::String.define(:resource, '0.0.1') do +end diff --git a/spec/unit/string/resource_type_spec.rb b/spec/unit/string/resource_type_spec.rb new file mode 100755 index 000000000..8b0b4aaa7 --- /dev/null +++ b/spec/unit/string/resource_type_spec.rb @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +describe Puppet::String.define(:resource_type, '0.0.1') do +end diff --git a/spec/unit/string/string_collection_spec.rb b/spec/unit/string/string_collection_spec.rb new file mode 100755 index 000000000..46c431f75 --- /dev/null +++ b/spec/unit/string/string_collection_spec.rb @@ -0,0 +1,249 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') +require 'tmpdir' + +describe Puppet::String::StringCollection do + before :all do + @strings = subject.instance_variable_get("@strings").dup + end + + before :each do + subject.instance_variable_get("@strings").clear + end + + after :all do + subject.instance_variable_set("@strings", @strings) + end + + describe "::strings" do + end + + describe "::versions" do + before :each do + @dir = Dir.mktmpdir + @lib = FileUtils.mkdir_p(File.join @dir, 'puppet', 'string') + $LOAD_PATH.push(@dir) + end + + after :each do + FileUtils.remove_entry_secure @dir + $LOAD_PATH.pop + end + + it "should return an empty array when no versions are loadable" do + subject.versions(:fozzie).should == [] + end + + it "should return versions loadable as puppet/string/v{version}/{name}" do + FileUtils.mkdir_p(File.join @lib, 'v1.0.0') + FileUtils.touch(File.join @lib, 'v1.0.0', 'fozzie.rb') + subject.versions(:fozzie).should == ['1.0.0'] + end + + it "should an ordered list of all versions loadable as puppet/string/v{version}/{name}" do + %w[ 1.2.1rc2 1.2.1beta1 1.2.1rc1 1.2.1 1.2.2 ].each do |version| + FileUtils.mkdir_p(File.join @lib, "v#{version}") + FileUtils.touch(File.join @lib, "v#{version}", 'fozzie.rb') + end + subject.versions(:fozzie).should == %w[ 1.2.1beta1 1.2.1rc1 1.2.1rc2 1.2.1 1.2.2 ] + end + + it "should not return a version for an empty puppet/string/v{version}/{name}" do + FileUtils.mkdir_p(File.join @lib, 'v1.0.0', 'fozzie') + subject.versions(:fozzie).should == [] + end + + it "should an ordered list of all versions loadable as puppet/string/v{version}/{name}/*.rb" do + %w[ 1.2.1rc2 1.2.1beta1 1.2.1rc1 1.2.1 1.2.2 ].each do |version| + FileUtils.mkdir_p(File.join @lib, "v#{version}", "fozzie") + FileUtils.touch(File.join @lib, "v#{version}", 'fozzie', 'action.rb') + end + subject.versions(:fozzie).should == %w[ 1.2.1beta1 1.2.1rc1 1.2.1rc2 1.2.1 1.2.2 ] + end + end + + describe "::validate_version" do + it 'should permit three number versions' do + subject.validate_version('10.10.10').should == true + end + + it 'should permit versions with appended descriptions' do + subject.validate_version('10.10.10beta').should == true + end + + it 'should not permit versions with more than three numbers' do + subject.validate_version('1.2.3.4').should == false + end + + it 'should not permit versions with only two numbers' do + subject.validate_version('10.10').should == false + end + + it 'should not permit versions with only one number' do + subject.validate_version('123').should == false + end + + it 'should not permit versions with text in any position but at the end' do + subject.validate_version('v1.1.1').should == false + end + end + + describe "::compare_versions" do + # (a <=> b) should be: + # -1 if a < b + # 0 if a == b + # 1 if a > b + it 'should sort major version numbers numerically' do + subject.compare_versions('1.0.0', '2.0.0').should == -1 + subject.compare_versions('2.0.0', '1.1.1').should == 1 + subject.compare_versions('2.0.0', '10.0.0').should == -1 + end + + it 'should sort minor version numbers numerically' do + subject.compare_versions('0.1.0', '0.2.0').should == -1 + subject.compare_versions('0.2.0', '0.1.1').should == 1 + subject.compare_versions('0.2.0', '0.10.0').should == -1 + end + + it 'should sort tiny version numbers numerically' do + subject.compare_versions('0.0.1', '0.0.2').should == -1 + subject.compare_versions('0.0.2', '0.0.1').should == 1 + subject.compare_versions('0.0.2', '0.0.10').should == -1 + end + + it 'should sort major version before minor version' do + subject.compare_versions('1.1.0', '1.2.0').should == -1 + subject.compare_versions('1.2.0', '1.1.1').should == 1 + subject.compare_versions('1.2.0', '1.10.0').should == -1 + + subject.compare_versions('1.1.0', '2.2.0').should == -1 + subject.compare_versions('2.2.0', '1.1.1').should == 1 + subject.compare_versions('2.2.0', '1.10.0').should == 1 + end + + it 'should sort minor version before tiny version' do + subject.compare_versions('0.1.1', '0.1.2').should == -1 + subject.compare_versions('0.1.2', '0.1.1').should == 1 + subject.compare_versions('0.1.2', '0.1.10').should == -1 + + subject.compare_versions('0.1.1', '0.2.2').should == -1 + subject.compare_versions('0.2.2', '0.1.1').should == 1 + subject.compare_versions('0.2.2', '0.1.10').should == 1 + end + + it 'should sort appended strings asciibetically' do + subject.compare_versions('0.0.0a', '0.0.0b').should == -1 + subject.compare_versions('0.0.0beta1', '0.0.0beta2').should == -1 + subject.compare_versions('0.0.0beta1', '0.0.0rc1').should == -1 + subject.compare_versions('0.0.0beta1', '0.0.0alpha1').should == 1 + subject.compare_versions('0.0.0beta1', '0.0.0beta1').should == 0 + end + + it "should sort appended strings before 'whole' versions" do + subject.compare_versions('0.0.1a', '0.0.1').should == -1 + subject.compare_versions('0.0.1', '0.0.1beta').should == 1 + end + end + + describe "::[]" do + before :each do + subject.instance_variable_get("@strings")[:foo]['0.0.1'] = 10 + end + + before :each do + @dir = Dir.mktmpdir + @lib = FileUtils.mkdir_p(File.join @dir, 'puppet', 'string') + $LOAD_PATH.push(@dir) + end + + after :each do + FileUtils.remove_entry_secure @dir + $LOAD_PATH.pop + end + + it "should return the string with the given name" do + subject["foo", '0.0.1'].should == 10 + end + + it "should attempt to load the string if it isn't found" do + subject.expects(:require).with('puppet/string/v0.0.1/bar') + subject["bar", '0.0.1'] + end + + it "should attempt to load the string with the greatest version for specified version :latest" do + %w[ 1.2.1 1.2.2 ].each do |version| + FileUtils.mkdir_p(File.join @lib, "v#{version}") + FileUtils.touch(File.join @lib, "v#{version}", 'fozzie.rb') + end + subject.expects(:require).with('puppet/string/v1.2.2/fozzie') + subject['fozzie', :latest] + end + end + + describe "::string?" do + before :each do + subject.instance_variable_get("@strings")[:foo]['0.0.1'] = 10 + end + + it "should return true if the string specified is registered" do + subject.string?("foo", '0.0.1').should == true + end + + it "should attempt to require the string if it is not registered" do + subject.expects(:require).with('puppet/string/v0.0.1/bar') + subject.string?("bar", '0.0.1') + end + + it "should return true if requiring the string registered it" do + subject.stubs(:require).with do + subject.instance_variable_get("@strings")[:bar]['0.0.1'] = 20 + end + subject.string?("bar", '0.0.1').should == true + end + + it "should return false if the string is not registered" do + subject.stubs(:require).returns(true) + subject.string?("bar", '0.0.1').should == false + end + + it "should return false if there is a LoadError requiring the string" do + subject.stubs(:require).raises(LoadError) + subject.string?("bar", '0.0.1').should == false + end + end + + describe "::register" do + it "should store the string by name" do + string = Puppet::String.new(:my_string, '0.0.1') + subject.register(string) + subject.instance_variable_get("@strings").should == {:my_string => {'0.0.1' => string}} + end + end + + describe "::underscorize" do + faulty = [1, "#foo", "$bar", "sturm und drang", :"sturm und drang"] + valid = { + "Foo" => :foo, + :Foo => :foo, + "foo_bar" => :foo_bar, + :foo_bar => :foo_bar, + "foo-bar" => :foo_bar, + :"foo-bar" => :foo_bar, + } + + valid.each do |input, expect| + it "should map #{input.inspect} to #{expect.inspect}" do + result = subject.underscorize(input) + result.should == expect + end + end + + faulty.each do |input| + it "should fail when presented with #{input.inspect} (#{input.class})" do + expect { subject.underscorize(input) }. + should raise_error ArgumentError, /not a valid string name/ + end + end + end +end diff --git a/spec/unit/string_spec.rb b/spec/unit/string_spec.rb new file mode 100755 index 000000000..73d1f2177 --- /dev/null +++ b/spec/unit/string_spec.rb @@ -0,0 +1,83 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb') + +describe Puppet::String do + before :all do + @strings = Puppet::String::StringCollection.instance_variable_get("@strings").dup + end + + before :each do + Puppet::String::StringCollection.instance_variable_get("@strings").clear + end + + after :all do + Puppet::String::StringCollection.instance_variable_set("@strings", @strings) + end + + describe "#define" do + it "should register the string" do + string = Puppet::String.define(:string_test_register, '0.0.1') + string.should == Puppet::String[:string_test_register, '0.0.1'] + end + + it "should load actions" do + Puppet::String.any_instance.expects(:load_actions) + Puppet::String.define(:string_test_load_actions, '0.0.1') + end + + it "should require a version number" do + proc { Puppet::String.define(:no_version) }.should raise_error(ArgumentError) + end + end + + describe "#initialize" do + it "should require a version number" do + proc { Puppet::String.new(:no_version) }.should raise_error(ArgumentError) + end + + it "should require a valid version number" do + proc { Puppet::String.new(:bad_version, 'Rasins') }.should raise_error(ArgumentError) + end + + it "should instance-eval any provided block" do + face = Puppet::String.new(:string_test_block,'0.0.1') do + action(:something) do + invoke { "foo" } + end + end + + face.something.should == "foo" + end + end + + it "should have a name" do + Puppet::String.new(:me,'0.0.1').name.should == :me + end + + it "should stringify with its own name" do + Puppet::String.new(:me,'0.0.1').to_s.should =~ /\bme\b/ + end + + it "should allow overriding of the default format" do + face = Puppet::String.new(:me,'0.0.1') + face.set_default_format :foo + face.default_format.should == :foo + end + + it "should default to :pson for its format" do + Puppet::String.new(:me, '0.0.1').default_format.should == :pson + end + + # Why? + it "should create a class-level autoloader" do + Puppet::String.autoloader.should be_instance_of(Puppet::Util::Autoload) + end + + it "should try to require strings that are not known" do + Puppet::String::StringCollection.expects(:require).with "puppet/string/v0.0.1/foo" + Puppet::String[:foo, '0.0.1'] + end + + it "should be able to load all actions in all search paths" +end -- cgit