Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maint/fix resource relationships #298

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion manifests/cli.pp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@
# Allow Jenkins commands to be issued from the command line
#
class jenkins::cli {

if $caller_module_name != $module_name {
fail("Use of private class ${name} by ${caller_module_name}")
}

include ::jenkins

# XXX Classes/defines which include the jenkins::cli class assume that they
# can use the cli even if $::jenkins::cli == false. This breaks the top
# level anchor pattern. The cli param should either be deprecated and
# essentially hardwired to true or attempting to use cli functionality
# without this param set should fail; either option is a backwards
# incompatible change.
#
# As an attempt to preserve backwards compatibility, there are includes and
# resource relationships being scattered throughout this module.
Class['jenkins::service'] ->
Class['jenkins::cli'] ->
Anchor['jenkins::end']

$jar = "${jenkins::libdir}/jenkins-cli.jar"
$extract_jar = "jar -xf ${jenkins::libdir}/jenkins.war WEB-INF/jenkins-cli.jar"
$move_jar = "mv WEB-INF/jenkins-cli.jar ${jar}"
Expand Down Expand Up @@ -40,4 +54,8 @@
refreshonly => true,
require => File[$jar],
}

# jenkins::cli::reload should be included only after $::jenkins::cli::cmd is
# defined
include ::jenkins::cli::reload
}
4 changes: 4 additions & 0 deletions manifests/cli/exec.pp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
include ::jenkins::cli_helper
include ::jenkins::cli::reload

Class['jenkins::cli_helper'] ->
Jenkins::Cli::Exec[$title] ->
Anchor['jenkins::end']

