Project

General

Profile

Download (20.5 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / test / resources / content_hosts_test.rb @ ebb00443

1
require './test/csv_test_helper'
2
require './lib/hammer_cli_csv'
3

    
4
# rubocop:disable LineLength
5
module Resources
6
  class TestContentHosts < MiniTest::Unit::TestCase
7
    def test_usage
8
      start_vcr
9
      set_user 'admin'
10

    
11
      stdout,stderr = capture {
12
        hammer.run(%W{--reload-cache csv content-hosts --help})
13
      }
14
      assert_equal '', stderr
15
      assert_equal stdout, <<-HELP
16
Usage:
17
     csv content-hosts [OPTIONS]
18

19
Options:
20
 --clear-subscriptions         When processing --itemized-subscriptions, clear existing subscriptions first
21
 --columns COLUMN_NAMES        Comma separated list of column names to export
22
 --continue-on-error           Continue processing even if individual resource error
23
 --export                      Export current data instead of importing
24
 --file FILE_NAME              CSV file (default to /dev/stdout with --export, otherwise required)
25
 --itemized-subscriptions      Export one subscription per row, only process update subscriptions on import
26
 --organization ORGANIZATION   Only process organization matching this name
27
 --search SEARCH               Only export search results
28
 -h, --help                    print help
29
 -v, --verbose                 be verbose
30

31
Columns:
32
 Name - Name of resource
33
 Search - Search for matching names during import (overrides 'Name' column)
34
 Organization - Organization name
35
 Environment - Lifecycle environment name
36
 Content View - Content view name
37
 Host Collections - Comma separated list of host collection names
38
 Virtual - Is a virtual host, Yes or No
39
 Guest of Host - Hypervisor host name for virtual hosts
40
 OS - Operating system
41
 Arch - Architecture
42
 Sockets - Number of sockets
43
 RAM - Quantity of RAM in bytes
44
 Cores - Number of cores
45
 SLA - Service Level Agreement value
46
 Products - Comma separated list of products, each of the format \"<sku>|<name>\"
47
 Subscriptions - Comma separated list of subscriptions, each of the format "<quantity>|<sku>|<name>|<contract>|<account>"
48
 Subscription Name - Subscription name (only applicable for --itemized-subscriptions)
49
 Subscription Type - Subscription type (only applicable for --itemized-subscriptions)
50
 Subscription Quantity - Subscription quantity (only applicable for --itemized-subscriptions)
51
 Subscription SKU - Subscription SKU (only applicable for --itemized-subscriptions)
52
 Subscription Contract - Subscription contract number (only applicable for --itemized-subscriptions)
53
 Subscription Account - Subscription account number (only applicable for --itemized-subscriptions)
54
 Subscription Start - Subscription start date (only applicable for --itemized-subscriptions)
55
 Subscription End - Subscription end date (only applicable for --itemized-subscriptions)
56
HELP
57
      stop_vcr
58
    end
59

    
60
    def test_export_with_clear_subscriptions
61
      start_vcr
62
      set_user 'admin'
63

    
64
      stdout,stderr = capture {
65
        hammer.run(%W{--reload-cache csv content-hosts --export --clear-subscriptions})
66
      }
67
      assert_equal "Error: --clear-subscriptions option only relevant during import\n", stderr
68
      assert_equal stdout, ''
69

    
70
      stop_vcr
71
    end
72

    
73
    def test_export_with_columns
74
      start_vcr
75
      set_user 'admin'
76

    
77
      stdout,stderr = capture {
78
        hammer.run(%W{--reload-cache csv content-hosts --columns Abc,Def})
79
      }
80
      assert_equal "Error: --columns option only relevant with --export\n", stderr
81
      assert_equal stdout, ''
82

    
83
      stop_vcr
84
    end
85

    
86
    def test_create_and_update
87
      start_vcr
88
      set_user 'admin'
89

    
90
      hostname = "testhost1"
91

    
92
      file = Tempfile.new('content_hosts_test')
93
      file.write("Name,Count,Organization,Environment,Content View,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
94
      file.write("#{hostname},1,Test Corporation,Library,Default Organization View,No,,RHEL 6.4,x86_64,1,4,1,,,\n")
95
      file.rewind
96

    
97
      stdout,stderr = capture {
98
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
99
      }
100
      assert_equal '', stderr
101
      assert_equal stdout[0..-2], "Creating content host '#{hostname}'...done"
102

    
103
      file.rewind
104

    
105
      stdout,stderr = capture {
106
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
107
      }
108
      assert_equal '', stderr
109
      assert_equal stdout[0..-2], "Updating content host '#{hostname}'...done"
110
      file.unlink
111

    
112
      stdout,stderr = capture {
113
        hammer.run(%W(--reload-cache host list --search name=#{hostname}))
114
      }
115
      assert_equal '', stderr
116
      assert_equal stdout.split("\n").length, 5
117
      host_delete(hostname)
118

    
119
      stop_vcr
120
    end
121

    
122
    def test_export
123
      start_vcr
124
      set_user 'admin'
125

    
126
      stdout,stderr = capture {
127
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation})
128
      }
129
      assert_equal '', stderr
130
      assert_equal stdout.split("\n")[0], "Name,Organization,Environment,Content View,Host Collections,Virtual,Guest of Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions"
131
      stop_vcr
132
    end
133

    
134
    def test_export_subscriptions
135
      start_vcr
136
      set_user 'admin'
137

    
138
      stdout,stderr = capture {
139
        hammer.run(%W{--reload-cache csv content-hosts --export --itemized-subscriptions --organization Test\ Corporation})
140
      }
141
      assert_equal '', stderr
142

    
143
      assert_equal stdout.split("\n")[0], "Name,Organization,Environment,Content View,Host Collections,Virtual,Guest of Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscription Name,Subscription Type,Subscription Quantity,Subscription SKU,Subscription Contract,Subscription Account,Subscription Start,Subscription End,Subscription Guest"
144
      stop_vcr
145
    end
146

    
147
    # import a single line, testing that subscription is added
148
    def test_import_single_line
149
      start_vcr
150
      set_user 'admin'
151

    
152
      hostname = 'testhypervisor1'
153

    
154
      file = Tempfile.new('content_hosts_test')
155
      file.write("Name,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
156
      file.write("#{hostname},Test Corporation,Library,Default Organization View,\"\",Yes,,RHEL 7.2,x86_64,2,3882752,1,\"\",\"69|Red Hat Enterprise Linux Server,290|Red Hat OpenShift Enterprise\",\"\"\"1|RH00001|Red Hat Enterprise Linux for Virtual Datacenters, Premium\"\"\"\n")
157
      file.rewind
158

    
159
      stdout,stderr = capture {
160
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
161
      }
162
      assert_equal '', stderr
163
      assert_equal "Creating content host '#{hostname}'...done\n", stdout
164

    
165
      stdout,stderr = capture {
166
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation})
167
      }
168
      assert_equal '', stderr
169
      lines = stdout.split("\n")
170
      lines.select! { |line| line.match(/testhypervisor1.*/) }
171
      assert_equal 1, lines.length
172
      assert_match(/.*Test Corporation,Library,Default Organization View,"",Yes,,RHEL 7.2,x86_64,2,3882752,1.*/, lines[0])
173
      assert_match(/.*1|RH00001|Red Hat Enterprise Linux for Virtual Datacenters, Premium.*/, lines[0])
174
      host_delete(hostname)
175

    
176
      stop_vcr
177
    end
178

    
179
    def test_import_search
180
      start_vcr
181
      set_user 'admin'
182

    
183
      file = Tempfile.new('content_hosts_test')
184
      file.write("Name,Count,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
185
      file.write("testaaa%d,2,Test Corporation,Library,Default Organization View,,No,,RHEL 6.4,x86_64,2,4 GB,4,Standard,\"69|Red Hat Enterprise Linux Server\",\n")
186
      file.write("testbbb%d,3,Test Corporation,Library,Default Organization View,,No,,RHEL 6.4,x86_64,4,16 GB,8,Premium,\"69|Red Hat Enterprise Linux Server\",\n")
187
      file.rewind
188

    
189
      stdout,stderr = capture {
190
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
191
      }
192
      assert_equal '', stderr
193

    
194
      file = Tempfile.new('content_hosts_test')
195
      file.write("Search,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
196
      file.write("name ~ testaaa,Test Corporation,Library,Default Organization View,,No,,RHEL 6.4,x86_64,2,4 GB,4,Standard,\"69|Red Hat Enterprise Linux Server\",\"\"\"2|RH00004|Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)|10999113|5700573\"\"\"\n")
197
      file.rewind
198

    
199
      stdout,stderr = capture {
200
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
201
      }
202
      assert_equal '', stderr
203
      assert_equal "Updating content host 'testaaa0'...clearing existing subscriptions...done\nUpdating content host 'testaaa1'...clearing existing subscriptions...done\n", stdout
204

    
205

    
206
      %w{testaaa0 testaaa1 testbbb0 testbbb1 testbbb2}.each do |hostname|
207
        host_delete(hostname)
208
      end
209

    
210
      stop_vcr
211
    end
212

    
213
    # import a single line, then import again w/ clearing
214
    def test_import_single_line_clear_subs
215
      start_vcr
216
      set_user 'admin'
217

    
218
      hostname = 'tester1'
219

    
220
      file = Tempfile.new('content_hosts_test')
221
      file.write("Name,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
222
      file.write("#{hostname},Test Corporation,Library,Default Organization View,\"\",Yes,,RHEL 7.2,x86_64,2,3882752,1,\"\",\"69|Red Hat Enterprise Linux Server,290|Red Hat OpenShift Enterprise\",\"\"\"1|RH00001|Red Hat Enterprise Linux for Virtual Datacenters, Premium\"\"\"\n")
223
      file.rewind
224

    
225
      stdout,stderr = capture {
226
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
227
      }
228
      assert_equal '', stderr
229
      assert_equal "Creating content host '#{hostname}'...done\n", stdout
230

    
231
      file = Tempfile.new('content_hosts_test')
232
      file.write("Name,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
233
      file.write("#{hostname},Test Corporation,Library,Default Organization View,\"\",Yes,,RHEL 7.2,x86_64,2,3882752,1,\"\",\"69|Red Hat Enterprise Linux Server,290|Red Hat OpenShift Enterprise\",\"\"\"|RH00004|Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)\"\"\"\n")
234
      file.rewind
235

    
236
      stdout,stderr = capture {
237
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path} --clear-subscriptions})
238
      }
239
      assert_equal '', stderr
240
      assert_equal "Updating content host '#{hostname}'...clearing existing subscriptions...done\n", stdout
241

    
242
      stdout,stderr = capture {
243
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation --itemized-subscriptions --search name=#{hostname}})
244
      }
245
      assert_equal '', stderr
246
      lines = stdout.split("\n")
247
      lines.select! { |line| line.match(/tester1.*/) }
248
      assert_equal 1, lines.length
249
      assert_match(/.*Test Corporation,Library,Default Organization View,"",Yes,,RHEL 7.2,x86_64,2,3882752,1.*/, lines[0])
250
      assert_match(/.*Red Hat Enterprise Linux Server, Standard.*/, lines[0])
251
      host_delete(hostname)
252

    
253
      stop_vcr
254
    end
255

    
256
    def test_columns_config
257
      start_vcr
258
      set_user 'admin'
259

    
260
      config_modify({
261
                     :"content-hosts" => {
262
                       :define => [{
263
                                     :name => "Subscription Status",
264
                                     :json => %w(subscription_status_label)
265
                                   },
266
                                   {
267
                                     :name => "Last Checkin",
268
                                     :json => %w(subscription_facet_attributes last_checkin)
269
                                   }],
270
                       :export => [
271
                         "Name",
272
                         "Organization",
273
                         "Subscription Status"
274
                       ]
275
                     }})
276
      stdout,stderr = capture {
277
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation})
278
      }
