Project

General

Profile

Revision 7b00acc6

Added by Thomas McKay almost 9 years ago

fixes #4926 - systems test and rubocop cleanup

View differences:

lib/hammer_cli_csv/systems.rb
33 33

  
34 34
module HammerCLICsv
35 35
  class SystemsCommand < BaseCommand
36

  
37 36
    ORGANIZATION = 'Organization'
38 37
    ENVIRONMENT = 'Environment'
39 38
    CONTENTVIEW = 'Content View'
......
52 51
    def export
53 52
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
54 53
        csv << [NAME, COUNT, ORGANIZATION, ENVIRONMENT, CONTENTVIEW, SYSTEMGROUPS, VIRTUAL, HOST,
55
               OPERATINGSYSTEM, ARCHITECTURE, SOCKETS, RAM, CORES, SLA, PRODUCTS, SUBSCRIPTIONS]
54
                OPERATINGSYSTEM, ARCHITECTURE, SOCKETS, RAM, CORES, SLA, PRODUCTS, SUBSCRIPTIONS]
56 55
        @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
57 56
          @api.resource(:systems).call(:index, {
58 57
                                'per_page' => 999999,
......
72 71
              column << system['systemGroups'].collect do |systemgroup|
73 72
                systemgroup['name']
74 73
              end
75
            end.delete!("\n")
74
            end
75
            systemgroups.delete!("\n")
76 76
            virtual = system['facts']['virt.is_guest'] == 'true' ? 'Yes' : 'No'
77 77
            host = system['host']
78 78
            operatingsystem = "#{system['facts']['distribution.name']} " if system['facts']['distribution.name']
......
81 81
            sockets = system['facts']['cpu.cpu_socket(s)']
82 82
            ram = system['facts']['memory.memtotal']
83 83
            cores = system['facts']['cpu.core(s)_per_socket']
84
            sla = ""
84
            sla = ''
85 85
            products = CSV.generate do |column|
86 86
              column << system['installedProducts'].collect do |product|
87 87
                "#{product['productId']}|#{product['productName']}"
88 88
              end
89
            end.delete!("\n")
89
            end
90
            products.delete!("\n")
90 91
            subscriptions = CSV.generate do |column|
91 92
              column << @api.resource(:subscriptions).call(:index, {
92 93
                                                    'system_id' => system['uuid']
93 94
                                                  })['results'].collect do |subscription|
94 95
                "#{subscription['product_id']}|#{subscription['product_name']}"
95 96
              end
96
            end.delete!("\n")
97
            end
98
            subscriptions.delete!("\n")
97 99
            csv << [name, count, organization_name, environment, contentview, systemgroups, virtual, host,
98 100
                    operatingsystem, architecture, sockets, ram, cores, sla, products, subscriptions]
99 101
          end
......
109 111
        create_systems_from_csv(line)
110 112
      end
111 113

  
112
      print "Updating host and guest associations..." if option_verbose?
114
      print 'Updating host and guest associations...' if option_verbose?
113 115
      @host_guests.each do |host_id, guest_ids|
114 116
        @api.resource(:systems).call(:update, {
115 117
                               'id' => host_id,
116 118
                               'guest_ids' => guest_ids
117 119
                             })
118 120
      end
119
      puts "done" if option_verbose?
121
      puts 'done' if option_verbose?
120 122
    end
121 123

  
122 124
    def create_systems_from_csv(line)
......
144 146
                                                     'organization_id' => katello_organization(:name => line[ORGANIZATION]),
145 147
                                                     'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
146 148
                                                     'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
147
                                                     'facts' => facts(line),
149
                                                     'facts' => facts(name, line),
148 150
                                                     'installed_products' => products(line),
149 151
                                                     'type' => 'system'
150 152
                                                   })['uuid']
......
157 159
                                                       'name' => name,
158 160
                                                       'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
159 161
                                                       'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
160
                                                       'facts' => facts(line),
162
                                                       'facts' => facts(name, line),
161 163
                                                       'installed_products' => products(line)
162 164
                                                     }
163 165
                                                   })['uuid']
......
171 173

  
172 174
        set_system_groups(system_id, line)
173 175

  
174
        puts "done" if option_verbose?
176
        puts 'done' if option_verbose?
175 177
      end
176 178
    rescue RuntimeError => e
177 179
      raise "#{e}\n       #{line}"
......
179 181

  
180 182
    private
181 183

  
182
    def facts(line)
184
    def facts(name, line)
183 185
      facts = {}
184 186
      facts['cpu.core(s)_per_socket'] = line[CORES]
185 187
      facts['cpu.cpu_socket(s)'] = line[SOCKETS]
......
191 193
        (facts['distribution.name'], facts['distribution.version']) = ['RHEL', line[OPERATINGSYSTEM]]
