-
Notifications
You must be signed in to change notification settings - Fork 8
/
cli_spec.rb
401 lines (338 loc) · 15.8 KB
/
cli_spec.rb
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
require 'spec_helper'
require 'suse/connect/cli'
describe SUSE::Connect::Cli do
let(:default_logger) { SUSE::Connect::GlobalLogger.instance.log }
let(:string_logger) { ::Logger.new(StringIO.new) }
let(:opts) { {} }
let(:cli) { described_class.new opts }
let(:config_file) { File.expand_path File.join(File.dirname(__FILE__), '../fixtures/SUSEConnect') }
before do
allow(Zypper).to receive_messages(base_product: {})
allow_any_instance_of(described_class).to receive_messages(puts: true)
SUSE::Connect::GlobalLogger.instance.log = string_logger
allow_any_instance_of(Status).to receive(:activated_base_product?).and_return(true)
end
after do
SUSE::Connect::GlobalLogger.instance.log = default_logger
end
describe '#execute!' do
subject { cli.execute! }
context 'server errors' do
let(:opts) { %w[-r 123] }
it 'should produce log output if ApiError encountered' do
expect(string_logger).to receive(:fatal).with("Error: Registration server returned 'test' (222)")
response = Net::HTTPResponse.new('1.1', 222, 'Test')
allow(response).to receive(:body).and_return('localized_error' => 'test')
allow_any_instance_of(Client).to receive(:register!).and_raise ApiError.new(response)
expect { cli.execute! }.to exit_with_code(67)
end
context 'system has proper credentials file' do
it 'should suggest re-registration if ApiError 401 encountered' do
response = Net::HTTPResponse.new('1.1', 401, 'Test')
allow(System).to receive(:credentials?).and_return true
expect_any_instance_of(Client).to receive(:register!).and_raise ApiError.new(response)
expect(string_logger).to receive(:fatal).with(match(/Error: Invalid system credentials/))
expect { cli.execute! }.to exit_with_code(67)
end
end
context 'system has no proper credentials file' do
it 'should suggest re-registration if ApiError 401 encountered' do
# INFO: Use double instead of HTTPResponse mock https://www.ruby-forum.com/topic/4407036
response = double(code: 401, body: { 'localized_error' => 'Invalid registration code' })
allow(System).to receive(:credentials?).and_return false
expect_any_instance_of(Client).to receive(:register!).and_raise ApiError.new(response)
error_message = "Error: Registration server returned 'Invalid registration code' (401)"
expect(string_logger).to receive(:fatal).with(error_message)
expect { cli.execute! }.to exit_with_code(67)
end
end
context 'while calling the obsolete RegistrationProxy' do
it 'should suggest updating the Registration Proxy server' do
expect_any_instance_of(Client).to receive(:register!).and_raise JSON::ParserError
allow_any_instance_of(SUSE::Connect::Config).to receive(:url_default?).and_return(false)
expect_any_instance_of(Api).to receive(:up_to_date?).and_return(false)
error = "Your Registration Proxy server doesn't support this function. Please update it and try again."
expect(string_logger).to receive(:fatal).with(error)
expect { cli.execute! }.to exit_with_code(66)
end
end
it 'should produce log output if connection refused' do
expect(string_logger).to receive(:fatal).with('Error: Connection refused by server https://scc.suse.com')
allow_any_instance_of(Client).to receive(:register!).and_raise Errno::ECONNREFUSED
expect { cli.execute! }.to exit_with_code(64)
end
it 'should produce log output if json parse error encountered' do
expect(string_logger).to receive(:fatal).with('Error: Cannot parse response from server')
allow_any_instance_of(Client).to receive(:register!).and_raise JSON::ParserError
expect { cli.execute! }.to exit_with_code(66)
end
it 'should produce log output if EACCES encountered' do
expect(string_logger).to receive(:fatal).with('Error: Access error - Permission denied')
allow_any_instance_of(Client).to receive(:register!).and_raise Errno::EACCES
expect { cli.execute! }.to exit_with_code(65)
end
it 'should produce log output if FileError encountered' do
expect(string_logger).to receive(:fatal).with('FileError: \'test\'')
allow_any_instance_of(Client).to receive(:register!).and_raise(FileError, 'test')
expect { cli.execute! }.to exit_with_code(68)
end
end
context 'zypper error' do
let(:opts) { %w[-r 456] }
it 'should produce log output if zypper errors' do
expect(string_logger).to receive(:fatal).with('Error: zypper returned (666) with \'<stream><error>zypper down</error></stream>\'')
allow_any_instance_of(Client).to receive(:register!).and_raise ZypperError.new(666, '<stream><error>zypper down</error></stream>')
expect { cli.execute! }.to raise_error(SystemExit)
end
end
context 'parameter dependencies' do
it 'requires no other parameters on --status' do
cli = described_class.new(%w[--status])
expect_any_instance_of(Status).to receive(:print_product_statuses)
cli.execute!
end
it 'does not require --regcode or --url when specifying a product (eg. an extension)' do
cli = described_class.new(%w[-p sle-module-web-scripting/12/x86_64])
expect_any_instance_of(Client).to receive(:register!)
cli.execute!
end
context 'when the system has no activated base product' do
it 'shows a properly rendered help page' do
expect_any_instance_of(Client).not_to receive(:register!)
expect_any_instance_of(described_class).to receive(:puts) do |option_parser|
expect(option_parser.instance_variable_get(:@opts).to_s.split("\n").map(&:length)).to all be <= 80
end
expect { cli.execute! }.to raise_error(SystemExit)
end
it 'registers the system if --regcode was provided' do
cli = described_class.new(%w[-r 456])
expect_any_instance_of(Client).to receive(:register!)
cli.execute!
end
it 'registers the system if --url was provided' do
cli = described_class.new(%w[--url http://somewhere.com])
expect_any_instance_of(Client).to receive(:register!)
expect_any_instance_of(SUSE::Connect::Config).to receive(:write!)
cli.execute!
end
it 'registers the system if using a configured proxy' do
allow_any_instance_of(SUSE::Connect::Config).to receive(:url_default?).and_return(false)
expect_any_instance_of(Client).to receive(:register!)
cli.execute!
end
end
it 'requires either --regcode or --url (regcode-less SMT registration) but respects config attributes' do
config = SUSE::Connect::Config.new(config_file)
config.url = 'https://smt.server'
allow(SUSE::Connect::Config).to receive(:new).and_return(config)
allow_any_instance_of(Client).to receive(:register!).and_return true
expect(string_logger).not_to receive(:error)
.with('Please set the regcode parameter to register against SCC, or the url parameter to register against SMT')
cli.execute!
end
it '--instance-data requires --url' do
cli = described_class.new(%w[--instance-data /tmp/test])
expect(string_logger).to receive(:error)
.with('Please use --instance-data only in combination with --url pointing to your RMT or SMT server')
expect { cli.execute! }.to raise_error(SystemExit)
end
it '--instance-data is mutually exclusive with --regcode' do
cli = described_class.new(%w[-r 123 --instance-data /tmp/test --url test])
expect(string_logger).to receive(:error)
.with('Please use either --regcode or --instance-data')
expect { cli.execute! }.to raise_error(SystemExit)
end
it '--url implies --write-config' do
cli = described_class.new(%w[-r 123 --url http://foo.test.com])
expect(cli.config.write_config).to eq true
allow_any_instance_of(SUSE::Connect::Client).to receive(:register!)
expect_any_instance_of(SUSE::Connect::Config).to receive(:write!)
cli.execute!
end
end
describe 'de-register command' do
let(:opts) { %w[--de-register] }
it '--de-register calls deregister! method' do
expect_any_instance_of(Client).to receive(:deregister!)
subject
end
context 'on unregistered system' do
before { allow(SUSE::Connect::System).to receive(:credentials).and_return(nil) }
it 'dies with error' do
expect(string_logger).to receive(:fatal).with(/Deregistration failed. Check if the system has been registered/)
expect { subject }.to exit_with_code(69)
end
end
context 'with product specified' do
let(:opts) { %w[--de-register -p foo/12/x86_64] }
before { allow(SUSE::Connect::System).to receive(:credentials?).and_return(true) }
context 'calling for base product' do
before do
allow(Zypper).to receive(:base_product).and_return SUSE::Connect::Zypper::Product.new(name: 'foo', version: '12', arch: 'x86_64')
end
it 'dies with error' do
expect(string_logger).to receive(:fatal).with(/Can not deregister base product/)
expect { subject }.to exit_with_code(70)
end
end
end
end
context 'cleanup command' do
it '--cleanup calls Systems cleanup! method' do
cli = described_class.new(%w[--cleanup])
expect(System).to receive(:cleanup!)
cli.execute!
end
end
context '--namespace option' do
it 'requires a namespace' do
expect(string_logger).to receive(:error).with('Please provide a namespace')
expect { described_class.new('--namespace') }.to exit_with_code(1)
end
it 'sets the given namespace in the config' do
cli = described_class.new(%w[--namespace mynamespace])
expect(cli.config[:namespace]).to eq 'mynamespace'
end
it 'automatically writes the config' do
cli = described_class.new(%w[--namespace mynamespace])
expect(cli.options[:write_config]).to be true
end
end
context 'status subcommand' do
it '--status calls json_product_status' do
cli = described_class.new(%w[--status])
expect_any_instance_of(Client).to_not receive(:register!)
expect_any_instance_of(Status).to receive(:json_product_status)
cli.execute!
end
it '--status-text calls text_product_status' do
cli = described_class.new(%w[--status-text])
expect_any_instance_of(Client).to_not receive(:register!)
expect_any_instance_of(Status).to receive(:text_product_status)
cli.execute!
end
end
describe 'list extensions subcommand' do
context 'on system with registered base product' do
it '--list-extensions lists all available extensions on the system' do
cli = described_class.new(%w[--list-extensions])
expect_any_instance_of(Client).not_to receive(:register!)
expect_any_instance_of(Status).to receive(:print_extensions_list)
cli.execute!
end
end
context 'on system with no registered base product' do
it '--list-extensions exits with an error explaining that a base product has to be registered first' do
allow_any_instance_of(Status).to receive(:activated_base_product?).and_return(false)
cli = described_class.new(%w[--list-extensions])
expect_any_instance_of(Client).not_to receive(:register!)
expect_any_instance_of(Status).not_to receive(:print_extensions_list)
expect(string_logger).to receive(:error)
.with(/To list extensions, you must first register the base product, using: SUSEConnect -r <registration code>/)
expect_any_instance_of(described_class).to receive(:exit)
cli.execute!
end
end
end
context 'rollback subcommand' do
it '--rollback calls SUSE::Connect::Migration.rollback' do
cli = described_class.new(%w[--rollback])
expect_any_instance_of(Client).not_to receive(:register!)
expect(Migration).to receive(:rollback)
expect { cli.execute! }.not_to exit_with_code(1)
end
end
describe 'config write' do
it 'writes config if appropriate cli param been passed' do
cli = described_class.new(%w[--write-config --status])
expect_any_instance_of(SUSE::Connect::Config).to receive(:write!)
allow_any_instance_of(Status).to receive(:print_product_statuses)
cli.execute!
end
end
end
describe '?extract_options' do
it 'sets token options' do
argv = %w[-r matoken]
cli = described_class.new(argv)
expect(cli.options[:token]).to eq 'matoken'
end
it 'sets product options' do
argv = %w[--product sles/12/i386]
cli = described_class.new(argv)
expect(cli.options[:product]).to eq Remote::Product.new(identifier: 'sles', version: '12', arch: 'i386')
end
it 'sets token options' do
argv = %w[--regcode matoken]
cli = described_class.new(argv)
expect(cli.options[:token]).to eq 'matoken'
end
it 'sets email options' do
argv = %w[--email me@hotmail.com]
cli = described_class.new(argv)
expect(cli.options[:email]).to eq 'me@hotmail.com'
end
it 'sets url options' do
argv = %w[--url test]
cli = described_class.new(argv)
expect(cli.options[:url]).to eq 'test'
end
it 'puts version on version flag' do
argv = %w[--version]
expect_any_instance_of(described_class).to receive(:puts).with(VERSION)
expect { described_class.new(argv) }.to exit_with_code(0)
end
it 'outputs help on help flag with no line longer than 80 characters' do
argv = %w[--help]
expect_any_instance_of(described_class).to receive(:puts) do |option_parser|
expect(option_parser.instance_variable_get(:@opts).to_s.split("\n").map(&:length)).to all be <= 80
end
expect { described_class.new(argv) }.to exit_with_code(0)
end
it 'sets verbose options' do
argv = %w[--debug]
cli = described_class.new(argv)
expect(cli.options[:debug]).to be true
end
it 'sets deregister option' do
argv = %w[--de-register]
cli = described_class.new(argv)
expect(cli.options[:deregister]).to be true
end
it 'sets root option' do
argv = %w[--root /path/to/root]
described_class.new(argv)
expect(SUSE::Connect::System.filesystem_root).to eq '/path/to/root'
SUSE::Connect::System.filesystem_root = nil
end
it 'requests status sub-command' do
argv = %w[--status]
expect(described_class.new(argv).options[:status]).to be true
end
it 'sets write_config option' do
argv = %w[--write-config]
cli = described_class.new(argv)
expect(cli.options[:write_config]).to be true
end
end
describe 'errors on invalid options format' do
it 'error on invalid product options format with hint where to find correct product identifiers' do
expect(string_logger).to receive(:error).with(/Please provide the product identifier in this format.*SUSEConnect --list-extensions/)
argv = %w[--product sles]
expect { described_class.new(argv) }.to exit_with_code(1)
end
end
describe '?check_if_param' do
it 'will exit with message if opt is nil' do
expect_any_instance_of(described_class).to receive(:exit)
expect(string_logger).to receive(:error).with('Kaboom')
cli.send(:check_if_param, nil, 'Kaboom')
end
end
describe 'reads environment variables' do
before { ENV['LANG'] = 'de' }
it 'sets language header based on LANG' do
expect(cli.options[:language]).to eq 'de'
end
end
end