279
      assert_equal '', stderr
280
      lines = stdout.split("\n")
281
      assert_equal "Name,Organization,Subscription Status", lines[0]
282
      lines.select! { |line| line.match(/testphysical.*/) }
283
      assert_equal 1, lines.length
284
      assert_equal "testphysical,Test Corporation,Fully entitled", lines[0]
285

    
286
      stop_vcr
287
    ensure
288
      config_restore
289
    end
290

    
291
    def test_columns_config_options
292
      start_vcr
293
      set_user 'admin'
294

    
295
      config_modify({
296
                     :"content-hosts" => {
297
                       :define => [{
298
                                     :name => "Subscription Status",
299
                                     :json => %w(subscription_status_label)
300
                                   },
301
                                   {
302
                                     :name => "Last Checkin",
303
                                     :json => %w(subscription_facet_attributes last_checkin)
304
                                   }],
305
                       :export => [
306
                         "Name",
307
                         "Organization",
308
                         "Subscription Status"
309
                       ]
310
                     }})
311

    
312
      stdout,stderr = capture {
313
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation})
314
      }
315
      assert_equal '', stderr
316
      lines = stdout.split("\n")
317
      assert_equal "Name,Organization,Subscription Status", lines[0]
318
      lines.select! { |line| line.match(/testphysical.*/) }
