Project

General

Profile

Revision 561a8ac9

Added by Thomas McKay over 8 years ago

update roles use of api with test and docs

Note: The roles test does not work completely since changing user is not working. Unsure how to accomplish this. Tests run as admin do work.

View differences:

README.md
417 417
* Sample data
418 418
  * [Mega Corporation](https://github.com/Katello/hammer-cli-csv/blob/master/test/data/roles.csv)
419 419
* Supported products and version
420
  * Foreman-1.5, Foreman-nightly
420
  * Foreman-1.6, Foreman-nightly
421 421
  * Foreman-nightly w/ Katello-nightly
422
  * Satellite-6.0.3
423
  * SAM-1.3, SAM-1.4
422
  * Satellite-6.0.4
424 423

  
425 424
**CSV Columns**
426 425

  
lib/hammer_cli_csv/roles.rb
59 59
      end
60 60

  
61 61
      def create_roles_from_csv(line)
62

  
63
        permissions = collect_column(line[PERMISSIONS]) do |permission|
64
          foreman_permission(:name => permission)
65
        end
66
        organizations = collect_column(line[ORGANIZATIONS]) do |organization|
67
          foreman_organization(:name => organization)
68
        end
69
        locations = collect_column(line[LOCATIONS]) do |location|
70
          foreman_location(:name => location)
71
        end
72

  
62 73
        line[COUNT].to_i.times do |number|
63 74
          name = namify(line[NAME], number)
64 75
          search = namify(line[SEARCH], number) if line[SEARCH]
......
76 87
                                       })
77 88
          end
78 89

  
79
          permissions = collect_column(line[PERMISSIONS]) do |permission|
80
            foreman_permission(:name => permission)
81
          end
82
          organizations = collect_column(line[ORGANIZATIONS]) do |organization|
83
            foreman_organization(:name => organization)
84
          end
85
          locations = collect_column(line[LOCATIONS]) do |location|
86
            foreman_location(:name => location)
87
          end
88

  
89 90
          filter_id = foreman_filter(name, line[RESOURCE], search)
90 91
          if !filter_id
91 92
            print " creating filter #{line[RESOURCE]}..."
92
            @api.resource(:filters).call(:create, {
93
            @api.resource(:filters).call(:create, { 'filter' => {
93 94
                                           'role_id' => @existing_roles[name],
94 95
                                           'search' => search,
95 96
                                           'organization_ids' => organizations,
96 97
                                           'location_ids' => locations,
97 98
                                           'permission_ids' => permissions
98
                                         })
99
                                         }})
99 100
          else
100 101
            print " updating filter #{line[RESOURCE]}..."
101 102
            @api.resource(:filters).call(:update, {
test/config.template.yml
1
:ui:
2
  :interactive: true
3
  :per_page: 20
4
  :history_file: './log/history'
5

  
6
:watch_plain: true # enable/disable color output of logger in Clamp commands
7

  
8
:log_dir: './log'
9
:log_level: 'error'
10
:log_api_calls: false
11
:log_size: 5 # MB
12

  
1 13
:csv:
2 14
  :enable_module: true
3 15
  :host:          'https://localhost'
......
12 24

  
13 25
:katello:
14 26
  :enable_module: true
15
  :host:          'https://localhost'
16
  :username:      'admin'
17
  :password:      'changeme'
test/csv_test_helper.rb
25 25
require 'hammer_cli_foreman'
26 26
require 'hammer_cli_katello'
27 27

  
28
def ctx
29
  {
30
    :interactive => false,
31
    :username => 'admin',
32
    :password => 'changeme'
33
  }
28
module HammerCLIForeman
29
  def self.clear_credentials
30
    @credentials = nil
31
  end
34 32
end
35 33

  
34

  
36 35
def hammer(context=nil)
37
  HammerCLI::MainCommand.new("", context || ctx)
36
  HammerCLI::MainCommand.new("", context || HammerCLI::Settings.dump)
38 37
end
39 38

  
40 39
def capture
......
50 49
end
51 50

  
52 51
def set_user(username, password='changeme')
52
  HammerCLI::Connection.drop_all
53
  HammerCLIForeman.clear_credentials
53 54
  HammerCLI::Settings.load({
54
                             :_params => {
55
                               :username => username,
56
                               :password => password,
57
                               :interactive => false
58
                             }})
55
                               :_params => {
56
                                   :username => username,
57
                                   :password => password
58
                               },
59
                               :foreman => {
60
                                   :username => username,
61
                                   :password => password
62
                               },
63
                               :csv => {
64
                                   :username => username,
65
                                   :password => password
66
                               }
67
                           })
