hammer_cli_foreman 2.3.2 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/doc/release_notes.md +6 -3
  3. data/lib/hammer_cli_foreman/architecture.rb +5 -5
  4. data/lib/hammer_cli_foreman/bookmark.rb +6 -6
  5. data/lib/hammer_cli_foreman/command_extensions/status.rb +1 -1
  6. data/lib/hammer_cli_foreman/compute_profile.rb +5 -5
  7. data/lib/hammer_cli_foreman/config_group.rb +5 -5
  8. data/lib/hammer_cli_foreman/hosts/common_update_options.rb +1 -1
  9. data/lib/hammer_cli_foreman/mail_notification.rb +2 -2
  10. data/lib/hammer_cli_foreman/model.rb +5 -5
  11. data/lib/hammer_cli_foreman/operating_system.rb +10 -9
  12. data/lib/hammer_cli_foreman/option_builders.rb +2 -21
  13. data/lib/hammer_cli_foreman/settings.rb +3 -3
  14. data/lib/hammer_cli_foreman/usergroup.rb +5 -5
  15. data/lib/hammer_cli_foreman/version.rb +1 -1
  16. data/test/functional/architecture_test.rb +49 -0
  17. data/test/functional/bookmark_test.rb +20 -0
  18. data/test/functional/compute_profile_test.rb +43 -0
  19. data/test/functional/config_group_test.rb +50 -0
  20. data/test/functional/filter_test.rb +114 -47
  21. data/test/functional/host_test.rb +63 -2
  22. data/test/functional/mail_notification_test.rb +20 -0
  23. data/test/functional/model_test.rb +50 -0
  24. data/test/functional/operating_system_test.rb +51 -0
  25. data/test/functional/settings_test.rb +21 -0
  26. data/test/functional/status_test.rb +79 -13
  27. data/test/functional/usergroup_test.rb +51 -0
  28. data/test/reports/TEST-Minitest-Result.xml +4344 -0
  29. data/test/unit/apipie_resource_mock.rb +21 -0
  30. data/test/unit/architecture_test.rb +10 -1
  31. data/test/unit/bookmark_test.rb +99 -0
  32. data/test/unit/compute_profile_test.rb +87 -0
  33. data/test/unit/config_group_test.rb +10 -0
  34. data/test/unit/mail_notification_test.rb +53 -0
  35. data/test/unit/model_test.rb +10 -0
  36. data/test/unit/operating_system_test.rb +14 -1
  37. data/test/unit/settings_test.rb +4 -0
  38. data/test/unit/usergroup_test.rb +10 -0
  39. metadata +119 -101
@@ -1,5 +1,5 @@
1
1
  module HammerCLIForeman
2
2
  def self.version
3
- @version ||= Gem::Version.new "2.3.2"
3
+ @version ||= Gem::Version.new "2.4.0"
4
4
  end
5
5
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.join(File.dirname(__FILE__), 'test_helper')
4
+
5
+ describe 'architecture' do
6
+ describe 'list' do
7
+ before do
8
+ @cmd = %w[architecture list]
9
+ @architectures = [{
10
+ id: 1,
11
+ name: 'i386',
12
+ }]
13
+ end
14
+
15
+ it 'should return a list of architectures' do
16
+ api_expects(:architectures, :index, 'List architectures').returns(@architectures)
17
+
18
+ output = IndexMatcher.new([
19
+ %w[ID NAME],
20
+ %w[1 i386]
21
+ ])
22
+ expected_result = success_result(output)
23
+
24
+ result = run_cmd(@cmd)
25
+ assert_cmd(expected_result, result)
26
+ end
27
+
28
+ it 'should run list command with defaults' do
29
+ providers = { 'foreman' => HammerCLIForeman::Defaults.new(api_connection({}, '2.1')) }
30
+ defaults = HammerCLI::Defaults.new(
31
+ {
32
+ organization_id: {
33
+ provider: 'foreman'
34
+ },
35
+ location_id: {
36
+ provider: 'foreman'
37
+ }
38
+ }
39
+ )
40
+ defaults.stubs(:write_to_file).returns(true)
41
+ defaults.stubs(:providers).returns(providers)
42
+ api_expects(:architectures, :index, 'List architectures').returns(@architectures)
43
+
44
+ result = run_cmd(@cmd, { use_defaults: true, defaults: defaults })
45
+ _(result.exit_code).must_equal HammerCLI::EX_OK
46
+ end
47
+ end
48
+ end
49
+
@@ -27,6 +27,26 @@ describe 'bookmark' do
27
27
  result = run_cmd(@cmd)
