Skip to content

Commit

Permalink
apt: Add apt::setting defined type.
Browse files Browse the repository at this point in the history
This is a 'base' type. It's a simple wrapper around a file which takes
`type`, `ensure`, `content`, `source` and `file_perms`. It is intended
for usage by `apt::conf`, `apt::source` and an upcoming `apt::pref`.
  • Loading branch information
daenney committed Feb 20, 2015
1 parent 59c362a commit 21ff418
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 0 deletions.
21 changes: 21 additions & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@
fail('This module only works on Debian or derivatives like Ubuntu')
}

$config_files = {
'conf' => {
'path' => $conf_d,
'ext' => '',
},
'pref' => {
'path' => $preferences_d,
'ext' => '',
},
'list' => {
'path' => $sources_list_d,
'ext' => '.list',
}
}

$file_defaults = {
'owner' => 0,
'group' => 0,
'mode' => '0644',
}

case $::lsbdistid {
'ubuntu', 'debian': {
$distid = $::lsbdistid
Expand Down
52 changes: 52 additions & 0 deletions manifests/setting.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
define apt::setting (
$type,
$priority = 50,
$ensure = file,
$source = undef,
$content = undef,
$file_perms = {},
) {

$_file = merge($::apt::file_defaults, $file_perms)

if $content and $source {
fail('apt::setting cannot have both content and source')
}

if !$content and !$source {
fail('apt::setting needs either of content or source')
}

validate_re($type, ['conf', 'pref', 'list'])
validate_re($ensure, ['file', 'present', 'absent'])

unless is_integer($priority) {
fail('apt::setting priority must be an integer')
}

if $source {
validate_string($source)
}

if $content {
validate_string($content)
}

if $type == 'list' {
$_priority = ''
} else {
$_priority = $priority
}

$_path = $::apt::config_files[$type]['path']
$_ext = $::apt::config_files[$type]['ext']

file { "${_path}/${_priority}${title}${_ext}":
ensure => $ensure,
owner => $_file['owner'],
group => $_file['group'],
mode => $_file['mode'],
content => $content,
source => $source,
}
}
133 changes: 133 additions & 0 deletions spec/defines/setting_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
require 'spec_helper'

describe 'apt::setting' do
let(:pre_condition) { 'class { "apt": }' }
let(:facts) { { :lsbdistid => 'Debian', :osfamily => 'Debian' } }
let(:title) { 'teddybear' }

let(:default_params) { { :type => 'conf', :content => 'di' } }

describe 'when using the defaults' do
context 'without type' do
it do
expect { should compile }.to raise_error(Puppet::Error, /Must pass type /)
end
end

context 'without source or content' do
let(:params) { { :type => 'conf' } }
it do
expect { should compile }.to raise_error(Puppet::Error, /needs either of /)
end
end

context 'with type=conf' do
let(:params) { default_params }
it { should contain_file('/etc/apt/apt.conf.d/50teddybear') }
end

context 'with type=pref' do
let(:params) { { :type => 'pref', :content => 'di' } }
it { should contain_file('/etc/apt/preferences.d/50teddybear') }
end

context 'with type=list' do
let(:params) { { :type => 'list', :content => 'di' } }
it { should contain_file('/etc/apt/sources.list.d/teddybear.list') }
end

context 'with source' do
let(:params) { { :type => 'conf', :source => 'puppet:///la/die/dah' } }
it {
should contain_file('/etc/apt/apt.conf.d/50teddybear').with({
:ensure => 'file',
:owner => 0,
:group => 0,
:mode => '0644',
:source => "#{params[:source]}",
})}
end

context 'with content' do
let(:params) { default_params }
it { should contain_file('/etc/apt/apt.conf.d/50teddybear').with({
:ensure => 'file',
:owner => 0,
:group => 0,
:mode => '0644',
:content => "#{params[:content]}",
})}
end
end

describe 'when trying to pull one over' do
context 'with source and content' do
let(:params) { default_params.merge({ :source => 'la' }) }
it do
expect { should compile }.to raise_error(Puppet::Error, /cannot have both /)
end
end

context 'with type=ext' do
let(:params) { default_params.merge({ :type => 'ext' }) }
it do
expect { should compile }.to raise_error(Puppet::Error, /"ext" does not /)
end
end

context 'with ensure=banana' do
let(:params) { default_params.merge({ :ensure => 'banana' }) }
it do
expect { should compile }.to raise_error(Puppet::Error, /"banana" does not /)
end
end

context 'with priority=1.2' do
let(:params) { default_params.merge({ :priority => 1.2 }) }
it do
expect { should compile }.to raise_error(Puppet::Error, /be an integer /)
end
end
end

describe 'with priority=100' do
let(:params) { default_params.merge({ :priority => 100 }) }
it { should contain_file('/etc/apt/apt.conf.d/100teddybear') }
end

describe 'with ensure=absent' do
let(:params) { default_params.merge({ :ensure => 'absent' }) }
it { should contain_file('/etc/apt/apt.conf.d/50teddybear').with({
:ensure => 'absent',
})}
end

describe 'with file_perms' do
context "{'owner' => 'roosevelt'}" do
let(:params) { default_params.merge({ :file_perms => {'owner' => 'roosevelt'} }) }
it { should contain_file('/etc/apt/apt.conf.d/50teddybear').with({
:owner => 'roosevelt',
:group => 0,
:mode => '0644',
})}
end

context "'group' => 'roosevelt'}" do
let(:params) { default_params.merge({ :file_perms => {'group' => 'roosevelt'} }) }
it { should contain_file('/etc/apt/apt.conf.d/50teddybear').with({
:owner => 0,
:group => 'roosevelt',
:mode => '0644',
})}
end

context "'owner' => 'roosevelt'}" do
let(:params) { default_params.merge({ :file_perms => {'mode' => '0600'} }) }
it { should contain_file('/etc/apt/apt.conf.d/50teddybear').with({
:owner => 0,
:group => 0,
:mode => '0600',
})}
end
end
end

0 comments on commit 21ff418

Please sign in to comment.