59 68
end
60 69

  
61 70

  
test/data/roles.csv
15 15
cinthiacadieux,1,ActivationKey,name = cinthia.cadieux@megacorp.com,"view_activation_keys",Mega Corporation,
16 16
coraleeculbert,1,ActivationKey,name = coralee.culbert@megacorp.com,"view_activation_keys",Mega Corporation,
17 17
damondials,1,ActivationKey,name = damon.dials@megacorp.com,"view_activation_keys",Mega Corporation,
18
damondials,1,Organization,"name = ""Mega Corporation""","view_organizations",Mega Corporation,
18 19
danaedrayer,1,ActivationKey,name = danae.drayer@megacorp.com,"view_activation_keys",Mega Corporation,
19 20
danettedewitt,1,ActivationKey,name = danette.dewitt@megacorp.com,"view_activation_keys",Mega Corporation,
20 21
danilodilbeck,1,ActivationKey,name = danilo.dilbeck@megacorp.com,"view_activation_keys",Mega Corporation,
test/data/subscriptions.csv
1 1
Name,Count,Organization,Manifest File,Content Set,Arch,Release
2
MegaCorp,1,Mega Corporation,~/Downloads/megacorp.zip,
2
MegaCorp,1,Mega Corporation,test/data/megacorp.zip,
3 3
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 6 Server (RPMs),x86_64,6Server
4 4
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 6 Server (RPMs),x86_64,6.1
5 5
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 6 Server (RPMs),x86_64,6.2
test/roles_test.rb
3 3
require 'stringio'
4 4
require 'tempfile'
5 5

  
6
describe 'something' do
6
describe 'roles tests' do
7 7

  
8 8
  extend CommandTestHelper
9 9

  
10 10

  
11
  before :each do
12
    HammerCLI::Settings.load_from_file 'test/config.yml'
13
  end
11
  #before :each do
12
  #  HammerCLI::Settings.load_from_file 'test/config'
13
  #end
14

  
15
  context 'roles' do
16

  
17
    it 'basic import' do
18
      set_user 'admin'
19

  
20
      rolename = "role#{rand(10000)}"
21

  
22
      file = Tempfile.new('roles_test')
23
      file.write("Name,Count,Resource,Search,Permissions,Organizations,Locations\n")
24
      file.write("#{rolename},1,ActivationKey,name = key_name,view_activation_keys,Mega Corporation,\n")
25
      file.rewind
14 26

  
15
  context 'activation keys' do
16 27

  
17
    # Expected output of the form:
18
    # ID,Name,Consumed
19
    # 1,damon.dials@megacorp.com,0 of Unlimited
20
    it 'allows show' do
21
      set_user 'damon.dials@megacorp.com'
28
      stdout,stderr = capture {
29
        hammer.run(%W{csv roles --verbose --csv-file #{file.path}})
30
      }
31
      stderr.must_equal ''
32
      stdout[0..-2].must_equal "Creating role '#{rolename}'... creating filter ActivationKey...done"
33
      file.unlink
34
    end
35

  
36
    it 'test role functionality' do
37
      set_user('damon.dials@megacorp.com', 'redhat')
22 38

  
23 39
      stdout,stderr = capture {
24
        hammer.run(%W{activation-key list --organization-id megacorp}).must_equal HammerCLI::EX_OK
40
        hammer.run(%W{activation-key list --organization-label megacorp}).must_equal HammerCLI::EX_OK
25 41
      }
26 42
      lines = stdout.split("\n")
27
      lines.length.must_equal 2
28
      lines[1].must_match /.*damon.dials@megacorp\.com.*/
43
      lines.length.must_equal 5
44
      lines[3].must_match /.*damon.dials@megacorp\.com.*/
29 45

  
30
      id = lines[1].split(',')[0]
46
      id = lines[3].split(' ')[0]
31 47
      stdout,stderr = capture {
32 48
        hammer.run(%W{activation-key info --id #{id}}).must_equal HammerCLI::EX_OK
33 49
      }
34
      stdout.split("\n")[1].must_match /.*damon.dials@megacorp.com,[0-9]+,Individual account,Library,Default Organization View/
50
      stdout.split("\n")[0].must_match /.*damon.dials@megacorp.com/
35 51
    end
36 52

  
37 53
  end

Also available in: Unified diff