319
      assert_equal 1, lines.length
320
      assert_equal "testphysical,Test Corporation,Fully entitled", lines[0]
321

    
322
      stdout,stderr = capture {
323
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation --columns Name,Organization,Environment,Subscription\ Status})
324
      }
325
      assert_equal '', stderr
326
      lines = stdout.split("\n")
327
      assert_equal "Name,Organization,Environment,Subscription Status", lines[0]
328
      lines.select! { |line| line.match(/testphysical.*/) }
329
      assert_equal 1, lines.length
330
      assert_equal "testphysical,Test Corporation,Library,Fully entitled", lines[0]
331

    
332
      stop_vcr
333
    ensure
334
      config_restore
335
    end
336

    
337
    def test_columns_options
338
      start_vcr
339
      set_user 'admin'
340

    
341
      file = Tempfile.new('content_hosts_test')
342
      file.write("Name,Count,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
343
      file.write("testcolopts%d,2,Test Corporation,Library,Default Organization View,,No,,RHEL 6.4,x86_64,2,4 GB,4,Standard,\"69|Red Hat Enterprise Linux Server\",\n")
344
      file.rewind
345

    
346
      stdout,stderr = capture {
347
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
348
      }
349
      assert_equal '', stderr
350

    
351
      stdout,stderr = capture {
352
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation --columns Name,Organization,Subscription\ Status,Environment})
353
      }
