puppet_litmus 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a8186fb041ca1c958fb32f75389eceb29cbe6c107c0510b725fdc2bba18b900
4
- data.tar.gz: 7ffe43266b6c14f59f1c306f86c018f44b312d9ee02e9253e3b3cbd38932f26e
3
+ metadata.gz: fe018c648a86597b46e5cdb5840468cb790e3471b49c25d5fd4003c57f140ca6
4
+ data.tar.gz: 180250778c72cb783085036e6ee1908f7d99533f2b26dd92b7fa963711942a03
5
5
  SHA512:
6
- metadata.gz: 12e95c5b22a998f54833adf7edd459ace61a85cceabc1463c5d572110aa2241c7642c96ab943ff6c9a6b8e5be4e42ff925fbaa19a6b4752f14ca5b9b0f050cda
7
- data.tar.gz: 0b7570f08589b2603390f89920cf689a4e4f5491ed279e0dc7498aa3b182b9aa586c96a81be13cb16297d5ab7f6e92ec7eb82e0c31a1eebb4c88552471a2ce74
6
+ metadata.gz: 2a7f75b8b6a1bd870e6df72d784f1bfa433b0a663b3e3cc505a6feb6d404998591f6bb62e183a385de44412bc45a0ca0986f15e969a7adfde8f89522de36deac
7
+ data.tar.gz: 5d571a1f5fba0acad3425fab872bcd1abe404137be45b0aec2cb433f7b89e6f3c3d1eca9ce27173c160cce76d50ef84f420b20399110c3bf744910bfbefe2ccc
@@ -137,15 +137,16 @@ module PuppetLitmus::Serverspec
137
137
  #
138
138
  # @param task_name [String] The name of the task to run.
139
139
  # @param params [Hash] key : value pairs to be passed to the task.
140
- # @return [Object] A result object from the task.
141
- def run_bolt_task(task_name, params = {})
140
+ # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
141
+ # @return [Object] A result object from the task.The values available are stdout, stderr and result.
142
+ def run_bolt_task(task_name, params = {}, opts = {})
142
143
  config_data = { 'modulepath' => File.join(Dir.pwd, 'spec', 'fixtures', 'modules') }
143
144
  inventory_hash = inventory_hash_from_inventory_file
144
145
  target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
145
146
 
146
147
  result = run_task(task_name, target_node_name, params, config: config_data, inventory: inventory_hash)
147
148
 
148
- raise "task failed\n`#{task_name}`\n======\n#{result}" if result.first['status'] != 'success'
149
+ raise "task failed\n`#{task_name}`\n======\n#{result}" if result.first['status'] != 'success' && opts[:expect_failures] != true
149
150
 
150
151
  exit_code = if result.first['status'] == 'success'
151
152
  0
@@ -154,7 +155,34 @@ module PuppetLitmus::Serverspec
154
155
  end
155
156
  result = OpenStruct.new(exit_code: exit_code,
156
157
  stdout: result.first['result']['status'],
157
- stderr: result.first['result']['status'])
158
+ stderr: result.first['result']['stderr'],
159
+ result: result.first['result'])
160
+ yield result if block_given?
161
+ result
162
+ end
163
+
164
+ # Runs a script against the target system.
165
+ #
166
+ # @param script [String] The path to the script on the source machine
167
+ # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
168
+ # @param arguments [Array] Array of arguments to pass to script on runtime
169
+ # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
170
+ # @return [Object] A result object from the script run.
171
+ def bolt_run_script(script, opts = {}, arguments: [])
172
+ target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
173
+ inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
174
+ nil
175
+ else
176
+ inventory_hash_from_inventory_file
177
+ end
178
+
179
+ result = run_script(script, target_node_name, arguments, options: opts, config: nil, inventory: inventory_hash)
180
+
181
+ raise "script run failed\n`#{script}`\n======\n#{result}" if result.first['result']['exit_code'] != 0 && opts[:expect_failures] != true
182
+
183
+ result = OpenStruct.new(exit_code: result.first['result']['exit_code'],
184
+ stdout: result.first['result']['stdout'],
185
+ stderr: result.first['result']['stderr'])
158
186
  yield result if block_given?
159
187
  result
160
188
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # version of this gem
4
4
  module PuppetLitmus
5
- VERSION ||= '0.6.0'
5
+ VERSION ||= '0.7.0'
6
6
  end
