hammer_cli_foreman_ansible 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,200 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ describe 'host' do
4
+ let(:ansible_role_1) do
5
+ {
6
+ 'id' => 1,
7
+ 'name' => 'role1',
8
+ 'inherited' => false,
9
+ 'directly_assigned' => true
10
+ }
11
+ end
12
+ let(:ansible_role_2) do
13
+ {
14
+ 'id' => 2,
15
+ 'name' => 'role2',
16
+ 'inherited' => true,
17
+ 'directly_assigned' => false
18
+ }
19
+ end
20
+ let(:ansible_roles) do
21
+ [
22
+ ansible_role_1,
23
+ ansible_role_2
24
+ ]
25
+ end
26
+
27
+ describe 'add ansible role' do
28
+ let(:cmd) { %w[host ansible-roles add] }
29
+
30
+ it 'requires host --id' do
31
+ expected_result = usage_error_result(
32
+ cmd,
33
+ 'At least one of options --name, --id is required.',
34
+ 'Could not associate the Ansible role'
35
+ )
36
+
37
+ api_expects_no_call
38
+
39
+ result = run_cmd(cmd)
40
+ assert_cmd(expected_result, result)
41
+ end
42
+
43
+ it 'requires host id and asnible role id' do
44
+ params = %w[--id=1]
45
+ expected_result = usage_error_result(
46
+ cmd,
47
+ 'At least one of options --ansible-role, --ansible-role-id is required.',
48
+ 'Could not associate the Ansible role'
49
+ )
50
+
51
+ api_expects_no_call
52
+
53
+ result = run_cmd(cmd + params)
54
+ assert_cmd(expected_result, result)
55
+ end
56
+
57
+ it 'associates ansible role with host' do
58
+ params = %w[--id=1 --ansible-role-id=2]
59
+
60
+ api_expects(:hosts, :ansible_roles) do |par|
61
+ par[:id] == '1'
62
+ end.returns([ansible_role_1])
63
+
64
+ api_expects(:hosts, :update) do |par|
65
+ par['id'] == '1' &&
66
+ par['host']['ansible_role_ids'] == %w[1 2]
67
+ end
68
+
69
+ result = run_cmd(cmd + params)
70
+ assert_cmd(success_result("Ansible role has been associated.\n"), result)
71
+ end
72
+
73
+ it 'raises an error on associating an inherited ansible role to host' do
74
+ params = %w[--id=1 --ansible-role-id=2]
75
+ expected_result = CommandExpectation.new
76
+ expected_result.expected_err = [
77
+ 'Could not associate the Ansible role:',
78
+ ' Ansible role role2 is already inherited from a host group. Please use --force for direct association.',
79
+ ''
80
+ ].join("\n")
81
+ expected_result.expected_exit_code = HammerCLI::EX_USAGE
82
+
83
+ api_expects(:hosts, :ansible_roles) do |par|
84
+ par[:id] == '1'
85
+ end.returns(ansible_roles)
86
+
87
+ api_expects_no_call
88
+
89
+ result = run_cmd(cmd + params)
90
+ assert_cmd(expected_result, result)
91
+ end
92
+
93
+ it 'associates inherited ansible role with host' do
94
+ params = %w[--id=1 --ansible-role-id=2 --force]
95
+
96
+ api_expects(:hosts, :ansible_roles) do |par|
97
+ par[:id] == '1'
98
+ end.returns(ansible_roles)
99
+
100
+ api_expects(:hosts, :update) do |par|
101
+ par['id'] == '1' &&
102
+ par['host']['ansible_role_ids'] == %w[1 2]
103
+ end
104
+
105
+ result = run_cmd(cmd + params)
106
+ assert_cmd(success_result("Ansible role has been associated.\n"), result)
107
+ end
108
+ end
109
+
110
+ describe 'remove ansible role' do
111
+ let(:cmd) { %w[host ansible-roles remove] }
112
+
113
+ it 'requires host --id' do
114
+ expected_result = usage_error_result(
115
+ cmd,
116
+ 'At least one of options --name, --id is required.',
117
+ 'Could not disassociate the Ansible role'
118
+ )
119
+
120
+ api_expects_no_call
121
+
122
+ result = run_cmd(cmd)
123
+ assert_cmd(expected_result, result)
124
+ end
125
+
126
+ it 'requires host id and asnible role id' do
127
+ params = %w[--id=1]
128
+ expected_result = usage_error_result(
129
+ cmd,
130
+ 'At least one of options --ansible-role, --ansible-role-id is required.',
131
+ 'Could not disassociate the Ansible role'
132
+ )
133
+
134
+ api_expects_no_call
135
+
136
+ result = run_cmd(cmd + params)
137
+ assert_cmd(expected_result, result)
138
+ end
139
+
140
+ it 'disassociates ansible role from host' do
141
+ params = %w[--id=1 --ansible-role-id=1]
142
+
143
+ api_expects(:hosts, :ansible_roles) do |par|
144
+ par[:id] == '1'
145
+ end.returns(ansible_roles)
146
+
147
+ api_expects(:hosts, :update) do |par|
148
+ par['id'] == '1' &&
149
+ par['host']['ansible_role_ids'] == %w[]
150
+ end
151
+
152
+ result = run_cmd(cmd + params)
153
+ assert_cmd(success_result("Ansible role has been disassociated.\n"), result)
154
+ end
155
+
156
+ it 'raises an error on disassociating an inherited ansible role from host by id' do
157
+ params = %w[--id=1 --ansible-role-id=2]
158
+ expected_result = CommandExpectation.new
159
+ expected_result.expected_err = [
160
+ 'Could not disassociate the Ansible role:',
161
+ ' Ansible role role2 is not assigned directly and cannot be removed.',
162
+ ''
163
+ ].join("\n")
164
+ expected_result.expected_exit_code = HammerCLI::EX_USAGE
165
+
166
+ api_expects(:hosts, :ansible_roles) do |par|
167
+ par[:id] == '1'
168
+ end.returns(ansible_roles)
169
+
170
+ api_expects_no_call
171
+
172
+ result = run_cmd(cmd + params)
173
+ assert_cmd(expected_result, result)
174
+ end
175
+
176
+ it 'raises an error on disassociating an inherited ansible role from host by name' do
177
+ params = %w[--id=1 --ansible-role=role2]
178
+ expected_result = CommandExpectation.new
179
+ expected_result.expected_err = [
180
+ 'Could not disassociate the Ansible role:',
181
+ ' Ansible role role2 is not assigned directly and cannot be removed.',
182
+ ''
183
+ ].join("\n")
184
+ expected_result.expected_exit_code = HammerCLI::EX_USAGE
185
+
186
+ api_expects(:ansible_roles, :index) do |par|
187
+ par[:search] == 'name = "role2"'
188
+ end.returns(ansible_role_2)
189
+
190
+ api_expects(:hosts, :ansible_roles) do |par|
191
+ par[:id] == '1'
192
+ end.returns(ansible_roles)
193
+
194
+ api_expects_no_call
195
+
196
+ result = run_cmd(cmd + params)
197
+ assert_cmd(expected_result, result)
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,200 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ describe 'hostgroup' do
4
+ let(:ansible_role_1) do
5
+ {
6
+ 'id' => 1,
7
+ 'name' => 'role1',
8
+ 'inherited' => false,
9
+ 'directly_assigned' => true
10
+ }
11
+ end
12
+ let(:ansible_role_2) do
13
+ {
14
+ 'id' => 2,
15
+ 'name' => 'role2',
16
+ 'inherited' => true,
17
+ 'directly_assigned' => false
18
+ }
19
+ end
20
+ let(:ansible_roles) do
21
+ [
22
+ ansible_role_1,
23
+ ansible_role_2
24
+ ]
25
+ end
26
+
27
+ describe 'add ansible role' do
28
+ let(:cmd) { %w[hostgroup ansible-roles add] }
29
+
30
+ it 'requires hostgroup --id' do
31
+ expected_result = usage_error_result(
32
+ cmd,
33
+ 'At least one of options --name, --title, --id is required.',
34
+ 'Could not associate the Ansible role'
35
+ )
36
+
37
+ api_expects_no_call
38
+
39
+ result = run_cmd(cmd)
40
+ assert_cmd(expected_result, result)
41
+ end
42
+
43
+ it 'requires hostgroup id and asnible role id' do
44
+ params = %w[--id=1]
45
+ expected_result = usage_error_result(
46
+ cmd,
47
+ 'At least one of options --ansible-role, --ansible-role-id is required.',
48
+ 'Could not associate the Ansible role'
49
+ )
50
+
51
+ api_expects_no_call
52
+
53
+ result = run_cmd(cmd + params)
54
+ assert_cmd(expected_result, result)
55
+ end
56
+
57
+ it 'associates ansible role with hostgroup' do
58
+ params = %w[--id=1 --ansible-role-id=2]
59
+
60
+ api_expects(:hostgroups, :ansible_roles) do |par|
61
+ par[:id] == '1'
62
+ end.returns([ansible_role_1])
63
+
64
+ api_expects(:hostgroups, :update) do |par|
65
+ par['id'] == '1' &&
66
+ par['hostgroup']['ansible_role_ids'] == %w[1 2]
67
+ end
68
+
69
+ result = run_cmd(cmd + params)
70
+ assert_cmd(success_result("Ansible role has been associated.\n"), result)
71
+ end
72
+
73
+ it 'raises an error on associating an inherited ansible role to host' do
74
+ params = %w[--id=1 --ansible-role-id=2]
75
+ expected_result = CommandExpectation.new
76
+ expected_result.expected_err = [
77
+ 'Could not associate the Ansible role:',
78
+ ' Ansible role role2 is already inherited from a host group. Please use --force for direct association.',
79
+ ''
80
+ ].join("\n")
81
+ expected_result.expected_exit_code = HammerCLI::EX_USAGE
82
+
83
+ api_expects(:hostgroups, :ansible_roles) do |par|
84
+ par[:id] == '1'
85
+ end.returns(ansible_roles)
86
+
87
+ api_expects_no_call
88
+
89
+ result = run_cmd(cmd + params)
90
+ assert_cmd(expected_result, result)
91
+ end
92
+
93
+ it 'associates inherited ansible role with hostgroup' do
94
+ params = %w[--id=1 --ansible-role-id=2 --force]
95
+
96
+ api_expects(:hostgroups, :ansible_roles) do |par|
97
+ par[:id] == '1'
98
+ end.returns(ansible_roles)
99
+
100
+ api_expects(:hostgroups, :update) do |par|
101
+ par['id'] == '1' &&
102
+ par['hostgroup']['ansible_role_ids'] == %w[1 2]
103
+ end
104
+
105
+ result = run_cmd(cmd + params)
106
+ assert_cmd(success_result("Ansible role has been associated.\n"), result)
107
+ end
108
+ end
109
+
110
+ describe 'remove ansible role' do
111
+ let(:cmd) { %w[hostgroup ansible-roles remove] }
112
+
113
+ it 'requires hostgroup --id' do
114
+ expected_result = usage_error_result(
115
+ cmd,
116
+ 'At least one of options --name, --title, --id is required.',
117
+ 'Could not disassociate the Ansible role'
118
+ )
119
+
120
+ api_expects_no_call
121
+
122
+ result = run_cmd(cmd)
123
+ assert_cmd(expected_result, result)
124
+ end
125
+
126
+ it 'requires hostgroup id and asnible role id' do
127
+ params = %w[--id=1]
128
+ expected_result = usage_error_result(
129
+ cmd,
130
+ 'At least one of options --ansible-role, --ansible-role-id is required.',
131
+ 'Could not disassociate the Ansible role'
132
+ )
133
+
134
+ api_expects_no_call
135
+
136
+ result = run_cmd(cmd + params)
137
+ assert_cmd(expected_result, result)
138
+ end
139
+
140
+ it 'disassociates ansible role from hostgroup' do
141
+ params = %w[--id=1 --ansible-role-id=1]
142
+
143
+ api_expects(:hostgroups, :ansible_roles) do |par|
144
+ par[:id] == '1'
145
+ end.returns(ansible_roles)
146
+
147
+ api_expects(:hostgroups, :update) do |par|
148
+ par['id'] == '1' &&
149
+ par['hostgroup']['ansible_role_ids'] == %w[]
150
+ end
151
+
152
+ result = run_cmd(cmd + params)
153
+ assert_cmd(success_result("Ansible role has been disassociated.\n"), result)
154
+ end
155
+
156
+ it 'raises an error on disassociating an inherited ansible role from hostgroup by id' do
157
+ params = %w[--id=1 --ansible-role-id=2]
158
+ expected_result = CommandExpectation.new
159
+ expected_result.expected_err = [
160
+ 'Could not disassociate the Ansible role:',
161
+ ' Ansible role role2 is not assigned directly and cannot be removed.',
162
+ ''
163
+ ].join("\n")
164
+ expected_result.expected_exit_code = HammerCLI::EX_USAGE
165
+
166
+ api_expects(:hostgroups, :ansible_roles) do |par|
167
+ par[:id] == '1'
168
+ end.returns(ansible_roles)
169
+
170
+ api_expects_no_call
171
+
172
+ result = run_cmd(cmd + params)
173
+ assert_cmd(expected_result, result)
174
+ end
175
+
176
+ it 'raises an error on disassociating an inherited ansible role from hostgroup by name' do
177
+ params = %w[--id=1 --ansible-role=role2]
178
+ expected_result = CommandExpectation.new
179
+ expected_result.expected_err = [
180
+ 'Could not disassociate the Ansible role:',
181
+ ' Ansible role role2 is not assigned directly and cannot be removed.',
182
+ ''
183
+ ].join("\n")
184
+ expected_result.expected_exit_code = HammerCLI::EX_USAGE
185
+
186
+ api_expects(:ansible_roles, :index) do |par|
187
+ par[:search] == 'name = "role2"'
188
+ end.returns(ansible_role_2)
189
+
190
+ api_expects(:hostgroups, :ansible_roles) do |par|
191
+ par[:id] == '1'
192
+ end.returns(ansible_roles)
193
+
194
+ api_expects_no_call
195
+
196
+ result = run_cmd(cmd + params)
197
+ assert_cmd(expected_result, result)
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,11 @@
1
+ require File.join(File.dirname(__FILE__), '../test_helper')
2
+
3
+ require 'hammer_cli/testing/output_matchers'
4
+ require 'hammer_cli/testing/command_assertions'
5
+ require 'hammer_cli/testing/data_helpers'
6
+ require 'hammer_cli_foreman/testing/api_expectations'
7
+
8
+ include HammerCLI::Testing::OutputMatchers
9
+ include HammerCLI::Testing::CommandAssertions
10
+ include HammerCLI::Testing::DataHelpers
11
+ include HammerCLIForeman::Testing::APIExpectations
data/test/test_helper.rb CHANGED
@@ -1,4 +1,18 @@
1
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
- require 'hammer_cli_foreman_ansible'
1
+ ENV['TEST_API_VERSION'] = ENV['TEST_API_VERSION'] || '3.3'
2
+ FOREMAN_ANSIBLE_VERSION = Gem::Version.new(ENV['TEST_API_VERSION']).to_s
3
3
 