192 194
      end
193 195
      facts['virt.is_guest'] = line[VIRTUAL] == 'Yes' ? true : false
196
      facts['virt.uuid'] = "#{line[ORGANIZATION]}/#{name}" if facts['virt.is_guest']
194 197
      facts
195 198
    end
196 199

  
......
204 207
    end
205 208

  
206 209
    def products(line)
210
      return nil if !line[PRODUCTS]
207 211
      products = CSV.parse_line(line[PRODUCTS]).collect do |product_details|
208 212
        product = {}
209 213
        # TODO: these get passed straight through to candlepin; probably would be better to process in server
......
215 219
    end
216 220

  
217 221
    def subscriptions(line)
222
      return nil if !line[SUBSCRIPTIONS]
218 223
      subscriptions = CSV.parse_line(line[SUBSCRIPTIONS]).collect do |subscription_details|
219 224
        subscription = {}
220 225
        (subscription[:number], subscription[:name]) = subscription_details.split('|')
......
222 227
      end
223 228
      subscriptions
224 229
    end
225

  
226 230
  end
227 231

  
228
  HammerCLI::MainCommand.subcommand("csv:systems", "import/export systems", HammerCLICsv::SystemsCommand)
232
  HammerCLI::MainCommand.subcommand('csv:systems', 'import/export systems', HammerCLICsv::SystemsCommand)
229 233
end
test/csv_test_helper.rb
1 1
require 'simplecov'
2 2
require 'pathname'
3
require 'stringio'
4
require 'tempfile'
3 5

  
4 6
SimpleCov.use_merging true
5 7
SimpleCov.start do
test/helpers/command.rb
1
# TODO: needed? require File.join(File.dirname(__FILE__), '../test_output_adapter')
2

  
3 1

  
4 2
module CommandTestHelper
5 3

  
test/systems_test.rb
1
require File.join(File.dirname(__FILE__), 'csv_test_helper')
2

  
3
describe 'systems' do
4

  
5
  extend CommandTestHelper
6

  
7
  before :each do
8
    HammerCLI::Settings.load_from_file 'test/config.yml'
9

  
10
    @api = ApipieBindings::API.new({
11
                                     :uri => HammerCLI::Settings.get(:csv, :host),
12
                                     :username => HammerCLI::Settings.get(:csv, :username),
13
                                     :password => HammerCLI::Settings.get(:csv, :password),
14
                                     :api_version => 2
15
                                   })
16

  
17
  end
18

  
19
  context "import" do
20

  
21
    # TODO: Bug #4922 - system facts not updating via API
22
    # http://projects.theforeman.org/issues/4922
23
    it "update system facts" do
24

  
25
      hostname = "host#{rand(10000)}"
26

  
27
      # Create system
28
      file = Tempfile.new('systems_test')
29
      file.write("Name,Count,Organization,Environment,Content View,System Groups,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
30
      file.write("#{hostname},1,Mega Corporation,Library,Default Organization View,Mega Corp HQ,No,,RHEL 6.4,x86_64,1,4,1,Standard,,\n")
31
      file.rewind
32

  
33
      stdout,stderr = capture {
34
        hammer.run(%W{-v csv:systems --csv-file #{file.path}})
35
      }
36
      stderr.must_equal ''
37
      stdout[0..-2].must_equal "Creating system '#{hostname}'...done\nUpdating host and guest associations...done"
38
      file.unlink
39

  
40
      # Update system
41
      file = Tempfile.new('systems_test')
42
      file.write("Name,Count,Organization,Environment,Content View,System Groups,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
43
      file.write("#{hostname},1,Mega Corporation,Library,Default Organization View,Mega Corp HQ,No,,RHEL 6.4,x86_64,1,8,1,Standard,,\n")
44
      file.rewind
45

  
46
      stdout,stderr = capture {
47
        hammer.run(%W{-v csv:systems --csv-file #{file.path}})
48
      }
49
      stderr.must_equal ''
50
      stdout[0..-2].must_equal "Updating system '#{hostname}'...done\nUpdating host and guest associations...done"
51
      file.unlink
52

  
53
      # Verify system
54
      system = @api.resource(:systems).call(:index, {
55
                                              'organization_id' => 'megacorp',
56
                                              'search' => "name=\"#{hostname}\""
57
                                            })['results']
58
      system.wont_be_nil
59
      system.wont_be_empty
60
      system[0]['name'].must_equal hostname
61

  
62
      # Clean up
63
      # TODO: Bug #4921 - bulk remove systems error "user not set"
64
      # http://projects.theforeman.org/issues/4921
65
      @api.resource(:systems).call(:destroy, {
66
                                              'id' => system[0]['id']
67
                                             })
68
    end
69

  
70
  end
71
end

Also available in: Unified diff