@@ -79,4 +79,72 @@ RSpec.describe PuppetLitmus::Serverspec do
79
79
  end
80
80
  end
81
81
  end
82
+
83
+ describe '.bolt_run_script' do
84
+ let(:script) { '/tmp/script.sh' }
85
+ let(:result) { ['result' => { 'exit_code' => 0, 'stdout' => nil, 'stderr' => nil }] }
86
+ let(:inventory_hash) { Hash.new(0) }
87
+
88
+ it 'responds to bolt_run_script' do
89
+ expect(dummy_class).to respond_to(:bolt_run_script).with(1..2).arguments
90
+ end
91
+
92
+ context 'when running against localhost and no inventory.yaml file' do
93
+ it 'does bolt_run_script against localhost without error' do
94
+ allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('localhost')
95
+ expect(dummy_class).not_to receive(:inventory_hash_from_inventory_file)
96
+ expect(dummy_class).to receive(:run_script).with(script, 'localhost', [], options: {}, config: nil, inventory: nil).and_return(result)
97
+ expect { dummy_class.bolt_run_script(script) }.not_to raise_error
98
+ end
99
+ end
100
+
101
+ context 'when running against remote host' do
102
+ it 'does bolt_run_script against remote host without error' do
103
+ allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('some.host')
104
+ expect(dummy_class).to receive(:inventory_hash_from_inventory_file)
105
+ expect(dummy_class).to receive(:run_script).with(script, 'some.host', [], options: {}, config: nil, inventory: nil).and_return(result)
106
+ expect { dummy_class.bolt_run_script(script) }.not_to raise_error
107
+ end
108
+ end
109
+
110
+ context 'when running with arguments' do
111
+ it 'does bolt_run_script with arguments without error' do
112
+ allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('localhost')
113
+ expect(dummy_class).not_to receive(:inventory_hash_from_inventory_file)
114
+ expect(dummy_class).to receive(:run_script).with(script, 'localhost', ['doot'], options: {}, config: nil, inventory: nil).and_return(result)
115
+ expect { dummy_class.bolt_run_script(script, arguments: ['doot']) }.not_to raise_error
116
+ end
117
+ end
118
+ end
119
+
120
+ describe '.run_bolt_task' do
121
+ let(:task_name) { 'testtask' }
122
+ let(:params) { { 'action' => 'install', 'name' => 'foo' } }
123
+ let(:config_data) { { 'modulepath' => File.join(Dir.pwd, 'spec', 'fixtures', 'modules') } }
124
+ let(:result_success) { ['status' => 'success', 'result' => { 'exit_code' => 0, 'stdout' => nil, 'stderr' => nil, 'result' => nil }] }
125
+ let(:result_failure) { ['status' => 'failure', 'result' => { 'exit_code' => 255, 'stdout' => 'failure', 'stderr' => 'failure', 'result' => nil }] }
126
+ let(:inventory_hash) { Hash.new(0) }
127
+
128
+ it 'responds to bolt_run_task' do
129
+ expect(dummy_class).to respond_to(:run_bolt_task).with(2..3).arguments
130
+ end
131
+
132
+ context 'when bolt returns success' do
133
+ it 'does bolt_task_run gives no runtime error for success' do
134
+ allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('some.host')
135
+ expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
136
+ expect(dummy_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_success)
137
+ expect { dummy_class.run_bolt_task(task_name, params, opts: {}) }.not_to raise_error
138
+ end
139
+ end
140
+
141
+ context 'when bolt returns failure' do
142
+ it 'does bolt_task_run gives runtime error for failure' do
143
+ allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('some.host')
144
+ expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
145
+ expect(dummy_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_failure)
146
+ expect { dummy_class.run_bolt_task(task_name, params, opts: {}) }.to raise_error(RuntimeError, %r{task failed})
147
+ end
148
+ end
149
+ end
82
150
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet_litmus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Puppet, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-24 00:00:00.000000000 Z
11
+ date: 2019-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bolt
@@ -110,7 +110,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
112
  requirements: []
113
- rubygems_version: 3.0.4
113
+ rubyforge_project:
114
+ rubygems_version: 2.7.7
114
115
  signing_key:
115
116
  specification_version: 4
116
117
  summary: Providing a simple command line tool for puppet content creators, to enable