hammer_cli_foreman_ansible 0.3.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hammer_cli_foreman_ansible/ansible.rb +6 -0
- data/lib/hammer_cli_foreman_ansible/ansible_inventory.rb +52 -0
- data/lib/hammer_cli_foreman_ansible/associated_ansible_role.rb +85 -0
- data/lib/hammer_cli_foreman_ansible/command_extensions/inventory.rb +120 -0
- data/lib/hammer_cli_foreman_ansible/command_extensions.rb +1 -0
- data/lib/hammer_cli_foreman_ansible/host.rb +43 -1
- data/lib/hammer_cli_foreman_ansible/hostgroup.rb +42 -1
- data/lib/hammer_cli_foreman_ansible/version.rb +1 -1
- data/lib/hammer_cli_foreman_ansible.rb +1 -0
- data/test/data/3.3/foreman_api.json +1 -0
- data/test/functional/host_test.rb +200 -0
- data/test/functional/hostgroup_test.rb +200 -0
- data/test/functional/test_helper.rb +11 -0
- data/test/test_helper.rb +16 -2
- metadata +13 -2
@@ -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
|
-
|
2
|
-
|
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.
|
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:
|
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
|
@@ -50,14 +50,21 @@ files:
|
|
50
50
|
- config/foreman_ansible.yml
|
51
51
|
- lib/hammer_cli_foreman_ansible.rb
|
52
52
|
- lib/hammer_cli_foreman_ansible/ansible.rb
|
53
|
+
- lib/hammer_cli_foreman_ansible/ansible_inventory.rb
|
53
54
|
- lib/hammer_cli_foreman_ansible/ansible_roles.rb
|
54
55
|
- lib/hammer_cli_foreman_ansible/ansible_variables.rb
|
56
|
+
- lib/hammer_cli_foreman_ansible/associated_ansible_role.rb
|
55
57
|
- lib/hammer_cli_foreman_ansible/command_extensions.rb
|
58
|
+
- lib/hammer_cli_foreman_ansible/command_extensions/inventory.rb
|
56
59
|
- lib/hammer_cli_foreman_ansible/command_extensions/resolver.rb
|
57
60
|
- lib/hammer_cli_foreman_ansible/host.rb
|
58
61
|
- lib/hammer_cli_foreman_ansible/hostgroup.rb
|
59
62
|
- lib/hammer_cli_foreman_ansible/i18n.rb
|
60
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
|
61
68
|
- test/test_helper.rb
|
62
69
|
homepage: https://github.com/theforeman/hammer-cli-foreman-ansible
|
63
70
|
licenses:
|
@@ -83,4 +90,8 @@ signing_key:
|
|
83
90
|
specification_version: 4
|
84
91
|
summary: Foreman Ansible plugin for Hammer CLI
|
85
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
|
86
97
|
- test/test_helper.rb
|