28
28
  assert_cmd(expected_result, result)
29
29
  end
30
+
31
+ it 'should run list command with defaults' do
32
+ providers = { 'foreman' => HammerCLIForeman::Defaults.new(api_connection({}, '2.1')) }
33
+ defaults = HammerCLI::Defaults.new(
34
+ {
35
+ organization_id: {
36
+ provider: 'foreman'
37
+ },
38
+ location_id: {
39
+ provider: 'foreman'
40
+ }
41
+ }
42
+ )
43
+ defaults.stubs(:write_to_file).returns(true)
44
+ defaults.stubs(:providers).returns(providers)
45
+ api_expects(:bookmarks, :index, 'List bookmarks').returns(@bookmarks)
46
+
47
+ result = run_cmd(@cmd, { use_defaults: true, defaults: defaults })
48
+ _(result.exit_code).must_equal HammerCLI::EX_OK
49
+ end
30
50
  end
31
51
 
32
52
  describe 'info' do
@@ -98,4 +98,47 @@ describe "parameters" do
98
98
  end
99
99
  end
100
100
 
101
+ describe 'list' do
102
+ before do
103
+ @cmd = %w[compute-profile list]
104
+ @compute_profiles = [{
105
+ id: 1,
106
+ name: '1-Small',
107
+ }]
108
+ end
109
+
110
+ it 'should return a list of compute profiles' do
111
+ api_expects(:compute_profiles, :index, 'List compute profiles').returns(@compute_profiles)
112
+
113
+ output = IndexMatcher.new([
114
+ %w[ID NAME],
115
+ %w[1 1-Small]
116
+ ])
117
+ expected_result = success_result(output)
118
+
119
+ result = run_cmd(@cmd)
120
+ assert_cmd(expected_result, result)
121
+ end
122
+
123
+ it 'should run list command with defaults' do
124
+ providers = { 'foreman' => HammerCLIForeman::Defaults.new(api_connection({}, '2.1')) }
125
+ defaults = HammerCLI::Defaults.new(
126
+ {
127
+ organization_id: {
128
+ provider: 'foreman'
129
+ },
130
+ location_id: {
131
+ provider: 'foreman'
132
+ }
133
+ }
134
+ )
135
+ defaults.stubs(:write_to_file).returns(true)
136
+ defaults.stubs(:providers).returns(providers)
137
+ api_expects(:compute_profiles, :index, 'List compute profiles').returns(@compute_profiles)
138
+
139
+ result = run_cmd(@cmd, { use_defaults: true, defaults: defaults })
140
+ _(result.exit_code).must_equal HammerCLI::EX_OK
141
+ end
142
+ end
143
+
101
144
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.join(File.dirname(__FILE__), 'test_helper')
4
+
5
+ describe 'config_group' do
6
+ describe 'list' do
7
+ before do
8
+ @cmd = %w[config-group list]
9
+ @config_groups = [{
10
+ id: 1,
11
+ name: 'config-group-test',
12
+ }]
13
+ end
14
+
15
+ it 'should return a list of config groups' do
16
+ api_expects(:config_groups, :index, 'List config groups').returns(@config_groups)
17
+
18
+ output = IndexMatcher.new([
19
+ %w[ID NAME],
20
+ %w[1 config-group-test]
21
+ ])
22
+ expected_result = success_result(output)
23
+
24
+ result = run_cmd(@cmd)
25
+ assert_cmd(expected_result, result)
26
+ end
27
+
28
+ it 'should run list command with defaults' do
29
+ providers = { 'foreman' => HammerCLIForeman::Defaults.new(api_connection({}, '2.1')) }
30
+ defaults = HammerCLI::Defaults.new(
31
+ {
32
+ organization_id: {
33
+ provider: 'foreman'
34
+ },
35
+ location_id: {
36
+ provider: 'foreman'
37
+ }
38
+ }
39
+ )
40
+ defaults.stubs(:write_to_file).returns(true)
41
+ defaults.stubs(:providers).returns(providers)
42
+ api_expects(:config_groups, :index, 'List config groups').returns(@config_groups)
43
+
44
+ result = run_cmd(@cmd, { use_defaults: true, defaults: defaults })
45
+ _(result.exit_code).must_equal HammerCLI::EX_OK
46
+ end
47
+ end
48
+ end
49
+
50
+
@@ -1,18 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.join(File.dirname(__FILE__), 'test_helper')
2
4
 