354
      assert_equal "Warning: Column 'Subscription Status' does not match any field, be sure to check spelling. A full list of supported columns are available with 'hammer csv content-hosts --help'\n", stderr
355
      lines = stdout.split("\n")
356
      assert_equal "Name,Organization,Subscription Status,Environment", lines[0]
357
      fields = lines[1].split(",")
358
      assert_equal 4, fields.length
359
      assert_equal "", fields[2]  # Subscription Status not defined in this test so blank
360
      assert_equal "Library", fields[3]
361

    
362
      %w{testcolopts0 testcolopts1}.each do |hostname|
363
        host_delete(hostname)
364
      end
365

    
366
      stop_vcr
367
    end
368

    
369
    def test_itemized_columns_options
370
      start_vcr
371
      set_user 'admin'
372

    
373
      stdout,stderr = capture {
374
        hammer.run(%W{--reload-cache csv content-hosts --itemized-subscriptions --export --organization Test\ Corporation --columns Name,Subscription\ Status,Subscription\ Name,Subscription\ Quantity,Subscription\ SKU})
375
      }
376
      assert_equal "Warning: Column 'Subscription Status' does not match any field, be sure to check spelling. A full list of supported columns are available with 'hammer csv content-hosts --help'\n", stderr
377
      lines = stdout.split("\n")
378
      assert_equal "Name,Subscription Status,Subscription Name,Subscription Quantity,Subscription SKU", lines[0]
379
      lines.select! { |line| line.match(/testphysical.*/) }
380
      assert_equal 1, lines.length
381
      assert_equal 'testphysical,,"Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)",1,RH00004', lines[0]
382

    
383
      stop_vcr
384
    end
385

    
386
    def test_itemized_columns_config_options
387
      start_vcr
388
      set_user 'admin'
389
      config_modify({
390
                     :"content-hosts" => {
391
                       :define => [{
392
                                     :name => "Subscription Status",
393
                                     :json => %w(subscription_status_label)
394
                                   },
395
                                   {
396
                                     :name => "Last Checkin",
397
                                     :json => %w(subscription_facet_attributes last_checkin)
398
                                   }],
399
                       :export => [
400
                         "Name",
401
                         "Subscription Status",
402
                         "Subscription Name",
403
                         "Subscription Quantity"
404
                       ]
405
                     }})
