summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2005-10-31 22:50:09 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2005-10-31 22:50:09 +0000
commit03357432210eecc5f370f812f473bc5d0a062d64 (patch)
tree255b176c9abe839d1d2d3e4919a7527a6f01d37c
parentba76eb2da506aee52b5bbbce842fa5ac361540a0 (diff)
downloadpuppet-03357432210eecc5f370f812f473bc5d0a062d64.tar.gz
puppet-03357432210eecc5f370f812f473bc5d0a062d64.tar.xz
puppet-03357432210eecc5f370f812f473bc5d0a062d64.zip
adding config file stuff, but not using it yet. I have decided to release the next version without them.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@738 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--lib/puppet/config.rb49
-rw-r--r--lib/puppet/type.rb1
-rwxr-xr-xtest/puppet/conffiles.rb104
3 files changed, 154 insertions, 0 deletions
diff --git a/lib/puppet/config.rb b/lib/puppet/config.rb
new file mode 100644
index 000000000..dc8b05f56
--- /dev/null
+++ b/lib/puppet/config.rb
@@ -0,0 +1,49 @@
+module Puppet # :nodoc:
+# The class for handling configuration files.
+class Config < Hash
+ # Slight override, since we can't seem to have a subclass where all instances
+ # have the same default block.
+ def [](section)
+ unless self.has_key?(section)
+ self[section] = {}
+ end
+ super
+ end
+
+ def initialize(file)
+ text = nil
+
+ begin
+ text = File.read(file)
+ rescue Errno::ENOENT
+ raise Puppet::Error, "No such file %s" % file
+ rescue Errno::EACCES
+ raise Puppet::Error, "Permission denied to file %s" % file
+ end
+
+ # Store it for later, in a way that we can test and such.
+ @file = Puppet::ParsedFile.new(file)
+
+ @values = Hash.new { |names, name|
+ names[name] = {}
+ }
+
+ section = "puppet"
+ text.split(/\n/).each { |line|
+ case line
+ when /^\[(\w+)\]$/: section = $1 # Section names
+ when /^\s*#/: next # Skip comments
+ when /^\s*$/: next # Skip blanks
+ when /^\s*(\w+)\s+(.+)$/: # settings
+ var = $1
+ value = $2
+ self[section][var] = value
+ else
+ raise Puppet::Error, "Could not match line %s" % line
+ end
+ }
+ end
+end
+end
+
+# $Id$
diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb
index 0735d3945..73963d2aa 100644
--- a/lib/puppet/type.rb
+++ b/lib/puppet/type.rb
@@ -815,6 +815,7 @@ class Type < Puppet::Element
# and such. This allows for two specifications of the same object and
# the same values, but it's pretty limited right now. The result of merging
# states is very different from the result of merging parameters or metaparams.
+ # This is currently unused.
def merge(hash)
hash.each { |param, value|
if param.is_a?(String)
diff --git a/test/puppet/conffiles.rb b/test/puppet/conffiles.rb
new file mode 100755
index 000000000..f966322f7
--- /dev/null
+++ b/test/puppet/conffiles.rb
@@ -0,0 +1,104 @@
+if __FILE__ == $0
+ $:.unshift '..'
+ $:.unshift '../../lib'
+ $puppetbase = ".."
+end
+
+require 'puppet'
+require 'puppet/config'
+require 'puppettest'
+require 'test/unit'
+
+class TestConfFiles < Test::Unit::TestCase
+ include TestPuppet
+
+ @@gooddata = [
+ {
+ "fun" => {
+ "a" => "b",
+ "c" => "d",
+ "e" => "f"
+ },
+ "yay" => {
+ "aa" => "bk",
+ "ca" => "dk",
+ "ea" => "fk"
+ },
+ "boo" => {
+ "eb" => "fb"
+ },
+ "rah" => {
+ "aa" => "this is a sentence",
+ "ca" => "dk",
+ "ea" => "fk"
+ },
+ },
+ {
+ "puppet" => {
+ "yay" => "rah"
+ },
+ "booh" => {
+ "okay" => "rah"
+ },
+ "back" => {
+ "okay" => "rah"
+ },
+ }
+ ]
+
+ def data2config(data)
+ str = ""
+
+ if data.include?("puppet")
+ # because we're modifying it
+ data = data.dup
+ str += "[puppet]\n"
+ data["puppet"].each { |var, value|
+ str += "%s %s\n" % [var, value]
+ }
+ data.delete("puppet")
+ end
+
+ data.each { |type, settings|
+ str += "[%s]\n" % type
+ settings.each { |var, value|
+ str += "%s %s\n" % [var, value]
+ }
+ }
+
+ return str
+ end
+
+ def sampledata
+ if block_given?
+ @@gooddata.each { |hash| yield hash }
+ else
+ return @@gooddata[0]
+ end
+ end
+
+ def test_readconfig
+ path = tempfile()
+
+ sampledata { |data|
+ # Write it out as a config file
+ File.open(path, "w") { |f| f.print data2config(data) }
+ config = nil
+ assert_nothing_raised {
+ config = Puppet::Config.new(path)
+ }
+
+ data.each { |section, hash|
+ hash.each { |var, value|
+ assert_equal(
+ data[section][var],
+ config[section][var],
+ "Got different values at %s/%s" % [section, var]
+ )
+ }
+ }
+ }
+ end
+end
+
+# $Id$