Added modules

This commit is contained in:
Ciaby 2014-07-11 13:30:23 -05:00
parent c53c931217
commit 59ec520742
646 changed files with 35182 additions and 0 deletions

View file

@ -0,0 +1,32 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'
require 'facter/facter_dot_d'
describe Facter::Util::DotD do
context 'returns a simple fact' do
before :each do
Facter.stubs(:version).returns('1.6.1')
subject.stubs(:entries).returns(['/etc/facter/facts.d/fake_fact.txt'])
File.stubs(:readlines).with('/etc/facter/facts.d/fake_fact.txt').returns(['fake_fact=fake fact'])
subject.create
end
it 'should return successfully' do
Facter.fact(:fake_fact).value.should == 'fake fact'
end
end
context 'returns a fact with equals signs' do
before :each do
Facter.stubs(:version).returns('1.6.1')
subject.stubs(:entries).returns(['/etc/facter/facts.d/foo.txt'])
File.stubs(:readlines).with('/etc/facter/facts.d/foo.txt').returns(['foo=1+1=2'])
subject.create
end
it 'should return successfully' do
Facter.fact(:foo).value.should == '1+1=2'
end
end
end

View file

@ -0,0 +1,76 @@
#!/usr/bin/env rspec
require 'spec_helper'
describe "PE Version specs" do
before :each do
# Explicitly load the pe_version.rb file which contains generated facts
# that cannot be automatically loaded. Puppet 2.x implements
# Facter.collection.load while Facter 1.x markes Facter.collection.load as
# a private method.
if Facter.collection.respond_to? :load
Facter.collection.load(:pe_version)
else
Facter.collection.loader.load(:pe_version)
end
end
context "If PE is installed" do
%w{ 2.6.1 2.10.300 }.each do |version|
puppetversion = "2.7.19 (Puppet Enterprise #{version})"
context "puppetversion => #{puppetversion}" do
before :each do
Facter.fact(:puppetversion).stubs(:value).returns(puppetversion)
end
(major,minor,patch) = version.split(".")
it "Should return true" do
Facter.fact(:is_pe).value.should == true
end
it "Should have a version of #{version}" do
Facter.fact(:pe_version).value.should == version
end
it "Should have a major version of #{major}" do
Facter.fact(:pe_major_version).value.should == major
end
it "Should have a minor version of #{minor}" do
Facter.fact(:pe_minor_version).value.should == minor
end
it "Should have a patch version of #{patch}" do
Facter.fact(:pe_patch_version).value.should == patch
end
end
end
end
context "When PE is not installed" do
before :each do
Facter.fact(:puppetversion).stubs(:value).returns("2.7.19")
end
it "is_pe is false" do
Facter.fact(:is_pe).value.should == false
end
it "pe_version is nil" do
Facter.fact(:pe_version).value.should be_nil
end
it "pe_major_version is nil" do
Facter.fact(:pe_major_version).value.should be_nil
end
it "pe_minor_version is nil" do
Facter.fact(:pe_minor_version).value.should be_nil
end
it "Should have a patch version" do
Facter.fact(:pe_patch_version).value.should be_nil
end
end
end

View file

@ -0,0 +1,52 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'
require 'facter/root_home'
describe Facter::Util::RootHome do
context "solaris" do
let(:root_ent) { "root:x:0:0:Super-User:/:/sbin/sh" }
let(:expected_root_home) { "/" }
it "should return /" do
Facter::Util::Resolution.expects(:exec).with("getent passwd root").returns(root_ent)
Facter::Util::RootHome.get_root_home.should == expected_root_home
end
end
context "linux" do
let(:root_ent) { "root:x:0:0:root:/root:/bin/bash" }
let(:expected_root_home) { "/root" }
it "should return /root" do
Facter::Util::Resolution.expects(:exec).with("getent passwd root").returns(root_ent)
Facter::Util::RootHome.get_root_home.should == expected_root_home
end
end
context "windows" do
before :each do
Facter::Util::Resolution.expects(:exec).with("getent passwd root").returns(nil)
end
it "should be nil on windows" do
Facter::Util::RootHome.get_root_home.should be_nil
end
end
end
describe 'root_home', :type => :fact do
before { Facter.clear }
after { Facter.clear }
context "macosx" do
before do
Facter.fact(:kernel).stubs(:value).returns("Darwin")
Facter.fact(:osfamily).stubs(:value).returns("Darwin")
end
let(:expected_root_home) { "/var/root" }
sample_dscacheutil = File.read(fixtures('dscacheutil','root'))
it "should return /var/root" do
Facter::Util::Resolution.stubs(:exec).with("dscacheutil -q user -a name root").returns(sample_dscacheutil)
Facter.fact(:root_home).value.should == expected_root_home
end
end
end