4
4
  require 'minitest/autorun'
5
+ require 'minitest/spec'
6
+ require 'minitest-spec-context'
7
+ require 'mocha/minitest'
8
+ require 'hammer_cli'
9
+
10
+ HammerCLI.context[:api_connection].create('foreman') do
11
+ HammerCLI::Apipie::ApiConnection.new(
12
+ apidoc_cache_dir: 'test/data/' + FOREMAN_ANSIBLE_VERSION,
13
+ apidoc_cache_name: 'foreman_api',
14
+ dry_run: true
15
+ )
16
+ end
17
+
18
+ require 'hammer_cli_foreman_ansible'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hammer_cli_foreman_ansible
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleh Fedorenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-21 00:00:00.000000000 Z
11
+ date: 2022-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hammer_cli_foreman
@@ -24,48 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.12.0
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '1.16'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.16'
41
- - !ruby/object:Gem::Dependency
42
- name: minitest
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '5.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '5.0'
55
27
  - !ruby/object:Gem::Dependency
56
28
  name: rake
57
29
  requirement: !ruby/object:Gem::Requirement
58
30
  requirements:
59
- - - "~>"
31
+ - - ">="
60
32
  - !ruby/object:Gem::Version
61
- version: '10.0'
33
+ version: 12.3.3
62
34
  type: :development