406

    
407
      stdout,stderr = capture {
408
        hammer.run(%W{--reload-cache csv content-hosts --itemized-subscriptions --export --organization Test\ Corporation})
409
      }
410
      assert_equal '', stderr
411
      lines = stdout.split("\n")
412
      assert_equal "Name,Subscription Status,Subscription Name,Subscription Quantity", lines[0]
413
      lines.select! { |line| line.match(/testphysical.*/) }
414
      assert_equal 1, lines.length
415
      assert_equal "testphysical,Fully entitled,\"Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)\",1", lines[0]
416

    
417
      stdout,stderr = capture {
418
        hammer.run(%W{--reload-cache csv content-hosts --itemized-subscriptions --export --organization Test\ Corporation --columns Name,Subscription\ Status,Subscription\ Name,Subscription\ Quantity,Subscription\ SKU})
419
      }
420
      assert_equal '', stderr
421
      lines = stdout.split("\n")
422
      assert_equal "Name,Subscription Status,Subscription Name,Subscription Quantity,Subscription SKU", lines[0]
423
      lines.select! { |line| line.match(/testphysical.*/) }
424
      assert_equal 1, lines.length
425
      assert_equal "testphysical,Fully entitled,\"Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)\",1,RH00004", lines[0]
426

    
427
      stop_vcr
428
    ensure
429
      config_restore
430
    end
431

    
432
    def config_modify(columns)
433
      config = HammerCLI::Settings.dump
434
      config[:csv][:columns] = columns
435
      # HammerCLI::Settings.clear
436
      # config_file = Tempfile.new('content_hosts_test')
437
      # FileUtils.cp(File.dirname(__FILE__) + "/../config.yml", config_file.path)
438
      # config_file.seek(0, IO::SEEK_END)
439
      # config_file.write settings
440
      # config_file.rewind
441
      # HammerCLI::Settings.load_from_file config_file.path
442
    end
443

    
444
    def config_restore
445
      config = HammerCLI::Settings.dump
446
      config[:csv].delete(:columns)
447
      # HammerCLI::Settings.load_from_file(File.dirname(__FILE__) + "/../config.yml")
448
    end
449

    
450
    def test_hypervisor_prefix
451
      start_vcr
452
      set_user 'admin'
453

    
454
      prefix = "abc"
455
      hypervisorname = "prefixhv"
456
      guestname = "prefixguest"
457

    
458
      file = Tempfile.new('content_hosts_test')
459
      file.write <<-EOF
460
Name,Count,Organization,Environment,Content View,Virtual,Guest of Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions
461
#{hypervisorname},1,Test Corporation,Library,Default Organization View,No,,RHEL 6.4,x86_64,1,4,1,,,
462
#{guestname},1,Test Corporation,Library,Default Organization View,Yes,#{hypervisorname},RHEL 6.4,x86_64,1,4,1,,,
463
EOF
464
      file.rewind
465

    
466
      stdout,stderr = capture {
467
        hammer.run(%W{--reload-cache csv content-hosts --verbose --prefix #{prefix} --file #{file.path}})
468
      }
469
      assert_equal '', stderr
470
      lines = stdout.split("\n")
471
      assert_equal lines[0], "Creating content host '#{prefix}#{hypervisorname}'...done"
472
      assert_equal lines[1], "Creating content host '#{prefix}#{guestname}'...done"
473

    
474
      stdout,stderr = capture {
475
        hammer.run(%W(--reload-cache csv content-hosts --export --organization Test\ Corporation --search name~#{prefix}))
476
      }
477
      assert_equal '', stderr
478
      lines = stdout.split("\n")
479
      assert_match(/#{prefix}#{hypervisorname}.*/, lines[1])
480
      assert_match(/#{prefix}#{guestname}.*#{prefix}#{hypervisorname}.*/, lines[2])
481
      host_delete("#{prefix}#{hypervisorname}")
482
      host_delete("#{prefix}#{guestname}")
483

    
484
      stop_vcr
485
    end
486
  end
487
end
488
# rubocop:enable LineLength