# $command may be either a string or an array due to the use of flatten()
$run = join(
delete_undef_values(
Expand Down
4 changes: 4 additions & 0 deletions manifests/cli_helper.pp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
include ::jenkins
include ::jenkins::cli

Class['jenkins::cli'] ->
Class['jenkins::cli_helper'] ->
Anchor['jenkins::end']

$libdir = $::jenkins::libdir
$cli_jar = $::jenkins::cli::jar
$port = jenkins_port()
Expand Down
4 changes: 4 additions & 0 deletions manifests/credentials.pp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

include ::jenkins::cli_helper

Class['jenkins::cli_helper'] ->
Jenkins::Credentials[$title] ->
Anchor['jenkins::end']

case $ensure {
'present': {
validate_string($password)
Expand Down
10 changes: 0 additions & 10 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@

if $cli {
include jenkins::cli
include jenkins::cli::reload
}

if $executors {
Expand All @@ -248,15 +247,6 @@
Class['jenkins::jobs'] ->
Anchor['jenkins::end']

if $cli {
Anchor['jenkins::begin'] ->
Class['jenkins::service'] ->
Class['jenkins::cli'] ->
Class['jenkins::cli::reload'] ->
Class['jenkins::jobs'] ->
Anchor['jenkins::end']
}

if $install_java {
Anchor['jenkins::begin'] ->
Class['java'] ->
Expand Down
5 changes: 5 additions & 0 deletions manifests/job.pp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
$enabled = 1,
$ensure = 'present',
){
include ::jenkins::cli

Class['jenkins::cli'] ->
Jenkins::Job[$title] ->
Anchor['jenkins::end']

if ($ensure == 'absent') {
jenkins::job::absent { $title:
Expand Down
2 changes: 0 additions & 2 deletions manifests/job/present.pp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
$jobname = $title,
$enabled = 1,
){
include jenkins::cli

if $jenkins::service_ensure == 'stopped' or $jenkins::service_ensure == false {
fail('Management of Jenkins jobs requires \$jenkins::service_ensure to be set to \'running\'')
}
Expand Down
4 changes: 4 additions & 0 deletions manifests/security.pp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

include ::jenkins::cli_helper

Class['jenkins::cli_helper'] ->
Class['jenkins::security'] ->
Anchor['jenkins::end']

# XXX not idempotent
jenkins::cli::exec { "jenkins-security-${security_model}":
command => [
Expand Down
4 changes: 4 additions & 0 deletions manifests/user.pp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

include ::jenkins::cli_helper

Class['jenkins::cli_helper'] ->
Jenkins::User[$title] ->
Anchor['jenkins::end']

case $ensure {
'present': {
validate_re($email, '^[^@]+@[^@]+$', "An email address is required, not '${email}'")
Expand Down
124 changes: 124 additions & 0 deletions spec/acceptance/job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
require 'spec_helper_acceptance'

describe 'jenkins::job' do
let(:test_build_job) {
example = <<'EOS'
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description>test job</description>
<keepDependencies>false</keepDependencies>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>/usr/bin/true</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>
EOS
# escape single quotes for puppet
example.gsub("'", %q(\\\'))
}

context 'create' do
it 'should work with no errors' do
pp = <<-EOS
class {'jenkins': }

# the historical assumption is that this will work without cli => true
# set on the jenkins class
jenkins::job { 'test-build-job':
config => \'#{test_build_job}\',
}
EOS

# Run it twice and test for idempotency
apply_manifest(pp, :catch_failures => true)
# XXX idempotency is broken with at least jenkins 1.613
#apply_manifest(pp, :catch_changes => true)
end

describe file('/var/lib/jenkins/jobs/test-build-job/config.xml') do
it { should be_file }
it { should be_owned_by 'jenkins' }
it { should be_grouped_into 'jenkins' }
it { should be_mode 644 }
it { should contain '<description>test job</description>' }
it { should contain '<disabled>false</disabled>' }
it { should contain '<command>/usr/bin/true</command>' }
end
end

context 'disable' do
it 'should work with no errors' do
pp = <<-EOS
class {'jenkins': }

jenkins::job { 'test-build-job':
config => \'#{test_build_job}\',
enabled => 0,
}
EOS

# Run it twice and test for idempotency
apply_manifest(pp, :catch_failures => true)
# XXX idempotency is broken with at least jenkins 1.613
#apply_manifest(pp, :catch_changes => true)
end

describe file('/var/lib/jenkins/jobs/test-build-job/config.xml') do
it { should be_file }
it { should be_owned_by 'jenkins' }
it { should be_grouped_into 'jenkins' }
it { should be_mode 644 }
it { should contain '<description>test job</description>' }
it { should contain '<disabled>true</disabled>' }
it { should contain '<command>/usr/bin/true</command>' }
end
end

context 'delete' do
it 'should work with no errors' do
# create a test job so it can be deleted; job creation is not what
# we're intending to be testing here
pp = <<-EOS
class {'jenkins': }

jenkins::job { 'test-build-job':
config => \'#{test_build_job}\',
}
EOS

apply_manifest(pp)

# test job deletion
pp = <<-EOS
class {'jenkins': }

jenkins::job { 'test-build-job':
ensure => 'absent',
config => \'#{test_build_job}\',
}
EOS

# Run it twice and test for idempotency
apply_manifest(pp, :catch_failures => true)
# XXX idempotency is broken with at least jenkins 1.613
#apply_manifest(pp, :catch_changes => true)
end

describe file('/var/lib/jenkins/jobs/test-build-job/config.xml') do
# XXX Serverspec::Type::File doesn't support exists?
it { should_not be_file }
end
end
end
17 changes: 17 additions & 0 deletions spec/classes/jenkins_cli_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe 'jenkins::cli_helper', :type => :class do
let(:facts) { { :osfamily => 'RedHat', :operatingsystem => 'RedHat' } }

describe 'relationships' do
it do
should contain_class('jenkins::cli_helper').
that_requires('Class[jenkins::cli]')
end
it do
should contain_class('jenkins::cli_helper').
that_comes_before('Anchor[jenkins::end]')
end
end
end

18 changes: 15 additions & 3 deletions spec/classes/jenkins_cli_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe 'jenkins', :type => :module do
describe 'jenkins', :type => :class do
let(:facts) { { :osfamily => 'RedHat', :operatingsystem => 'RedHat' } }

context 'cli' do
Expand All @@ -12,12 +12,24 @@
let(:params) {{ :cli => true,
:config_hash => { 'HTTP_PORT' => { 'value' => '9000' } }
}}
it { should create_class('jenkins::cli') }
it { should contain_class('jenkins::cli') }
it { should contain_exec('jenkins-cli') }
it { should contain_exec('reload-jenkins').with_command(/http:\/\/localhost:9000/) }
it { should contain_exec('safe-restart-jenkins') }
it { should contain_jenkins__sysconfig('HTTP_PORT').with_value('9000') }

describe 'jenkins::cli' do
describe 'relationships' do
it do
should contain_class('jenkins::cli').
that_requires('Class[jenkins::service]')
end
it do
should contain_class('jenkins::cli').
that_comes_before('Anchor[jenkins::end]')
end
end
end
end
end

end
16 changes: 16 additions & 0 deletions spec/classes/jenkins_security_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

describe 'jenkins::security', :type => :class do
let(:facts) {{ :osfamily => 'RedHat', :operatingsystem => 'RedHat' }}

describe 'relationships' do
it do
should contain_class('jenkins::security').
that_requires('Class[jenkins::cli_helper]')
end
it do
should contain_class('jenkins::security').
that_comes_before('Anchor[jenkins::end]')
end
end
end
11 changes: 11 additions & 0 deletions spec/defines/jenkins_cli_exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@

let(:helper_cmd) { '/usr/bin/java -jar /usr/lib/jenkins/jenkins-cli.jar -s http://127.0.0.1:8080 groovy /usr/lib/jenkins/puppet_helper.groovy' }

describe 'relationships' do
it do
should contain_jenkins__cli__exec('foo').
that_requires('Class[jenkins::cli_helper]')
end
it do
should contain_jenkins__cli__exec('foo').
that_comes_before('Anchor[jenkins::end]')
end
end

describe 'title =>' do
context 'foo' do
# default title...
Expand Down
18 changes: 18 additions & 0 deletions spec/defines/jenkins_credentials_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec_helper'

describe 'jenkins::credentials', :type => :define do
let(:title) { 'foo' }
let(:facts) {{ :osfamily => 'RedHat', :operatingsystem => 'RedHat' }}

describe 'relationships' do
let(:params) {{ :password => 'foo' }}
it do
should contain_jenkins__credentials('foo').
that_requires('Class[jenkins::cli_helper]')
end
it do
should contain_jenkins__credentials('foo').
that_comes_before('Anchor[jenkins::end]')
end
end
end
13 changes: 13 additions & 0 deletions spec/defines/jenkins_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

describe 'jenkins::job' do
let(:title) { 'myjob' }
let(:facts) {{ :osfamily => 'RedHat', :operatingsystem => 'RedHat' }}

describe 'relationships' do
let(:params) {{ :config => '' }}
it do
should contain_jenkins__job('myjob').
that_requires('Class[jenkins::cli]')
end
it do
should contain_jenkins__job('myjob').
that_comes_before('Anchor[jenkins::end]')
end
end

describe 'with defaults' do
let(:params) {{ :config => '' }}
Expand Down
Loading