3
5
  describe 'filter' do
4
6
  def api_expects_filter_info(options)
5
7
  override = !!options[:override]
6
- api_expects(:filters, :show, 'Get filter info').with_params({
7
- :id => '1'
8
- }).returns(@filter.merge('override?' => override))
8
+ api_expects(:filters, :show, 'Get filter info').with_params(
9
+ id: '1'
10
+ ).returns(@filter.merge('override?' => override))
9
11
  end
10
12
 
11
13
  def taxonomy_usage_error(action, cmd)
12
14
  usage_error_result(
13
15
  cmd,
14
16
  'Organizations and locations can be set only for overriding filters.',
15
- "Could not #{action} the permission filter")
17
+ "Could not #{action} the permission filter"
18
+ )
16
19
  end
17
20
 
18
21
  def assert_update_success(result)
@@ -21,7 +24,7 @@ describe 'filter' do
21
24
 
22
25
  describe 'create' do
23
26
  before do
24
- @cmd = %w(filter create)
27
+ @cmd = %w[filter create]
25
28
  end
26
29
 
27
30
  it 'prints error when taxonomies are used for a not-overriding filter' do
@@ -32,41 +35,105 @@ describe 'filter' do
32
35
  result = run_cmd(@cmd + params)
33
36
  assert_cmd(taxonomy_usage_error('create', @cmd), result)
34
37
  end
38
+
39
+ it 'should create a filter' do
40
+ params = ['--role-id=1', '--permission-ids=[1]']
41
+
42
+ api_expects(:filters, :create) do |params|
43
+ (params['filter']['role_id'] == 1)
44
+ (params['filter']['permission_ids'] == [1])
45
+ end
46
+
47
+ result = run_cmd(@cmd + params)
48
+ assert_cmd(success_result("Permission filter for [%<resource_type>s] created.\n"), result)
49
+ end
50
+ end
51
+
52
+ describe 'list' do
53
+ before do
54
+ @cmd = %w[filter list]
55
+ @filters = [{
56
+ id: 1,
57
+ resource_type: 'Architecture',
58
+ search: 'none',
59
+ unlimited?: true,
60
+ override?: false,
61
+ role: { name: 'Manager', id: 2, description: 'Role granting all available permissions.', origin: 'foreman' },
62
+ permissions: 'view_architectures'
63
+ }]
64
+ end
65
+
66
+ it 'should return a list of filters' do
67
+ api_expects(:filters, :index, 'List filters').returns(@filters)
68
+
69
+ output = IndexMatcher.new([
70
+ ['ID', 'RESOURCE TYPE', 'SEARCH', 'UNLIMITED?', 'OVERRIDE?', 'ROLE', 'PERMISSIONS'],
71
+ ['1', 'Architecture', 'none', 'yes', 'no', 'Manager', 'view_architectures']
72
+ ])
73
+ expected_result = success_result(output)
74
+
75
+ result = run_cmd(@cmd)
76
+ assert_cmd(expected_result, result)
77
+ end
78
+ end
79
+
80
+ describe 'delete' do
81
+ before do
82
+ @cmd = %w(filter delete)
83
+ end
84
+
85
+ it 'should print error missing argument id' do
86
+ expected_result = "Could not delete the permission filter:\n Missing arguments for '--id'.\n"
87
+
88
+ api_expects_no_call
89
+
90
+ result = run_cmd(@cmd)
91
+ assert_match(expected_result, result.err)
92
+ end
93
+
94
+ it 'should delete a filter' do
95
+ params = ['--id=1']
96
+
97
+ api_expects(:filters, :destroy, 'Delete a filter').with_params(id: '1')
98
+
99
+ result = run_cmd(@cmd + params)
100
+ assert_cmd(success_result("Permission filter deleted.\n"), result)
101
+ end
35
102
  end
36
103
 
37
104
  describe 'update' do
38
105
  before do
