-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcli_spec.rb
More file actions
409 lines (340 loc) · 13.6 KB
/
cli_spec.rb
File metadata and controls
409 lines (340 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# Copyright:: Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "spec_helper"
require "stringio"
require "chef-cli/cli"
require "unit/fixtures/command/cli_test_command"
describe ChefCLI::CLI do
let(:argv) { [] }
# Setup a new commands map so we control what subcommands exist. Otherwise
# we'd have to update this test for every new subcommand we add or code the
# tests defensively.
let(:commands_map) { ChefCLI::CommandsMap.new }
let(:stdout_io) { StringIO.new }
let(:stderr_io) { StringIO.new }
def stdout
stdout_io.string
end
def stderr
stderr_io.string
end
let(:base_help_message) do
<<~E
#{ChefCLI::Dist::WORKSTATION_HEADER}
Docs: #{ChefCLI::Dist::WORKSTATION_DOCS}
Patents: #{ChefCLI::Dist::PATENTS}
Usage:
chef-cli -h/--help
chef-cli -v/--version
chef-cli command [arguments...] [options...]
Available Commands:
gem Runs the `gem` command in context of the embedded ruby
example Example subcommand for testing
E
end
def run_cli(expected_exit_code)
expect(cli).to receive(:exit).with(expected_exit_code)
expect(cli).to receive(:path_check!)
cli.run
end
def run_cli_with_sanity_check(expected_exit_code)
expect(cli).to receive(:exit).with(expected_exit_code)
cli.run
end
def mock_shell_out(exitstatus, stdout, stderr)
shell_out = double("mixlib_shell_out")
allow(shell_out).to receive(:exitstatus).and_return(exitstatus)
allow(shell_out).to receive(:stdout).and_return(stdout)
allow(shell_out).to receive(:stderr).and_return(stderr)
shell_out
end
subject(:cli) do
ChefCLI::CLI.new(argv).tap do |c|
allow(c).to receive(:commands_map).and_return(commands_map)
allow(c).to receive(:stdout).and_return(stdout_io)
allow(c).to receive(:stderr).and_return(stderr_io)
end
end
before do
commands_map.builtin "gem", :GemForwarder, require_path: "chef-cli/command/gem",
desc: "Runs the `gem` command in context of the embedded ruby"
commands_map.builtin "example", :TestCommand, require_path: "unit/fixtures/command/cli_test_command",
desc: "Example subcommand for testing"
allow(ChefCLI::Licensing::Base).to receive(:validate).and_return(true)
end
context "given no arguments or options" do
it "prints the help output" do
run_cli(0)
expect(stdout).to eq(base_help_message)
end
end
context "given -h" do
let(:argv) { %w{-h} }
it "prints the help output" do
run_cli(0)
expect(stdout).to eq(base_help_message)
end
end
context "given -v" do
let(:argv) { %w{-v} }
context "when installed via omnibus" do
let(:mocked_version_manifest_json) do
<<~E
{
"manifest_format": 2,
"build_version": "d.e.v",
"software": {
"habitat": {
"locked_version": "1.2.3"
}
}
}
E
end
let(:mocked_gem_version_manifest_json) do
<<~E
{
"chef-cli": [
"0.0.1"
],
"chef": [
"0.0.2"
],
"inspec": [
"0.0.3"
],
"test-kitchen": [
"0.0.4"
],
"cookstyle": [
"0.0.6"
]
}
E
end
# rubocop:disable Layout/TrailingWhitespace
let(:full_table_with_version_message) do
<<~E
Chef Workstation version: d.e.v
Chef Infra Client version: 0.0.2
Chef InSpec version: 0.0.3
Chef CLI version: 0.0.1
Chef Habitat version: 1.2.3
Test Kitchen version: 0.0.4
Cookstyle version: 0.0.6
E
end
let(:full_table_with_unknown_version_message) do
<<~E
Chef Workstation version: unknown
Chef Infra Client version: unknown
Chef InSpec version: unknown
Chef CLI version: unknown
Chef Habitat version: unknown
Test Kitchen version: unknown
Cookstyle version: unknown
E
end
before do
allow(cli).to receive(:omnibus_install?).and_return true
allow(cli).to receive(:read_version_manifest_json).and_return(mocked_version_manifest_json)
allow(cli).to receive(:read_gem_version_manifest_json).and_return(mocked_gem_version_manifest_json)
end
it "does not print versions of tools with missing or errored tools" do
allow(cli).to receive(:read_gem_version_manifest_json).and_return("{}")
allow(cli).to receive(:read_version_manifest_json).and_return("{}")
run_cli(0)
expect(stdout).to eq(full_table_with_unknown_version_message)
end
it "prints a table with the version of all the tools" do
run_cli(0)
expect(stdout).to eq(full_table_with_version_message)
end
end
context "when installed as a gem" do
let(:gem_version_message) { "#{ChefCLI::Dist::CLI_PRODUCT} version: #{ChefCLI::VERSION}\n" }
before do
allow(cli).to receive(:omnibus_install?).and_return false
end
it "prints the version and versions of chef-cli tools" do
run_cli(0)
expect(stdout).to eq(gem_version_message)
end
end
end
context "given an invalid option" do
let(:argv) { %w{-nope} }
it "prints an 'invalid option message and the help output, then exits non-zero" do
run_cli(1)
expect(stdout).to eq(base_help_message)
expect(stderr).to eq("invalid option: -nope\n")
end
end
context "given an invalid/unknown subcommand" do
let(:argv) { %w{ancient-aliens} }
it "prints an 'unknown command' message and the help output" do
expected_err = "Unknown command `ancient-aliens'.\n"
run_cli(1)
expect(stderr).to eq(expected_err)
expect(stdout).to eq(base_help_message)
end
end
context "given a valid subcommand" do
let(:argv) { %w{example with some args --and-an-option} }
def test_result
ChefCLI::Command::TestCommand.test_result
end
before do
ChefCLI::Command::TestCommand.reset!
end
it "runs the subcommand" do
run_cli(23)
expect(test_result[:status]).to eq(:success)
end
it "exits with the return code given by the subcommand" do
run_cli(23)
end
it "passes arguments and options to the subcommand" do
params = %w{with some args --and-an-option}
run_cli(23)
expect(test_result[:params]).to eq(params)
end
end
context "path_check!" do
before do
allow(Gem).to receive(:ruby).and_return(ruby_path)
allow(cli).to receive(:package_home).and_return("/opt/chef-workstation")
end
context "when installed via omnibus" do
before do
allow(cli).to receive(:omnibus_install?).and_return true
end
context "on unix" do
let(:ruby_path) { "/opt/chef-workstation/embedded/bin/ruby" }
before do
stub_const("File::PATH_SEPARATOR", ":")
allow(Chef::Util::PathHelper).to receive(:cleanpath) do |path|
path
end
end
it "complains if embedded is first" do
allow(cli).to receive(:env).and_return({ "PATH" => "/opt/chef-workstation/embedded/bin:/opt/chef-workstation/bin" })
allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("/opt/chef-workstation/embedded/bin")
allow(cli).to receive(:omnibus_bin_dir).and_return("/opt/chef-workstation/bin")
run_cli_with_sanity_check(0)
expect(stdout).to eq(base_help_message)
expect(stderr).to include("please reverse that order")
expect(stderr).to include("chef-cli shell-init")
end
it "complains if only embedded is present" do
allow(cli).to receive(:env).and_return({ "PATH" => "/opt/chef-workstation/embedded/bin" })
allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("/opt/chef-workstation/embedded/bin")
allow(cli).to receive(:omnibus_bin_dir).and_return("/opt/chef-workstation/bin")
run_cli_with_sanity_check(0)
expect(stdout).to eq(base_help_message)
expect(stderr).to include("you must add")
expect(stderr).to include("chef-cli shell-init")
end
it "passes when both are present in the correct order" do
allow(cli).to receive(:env).and_return({ "PATH" => "/opt/chef-workstation/bin:/opt/chef-workstation/embedded/bin" })
allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("/opt/chef-workstation/embedded/bin")
allow(cli).to receive(:omnibus_bin_dir).and_return("/opt/chef-workstation/bin")
run_cli_with_sanity_check(0)
expect(stdout).to eq(base_help_message)
end
it "passes when only the omnibus bin dir is present" do
allow(cli).to receive(:env).and_return({ "PATH" => "/opt/chef-workstation/bin" })
allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("/opt/chef-workstation/embedded/bin")
allow(cli).to receive(:omnibus_bin_dir).and_return("/opt/chef-workstation/bin")
run_cli_with_sanity_check(0)
expect(stdout).to eq(base_help_message)
end
end
context "on windows" do
let(:ruby_path) { "c:/opscode/chef-workstation/embedded/bin/ruby.exe" }
let(:omnibus_root) { "c:/opscode/chef-workstation" }
before do
# Would be preferable not to stub this, but `File.expand_path` does
# weird things with windows paths on unix machines.
#
# I manually verified the behavior:
#
# $ /c/opscode/chef-workstation/embedded/bin/ruby -e 'p File.expand_path(File.join(Gem.ruby, "..", "..", ".."))'
# "c:/opscode/chef-workstation"
allow(cli).to receive(:expected_omnibus_root).and_return(ruby_path)
allow(cli).to receive(:omnibus_install?).and_return(true)
allow(Chef::Platform).to receive(:windows?).and_return(true)
stub_const("File::PATH_SEPARATOR", ";")
allow(Chef::Util::PathHelper).to receive(:cleanpath) do |path|
path.tr "/", "\\"
end
end
it "complains if embedded is first" do
allow(cli).to receive(:env).and_return({ "PATH" => 'C:\opscode\chef-workstation\embedded\bin;C:\opscode\chef-workstation\bin' })
allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("c:/opscode/chef-workstation/embedded/bin")
allow(cli).to receive(:omnibus_bin_dir).and_return("c:/opscode/chef-workstation/bin")
run_cli_with_sanity_check(0)
expect(stdout).to eq(base_help_message)
expect(stderr).to include("please reverse that order")
expect(stderr).to include("chef-cli shell-init")
end
it "complains if only embedded is present" do
allow(cli).to receive(:env).and_return({ "PATH" => 'C:\opscode\chef-workstation\embedded\bin' })
allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("c:/opscode/chef-workstation/embedded/bin")
allow(cli).to receive(:omnibus_bin_dir).and_return("c:/opscode/chef-workstation/bin")
run_cli_with_sanity_check(0)
expect(stdout).to eq(base_help_message)
expect(stderr).to include("you must add")
expect(stderr).to include("chef-cli shell-init")
end
it "passes when both are present in the correct order" do
allow(cli).to receive(:env).and_return({ "PATH" => 'C:\opscode\chef-workstation\bin;C:\opscode\chef-workstation\embedded\bin' })
allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("c:/opscode/chef-workstation/embedded/bin")
allow(cli).to receive(:omnibus_bin_dir).and_return("c:/opscode/chef-workstation/bin")
run_cli_with_sanity_check(0)
expect(stdout).to eq(base_help_message)
end
it "passes when only the omnibus bin dir is present" do
allow(cli).to receive(:env).and_return({ "PATH" => 'C:\opscode\chef-workstation\bin' })
allow(cli).to receive(:omnibus_embedded_bin_dir).and_return("c:/opscode/chef-workstation/embedded/bin")
allow(cli).to receive(:omnibus_bin_dir).and_return("c:/opscode/chef-workstation/bin")
run_cli_with_sanity_check(0)
expect(stdout).to eq(base_help_message)
end
end
end
context "when not installed via omnibus" do
before do
allow(cli).to receive(:omnibus_install?).and_return false
end
let(:ruby_path) { "/Users/bog/.lots_o_rubies/2.1.2/bin/ruby" }
let(:expected_root_path) { "/Users/bog/.lots_o_rubies" }
before do
allow(File).to receive(:exist?).with(expected_root_path).and_return(false)
%i{
omnibus_root
omnibus_bin_dir
omnibus_embedded_bin_dir
}.each do |method_name|
allow(cli).to receive(method_name).and_raise(ChefCLI::OmnibusInstallNotFound.new)
end
end
it "skips the sanity check without error" do
run_cli_with_sanity_check(0)
end
end
end
end