summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2005-06-06 15:13:14 +0000
committerLuke Kanies <luke@madstop.com>2005-06-06 15:13:14 +0000
commit53240625be7d35dabe29a2e71d5ac99d6f996427 (patch)
tree594a182c69951b3b8f6fa88f99e0e4cc4aae412e
parent0dad57a29e84d510a9d530c79b12aa4fba9f334f (diff)
downloadpuppet-53240625be7d35dabe29a2e71d5ac99d6f996427.tar.gz
puppet-53240625be7d35dabe29a2e71d5ac99d6f996427.tar.xz
puppet-53240625be7d35dabe29a2e71d5ac99d6f996427.zip
adding new classes and tests
git-svn-id: https://reductivelabs.com/svn/puppet/library/trunk@294 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--lib/blink/message.rb64
-rw-r--r--lib/blink/storage.rb48
-rw-r--r--test/other/tc_state.rb60
3 files changed, 172 insertions, 0 deletions
diff --git a/lib/blink/message.rb b/lib/blink/message.rb
new file mode 100644
index 000000000..31d5fa503
--- /dev/null
+++ b/lib/blink/message.rb
@@ -0,0 +1,64 @@
+# $Id$
+
+module Blink
+ #------------------------------------------------------------
+ # provide feedback of various types to the user
+ # modeled after syslog messages
+ # each level of message prints in a different color
+ class Message
+ @@messages = Array.new
+ @@levels = [ :debug, :verbose, :notice, :warning, :error ]
+ @@colors = {
+ :debug => SLATE,
+ :verbose => ORANGE,
+ :notice => PINK,
+ :warning => GREEN,
+ :error => YELLOW
+ }
+
+ attr_accessor :level, :message, :source
+
+ def Message.loglevels
+ return @@levels
+ end
+
+ def initialize(args)
+ unless args.include?(:level) && args.include?(:message) &&
+ args.include?(:source)
+ raise "Blink::Message called incorrectly"
+ end
+
+ if args[:level].class == String
+ @level = args[:level].intern
+ elsif args[:level].class == Symbol
+ @level = args[:level]
+ else
+ raise "Level is not a string or symbol: #{args[:level].class}"
+ end
+ @message = args[:message]
+ @source = args[:source]
+ @time = Time.now
+ # this should include the host name, and probly lots of other
+ # stuff, at some point
+ unless @@levels.include?(level)
+ raise "Invalid message level #{level}"
+ end
+
+ @@messages.push(self)
+ Blink.newmessage(self)
+ end
+
+ def to_s
+ # this probably won't stay, but until this leaves the console,
+ # i'm going to use coloring...
+ #return "#{@time} #{@source} (#{@level}): #{@message}"
+ #return @@colors[@level] + "%s %s (%s): %s" % [
+ # @time, @source, @level, @message
+ #] + RESET
+ return @@colors[@level] + "%s (%s): %s" % [
+ @source, @level, @message
+ ] + RESET
+ end
+ end
+ #------------------------------------------------------------
+end
diff --git a/lib/blink/storage.rb b/lib/blink/storage.rb
new file mode 100644
index 000000000..9fc21d38b
--- /dev/null
+++ b/lib/blink/storage.rb
@@ -0,0 +1,48 @@
+# $Id$
+
+module Blink
+ # a class for storing state
+ class Storage
+ include Singleton
+ @@state = Hash.new { |hash,key|
+ hash[key] = Hash.new(nil)
+ }
+ @@splitchar = "\t"
+
+ def initialize
+ self.class.load
+ end
+
+ def Storage.load
+ # XXX I should probably use a better default state dir
+ Blink[:statefile] ||= "/var/tmp/blinkstate"
+ return unless File.exists?(Blink[:statefile])
+ File.open(Blink[:statefile]) { |file|
+ file.gets { |line|
+ myclass, key, value = line.split(@@splitchar)
+
+ @@state[myclass][key] = Marshal::load(value)
+ }
+ }
+ end
+
+ def Storage.state(myclass)
+ unless myclass.is_a? Class
+ myclass = myclass.class
+ end
+ result = @@state[myclass]
+ return result
+ end
+
+ def Storage.store
+ File.open(Blink[:statefile], File::CREAT|File::WRONLY, 0600) { |file|
+ @@state.each { |klass, thash|
+ thash.each { |key,value|
+ mvalue = Marshal::dump(value)
+ file.puts([klass,key,mvalue].join(@@splitchar))
+ }
+ }
+ }
+ end
+ end
+end
diff --git a/test/other/tc_state.rb b/test/other/tc_state.rb
new file mode 100644
index 000000000..ab496299e
--- /dev/null
+++ b/test/other/tc_state.rb
@@ -0,0 +1,60 @@
+if __FILE__ == $0
+ $:.unshift '..'
+ $:.unshift '../../lib'
+ $blinkbase = "../../../../language/trunk"
+end
+
+require 'blink'
+require 'test/unit'
+
+# $Id$
+
+class TestStorage < Test::Unit::TestCase
+ def setup
+ Blink[:debug] = true
+ Blink[:statefile] = "/var/tmp/blinkteststate"
+ end
+
+ def test_simple
+ state = nil
+ assert_nothing_raised {
+ Blink::Storage.load
+ }
+ assert_nothing_raised {
+ state = Blink::Storage.state(Blink::Type)
+ }
+ assert(state)
+ state["/etc/passwd"] = ["md5","9ebebe0c02445c40b9dc6871b64ee416"]
+ assert_nothing_raised {
+ Blink::Storage.store
+ }
+ assert_nothing_raised {
+ Blink::Storage.load
+ }
+ assert_equal(
+ ["md5","9ebebe0c02445c40b9dc6871b64ee416"],
+ Blink::Storage.state(Blink::Type)["/etc/passwd"]
+ )
+ end
+
+ def test_instance
+ file = nil
+ state = nil
+ assert_nothing_raised {
+ file = Blink::Type::File.new(
+ :path => "/etc/passwd"
+ )
+ }
+ assert_nothing_raised {
+ Blink::Storage.load
+ }
+ assert_nothing_raised {
+ state = Blink::Storage.state(file)
+ }
+ assert(state)
+ end
+
+ def teardown
+ system("rm -f %s" % Blink[:statefile])
+ end
+end