summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-04-03 23:37:12 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-04-03 23:37:12 +0000
commit3327dc809cb645a2e0d81e892c21cf9872f38712 (patch)
treebca91363b534ee61a7efe7c8a4783cbbaba6971b
parent97d5ab656d5d05678eb37800452fb8ac7b615da2 (diff)
downloadpuppet-3327dc809cb645a2e0d81e892c21cf9872f38712.tar.gz
puppet-3327dc809cb645a2e0d81e892c21cf9872f38712.tar.xz
puppet-3327dc809cb645a2e0d81e892c21cf9872f38712.zip
Adding netinfo type and some tests
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1061 980ebf18-57e1-0310-9a29-db15c13687c0
-rwxr-xr-xlib/puppet/filetype.rb60
-rwxr-xr-xtest/other/filetype.rb96
-rw-r--r--test/types/filetype.rb160
3 files changed, 156 insertions, 160 deletions
diff --git a/lib/puppet/filetype.rb b/lib/puppet/filetype.rb
index 6525204ab..c13c740f0 100755
--- a/lib/puppet/filetype.rb
+++ b/lib/puppet/filetype.rb
@@ -205,6 +205,66 @@ module Puppet
}
end
end
+
+ # Treat netinfo tables as a single file, just for simplicity of certain types
+ newfiletype(:netinfo) do
+ def read
+ %x{nidump -r /#{@path} /}
+ end
+
+ # This really only makes sense for cron tabs.
+ def remove
+ raise TypeError, "Cannot remove netinfo tables"
+ end
+
+ # Convert our table to an array of hashes. This only works for handling one
+ # table at a time.
+ def to_array(text = nil)
+ text ||= read
+
+ hash = nil
+
+ # Initialize it with the first record
+ records = []
+ text.split("\n").each do |line|
+ next if line =~ /^[{}]$/ # Skip the wrapping lines
+ next if line =~ /"name" = \( "#{@path}" \)/ # Skip the table name
+ next if line =~ /CHILDREN = \(/ # Skip this header
+ next if line =~ /^ \)/ # and its closer
+
+ # Now we should have nothing but records, wrapped in braces
+
+ case line
+ when /^\s+\{/: hash = {}
+ when /^\s+\}/: records << hash
+ when /\s+"(\w+)" = \( (.+) \)/
+ field = $1
+ values = $2
+
+ # Always use an array
+ hash[field] = []
+
+ values.split(/, /).each do |value|
+ if value =~ /^"(.*)"$/
+ hash[field] << $1
+ else
+ raise ArgumentError, "Could not match value %s" % value
+ end
+ end
+ else
+ raise ArgumentError, "Could not match line %s" % line
+ end
+ end
+
+ records
+ end
+
+ def write(text)
+ IO.popen("niload -d #{@path} .", "w") { |p|
+ p.print text
+ }
+ end
+ end
end
end
diff --git a/test/other/filetype.rb b/test/other/filetype.rb
new file mode 100755
index 000000000..b965ade39
--- /dev/null
+++ b/test/other/filetype.rb
@@ -0,0 +1,96 @@
+if __FILE__ == $0
+ $:.unshift '..'
+ $:.unshift '../../lib'
+ $puppetbase = "../.."
+end
+
+require 'puppet'
+require 'puppet/filetype'
+require 'puppettest'
+require 'test/unit'
+
+class TestFileType < Test::Unit::TestCase
+ include TestPuppet
+
+ def test_flat
+ obj = nil
+ path = tempfile()
+ type = nil
+
+ assert_nothing_raised {
+ type = Puppet::FileType.filetype(:flat)
+ }
+
+ assert(type, "Could not retrieve flat filetype")
+
+ assert_nothing_raised {
+ obj = type.new(path)
+ }
+
+ text = "This is some text\n"
+
+ newtext = nil
+ assert_nothing_raised {
+ newtext = obj.read
+ }
+
+ # The base class doesn't allow a return of nil
+ assert_equal("", newtext, "Somehow got some text")
+
+ assert_nothing_raised {
+ obj.write(text)
+ }
+ assert_nothing_raised {
+ newtext = obj.read
+ }
+
+ assert_equal(text, newtext, "Text was changed somehow")
+
+ File.open(path, "w") { |f| f.puts "someyayness" }
+
+ text = File.read(path)
+ assert_nothing_raised {
+ newtext = obj.read
+ }
+
+ assert_equal(text, newtext, "Text was changed somehow")
+ end
+
+ if Facter["operatingsystem"].value == "Darwin"
+ def test_ninfotoarray
+ obj = nil
+ type = nil
+
+ assert_nothing_raised {
+ type = Puppet::FileType.filetype(:netinfo)
+ }
+
+ assert(type, "Could not retrieve netinfo filetype")
+ %w{users groups aliases}.each do |map|
+ assert_nothing_raised {
+ obj = type.new(map)
+ }
+
+ assert_nothing_raised("could not read map %s" % map) {
+ obj.read
+ }
+
+ array = nil
+
+ assert_nothing_raised {
+ array = obj.to_array
+ }
+
+ assert_instance_of(Array, array)
+
+ array.each do |record|
+ assert_instance_of(Hash, record)
+ assert(record.length != 0)
+ p record
+ end
+ end
+ end
+ end
+end
+
+# $Id$
diff --git a/test/types/filetype.rb b/test/types/filetype.rb
deleted file mode 100644
index 7c5e4583c..000000000
--- a/test/types/filetype.rb
+++ /dev/null
@@ -1,160 +0,0 @@
-if __FILE__ == $0
- $:.unshift '..'
- $:.unshift '../../lib'
- $puppetbase = "../../../../language/trunk"
-end
-
-require 'puppet'
-#require 'puppet/type/typegen/filetype'
-#require 'puppet/type/typegen/filerecord'
-require 'test/unit'
-
-#class TestFileType < Test::Unit::TestCase
-class TestFileType
- def disabled_setup
-
- @passwdtype = Puppet.type(:filetype)["passwd"]
- if @passwdtype.nil?
- assert_nothing_raised() {
- @passwdtype = Puppet.type(:filetype).createtype(
- :name => "passwd"
- )
- @passwdtype.addrecord(
- :name => "user",
- :splitchar => ":",
- :fields => %w{name password uid gid gcos home shell}
- )
- }
- end
-
- @syslogtype = Puppet.type(:filetype)["syslog"]
- if @syslogtype.nil?
- assert_nothing_raised() {
- @syslogtype = Puppet.type(:filetype).createtype(
- :escapednewlines => true,
- :name => "syslog"
- )
- @syslogtype.addrecord(
- :name => "data",
- :regex => %r{^([^#\s]+)\s+(\S+)$},
- :joinchar => "\t",
- :fields => %w{logs dest}
- )
- @syslogtype.addrecord(
- :name => "comment",
- :regex => %r{^(#.*)$},
- :joinchar => "", # not really necessary...
- :fields => %w{comment}
- )
- @syslogtype.addrecord(
- :name => "blank",
- :regex => %r{^(\s*)$},
- :joinchar => "", # not really necessary...
- :fields => %w{blank}
- )
- }
- end
-
- end
-
- def disabled_test_passwd1_nochange
- file = nil
- type = nil
- assert_nothing_raised() {
- file = @passwdtype.new("/etc/passwd")
- }
- assert_nothing_raised() {
- file.retrieve
- }
-
- assert(file.insync?)
-
- contents = ""
- ::File.open("/etc/passwd") { |ofile|
- ofile.each { |line|
- contents += line
- }
- }
-
- assert_equal(
- contents,
- file.to_s
- )
-
- end
-
- def disabled_test_passwd2_change
- file = nil
- type = nil
- newfile = tempfile()
- Kernel.system("cp /etc/passwd #{newfile}")
- assert_nothing_raised() {
- file = @passwdtype.new(newfile)
- }
- assert_nothing_raised() {
- file.retrieve
- }
-
- assert(file.insync?)
-
- assert_nothing_raised() {
- file.add("user") { |obj|
- obj["name"] = "yaytest"
- obj["password"] = "x"
- obj["uid"] = "10000"
- obj["gid"] = "10000"
- obj["home"] = "/home/yaytest"
- obj["gcos"] = "The Yaytest"
- obj["shell"] = "/bin/sh"
- }
- }
-
- assert(!file.insync?)
-
- assert_nothing_raised() {
- file.evaluate
- }
-
- assert(file.insync?)
-
- assert_nothing_raised() {
- file.delete("bin")
- }
-
- assert(!file.insync?)
-
- assert_nothing_raised() {
- file.evaluate
- }
-
- assert(file.insync?)
- end
-
- def disabled_test_syslog_nochange
- file = nil
- type = nil
- syslog = File.join($puppetbase, "examples/root/etc/debian-syslog.conf")
- assert_nothing_raised() {
- file = @syslogtype.new(syslog)
- }
- assert_nothing_raised() {
- file.retrieve
- }
-
- assert(file.insync?)
-
- contents = ""
- ::File.open(syslog) { |ofile|
- ofile.each { |line|
- contents += line
- }
- }
- #assert_equal(
- # contents,
- # file.to_s
- #)
-
- end
-end
-
-# $Id$