lib/stella.rb in stella-0.8.8.001 vs lib/stella.rb in stella-2.0.1.001

- old
+ new

@@ -1,29 +1,31 @@ - +# encoding: utf-8 STELLA_LIB_HOME = File.expand_path File.dirname(__FILE__) unless defined?(STELLA_LIB_HOME) -#%w{attic hexoid storable sysinfo gibbler benelux}.each do |dir| -# $:.unshift File.join(STELLA_LIB_HOME, '..', '..', dir, 'lib') -#end +if RUBY_VERSION =~ /1.9/ + Encoding.default_external = Encoding::UTF_8 + Encoding.default_internal = Encoding::UTF_8 +end -require 'sysinfo' -autoload :Drydock, 'drydock' -autoload :URI, 'uri' -autoload :OpenStruct, 'ostruct' -autoload :Storable, 'storable' -autoload :Attic, 'attic' -autoload :ERB, 'erb' +require 'bundler/setup' +$:.unshift STELLA_LIB_HOME -require 'gibbler/aliases' # important for run time digests and freezes +%w{tryouts benelux storable gibbler familia bluth}.each do |dir| + $:.unshift File.join(STELLA_LIB_HOME, '..', '..', dir, 'lib') +end + +require 'yajl' +require 'erb' +require 'storable' require 'benelux' -require 'proc_source' +require 'gibbler/aliases' +require 'stella/core_ext' +require 'familia' -class OpenStruct - include Gibbler::Object -end +autoload :Nokogiri, 'nokogiri' -module Stella +class Stella module VERSION def self.to_s load_config [@version[:MAJOR], @version[:MINOR], @version[:PATCH]].join('.') end @@ -36,133 +38,199 @@ @version ||= YAML.load_file(File.join(STELLA_LIB_HOME, '..', 'VERSION.yml')) end end end -module Stella - class Error < RuntimeError - def initialize(obj=nil); @obj = obj; end - def message; @obj; end +class MatchData + include Gibbler::String +end + +module Addressable + class URI + include Gibbler::String end - class WackyRatio < Stella::Error; end - class WackyDuration < Stella::Error; end - class InvalidOption < Stella::Error; end - class NoHostDefined < Stella::Error - def message; "No host specified for uri: #{@obj}"; end +end + +class OpenStruct + include Gibbler::Object +end + +# +# Any object that wants to be serialized to JSON +# ought to inherit from this class. +# +# NOTE: you cannot define Storable fields here. +# Most notably, you'll prob want to include this. +# +# field :id, :class => Gibbler::Digest, :meth => :gibbler, &gibbler_id_processor +# +#module StellaObject < Storable +# include Gibbler::Complex +# def id +# @id ||= self.digest +# @id +# end +#end + +# All errors inherit from this class. +class StellaError < RuntimeError + def initialize(msg) + @message = msg end + attr_reader :message end -class Stella::Template - include Gibbler::String - attr_reader :src - def initialize(src) - src = src.to_s - @src, @template = src, ERB.new(src) +class StellaBehavior < Exception + def initialize(msg) + @message = msg end - def result(binding) - @template.result(binding) - end - def to_s() src end + attr_reader :message end -module Stella - extend self - - require 'stella/logger' - - unless defined?(START_TIME) - START_TIME = Time.now.freeze +class Stella + class ForcedRedirect < StellaBehavior + attr_accessor :location + def initialize(l) + @location = l + end end - - @globals = {} - @sysinfo = nil - @debug = false - @abort = false - @quiet = false - @agent = "Stella/#{Stella::VERSION}" - @log = Stella::SyncLogger.new - @stdout = Stella::Logger.new STDOUT - - class << self - attr_accessor :log, :stdout, :agent + class RepeatRequest < StellaBehavior end - - def le(*msg); stdout.info " " << msg.join("#{$/} ").colour(:red); end - def ld(*msg) - return unless Stella.debug? - prefix = "D(#{Thread.current.object_id}): " - Stella.stdout.info("#{prefix}#{msg.join("#{$/}#{prefix}")}".colour(:yellow)) + class UsecaseFail < StellaBehavior end - - def sysinfo - @sysinfo = SysInfo.new.freeze if @sysinfo.nil? - @sysinfo + class TestplanQuit < StellaBehavior end - - def debug?() @debug == true end - def enable_debug() @debug = true end - def disable_debug() @debug = false end - - def abort?() @abort == true end - def abort!() @abort = true end - - def quiet?() @quiet == true end - def enable_quiet() @quiet = true; Stella::Logger.disable!; end - def disable_quiet() @quiet = false; Stella::Logger.enable!; end - - def add_global(n,v) - Stella.ld "SETGLOBAL: #{n}=#{v}" - @globals[n.strip] = v.strip + class RequestError < StellaBehavior end - - def rescue(&blk) - blk.call - rescue => ex - Stella.le "ERROR: #{ex.message}" - Stella.ld ex.backtrace + class TimeoutError < StellaError end - - require 'stella/common' - - autoload :Utils, 'stella/utils' - autoload :Config, 'stella/config' - autoload :Data, 'stella/data' - autoload :Testplan, 'stella/testplan' - autoload :Testrun, 'stella/engine' - autoload :Engine, 'stella/engine' - autoload :Client, 'stella/client' - autoload :Service, 'stella/service' - - def get(uri, params={}, headers={}, &blk) - http_client = HTTPClient.new :agent_name => Stella.agent - res = http_client.get(uri, params, headers) - if blk.nil? - res.body.content - else - blk.call res + class UsecaseError < StellaError + end + class PageError < StellaError + end + class HTTPError < StellaError + attr_reader :status + def initialize(status) + @status = status end - rescue => ex - Stella.stdout.info "#{ex.message} (#{ex.class})" - Stella.stdout.info ex.backtrace if Stella.debug? - nil + def message + "#{status} error" + end end + require 'stella/utils' + require 'stella/client' + require 'stella/engine' + require 'stella/report' + require 'stella/testplan' + attr_reader :plan + def initialize *args + @plan = Stella::TP === args.first ? + args.first.clone : Stella::TP.new(args.first) + #@plan.freeze + @runner + end +end + + +class Stella + @sysinfo = nil + @debug = false + @noise = 1 + @abort = false + @quiet = false + @agent = "Mozilla/5.0 (compatible; Stella/#{Stella::VERSION}; +http://solutious.com/projects/stella)" + # static methods + class << self + attr_accessor :log, :stdout, :agent, :debug, :quiet, :noise + def debug?() @debug == true end + def quiet?() @quiet == true end + def li(*msg) STDERR.puts *msg unless quiet? end + def le(*msg); li " " << msg.join("#{$/} ") end + def ld(*msg) + return unless Stella.debug? + prefix = "D(#{Thread.current.object_id}): " + li("#{prefix}#{msg.join("#{$/}#{prefix}")}") + end + + def get(uri, opts={}) + opts[:concurrency] ||= 1 + opts[:repetitions] ||= 1 + run = checkup uri, opts + report = run.report + if report.processed? && report.content && report.statuses.success? + report.content.response_body + else + nil + end + end + def checkup(uri, opts={}) + plan = Stella::Testplan.new uri + run = Stella::Testrun.new plan, :checkup, opts + Stella::Engine.run run + run + end + def now + Time.now.utc.to_f + end + # http://blamestella.com/ => blamestella.com + # https://blamestella.com/ => blamestella.com:443 + def canonical_host(host) + return nil if host.nil? + if host.kind_of?(URI) + uri = host + else + host &&= host.to_s + host.strip! + host = host.to_s unless String === host + host = "http://#{host}" unless host.match(/^https?:\/\//) + uri = URI.parse(host) + end + str = "#{uri.host}".downcase + #str << ":#{uri.port}" if uri.port && uri.port != 80 + str + end - def post(uri, params={}, headers={}, &blk) - http_client = HTTPClient.new :agent_name => Stella.agent - res = http_client.post(uri, params, headers) - if blk.nil? - res.body.content - else - blk.call res + def canonical_uri(uri) + return nil if uri.nil? + if uri.kind_of?(URI) + uri = Addressable::URI.parse uri.to_s + elsif uri.kind_of?(String) + uri &&= uri.to_s + uri.strip! unless uri.frozen? + uri = "http://#{uri}" unless uri.match(/^https?:\/\//) + uri = Addressable::URI.parse(uri) + end + uri.scheme ||= 'http' + uri.path = '/' if uri.path.to_s.empty? + uri end - rescue => ex - Stella.stdout.info ex.message - Stella.stdout.info ex.backtrace if Stella.debug? - nil + + def rescue(&blk) + blk.call + rescue StellaError => ex + Stella.le ex.message + Stella.ld ex.backtrace + #rescue => ex + # Stella.le ex.message + # Stella.le ex.backtrace + end + end - end -Stella.stdout.lev = Stella.quiet? ? 0 : 1 -Stella.stdout.autoflush! +class Stella::Template + include Gibbler::String + attr_reader :src + def initialize(src) + src = src.to_s + @src, @template = src, ERB.new(src) + end + def result(binding) + @template.result(binding) + end + def self.from_file(path) + new File.read(path) + end + def to_s() src end +end