Permalink
Browse files

Merge commit 'f64c3700cf529fbedf2fb5234b851121d5ce7273' into dev/auge…

…asproviders
  • Loading branch information...
2 parents f1fc244 + f64c370 commit 81e03cb1241524f2bedebb7136e03acb692e53f1 @raphink committed Jul 10, 2012
View
@@ -1,4 +1,5 @@
pkg
+coverage
## MAC OS
.DS_Store
View
@@ -0,0 +1,45 @@
+# Contributing to augeasproviders
+
+## Writing tests
+
+Tests for a `typename` provider live at `spec/unit/puppet/typename_spec.rb` and
+their corresponding fixture (starting file) under
+`spec/fixture/unit/puppet/typename/`.
+
+Use an rspec context section per fixture and have multiple examples within the
+section using it.
+
+Tests use real resources which are applied to the system via the
+`AugeasSpec::Fixtures::apply` method to a temporary file created from the
+original fixture. Once applied, the temporary file is loaded back into Augeas
+and the contents/structure of the file can be tested.
+
+Execute `rake spec` in the root directory to run them.
+
+## Thoughts about testing methods
+
+After applying the resource, there are a few ways we could test the results of
+the file.
+
+* use augparse? No API today, could generate module file and shell out.
+* use Config::Augeas::Validator? Need to write separate rules, no rootdir
+ support and is Perl, not Ruby.
+* use XML comparison? No ruby-augeas support for aug_to_xml.
+* use ruby-augeas? Using this as we can test for specific nodes, values etc
+ and compare with rspec.
+* use File.read + rspec? Comparing the whole file will be a problem if Augeas
+ lenses change whitespace.
+
+## Requirements
+
+Besides runtime pre-requisites (see README), you need:
+
+* [puppetlabs_spec_helper](https://github.com/puppetlabs/puppetlabs_spec_helper) gem installed
+* rspec2
+* mocha
+* simplecov
+
+## Patches
+
+Please send pull requests via Github, or patches via git send-email to the
+author.
View
15 LICENCE
@@ -0,0 +1,15 @@
+augeasproviders: alternative Augeas-based providers for Puppet
+
+Copyright (c) 2012 Dominic Cleal
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
View
@@ -1,5 +1,5 @@
name 'domcleal-augeasproviders'
-version '0.0.3'
+version '0.0.4'
source 'git://github.com/domcleal/augeasproviders'
author 'domcleal'
license 'Apache 2.0'
View
@@ -0,0 +1,2 @@
+require 'rubygems'
+require 'puppetlabs_spec_helper/rake_tasks'
@@ -1,4 +1,4 @@
-# Alternative Augeas-based providers for Puppet
+# Alternative Augeas-based provider for host type (Puppet builtin)
#
# Copyright (c) 2012 Dominic Cleal
# Licensed under the Apache License, Version 2.0
@@ -20,7 +20,7 @@ def self.file(resource = nil)
file.chomp("/")
end
- confine :true => Puppet.features.augeas?
+ confine :feature => :augeas
confine :exists => file
def self.augopen(resource = nil)
@@ -53,7 +53,7 @@ def self.instances
resources = []
aug = augopen
aug.match("#{path}/*").each do |hpath|
- host = {}
+ host = {:ensure => :present}
host[:name] = aug.get("#{hpath}/canonical")
next unless host[:name]
host[:ip] = aug.get("#{hpath}/ipaddr")
@@ -62,7 +62,10 @@ def self.instances
aug.match("#{hpath}/alias").each do |apath|
aliases << aug.get(apath)
end
- host[:host_aliases] = aliases
+ host[:host_aliases] = aliases unless aliases.empty?
+
+ comment = aug.get("#{hpath}/#comment")
+ host[:comment] = comment if comment
resources << new(host)
end
@@ -92,12 +95,13 @@ def create
aug.set("#{path}/01/canonical", resource[:name])
if resource[:host_aliases]
- resource[:host_aliases].each do |halias|
+ resource[:host_aliases].split.each do |halias|
aug.set("#{path}/01/alias[last()+1]", halias)
end
end
- if resource[:comment]
+ # comment property only available in Puppet 2.7+
+ if Puppet::Type.type(:host).validattr? :comment and resource[:comment]
aug.set("#{path}/01/#comment", resource[:comment])
end
@@ -198,7 +202,11 @@ def comment=(value)
path = "/files#{self.class.file(resource)}"
begin
aug = self.class.augopen(resource)
- aug.set("#{path}/*[canonical = '#{resource[:name]}']/#comment", value)
+ if value.empty?
+ aug.rm("#{path}/*[canonical = '#{resource[:name]}']/#comment")
+ else
+ aug.set("#{path}/*[canonical = '#{resource[:name]}']/#comment", value)
+ end
aug.save!
ensure
aug.close if aug
@@ -1,4 +1,4 @@
-# Alternative Augeas-based providers for Puppet
+# Alternative Augeas-based provider for mailalias type (Puppet builtin)
#
# Copyright (c) 2012 Dominic Cleal
# Licensed under the Apache License, Version 2.0
@@ -8,7 +8,7 @@
Puppet::Type.type(:mailalias).provide(:augeas) do
desc "Uses Augeas API to update mail aliases file"
- confine :true => Puppet.features.augeas?
+ confine :feature => :augeas
confine :exists => "/etc/aliases"
def self.file(resource = nil)
@@ -47,7 +47,7 @@ def self.instances
resources = []
aug = augopen
aug.match("#{path}/*").each do |apath|
- malias = {}
+ malias = {:ensure => :present}
malias[:name] = aug.get("#{apath}/name")
next unless malias[:name]
@@ -0,0 +1 @@
+broken
@@ -0,0 +1 @@
+
@@ -0,0 +1,4 @@
+127.0.0.1 localhost.localdomain localhost
+::1 localhost6.localdomain6 localhost6
+192.168.0.5 iridium iridium.example.com
+192.168.0.10 argon # NAS
@@ -0,0 +1 @@
+broken
No changes.
@@ -0,0 +1,4 @@
+# Basic system aliases -- these MUST be present.
+mailer-daemon: postmaster
+postmaster: root
+test: user1, user2
View
@@ -0,0 +1,13 @@
+module AugeasSpec
+end
+
+require 'augeas_spec/fixtures'
+
+RSpec.configure do |config|
+ config.extend AugeasSpec::Fixtures
+ config.include AugeasSpec::Fixtures
+
+ config.before :each do
+ Puppet::Util::Storage.stubs(:store)
+ end
+end
@@ -0,0 +1,52 @@
+require 'augeas'
+
+module AugeasSpec::Fixtures
+ # Creates a temp file from a given fixture name
+ # Doesn't explicitly clean up the temp file as we can't evaluate a block with
+ # "let" or pass the path back via an "around" hook.
+ def aug_fixture(name)
+ tmp = Tempfile.new("target")
+ tmp.write(File.read(my_fixture(name)))
+ tmp.close
+ return tmp
+ end
+
+ # Runs a particular resource via a catalog
+ def apply(resource)
+ catalog = Puppet::Resource::Catalog.new
+ catalog.add_resource resource
+ catalog.apply
+ end
+
+ # Runs a resource and checks for warnings and errors
+ def apply!(resource)
+ txn = apply(resource)
+
+ # Check for warning+ log messages
+ loglevels = Puppet::Util::Log.levels[3, 999]
+ @logs.select { |log| loglevels.include? log.level }.should == []
+
+ # Check for transaction success after, as it's less informative
+ txn.any_failed?.should == nil
+ txn
+ end
+
+ # Open Augeas on a given file. Used for testing the results of running
+ # Puppet providers.
+ def aug_open(file, lens, &block)
+ aug = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD)
+ begin
+ aug.transform(
+ :lens => lens,
+ :name => lens.split(".")[0],
+ :incl => file
+ )
+ aug.set("/augeas/context", "/files#{file}")
+ aug.load!
+ raise LoadError("Augeas didn't load #{file}") if aug.match(".").empty?
+ yield aug
+ ensure
+ aug.close
+ end
+ end
+end
View
@@ -0,0 +1,6 @@
+--format
+s
+--colour
+--loadby
+mtime
+--backtrace
View
@@ -0,0 +1,17 @@
+require 'pathname'
+dir = Pathname.new(__FILE__).parent
+$LOAD_PATH.unshift(dir, dir + 'lib', dir + '../lib')
+
+require 'rubygems'
+
+require 'simplecov'
+SimpleCov.start
+require 'mocha'
+require 'rspec'
+
+require 'puppetlabs_spec_helper/module_spec_helper'
+require 'augeas_spec'
+
+RSpec.configure do |config|
+ config.mock_with :mocha
+end
Oops, something went wrong.

0 comments on commit 81e03cb

Please sign in to comment.