Project

General

Profile

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

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

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

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

    
59
    def test_export_with_columns
60
      start_vcr
61
      set_user 'admin'
62

    
63
      stdout,stderr = capture {
64
        hammer.run(%W{--reload-cache csv content-hosts --columns Abc,Def})
65
      }
66
      assert_equal "Error: --columns option only relevant with --export\n", stderr
67
      assert_equal stdout, ''
68

    
69
      stop_vcr
70
    end
71

    
72
    def test_create_and_update
73
      start_vcr
74
      set_user 'admin'
75

    
76
      hostname = "testhost1"
77

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

    
83
      stdout,stderr = capture {
84
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
85
      }
86
      assert_equal '', stderr
87
      assert_equal stdout[0..-2], "Creating content host '#{hostname}'...done"
88

    
89
      file.rewind
90

    
91
      stdout,stderr = capture {
92
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
93
      }
94
      assert_equal '', stderr
95
      assert_equal stdout[0..-2], "Updating content host '#{hostname}'...done"
96
      file.unlink
97

    
98
      stdout,stderr = capture {
99
        hammer.run(%W(--reload-cache host list --search name=#{hostname}))
100
      }
101
      assert_equal '', stderr
102
      assert_equal stdout.split("\n").length, 5
103
      host_delete(hostname)
104

    
105
      stop_vcr
106
    end
107

    
108
    def test_export
109
      start_vcr
110
      set_user 'admin'
111

    
112
      stdout,stderr = capture {
113
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation})
114
      }
115
      assert_equal '', stderr
116
      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"
117
      stop_vcr
118
    end
119

    
120
    def test_export_subscriptions
121
      start_vcr
122
      set_user 'admin'
123

    
124
      stdout,stderr = capture {
125
        hammer.run(%W{--reload-cache csv content-hosts --export --itemized-subscriptions --organization Test\ Corporation})
126
      }
127
      assert_equal '', stderr
128

    
129
      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"
130
      stop_vcr
131
    end
132

    
133
    # import a single line, testing that subscription is added
134
    def test_import_single_line
135
      start_vcr
136
      set_user 'admin'
137

    
138
      hostname = 'testhypervisor1'
139

    
140
      file = Tempfile.new('content_hosts_test')
141
      file.write("Name,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
142
      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")
143
      file.rewind
144

    
145
      stdout,stderr = capture {
146
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
147
      }
148
      assert_equal '', stderr
149
      assert_equal "Creating content host '#{hostname}'...done\n", stdout
150

    
151
      stdout,stderr = capture {
152
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation})
153
      }
154
      assert_equal '', stderr
155
      lines = stdout.split("\n")
156
      lines.select! { |line| line.match(/testhypervisor1.*/) }
157
      assert_equal 1, lines.length
158
      assert_match(/.*Test Corporation,Library,Default Organization View,"",Yes,,RHEL 7.2,x86_64,2,3882752,1.*/, lines[0])
159
      assert_match(/.*1|RH00001|Red Hat Enterprise Linux for Virtual Datacenters, Premium.*/, lines[0])
160
      host_delete(hostname)
161

    
162
      stop_vcr
163
    end
164

    
165
    def test_import_search
166
      start_vcr
167
      set_user 'admin'
168

    
169
      file = Tempfile.new('content_hosts_test')
170
      file.write("Name,Count,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
171
      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")
172
      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")
173
      file.rewind
174

    
175
      stdout,stderr = capture {
176
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
177
      }
178
      assert_equal '', stderr
179

    
180
      file = Tempfile.new('content_hosts_test')
181
      file.write("Search,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
182
      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")
183
      file.rewind
184

    
185
      stdout,stderr = capture {
186
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
187
      }
188
      assert_equal '', stderr
189
      assert_equal "Updating content host 'testaaa0'...done\nUpdating content host 'testaaa1'...done\n", stdout
190

    
191

    
192
      %w{testaaa0 testaaa1 testbbb0 testbbb1 testbbb2}.each do |hostname|
193
        host_delete(hostname)
194
      end
195

    
196
      stop_vcr
197
    end
198

    
199
    def test_columns_config
200
      start_vcr
201
      set_user 'admin'
202

    
203
      config_modify({
204
                     :"content-hosts" => {
205
                       :define => [{
206
                                     :name => "Subscription Status",
207
                                     :json => %w(subscription_status_label)
208
                                   },
209
                                   {
210
                                     :name => "Last Checkin",
211
                                     :json => %w(subscription_facet_attributes last_checkin)
212
                                   }],
213
                       :export => [
214
                         "Name",
215
                         "Organization",
216
                         "Subscription Status"
217
                       ]
218
                     }})
219
      stdout,stderr = capture {
220
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation})
221
      }