63
35
  prerelease: false
64
36
  version_requirements: !ruby/object:Gem::Requirement
65
37
  requirements:
66
- - - "~>"
38
+ - - ">="
67
39
  - !ruby/object:Gem::Version
68
- version: '10.0'
40
+ version: 12.3.3
69
41
  description:
70
42
  email:
71
43
  - ofedoren@redhat.com
@@ -78,12 +50,21 @@ files:
78
50
  - config/foreman_ansible.yml
79
51
  - lib/hammer_cli_foreman_ansible.rb
80
52
  - lib/hammer_cli_foreman_ansible/ansible.rb
53
+ - lib/hammer_cli_foreman_ansible/ansible_inventory.rb
81
54
  - lib/hammer_cli_foreman_ansible/ansible_roles.rb
82
55
  - lib/hammer_cli_foreman_ansible/ansible_variables.rb
56
+ - lib/hammer_cli_foreman_ansible/associated_ansible_role.rb
57
+ - lib/hammer_cli_foreman_ansible/command_extensions.rb
58
+ - lib/hammer_cli_foreman_ansible/command_extensions/inventory.rb
59
+ - lib/hammer_cli_foreman_ansible/command_extensions/resolver.rb
83
60
  - lib/hammer_cli_foreman_ansible/host.rb
84
61
  - lib/hammer_cli_foreman_ansible/hostgroup.rb
85
62
  - lib/hammer_cli_foreman_ansible/i18n.rb
86
63
  - lib/hammer_cli_foreman_ansible/version.rb
64
+ - test/data/3.3/foreman_api.json
65
+ - test/functional/host_test.rb
66
+ - test/functional/hostgroup_test.rb
67
+ - test/functional/test_helper.rb
87
68
  - test/test_helper.rb
88
69
  homepage: https://github.com/theforeman/hammer-cli-foreman-ansible
89
70
  licenses:
@@ -104,10 +85,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
85
  - !ruby/object:Gem::Version
105
86
  version: '0'
106
87
  requirements: []
107
- rubyforge_project:
108
- rubygems_version: 2.6.14
88
+ rubygems_version: 3.1.2
109
89
  signing_key:
110
90
  specification_version: 4
111
91
  summary: Foreman Ansible plugin for Hammer CLI
112
92
  test_files:
93
+ - test/data/3.3/foreman_api.json
94
+ - test/functional/test_helper.rb
95
+ - test/functional/host_test.rb
96
+ - test/functional/hostgroup_test.rb
113
97
  - test/test_helper.rb