39
- @cmd = %w(filter update)
106
+ @cmd = %w[filter update]
40
107
  @filter = {
41
- "search" => nil,
42
- "resource_type" => "User",
43
- "unlimited?" => false,
44
- "created_at" => "2017-07-18 14:34:09 UTC",
45
- "updated_at" => "2017-07-18 14:34:09 UTC",
46
- "override?" => true,
47
- "id" => 404,
48
- "role" => {
49
- "name" => "Some Role",
50
- "id" => 28,
51
- "description" => "Description\nof the new\nrole",
52
- "origin" => nil
108
+ 'search' => nil,
109
+ 'resource_type' => 'User',
110
+ 'unlimited?' => false,
111
+ 'created_at' => '2017-07-18 14:34:09 UTC',
112
+ 'updated_at' => '2017-07-18 14:34:09 UTC',
113
+ 'override?' => true,
114
+ 'id' => 404,
115
+ 'role' => {
116
+ 'name' => 'Some Role',
117
+ 'id' => 28,
118
+ 'description' => "Description\nof the new\nrole",
119
+ 'origin' => nil
53
120
  },
54
- "permissions" => [{
55
- "name" => "view_users",
56
- "id" => 164,
57
- "resource_type" => "User"
121
+ 'permissions' => [{
122
+ 'name' => 'view_users',
123
+ 'id' => 164,
124
+ 'resource_type' => 'User'
58
125
  }],
59
- "locations" => [{
60
- "id" => 28,
61
- "name" => "location74",
62
- "title" => "location74",
63
- "description" => nil
126
+ 'locations' => [{
127
+ 'id' => 28,
128
+ 'name' => 'location74',
129
+ 'title' => 'location74',
130
+ 'description' => nil
64
131
  }],
65
- "organizations" => [{
66
- "id" => 27,
67
- "name" => "organization74",
68
- "title" => "organization74",
69
- "description" => nil
132
+ 'organizations' => [{
133
+ 'id' => 27,
134
+ 'name' => 'organization74',
135
+ 'title' => 'organization74',
136
+ 'description' => nil
70
137
  }]
71
138
  }
72
139
  end
@@ -74,13 +141,13 @@ describe 'filter' do
74
141
  it 'resets taxonomies when a filter is not-overriding' do
75
142
  params = ['--id=1']
76
143
 
77
- api_expects_filter_info(:override => false)
78
- api_expects(:filters, :update, 'Update the filter').with_params({
144
+ api_expects_filter_info(override: false)
145
+ api_expects(:filters, :update, 'Update the filter').with_params(
79
146
  'filter' => {
80
147
  'organization_ids' => [],
81
148
  'location_ids' => []
82
149
  }
83
- }).returns(@filter)
150
+ ).returns(@filter)
84
151
 
85
152
  assert_update_success(run_cmd(@cmd + params))
86
153
  end
@@ -88,12 +155,12 @@ describe 'filter' do
88
155
  it 'resets taxonomies when switching a filter to not-overriding' do
89
156
  params = ['--id=1', '--override=false']
90
157
 
91
- api_expects(:filters, :update, 'Update the filter').with_params({
158
+ api_expects(:filters, :update, 'Update the filter').with_params(
92
159
  'filter' => {
93
160
  'organization_ids' => [],
94
161
  'location_ids' => []
95
162
  }
96
- }).returns(@filter)
163
+ ).returns(@filter)
97
164
 
98
165
  assert_update_success(run_cmd(@cmd + params))
99
166
  end
@@ -101,13 +168,13 @@ describe 'filter' do
101
168
  it 'can add taxonomies when a filter is overriding' do
102
169
  params = ['--id=1', '--organization-ids=1,2', '--location-ids=3,4']
103
170
 
104
- api_expects_filter_info(:override => true)
105
- api_expects(:filters, :update, 'Update the filter').with_params({
171
+ api_expects_filter_info(override: true)
172
+ api_expects(:filters, :update, 'Update the filter').with_params(
106
173
  'filter' => {
107
- 'organization_ids' => ['1', '2'],
108
- 'location_ids' => ['3', '4']
174
+ 'organization_ids' => %w[1 2],
175
+ 'location_ids' => %w[3 4]
109
176
  }
110
- }).returns(@filter)
177
+ ).returns(@filter)
111
178
 
112
179
  assert_update_success(run_cmd(@cmd + params))
113
180
  end
@@ -115,12 +182,12 @@ describe 'filter' do
115
182
  it 'can add taxonomies when switching a filter to overriding' do
116
183
  params = ['--id=1', '--organization-ids=1,2', '--location-ids=3,4', '--override=true']
117
184
 
118
- api_expects(:filters, :update, 'Update the filter').with_params({
185
+ api_expects(:filters, :update, 'Update the filter').with_params(
119
186
  'filter' => {
120
- 'organization_ids' => ['1', '2'],
121
- 'location_ids' => ['3', '4']
187
+ 'organization_ids' => %w[1 2],
188
+ 'location_ids' => %w[3 4]
122
189
  }
123
- }).returns(@filter)
190
+ ).returns(@filter)
124
191
 
125
192
  assert_update_success(run_cmd(@cmd + params))
126
193
  end
@@ -128,7 +195,7 @@ describe 'filter' do
128
195
  it 'prints error when taxonomies are used on not-overriding' do
129
196
  params = ['--id=1', '--organization-ids=1,2', '--location-ids=3,4']
130
197
 
131
- api_expects_filter_info(:override => false)
198
+ api_expects_filter_info(override: false)
132
199
 
133
200
  result = run_cmd(@cmd + params)
134
201
  assert_cmd(taxonomy_usage_error('update', @cmd), result)
@@ -297,6 +297,21 @@ describe "host create" do
297
297
  assert_cmd(expected_result, result)
298
298
  end
299
299
 
300
+ it 'should create a host with an owner' do
301
+ params = ['--owner-id=1']
302
+
303
+ api_expects(:hosts, :create, 'Create a host with an owner').with_params(
304
+ 'location_id' => 1, 'organization_id' => 1, 'host' => {
305
+ 'owner_id' => '1', 'name' => 'test'
306
+ }
307
+ ).returns(results: { 'owner_id' => '1', 'name' => 'test' })
308
+
309
+ expected_result = success_result("Host created.\n")
310
+
311
+ result = run_cmd(cmd + minimal_params_without_hostgroup + params)
312
+
313
+ assert_cmd(expected_result, result)
314
+ end
300
315
  end
301
316
 
302
317
  describe 'host update' do
@@ -410,7 +425,25 @@ describe 'host update' do
410
425
  )
411
426
  api_expects(:hosts, :update, 'Update host with new owner').with_params(
412
427
  'id' => '1', 'location_id' => 1, 'organization_id' => 1, 'host' => {
413
- 'owner_id' => '1' }
428
+ 'owner_id' => '1' }
429
+ ) do |par|
430
+ par['id'] == '1' && par['host']['owner_id'] == '1'
431
+ end.returns(updated_host)
432
+
433
+ expected_result = success_result("Host updated.\n")
434
+
435
+ result = run_cmd(cmd + minimal_params + params)
436
+
437
+ assert_cmd(expected_result, result)
438
+ end
439
+
440
+ it 'should update the host owner with id' do
441
+ params = ['--owner-id=1']
442
+
443
+ api_expects(:hosts, :update, 'Update host with new owner').with_params(
444
+ 'id' => '1', 'location_id' => 1, 'organization_id' => 1, 'host' => {
445
+ 'owner_id' => '1'
446
+ }
414
447
  ) do |par|
415
448
  par['id'] == '1' && par['host']['owner_id'] == '1'
416
449
  end.returns(updated_host)
@@ -426,7 +459,7 @@ end
426
459
  describe 'host config reports' do
427
460
  let(:report15) do
428
461
  {
429
- "id" => 15,
462
+ "id" => 15,
430
463
  "host_id" => 1,
431
464
  "host_name" => "host.example.com",
432
465
  "reported_at" => "2017-11-13 03:04:53 UTC",
@@ -516,3 +549,31 @@ describe 'run puppetrun for host' do
516
549
  assert_cmd(expected_result, result)
517
550
  end
518
551
  end
552
+
553
+ describe 'list' do
554
+ before do
555
+ @cmd = %w[host list]
556
+ end
557
+
558
+ it 'should run list command with defaults' do
559
+ providers = { 'foreman' => HammerCLIForeman::Defaults.new(api_connection({}, '2.1')) }
560
+ defaults = HammerCLI::Defaults.new(
561
+ {
562
+ organization_id: {
563
+ provider: 'foreman'
564
+ },
565
+ location_id: {
566
+ provider: 'foreman'
567
+ }
568
+ }
569
+ )
570
+ defaults.stubs(:write_to_file).returns(true)
571
+ defaults.stubs(:providers).returns(providers)
572
+ api_expects(:users, :index, 'Find user').with_params(search: 'login=admin').returns(index_response([{ 'default_organization' => { 'id' => 2 } }]))
573
+ api_expects(:users, :index, 'Find user').with_params(search: 'login=admin').returns(index_response([{ 'default_location' => { 'id' => 1 } }]))
574
+ api_expects(:hosts, :index, 'List hosts').returns(index_response([{ 'id' => '42' }]))
575
+
576
+ result = run_cmd(@cmd, { use_defaults: true, defaults: defaults })
577
+ _(result.exit_code).must_equal HammerCLI::EX_OK
578
+ end
579
+ end