rspec-system-puppet 1.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/CHANGELOG.md +34 -0
- data/README.md +17 -7
- data/lib/rspec-system-puppet/helpers.rb +44 -206
- data/lib/rspec-system-puppet/helpers/facter.rb +27 -0
- data/lib/rspec-system-puppet/helpers/puppet_agent.rb +28 -0
- data/lib/rspec-system-puppet/helpers/puppet_apply.rb +49 -0
- data/lib/rspec-system-puppet/helpers/puppet_install.rb +69 -0
- data/lib/rspec-system-puppet/helpers/puppet_master_install.rb +26 -0
- data/lib/rspec-system-puppet/helpers/puppet_module_install.rb +28 -0
- data/lib/rspec-system-puppet/helpers/puppet_resource.rb +25 -0
- data/rspec-system-puppet.gemspec +2 -2
- data/spec/spec_helper_system.rb +2 -0
- data/spec/system/00_initial_spec.rb +24 -0
- data/spec/system/facter_spec.rb +28 -0
- data/spec/system/puppet_agent_spec.rb +17 -0
- data/spec/system/puppet_apply_spec.rb +36 -0
- data/spec/system/puppet_module_install_spec.rb +24 -0
- data/spec/system/puppet_resource_spec.rb +18 -0
- metadata +26 -16
- data/spec/system/basic_spec.rb +0 -88
data/.gitignore
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
2.0.0
|
2
|
+
=====
|
3
|
+
|
4
|
+
This major release adds rspec-system 2 support for using helpers in a subject. So now, all rspec-system-puppet helpers can be used as a normal helper or in a subject like so:
|
5
|
+
|
6
|
+
describe 'puppet tests' do
|
7
|
+
pp = 'notice("foo")'
|
8
|
+
|
9
|
+
context puppet_apply(pp) do
|
10
|
+
its(:stdout) { should =~ /foo/ }
|
11
|
+
its(:stderr) { should be_empty }
|
12
|
+
its(:exit_code) { should be_zero }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
You can also refresh a helper in the middle of a test, here is a neat way to test idempotency:
|
17
|
+
|
18
|
+
describe 'puppet tests' do
|
19
|
+
it 'test that code is idempotent' do
|
20
|
+
pp = "include myclass"
|
21
|
+
|
22
|
+
puppet_apply(pp) do |r|
|
23
|
+
r.stderr.should be_empty
|
24
|
+
r.exit_code.should_not == 1
|
25
|
+
r.refresh # this re-runs the puppet code
|
26
|
+
r.stderr.should be_empty
|
27
|
+
r.exit_code.should be_zero
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
#### Detailed Changes
|
33
|
+
|
34
|
+
* rspec-system 2 helpers (Ken Barber)
|
data/README.md
CHANGED
@@ -13,7 +13,7 @@ While unit testing using [rspec-puppet](https://rubygems.org/gems/rspec-puppet)
|
|
13
13
|
|
14
14
|
Recommended to be installed first:
|
15
15
|
|
16
|
-
* Vagrant 1.
|
16
|
+
* Vagrant 1.2.2 or greater
|
17
17
|
* VirtualBox 4.2.10 or greater
|
18
18
|
* RVM or RBenv (current instructions are based on RVM however)
|
19
19
|
|
@@ -88,6 +88,8 @@ You will need a spec helper for your tests to `require`. So create the file `spe
|
|
88
88
|
require 'rspec-system/spec_helper'
|
89
89
|
require 'rspec-system-puppet/helpers'
|
90
90
|
|
91
|
+
include RSpecSystemPuppet::Helpers
|
92
|
+
|
91
93
|
RSpec.configure do |c|
|
92
94
|
# Project root for the firewall code
|
93
95
|
proj_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
@@ -95,6 +97,8 @@ You will need a spec helper for your tests to `require`. So create the file `spe
|
|
95
97
|
# Enable colour in Jenkins
|
96
98
|
c.tty = true
|
97
99
|
|
100
|
+
c.include RSpecSystemPuppet::Helpers
|
101
|
+
|
98
102
|
# This is where we 'setup' the nodes before running our tests
|
99
103
|
c.before :suite do
|
100
104
|
# Install puppet
|
@@ -114,19 +118,25 @@ Now if you are using rspec-puppet, I advise you to seperate the location of your
|
|
114
118
|
And create your first system tests in say `spec/system/basic_spec.rb` (make sure it has the _spec.rb suffix!):
|
115
119
|
|
116
120
|
require 'spec_helper_system'
|
121
|
+
|
117
122
|
describe 'basic tests:' do
|
123
|
+
# Using puppet_apply as a subject
|
124
|
+
context puppet_apply 'notice("foo")' do
|
125
|
+
its(:stdout) { should =~ /foo/ }
|
126
|
+
its(:stderr) { should be_empty }
|
127
|
+
its(:exit_code) { should be_zero }
|
128
|
+
end
|
129
|
+
|
130
|
+
# Using puppet_apply as a helper
|
118
131
|
it 'my class should work with no errors' do
|
119
132
|
pp = <<-EOS
|
120
133
|
class { 'mymodule': }
|
121
134
|
EOS
|
122
135
|
|
123
|
-
# Run it
|
124
|
-
puppet_apply(pp) do |r|
|
125
|
-
r.exit_code.should_not eq(1)
|
126
|
-
end
|
127
|
-
|
128
|
-
# Run it again and make sure no changes occurred this time, proving idempotency
|
136
|
+
# Run it twice and test for idempotency
|
129
137
|
puppet_apply(pp) do |r|
|
138
|
+
r.exit_code.should_not == 1
|
139
|
+
r.refresh
|
130
140
|
r.exit_code.should be_zero
|
131
141
|
end
|
132
142
|
end
|
@@ -1,95 +1,36 @@
|
|
1
1
|
require 'rspec-system-puppet'
|
2
|
-
|
2
|
+
require 'rspec-system-puppet/helpers/facter'
|
3
|
+
require 'rspec-system-puppet/helpers/puppet_apply'
|
4
|
+
require 'rspec-system-puppet/helpers/puppet_resource'
|
5
|
+
require 'rspec-system-puppet/helpers/puppet_module_install'
|
6
|
+
require 'rspec-system-puppet/helpers/puppet_install'
|
7
|
+
require 'rspec-system-puppet/helpers/puppet_master_install'
|
8
|
+
require 'rspec-system-puppet/helpers/puppet_agent'
|
9
|
+
|
10
|
+
# This module contains the methods provide by rspec-system-puppet
|
3
11
|
module RSpecSystemPuppet::Helpers
|
4
12
|
include RSpecSystem::Helpers
|
5
|
-
include RSpecSystem::Log
|
6
13
|
|
7
14
|
# Basic helper to install puppet
|
8
15
|
#
|
9
16
|
# @param opts [Hash] a hash of opts
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
if facts['osfamily'] == 'Debian'
|
17
|
-
log.info("Remove 'mesg n' from profile to stop noise")
|
18
|
-
shell "sed -i 's/^mesg n/# mesg n/' /root/.profile"
|
19
|
-
end
|
20
|
-
|
21
|
-
# Grab PL repository and install PL copy of puppet
|
22
|
-
log.info "Starting installation of puppet from PL repos"
|
23
|
-
if facts['osfamily'] == 'RedHat'
|
24
|
-
if facts['operatingsystem'] == 'Fedora'
|
25
|
-
# Fedora testing is probably the best for now
|
26
|
-
shell 'sed -i "0,/RE/s/enabled=0/enabled=1/" /etc/yum.repos.d/fedora-updates-testing.repo'
|
27
|
-
else
|
28
|
-
if facts['operatingsystemrelease'] =~ /^6\./
|
29
|
-
shell 'rpm -ivh http://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-6.noarch.rpm'
|
30
|
-
else
|
31
|
-
shell 'rpm -ivh http://yum.puppetlabs.com/el/5/products/x86_64/puppetlabs-release-5-6.noarch.rpm'
|
32
|
-
end
|
33
|
-
end
|
34
|
-
shell 'yum install -y puppet'
|
35
|
-
elsif facts['osfamily'] == 'Debian'
|
36
|
-
shell "wget http://apt.puppetlabs.com/puppetlabs-release-#{facts['lsbdistcodename']}.deb"
|
37
|
-
shell "dpkg -i puppetlabs-release-#{facts['lsbdistcodename']}.deb"
|
38
|
-
shell 'apt-get update'
|
39
|
-
shell 'apt-get install -y puppet'
|
40
|
-
end
|
41
|
-
|
42
|
-
# Prep modules dir
|
43
|
-
log.info("Preparing modules dir")
|
44
|
-
shell 'mkdir -p /etc/puppet/modules'
|
45
|
-
|
46
|
-
# Create alias for puppet
|
47
|
-
pp = <<-EOS
|
48
|
-
host { 'puppet':
|
49
|
-
ip => '127.0.0.1',
|
50
|
-
}
|
51
|
-
EOS
|
52
|
-
puppet_apply(pp)
|
53
|
-
|
54
|
-
# Create hiera.yaml
|
55
|
-
file = Tempfile.new('hierayaml')
|
56
|
-
begin
|
57
|
-
file.write(<<-EOS)
|
58
|
-
---
|
59
|
-
:logger: noop
|
60
|
-
EOS
|
61
|
-
file.close
|
62
|
-
rcp(:sp => file.path, :dp => '/etc/puppet/hiera.yaml')
|
63
|
-
ensure
|
64
|
-
file.unlink
|
65
|
-
end
|
17
|
+
# @option opts [RSpecSystem::Node] :node node to execute DSL on
|
18
|
+
# @return [RspecSystemPuppet::Helpers::PuppetInstall] results
|
19
|
+
# @yield [result] yields result when called as a block
|
20
|
+
# @yieldparam result [RSpecSystem::Helpers::PuppetInstall] results
|
21
|
+
def puppet_install(opts = {}, &block)
|
22
|
+
RSpecSystemPuppet::Helpers::PuppetInstall.new(opts, self, &block)
|
66
23
|
end
|
67
24
|
|
68
25
|
# Basic helper to install a puppet master
|
69
26
|
#
|
70
27
|
# @param opts [Hash] a hash of opts
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
node = opts[:node]
|
78
|
-
|
79
|
-
# Grab facts from node
|
80
|
-
facts = node(:node => node).facts
|
81
|
-
|
82
|
-
if facts['osfamily'] == 'RedHat'
|
83
|
-
shell(:n => node, :c => 'yum install -y puppet-server')
|
84
|
-
if facts['operatingsystemrelease'] =~ /^5\./
|
85
|
-
shell(:n => node, :c => '/etc/init.d/puppetmaster start')
|
86
|
-
else
|
87
|
-
shell(:n => node, :c => 'service puppetmaster start')
|
88
|
-
end
|
89
|
-
elsif facts['osfamily'] == 'Debian'
|
90
|
-
shell(:n => node, :c => 'apt-get install -y puppetmaster')
|
91
|
-
shell(:n => node, :c => 'service puppetmaster start')
|
92
|
-
end
|
28
|
+
# @option opts [RSpecSystem::Node] :node node to execute DSL on
|
29
|
+
# @return [RspecSystemPuppet::Helpers::PuppetMasterInstall] results
|
30
|
+
# @yield [result] yields result when called as a block
|
31
|
+
# @yieldparam result [RSpecSystem::Helpers::PuppetMasterInstall] results
|
32
|
+
def puppet_master_install(opts = {}, &block)
|
33
|
+
RSpecSystemPuppet::Helpers::PuppetMasterInstall.new(opts, self, &block)
|
93
34
|
end
|
94
35
|
|
95
36
|
# Run puppet agent
|
@@ -98,9 +39,9 @@ host { 'puppet':
|
|
98
39
|
# @option opts [RSpecSystem::Node] :node node to execute DSL on
|
99
40
|
# @option opts [Boolean] :debug true if debugging required
|
100
41
|
# @option opts [Boolean] :trace true if trace required
|
101
|
-
# @return [RSpecSystem::
|
42
|
+
# @return [RSpecSystem::Helpers::PuppetAgent] results
|
102
43
|
# @yield [result] yields result when called as a block
|
103
|
-
# @yieldparam result [RSpecSystem::
|
44
|
+
# @yieldparam result [RSpecSystem::Helpers::PuppetAgent] results
|
104
45
|
# @example
|
105
46
|
# puppet_agent.do |r|
|
106
47
|
# r.exit_code.should == 0
|
@@ -109,78 +50,34 @@ host { 'puppet':
|
|
109
50
|
# puppet_agent(:debug => true).do |r|
|
110
51
|
# r.exit_code.should == 0
|
111
52
|
# end
|
112
|
-
def puppet_agent(opts = {})
|
113
|
-
|
114
|
-
opts = {
|
115
|
-
:node => rspec_system_node_set.default_node,
|
116
|
-
:debug => false,
|
117
|
-
:trace => true,
|
118
|
-
}.merge(opts)
|
119
|
-
|
120
|
-
node = opts[:node]
|
121
|
-
|
122
|
-
cmd = "puppet agent -t --detailed-exitcodes"
|
123
|
-
cmd += " --debug" if opts[:debug]
|
124
|
-
cmd += " --trace" if opts[:trace]
|
125
|
-
result = shell(:n => node, :c => cmd)
|
126
|
-
|
127
|
-
if block_given?
|
128
|
-
yield(result)
|
129
|
-
else
|
130
|
-
result
|
131
|
-
end
|
53
|
+
def puppet_agent(opts = {}, &block)
|
54
|
+
RSpecSystemPuppet::Helpers::PuppetAgent.new(opts, self, &block)
|
132
55
|
end
|
133
56
|
|
134
57
|
# Helper to copy a module onto a node from source
|
135
58
|
#
|
136
59
|
# @param opts [Hash] a hash of opts
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
source = opts[:source]
|
145
|
-
module_name = opts[:module_name]
|
146
|
-
module_path = opts[:module_path]
|
147
|
-
node = opts[:node]
|
148
|
-
|
149
|
-
raise "Must provide :source and :module_name parameters" unless source && module_name
|
150
|
-
|
151
|
-
log.info("Now transferring module onto node")
|
152
|
-
rcp(:sp => source, :d => node, :dp => File.join(module_path, module_name))
|
60
|
+
# @option opts [RSpecSystem::Node] :node node to execute DSL on
|
61
|
+
# @return [RSpecSystem::Helpers::PuppetModuleInstall] results
|
62
|
+
# @yield [result] yields result when called as a block
|
63
|
+
# @yieldparam result [RSpecSystem::Helpers::PuppetModuleInstall] results
|
64
|
+
def puppet_module_install(opts, &block)
|
65
|
+
RSpecSystemPuppet::Helpers::PuppetModuleInstall.new(opts, self, &block)
|
153
66
|
end
|
154
67
|
|
155
68
|
# Runs puppet resource commands
|
156
69
|
#
|
157
70
|
# @param opts [Hash] a hash of opts
|
158
|
-
# @
|
71
|
+
# @option opts [RSpecSystem::Node] :node node to execute DSL on
|
72
|
+
# @return [RSpecSystem::Helpers::PuppetResource] results
|
159
73
|
# @yield [result] yields result when called as a block
|
160
|
-
# @yieldparam result [RSpecSystem::
|
161
|
-
def puppet_resource(opts)
|
74
|
+
# @yieldparam result [RSpecSystem::Helpers::PuppetResource] results
|
75
|
+
def puppet_resource(opts, &block)
|
162
76
|
if opts.is_a?(String)
|
163
77
|
opts = {:resource => opts}
|
164
78
|
end
|
165
79
|
|
166
|
-
|
167
|
-
opts = {
|
168
|
-
:node => rspec_system_node_set.default_node
|
169
|
-
}.merge(opts)
|
170
|
-
|
171
|
-
resource = opts[:resource]
|
172
|
-
node = opts[:node]
|
173
|
-
|
174
|
-
raise 'Must provide resource' unless resource
|
175
|
-
|
176
|
-
log.info("Now running puppet resource")
|
177
|
-
result = shell(:n => node, :c => "puppet resource #{resource}")
|
178
|
-
|
179
|
-
if block_given?
|
180
|
-
yield(result)
|
181
|
-
else
|
182
|
-
result
|
183
|
-
end
|
80
|
+
RSpecSystemPuppet::Helpers::PuppetResource.new(opts, self, &block)
|
184
81
|
end
|
185
82
|
|
186
83
|
# Run puppet DSL code directly with `puppet apply`.
|
@@ -194,89 +91,30 @@ host { 'puppet':
|
|
194
91
|
# @option opts [RSpecSystem::Node] :node node to execute DSL on
|
195
92
|
# @option opts [Boolean] :debug true if debugging required
|
196
93
|
# @option opts [Boolean] :trace true if trace required
|
197
|
-
# @return [RSpecSystem::
|
94
|
+
# @return [RSpecSystem::Helpers::PuppetApply] helper object
|
198
95
|
# @yield [result] yields result when called as a block
|
199
|
-
# @yieldparam result [
|
200
|
-
# @example
|
96
|
+
# @yieldparam result [RSpecSystemPuppet::Helpers::PuppetApply] helper object
|
201
97
|
# it "run notice" do
|
202
98
|
# puppet_apply("notice('foo')") do |r|
|
203
99
|
# r.stdout.should =~ /foo/
|
204
100
|
# end
|
205
101
|
# end
|
206
|
-
def puppet_apply(opts)
|
102
|
+
def puppet_apply(opts, &block)
|
207
103
|
if opts.is_a?(String)
|
208
104
|
opts = {:code => opts}
|
209
105
|
end
|
210
106
|
|
211
|
-
|
212
|
-
opts = {
|
213
|
-
:node => rspec_system_node_set.default_node,
|
214
|
-
:debug => false,
|
215
|
-
:trace => true,
|
216
|
-
}.merge(opts)
|
217
|
-
|
218
|
-
code = opts[:code]
|
219
|
-
node = opts[:node]
|
220
|
-
|
221
|
-
raise 'Must provide code' unless code
|
222
|
-
|
223
|
-
log.info("Copying DSL to remote host")
|
224
|
-
file = Tempfile.new('rsp_puppet_apply')
|
225
|
-
file.write(code)
|
226
|
-
file.close
|
227
|
-
|
228
|
-
remote_path = '/tmp/puppetapply.' + rand(1000000000).to_s
|
229
|
-
r = rcp(:sp => file.path, :dp => remote_path, :d => node)
|
230
|
-
file.unlink
|
231
|
-
|
232
|
-
log.info("Cat file to see contents")
|
233
|
-
shell(:n => node, :c => "cat #{remote_path}")
|
234
|
-
|
235
|
-
log.info("Now running puppet apply")
|
236
|
-
cmd = "puppet apply --detailed-exitcodes"
|
237
|
-
cmd += " --debug" if opts[:debug]
|
238
|
-
cmd += " --trace" if opts[:trace]
|
239
|
-
cmd += " #{remote_path}"
|
240
|
-
result = shell(:n => node, :c => cmd)
|
241
|
-
|
242
|
-
if block_given?
|
243
|
-
yield(result)
|
244
|
-
else
|
245
|
-
result
|
246
|
-
end
|
107
|
+
RSpecSystemPuppet::Helpers::PuppetApply.new(opts, self, &block)
|
247
108
|
end
|
248
109
|
|
249
110
|
# Run facter on a remote machine
|
250
111
|
#
|
251
112
|
# @param opts [Hash] a hash of opts
|
252
113
|
# @option opts [RSpecSystem::Node] :node node to execute DSL on
|
253
|
-
# @return [
|
114
|
+
# @return [RSpecSystemPuppet::Helpers::Facter] helper object
|
254
115
|
# @yield [result] yields result when called as a block
|
255
|
-
# @yieldparam result [
|
256
|
-
|
257
|
-
|
258
|
-
# Defaults
|
259
|
-
opts = {
|
260
|
-
:node => rspec_system_node_set.default_node,
|
261
|
-
}.merge(opts)
|
262
|
-
|
263
|
-
node = opts[:node]
|
264
|
-
|
265
|
-
raise "Must specify a node" unless node
|
266
|
-
|
267
|
-
cmd = "facter -y"
|
268
|
-
result = shell(:n => node, :c => cmd)
|
269
|
-
|
270
|
-
begin
|
271
|
-
facts = YAML::load(result.stdout)
|
272
|
-
result.facts = facts
|
273
|
-
rescue
|
274
|
-
end
|
275
|
-
|
276
|
-
if block_given?
|
277
|
-
yield(result)
|
278
|
-
else
|
279
|
-
result
|
280
|
-
end
|
116
|
+
# @yieldparam result [RSpecSystemPuppet::Helpers::Facter] helper object
|
117
|
+
def facter(opts = {}, &block)
|
118
|
+
RSpecSystemPuppet::Helpers::Facter.new(opts, self, &block)
|
281
119
|
end
|
282
120
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rspec-system'
|
2
|
+
require 'rspec-system/helper'
|
3
|
+
require 'rspec-system/result'
|
4
|
+
|
5
|
+
module RSpecSystem::Helpers
|
6
|
+
# Helper object behind RSpecSystemPuppet::Helpers#facter
|
7
|
+
class Facter < RSpecSystem::Helper
|
8
|
+
name 'facter'
|
9
|
+
properties :stdout, :stderr, :exit_code, :facts
|
10
|
+
|
11
|
+
# Gathers new results by executing the resource action
|
12
|
+
#
|
13
|
+
# @return [RSpecSystem::Result] raw execution results
|
14
|
+
def execute
|
15
|
+
node = opts[:node]
|
16
|
+
sh = shell :c => "facter -y", :n => node
|
17
|
+
|
18
|
+
rd = sh.to_hash
|
19
|
+
rd[:facts] = begin
|
20
|
+
YAML::load(sh.stdout)
|
21
|
+
rescue
|
22
|
+
end
|
23
|
+
|
24
|
+
rd
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rspec-system'
|
2
|
+
|
3
|
+
module RSpecSystem::Helpers
|
4
|
+
class PuppetAgent < RSpecSystem::Helper
|
5
|
+
name 'puppet_agent'
|
6
|
+
properties :stdout, :stderr, :exit_code
|
7
|
+
|
8
|
+
def initialize(opts, clr, &block)
|
9
|
+
# Defaults etc.
|
10
|
+
opts = {
|
11
|
+
:debug => false,
|
12
|
+
:trace => true,
|
13
|
+
}.merge(opts)
|
14
|
+
|
15
|
+
super(opts, clr, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def execute
|
19
|
+
node = opts[:node]
|
20
|
+
|
21
|
+
cmd = "puppet agent -t --detailed-exitcodes"
|
22
|
+
cmd += " --debug" if opts[:debug]
|
23
|
+
cmd += " --trace" if opts[:trace]
|
24
|
+
|
25
|
+
shell(:c => cmd, :n => node).to_hash
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rspec-system'
|
2
|
+
|
3
|
+
module RSpecSystem::Helpers
|
4
|
+
# puppet_apply helper
|
5
|
+
class PuppetApply < RSpecSystem::Helper
|
6
|
+
name 'puppet_apply'
|
7
|
+
properties :stdout, :stderr, :exit_code
|
8
|
+
|
9
|
+
def initialize(opts, clr, &block)
|
10
|
+
# Defaults
|
11
|
+
opts = {
|
12
|
+
:debug => false,
|
13
|
+
:trace => true,
|
14
|
+
}.merge(opts)
|
15
|
+
|
16
|
+
raise 'Must provide code' unless opts[:code]
|
17
|
+
|
18
|
+
super(opts, clr, &block)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Run puppet apply in a shell and return results
|
22
|
+
#
|
23
|
+
# @return [Hash] results
|
24
|
+
def execute
|
25
|
+
code = opts[:code]
|
26
|
+
node = opts[:node]
|
27
|
+
|
28
|
+
log.info("Copying DSL to remote host")
|
29
|
+
file = Tempfile.new('rcp_puppet_apply')
|
30
|
+
file.write(code)
|
31
|
+
file.close
|
32
|
+
|
33
|
+
remote_path = '/tmp/puppetapply.' + rand(1000000000).to_s
|
34
|
+
r = rcp(:sp => file.path, :dp => remote_path, :d => node)
|
35
|
+
file.unlink
|
36
|
+
|
37
|
+
log.info("Cat file to see contents")
|
38
|
+
shell :c => "cat #{remote_path}", :n => node
|
39
|
+
|
40
|
+
log.info("Now running puppet apply")
|
41
|
+
cmd = "puppet apply --detailed-exitcodes"
|
42
|
+
cmd += " --debug" if opts[:debug]
|
43
|
+
cmd += " --trace" if opts[:trace]
|
44
|
+
cmd += " #{remote_path}"
|
45
|
+
|
46
|
+
shell(:c => cmd, :n => node).to_hash
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'rspec-system'
|
2
|
+
|
3
|
+
module RSpecSystem::Helpers
|
4
|
+
class PuppetInstall < RSpecSystem::Helper
|
5
|
+
name 'puppet_install'
|
6
|
+
|
7
|
+
def execute
|
8
|
+
node = opts[:node]
|
9
|
+
|
10
|
+
# Grab facts from node
|
11
|
+
facts = node.facts
|
12
|
+
|
13
|
+
# Remove annoying mesg n from profile, otherwise on Debian we get:
|
14
|
+
# stdin: is not a tty which messes with our tests later on.
|
15
|
+
if facts['osfamily'] == 'Debian'
|
16
|
+
log.info("Remove 'mesg n' from profile to stop noise")
|
17
|
+
shell :c => "sed -i 's/^mesg n/# mesg n/' /root/.profile", :n => node
|
18
|
+
end
|
19
|
+
|
20
|
+
# Grab PL repository and install PL copy of puppet
|
21
|
+
log.info "Starting installation of puppet from PL repos"
|
22
|
+
if facts['osfamily'] == 'RedHat'
|
23
|
+
if facts['operatingsystem'] == 'Fedora'
|
24
|
+
# Fedora testing is probably the best for now
|
25
|
+
shell :c => 'sed -i "0,/RE/s/enabled=0/enabled=1/" /etc/yum.repos.d/fedora-updates-testing.repo', :n => node
|
26
|
+
else
|
27
|
+
if facts['operatingsystemrelease'] =~ /^6\./
|
28
|
+
shell :c => 'rpm -ivh http://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-6.noarch.rpm', :n => node
|
29
|
+
else
|
30
|
+
shell :c => 'rpm -ivh http://yum.puppetlabs.com/el/5/products/x86_64/puppetlabs-release-5-6.noarch.rpm', :n => node
|
31
|
+
end
|
32
|
+
end
|
33
|
+
shell :c => 'yum install -y puppet', :n => node
|
34
|
+
elsif facts['osfamily'] == 'Debian'
|
35
|
+
shell :c => "wget http://apt.puppetlabs.com/puppetlabs-release-#{facts['lsbdistcodename']}.deb", :n => node
|
36
|
+
shell :c => "dpkg -i puppetlabs-release-#{facts['lsbdistcodename']}.deb", :n => node
|
37
|
+
shell :c => 'apt-get update', :n => node
|
38
|
+
shell :c => 'apt-get install -y puppet', :n => node
|
39
|
+
end
|
40
|
+
|
41
|
+
# Prep modules dir
|
42
|
+
log.info("Preparing modules dir")
|
43
|
+
shell :c => 'mkdir -p /etc/puppet/modules', :n => node
|
44
|
+
|
45
|
+
# Create alias for puppet
|
46
|
+
pp = <<-EOS
|
47
|
+
host { 'puppet':
|
48
|
+
ip => '127.0.0.1',
|
49
|
+
}
|
50
|
+
EOS
|
51
|
+
puppet_apply(pp)
|
52
|
+
|
53
|
+
# Create hiera.yaml
|
54
|
+
file = Tempfile.new('hierayaml')
|
55
|
+
begin
|
56
|
+
file.write(<<-EOS)
|
57
|
+
---
|
58
|
+
:logger: noop
|
59
|
+
EOS
|
60
|
+
file.close
|
61
|
+
rcp(:sp => file.path, :dp => '/etc/puppet/hiera.yaml', :d => node)
|
62
|
+
ensure
|
63
|
+
file.unlink
|
64
|
+
end
|
65
|
+
|
66
|
+
{}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rspec-system'
|
2
|
+
|
3
|
+
module RSpecSystem::Helpers
|
4
|
+
class PuppetMasterInstall < RSpecSystem::Helper
|
5
|
+
name 'puppet_master_install'
|
6
|
+
|
7
|
+
def execute
|
8
|
+
node = opts[:node]
|
9
|
+
facts = node.facts
|
10
|
+
|
11
|
+
if facts['osfamily'] == 'RedHat'
|
12
|
+
shell :c => 'yum install -y puppet-server', :n => node
|
13
|
+
if facts['operatingsystemrelease'] =~ /^5\./
|
14
|
+
shell :c => '/etc/init.d/puppetmaster start', :n => node
|
15
|
+
else
|
16
|
+
shell :c => 'service puppetmaster start', :n => node
|
17
|
+
end
|
18
|
+
elsif facts['osfamily'] == 'Debian'
|
19
|
+
shell :c => 'apt-get install -y puppetmaster', :n => node
|
20
|
+
shell :c => 'service puppetmaster start', :n => node
|
21
|
+
end
|
22
|
+
|
23
|
+
{}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rspec-system'
|
2
|
+
|
3
|
+
module RSpecSystem::Helpers
|
4
|
+
class PuppetModuleInstall < RSpecSystem::Helper
|
5
|
+
name 'puppet_module_install'
|
6
|
+
|
7
|
+
def initialize(opts, clr, &block)
|
8
|
+
opts = {
|
9
|
+
:module_path => "/etc/puppet/modules",
|
10
|
+
}.merge(opts)
|
11
|
+
|
12
|
+
raise "Must provide :source and :module_name parameters" unless opts[:source] && opts[:module_name]
|
13
|
+
|
14
|
+
super(opts, clr, &block)
|
15
|
+
end
|
16
|
+
|
17
|
+
def execute
|
18
|
+
source = opts[:source]
|
19
|
+
module_name = opts[:module_name]
|
20
|
+
module_path = opts[:module_path]
|
21
|
+
node = opts[:node]
|
22
|
+
|
23
|
+
log.info("Now transferring module onto node")
|
24
|
+
result = rcp :sp => source, :d => node, :dp => File.join(module_path, module_name)
|
25
|
+
result.to_hash
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rspec-system'
|
2
|
+
|
3
|
+
module RSpecSystem::Helpers
|
4
|
+
class PuppetResource < RSpecSystem::Helper
|
5
|
+
name 'puppet_resource'
|
6
|
+
properties :stdout, :stderr, :exit_code
|
7
|
+
|
8
|
+
def initialize(opts, clr, &block)
|
9
|
+
raise "Must provide resource" unless opts[:resource]
|
10
|
+
super(opts, clr, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Run puppet resource in a shell and return results
|
14
|
+
#
|
15
|
+
# @return [Hash] results
|
16
|
+
def execute
|
17
|
+
resource = opts[:resource]
|
18
|
+
node = opts[:node]
|
19
|
+
|
20
|
+
log.info("Now running puppet resource")
|
21
|
+
result = shell :c => "puppet resource #{resource}", :n => node
|
22
|
+
result.to_hash
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/rspec-system-puppet.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
Gem::Specification.new do |s|
|
3
3
|
# Metadata
|
4
4
|
s.name = "rspec-system-puppet"
|
5
|
-
s.version = "
|
5
|
+
s.version = "2.0.0"
|
6
6
|
s.authors = ["Ken Barber"]
|
7
7
|
s.email = ["ken@bob.sh"]
|
8
8
|
s.homepage = "https://github.com/puppetlabs/rspec-system-puppet"
|
@@ -16,5 +16,5 @@ Gem::Specification.new do |s|
|
|
16
16
|
|
17
17
|
# Dependencies
|
18
18
|
s.required_ruby_version = '>= 1.8.7'
|
19
|
-
s.add_runtime_dependency "rspec-system", '~>
|
19
|
+
s.add_runtime_dependency "rspec-system", '~> 2.0'
|
20
20
|
end
|
data/spec/spec_helper_system.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper_system'
|
2
|
+
|
3
|
+
describe "installation tasks" do
|
4
|
+
it "check puppet_install works" do
|
5
|
+
puppet_install()
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'check puppet_master_install works' do
|
9
|
+
puppet_master_install()
|
10
|
+
|
11
|
+
puppet_agent do |r|
|
12
|
+
r.stderr.should == ''
|
13
|
+
r.exit_code.should == 0
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'try puppet_agent with debug enabled' do
|
18
|
+
puppet_agent(:debug => true) do |r|
|
19
|
+
r.stderr.should == ''
|
20
|
+
r.stdout.should =~ /Debug:/
|
21
|
+
r.exit_code.should == 0
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper_system'
|
2
|
+
|
3
|
+
describe "helper facter" do
|
4
|
+
it 'domain fact should return something valid' do
|
5
|
+
facter do |r|
|
6
|
+
r.stderr.should be_empty
|
7
|
+
r.facts['domain'].should =~ /[a-z]+/
|
8
|
+
r.exit_code.should be_zero
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'fqdn fact should return something valid' do
|
13
|
+
facter do |r|
|
14
|
+
r.stderr.should be_empty
|
15
|
+
r.facts['fqdn'].should =~ /vm/
|
16
|
+
r.exit_code.should be_zero
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'test as a subject' do
|
21
|
+
context facter do
|
22
|
+
its(:stderr) { should be_empty }
|
23
|
+
its(:exit_code) { should be_zero }
|
24
|
+
|
25
|
+
it { subject.facts['fqdn'].should =~ /vm/ }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper_system'
|
2
|
+
|
3
|
+
describe "helper puppet_agent" do
|
4
|
+
it 'should run without error' do
|
5
|
+
puppet_agent do |r|
|
6
|
+
r.stderr.should be_empty
|
7
|
+
r.exit_code.should be_zero
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'test as a subject' do
|
12
|
+
context puppet_agent do
|
13
|
+
its(:stderr) { should be_empty }
|
14
|
+
its(:exit_code) { should be_zero }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper_system'
|
2
|
+
|
3
|
+
describe "helper puppet_apply" do
|
4
|
+
it 'should print notice message to stdout' do
|
5
|
+
puppet_apply('notice("foo")') do |r|
|
6
|
+
r.stdout.should =~ /foo/
|
7
|
+
r.stderr.should be_empty
|
8
|
+
r.exit_code.should be_zero
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'in debug mode should return notice message to stdout and Debug: labels' do
|
13
|
+
puppet_apply(:code => 'notice("foo")', :debug => true) do |r|
|
14
|
+
r.stdout.should =~ /foo/
|
15
|
+
r.stdout.should =~ /Debug:/
|
16
|
+
r.stderr.should be_empty
|
17
|
+
r.exit_code.should be_zero
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'with trace off it should work normally' do
|
22
|
+
puppet_apply(:code => 'notice("foo")', :trace => false) do |r|
|
23
|
+
r.stdout.should =~ /foo/
|
24
|
+
r.stderr.should be_empty
|
25
|
+
r.exit_code.should be_zero
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'test as a subject' do
|
30
|
+
context puppet_apply 'notice("foo")' do
|
31
|
+
its(:stdout) { should =~ /foo/ }
|
32
|
+
its(:stderr) { should be_empty }
|
33
|
+
its(:exit_code) { should be_zero }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper_system'
|
2
|
+
|
3
|
+
describe "helper puppet_module_install" do
|
4
|
+
let(:test_path) do
|
5
|
+
proj_root + 'spec' + 'fixtures' + 'mymodule'
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should return no errors when including a class' do
|
9
|
+
puppet_module_install(
|
10
|
+
:source => test_path,
|
11
|
+
:module_name => 'mymodule'
|
12
|
+
)
|
13
|
+
pp = <<-EOS.gsub(/^\s{6}/, '')
|
14
|
+
class { 'mymodule':
|
15
|
+
param1 => 'bar',
|
16
|
+
}
|
17
|
+
EOS
|
18
|
+
puppet_apply(pp) do |r|
|
19
|
+
r.stdout.should =~ /Param1: bar/
|
20
|
+
r.stderr.should be_empty
|
21
|
+
r.exit_code.should be_zero
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper_system'
|
2
|
+
|
3
|
+
describe "helper puppet_resource" do
|
4
|
+
it 'for "user" should returns an exit code of 0' do
|
5
|
+
puppet_resource('user') do |r|
|
6
|
+
r.stderr.should be_empty
|
7
|
+
r.exit_code.should be_zero
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'test as a subject' do
|
12
|
+
context puppet_resource 'user' do
|
13
|
+
its(:stdout) { should_not be_empty }
|
14
|
+
its(:stderr) { should be_empty }
|
15
|
+
its(:exit_code) { should be_zero }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-system-puppet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
|
-
- 1
|
8
7
|
- 2
|
9
8
|
- 0
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 2.0.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ken Barber
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2013-
|
18
|
+
date: 2013-06-10 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rspec-system
|
@@ -24,20 +24,12 @@ dependencies:
|
|
24
24
|
none: false
|
25
25
|
requirements:
|
26
26
|
- - ~>
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 5
|
29
|
-
segments:
|
30
|
-
- 1
|
31
|
-
- 5
|
32
|
-
version: "1.5"
|
33
|
-
- - ">="
|
34
27
|
- !ruby/object:Gem::Version
|
35
28
|
hash: 3
|
36
29
|
segments:
|
37
|
-
-
|
38
|
-
- 5
|
30
|
+
- 2
|
39
31
|
- 0
|
40
|
-
version:
|
32
|
+
version: "2.0"
|
41
33
|
type: :runtime
|
42
34
|
version_requirements: *id001
|
43
35
|
description:
|
@@ -53,17 +45,30 @@ files:
|
|
53
45
|
- .gitignore
|
54
46
|
- .nodeset.yml
|
55
47
|
- .ruby-version
|
48
|
+
- CHANGELOG.md
|
56
49
|
- Gemfile
|
57
50
|
- LICENSE
|
58
51
|
- README.md
|
59
52
|
- Rakefile
|
60
53
|
- lib/rspec-system-puppet.rb
|
61
54
|
- lib/rspec-system-puppet/helpers.rb
|
55
|
+
- lib/rspec-system-puppet/helpers/facter.rb
|
56
|
+
- lib/rspec-system-puppet/helpers/puppet_agent.rb
|
57
|
+
- lib/rspec-system-puppet/helpers/puppet_apply.rb
|
58
|
+
- lib/rspec-system-puppet/helpers/puppet_install.rb
|
59
|
+
- lib/rspec-system-puppet/helpers/puppet_master_install.rb
|
60
|
+
- lib/rspec-system-puppet/helpers/puppet_module_install.rb
|
61
|
+
- lib/rspec-system-puppet/helpers/puppet_resource.rb
|
62
62
|
- rspec-system-puppet.gemspec
|
63
63
|
- spec/fixtures/mymodule/manifests/init.pp
|
64
64
|
- spec/spec_helper.rb
|
65
65
|
- spec/spec_helper_system.rb
|
66
|
-
- spec/system/
|
66
|
+
- spec/system/00_initial_spec.rb
|
67
|
+
- spec/system/facter_spec.rb
|
68
|
+
- spec/system/puppet_agent_spec.rb
|
69
|
+
- spec/system/puppet_apply_spec.rb
|
70
|
+
- spec/system/puppet_module_install_spec.rb
|
71
|
+
- spec/system/puppet_resource_spec.rb
|
67
72
|
homepage: https://github.com/puppetlabs/rspec-system-puppet
|
68
73
|
licenses: []
|
69
74
|
|
@@ -101,5 +106,10 @@ signing_key:
|
|
101
106
|
specification_version: 3
|
102
107
|
summary: Puppet rspec-system plugin
|
103
108
|
test_files:
|
104
|
-
- spec/system/
|
109
|
+
- spec/system/00_initial_spec.rb
|
110
|
+
- spec/system/facter_spec.rb
|
111
|
+
- spec/system/puppet_agent_spec.rb
|
112
|
+
- spec/system/puppet_apply_spec.rb
|
113
|
+
- spec/system/puppet_module_install_spec.rb
|
114
|
+
- spec/system/puppet_resource_spec.rb
|
105
115
|
has_rdoc:
|
data/spec/system/basic_spec.rb
DELETED
@@ -1,88 +0,0 @@
|
|
1
|
-
require 'spec_helper_system'
|
2
|
-
|
3
|
-
describe "basic tests:" do
|
4
|
-
it "check puppet_install works" do
|
5
|
-
puppet_install()
|
6
|
-
end
|
7
|
-
|
8
|
-
it 'check master install works' do
|
9
|
-
puppet_master_install()
|
10
|
-
|
11
|
-
puppet_agent do |r|
|
12
|
-
r.stderr.should == ''
|
13
|
-
r.exit_code.should == 0
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'try puppet agent with debug enabled' do
|
18
|
-
puppet_agent(:debug => true) do |r|
|
19
|
-
r.stderr.should == ''
|
20
|
-
r.stdout.should =~ /Debug:/
|
21
|
-
r.exit_code.should == 0
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'facter domain should return something valid' do
|
26
|
-
facter do |r|
|
27
|
-
r.facts['domain'].should =~ /[a-z]+/
|
28
|
-
r.exit_code.should == 0
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'facter fqdn should return something valid' do
|
33
|
-
facter do |r|
|
34
|
-
r.stderr.should == ''
|
35
|
-
r.facts['fqdn'].should =~ /vm/
|
36
|
-
r.exit_code.should == 0
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'check puppet_resource returns an exit code of 0' do
|
41
|
-
puppet_resource('user') do |r|
|
42
|
-
r.stderr.should == ''
|
43
|
-
r.exit_code.should == 0
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'check puppet apply with just a notice' do
|
48
|
-
puppet_apply('notice("foo")') do |r|
|
49
|
-
r.stdout.should =~ /foo/
|
50
|
-
r.stderr.should == ''
|
51
|
-
r.exit_code.should == 0
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'try puppet apply with debug mode' do
|
56
|
-
puppet_apply(:code => 'notice("foo")', :debug => true) do |r|
|
57
|
-
r.stdout.should =~ /foo/
|
58
|
-
r.stdout.should =~ /Debug:/
|
59
|
-
r.stderr.should == ''
|
60
|
-
r.exit_code.should == 0
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'try puppet apply with trace off' do
|
65
|
-
puppet_apply(:code => 'notice("foo")', :trace => false) do |r|
|
66
|
-
r.stdout.should =~ /foo/
|
67
|
-
r.stderr.should == ''
|
68
|
-
r.exit_code.should == 0
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'check for no errors when including a class' do
|
73
|
-
puppet_module_install(
|
74
|
-
:source => proj_root + 'spec' + 'fixtures' + 'mymodule',
|
75
|
-
:module_name => 'mymodule'
|
76
|
-
)
|
77
|
-
pp = <<-EOS.gsub(/^\s{6}/, '')
|
78
|
-
class { 'mymodule':
|
79
|
-
param1 => 'bar',
|
80
|
-
}
|
81
|
-
EOS
|
82
|
-
puppet_apply(pp) do |r|
|
83
|
-
r.stdout.should =~ /Param1: bar/
|
84
|
-
r.stderr.should == ''
|
85
|
-
r.exit_code.should == 0
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|