222
      assert_equal '', stderr
223
      lines = stdout.split("\n")
224
      assert_equal "Name,Organization,Subscription Status", lines[0]
225
      lines.select! { |line| line.match(/testphysical.*/) }
226
      assert_equal 1, lines.length
227
      assert_equal "testphysical,Test Corporation,Fully entitled", lines[0]
228

    
229
      stop_vcr
230
    ensure
231
      config_restore
232
    end
233

    
234
    def test_columns_config_options
235
      start_vcr
236
      set_user 'admin'
237

    
238
      config_modify({
239
                     :"content-hosts" => {
240
                       :define => [{
241
                                     :name => "Subscription Status",
242
                                     :json => %w(subscription_status_label)
243
                                   },
244
                                   {
245
                                     :name => "Last Checkin",
246
                                     :json => %w(subscription_facet_attributes last_checkin)
247
                                   }],
248
                       :export => [
249
                         "Name",
250
                         "Organization",
251
                         "Subscription Status"
252
                       ]
253
                     }})
254

    
255
      stdout,stderr = capture {
256
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation})
257
      }
258
      assert_equal '', stderr
259
      lines = stdout.split("\n")
260
      assert_equal "Name,Organization,Subscription Status", lines[0]
261
      lines.select! { |line| line.match(/testphysical.*/) }
262
      assert_equal 1, lines.length
263
      assert_equal "testphysical,Test Corporation,Fully entitled", lines[0]
264

    
265
      stdout,stderr = capture {
266
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation --columns Name,Organization,Environment,Subscription\ Status})
267
      }
268
      assert_equal '', stderr
269
      lines = stdout.split("\n")
270
      assert_equal "Name,Organization,Environment,Subscription Status", lines[0]
271
      lines.select! { |line| line.match(/testphysical.*/) }
272
      assert_equal 1, lines.length
273
      assert_equal "testphysical,Test Corporation,Library,Fully entitled", lines[0]
274

    
275
      stop_vcr
276
    ensure
277
      config_restore
278
    end
279

    
280
    def test_columns_options
281
      start_vcr
282
      set_user 'admin'
283

    
284
      file = Tempfile.new('content_hosts_test')
285
      file.write("Name,Count,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
286
      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")
287
      file.rewind
288

    
289
      stdout,stderr = capture {
290
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
291
      }
292
      assert_equal '', stderr
293

    
294
      stdout,stderr = capture {
295
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation --columns Name,Organization,Subscription\ Status,Environment})
296
      }
297
      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
298
      lines = stdout.split("\n")
299
      assert_equal "Name,Organization,Subscription Status,Environment", lines[0]
300
      fields = lines[1].split(",")
301
      assert_equal 4, fields.length
302
      assert_equal "", fields[2]  # Subscription Status not defined in this test so blank
303
      assert_equal "Library", fields[3]
304

    
305
      %w{testcolopts0 testcolopts1}.each do |hostname|
306
        host_delete(hostname)
307
      end
308

    
309
      stop_vcr
310
    end
311

    
312
    def test_itemized_columns_options
313
      start_vcr
314
      set_user 'admin'
315

    
316
      stdout,stderr = capture {
317
        hammer.run(%W{--reload-cache csv content-hosts --itemized-subscriptions --export --organization Test\ Corporation --columns Name,Subscription\ Status,Subscription\ Name,Subscription\ Quantity,Subscription\ SKU})
318
      }
319
      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
320
      lines = stdout.split("\n")
321
      assert_equal "Name,Subscription Status,Subscription Name,Subscription Quantity,Subscription SKU", lines[0]
322
      lines.select! { |line| line.match(/testphysical.*/) }
323
      assert_equal 1, lines.length
324
      assert_equal 'testphysical,,"Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)",1,RH00004', lines[0]
325

    
326
      stop_vcr
327
    end
328

    
329
    def test_itemized_columns_config_options
330
      start_vcr
331
      set_user 'admin'