View file

@ -0,0 +1,36 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'
require 'facter/util/puppet_settings'
describe Facter::Util::PuppetSettings do
describe "#with_puppet" do
context "Without Puppet loaded" do
before(:each) do
Module.expects(:const_get).with("Puppet").raises(NameError)
end
it 'should be nil' do
subject.with_puppet { Puppet[:vardir] }.should be_nil
end
it 'should not yield to the block' do
Puppet.expects(:[]).never
subject.with_puppet { Puppet[:vardir] }.should be_nil
end
end
context "With Puppet loaded" do
module Puppet; end
let(:vardir) { "/var/lib/puppet" }
before :each do
Puppet.expects(:[]).with(:vardir).returns vardir
end
it 'should yield to the block' do
subject.with_puppet { Puppet[:vardir] }
end
it 'should return the nodes vardir' do
subject.with_puppet { Puppet[:vardir] }.should eq vardir
end
end
end
end

View file

@ -0,0 +1,225 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'
require 'tempfile'
provider_class = Puppet::Type.type(:file_line).provider(:ruby)
describe provider_class do
context "when adding" do
let :tmpfile do
tmp = Tempfile.new('tmp')
path = tmp.path
tmp.close!
path
end
let :resource do
Puppet::Type::File_line.new(
{:name => 'foo', :path => tmpfile, :line => 'foo'}
)
end
let :provider do
provider_class.new(resource)
end
it 'should detect if the line exists in the file' do
File.open(tmpfile, 'w') do |fh|
fh.write('foo')
end
provider.exists?.should be_true
end
it 'should detect if the line does not exist in the file' do
File.open(tmpfile, 'w') do |fh|
fh.write('foo1')
end
provider.exists?.should be_nil
end
it 'should append to an existing file when creating' do
provider.create
File.read(tmpfile).chomp.should == 'foo'
end
end
context "when matching" do
before :each do
# TODO: these should be ported over to use the PuppetLabs spec_helper
# file fixtures once the following pull request has been merged:
# https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
tmp = Tempfile.new('tmp')
@tmpfile = tmp.path
tmp.close!
@resource = Puppet::Type::File_line.new(
{
:name => 'foo',
:path => @tmpfile,
:line => 'foo = bar',
:match => '^foo\s*=.*$',
}
)
@provider = provider_class.new(@resource)
end
describe 'using match' do
it 'should raise an error if more than one line matches, and should not have modified the file' do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
end
@provider.exists?.should be_nil
expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/)
File.read(@tmpfile).should eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
end
it 'should replace all lines that matches' do
@resource = Puppet::Type::File_line.new(
{
:name => 'foo',
:path => @tmpfile,
:line => 'foo = bar',
:match => '^foo\s*=.*$',
:multiple => true
}
)
@provider = provider_class.new(@resource)
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
end
@provider.exists?.should be_nil
@provider.create
File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
end
it 'should raise an error with invalid values' do
expect {
@resource = Puppet::Type::File_line.new(
{
:name => 'foo',
:path => @tmpfile,
:line => 'foo = bar',
:match => '^foo\s*=.*$',
:multiple => 'asgadga'
}
)
}.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./)
end
it 'should replace a line that matches' do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo=blah\nfoo2")
end
@provider.exists?.should be_nil
@provider.create
File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2")
end
it 'should add a new line if no lines match' do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo2")
end
@provider.exists?.should be_nil
@provider.create
File.read(@tmpfile).should eql("foo1\nfoo2\nfoo = bar\n")
end
it 'should do nothing if the exact line already exists' do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo = bar\nfoo2")
end
@provider.exists?.should be_true
@provider.create
File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2")
end
end
describe 'using after' do
let :resource do
Puppet::Type::File_line.new(
{
:name => 'foo',
:path => @tmpfile,
:line => 'inserted = line',
:after => '^foo1',
}
)
end
let :provider do
provider_class.new(resource)
end
context 'with one line matching the after expression' do
before :each do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz")
end
end
it 'inserts the specified line after the line matching the "after" expression' do
provider.create
File.read(@tmpfile).chomp.should eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo = baz")
end
end
context 'with two lines matching the after expression' do
before :each do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz")
end
end
it 'errors out stating "One or no line must match the pattern"' do
expect { provider.create }.to raise_error(Puppet::Error, /One or no line must match the pattern/)
end
end
context 'with no lines matching the after expression' do
let :content do
"foo3\nfoo = blah\nfoo2\nfoo = baz\n"
end
before :each do
File.open(@tmpfile, 'w') do |fh|
fh.write(content)
end
end
it 'appends the specified line to the file' do
provider.create
File.read(@tmpfile).should eq(content << resource[:line] << "\n")
end
end
end
end
context "when removing" do
before :each do
# TODO: these should be ported over to use the PuppetLabs spec_helper
# file fixtures once the following pull request has been merged:
# https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
tmp = Tempfile.new('tmp')
@tmpfile = tmp.path
tmp.close!
@resource = Puppet::Type::File_line.new(
{:name => 'foo', :path => @tmpfile, :line => 'foo', :ensure => 'absent' }
)
@provider = provider_class.new(@resource)
end
it 'should remove the line if it exists' do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo\nfoo2")
end
@provider.destroy
File.read(@tmpfile).should eql("foo1\nfoo2")
end
it 'should remove the line without touching the last new line' do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo\nfoo2\n")
end
@provider.destroy
File.read(@tmpfile).should eql("foo1\nfoo2\n")
end
it 'should remove any occurence of the line' do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo\nfoo2\nfoo\nfoo")
end
@provider.destroy
File.read(@tmpfile).should eql("foo1\nfoo2\n")
end
end
end