332
      config_modify({
333
                     :"content-hosts" => {
334
                       :define => [{
335
                                     :name => "Subscription Status",
336
                                     :json => %w(subscription_status_label)
337
                                   },
338
                                   {
339
                                     :name => "Last Checkin",
340
                                     :json => %w(subscription_facet_attributes last_checkin)
341
                                   }],
342
                       :export => [
343
                         "Name",
344
                         "Subscription Status",
345
                         "Subscription Name",
346
                         "Subscription Quantity"
347
                       ]
348
                     }})
349

    
350
      stdout,stderr = capture {
351
        hammer.run(%W{--reload-cache csv content-hosts --itemized-subscriptions --export --organization Test\ Corporation})
352
      }
353
      assert_equal '', stderr
354
      lines = stdout.split("\n")
355
      assert_equal "Name,Subscription Status,Subscription Name,Subscription Quantity", lines[0]
356
      lines.select! { |line| line.match(/testphysical.*/) }
357
      assert_equal 1, lines.length
358
      assert_equal "testphysical,Fully entitled,\"Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)\",1", lines[0]
359

    
360
      stdout,stderr = capture {
361
        hammer.run(%W{--reload-cache csv content-hosts --itemized-subscriptions --export --organization Test\ Corporation --columns Name,Subscription\ Status,Subscription\ Name,Subscription\ Quantity,Subscription\ SKU})
362
      }
363
      assert_equal '', stderr
364
      lines = stdout.split("\n")
365
      assert_equal "Name,Subscription Status,Subscription Name,Subscription Quantity,Subscription SKU", lines[0]
366
      lines.select! { |line| line.match(/testphysical.*/) }
367
      assert_equal 1, lines.length
368
      assert_equal "testphysical,Fully entitled,\"Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)\",1,RH00004", lines[0]
369

    
370
      stop_vcr
371
    ensure
372
      config_restore
373
    end
374

    
375
    def config_modify(columns)
376
      config = HammerCLI::Settings.dump
377
      config[:csv][:columns] = columns
378
      # HammerCLI::Settings.clear
379
      # config_file = Tempfile.new('content_hosts_test')
380
      # FileUtils.cp(File.dirname(__FILE__) + "/../config.yml", config_file.path)
381
      # config_file.seek(0, IO::SEEK_END)
382
      # config_file.write settings
383
      # config_file.rewind
384
      # HammerCLI::Settings.load_from_file config_file.path
385
    end
386

    
387
    def config_restore
388
      config = HammerCLI::Settings.dump
389
      config[:csv].delete(:columns)
390
      # HammerCLI::Settings.load_from_file(File.dirname(__FILE__) + "/../config.yml")
391
    end
392

    
393
    def test_hypervisor_prefix
394
      start_vcr
395
      set_user 'admin'
396

    
397
      prefix = "abc"
398
      hypervisorname = "prefixhv"
399
      guestname = "prefixguest"
400

    
401
      file = Tempfile.new('content_hosts_test')
402
      file.write <<-EOF
403
Name,Count,Organization,Environment,Content View,Virtual,Guest of Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions
404
#{hypervisorname},1,Test Corporation,Library,Default Organization View,No,,RHEL 6.4,x86_64,1,4,1,,,
405
#{guestname},1,Test Corporation,Library,Default Organization View,Yes,#{hypervisorname},RHEL 6.4,x86_64,1,4,1,,,
406
EOF
407
      file.rewind
408

    
409
      stdout,stderr = capture {
410
        hammer.run(%W{--reload-cache csv content-hosts --verbose --prefix #{prefix} --file #{file.path}})
411
      }
412
      assert_equal '', stderr
413
      lines = stdout.split("\n")
414
      assert_equal lines[0], "Creating content host '#{prefix}#{hypervisorname}'...done"
415
      assert_equal lines[1], "Creating content host '#{prefix}#{guestname}'...done"
416

    
417
      stdout,stderr = capture {
418
        hammer.run(%W(--reload-cache csv content-hosts --export --organization Test\ Corporation --search name~#{prefix}))
419
      }
420
      assert_equal '', stderr
421
      lines = stdout.split("\n")
422
      assert_match(/#{prefix}#{hypervisorname}.*/, lines[1])
423
      assert_match(/#{prefix}#{guestname}.*#{prefix}#{hypervisorname}.*/, lines[2])
424
      host_delete("#{prefix}#{hypervisorname}")
425
      host_delete("#{prefix}#{guestname}")
426

    
427
      stop_vcr
428
    end
429
  end
430
end
431
# rubocop:enable LineLength