View file

@ -0,0 +1,11 @@
#!/usr/bin/env ruby
require 'spec_helper'
anchor = Puppet::Type.type(:anchor).new(:name => "ntp::begin")
describe anchor do
it "should stringify normally" do
anchor.to_s.should == "Anchor[ntp::begin]"
end
end

View file

@ -0,0 +1,70 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'
require 'tempfile'
describe Puppet::Type.type(:file_line) do
let :file_line do
Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'line', :path => '/tmp/path')
end
it 'should accept a line and path' do
file_line[:line] = 'my_line'
file_line[:line].should == 'my_line'
file_line[:path] = '/my/path'
file_line[:path].should == '/my/path'
end
it 'should accept a match regex' do
file_line[:match] = '^foo.*$'
file_line[:match].should == '^foo.*$'
end
it 'should not accept a match regex that does not match the specified line' do
expect {
Puppet::Type.type(:file_line).new(
:name => 'foo',
:path => '/my/path',
:line => 'foo=bar',
:match => '^bar=blah$'
)}.to raise_error(Puppet::Error, /the value must be a regex that matches/)
end
it 'should accept a match regex that does match the specified line' do
expect {
Puppet::Type.type(:file_line).new(
:name => 'foo',
:path => '/my/path',
:line => 'foo=bar',
:match => '^\s*foo=.*$'
)}.not_to raise_error
end
it 'should accept posix filenames' do
file_line[:path] = '/tmp/path'
file_line[:path].should == '/tmp/path'
end
it 'should not accept unqualified path' do
expect { file_line[:path] = 'file' }.to raise_error(Puppet::Error, /File paths must be fully qualified/)
end
it 'should require that a line is specified' do
expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => '/tmp/file') }.to raise_error(Puppet::Error, /Both line and path are required attributes/)
end
it 'should require that a file is specified' do
expect { Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'path') }.to raise_error(Puppet::Error, /Both line and path are required attributes/)
end
it 'should default to ensure => present' do
file_line[:ensure].should eq :present
end
it "should autorequire the file it manages" do
catalog = Puppet::Resource::Catalog.new
file = Puppet::Type.type(:file).new(:name => "/tmp/path")
catalog.add_resource file
catalog.add_resource file_line
relationship = file_line.autorequire.find do |rel|
(rel.source.to_s == "File[/tmp/path]") and (rel.target.to_s == file_line.to_s)
end
relationship.should be_a Puppet::Relationship
end
it "should not autorequire the file it manages if it is not managed" do
catalog = Puppet::Resource::Catalog.new
catalog.add_resource file_line
file_line.autorequire.should be_empty
end
end