-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathcredential.rb
207 lines (190 loc) · 9.4 KB
/
credential.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
require 'ansible_tower_client'
shared_examples_for "ansible credential" do
let(:finished_task) { FactoryBot.create(:miq_task, :state => "Finished") }
let(:atc) { double("AnsibleTowerClient::Connection", :api => api) }
let(:api) { double("AnsibleTowerClient::Api", :credentials => credentials) }
context "Tower 3.3 needs pk as integer" do
let(:machine_credential) { FactoryBot.create(:ansible_machine_credential, :manager_ref => '1', :resource => manager) }
it "native_ref returns integer" do
expect(machine_credential.manager_ref).to eq('1')
expect(machine_credential.native_ref).to eq(1)
end
it "native_ref blows up for nil manager_ref" do
machine_credential.manager_ref = nil
expect(machine_credential.manager_ref).to be_nil
expect{ machine_credential.native_ref }.to raise_error(TypeError)
end
end
context "Create through API" do
let(:credentials) { double("AnsibleTowerClient::Collection", :create! => credential) }
let(:credential) { AnsibleTowerClient::Credential.new(nil, credential_json) }
let(:credential_json) do
params.merge(
:id => 10,
).stringify_keys.to_json
end
let(:params) do
{
:description => "Description",
:name => "My Credential",
:related => {},
:userid => 'john'
}
end
let(:expected_params) do
{
:description => "Description",
:name => "My Credential",
:related => {},
:username => "john",
:kind => described_class::TOWER_KIND
}
end
let(:expected_notify) do
{
:type => :tower_op_success,
:options => {
:op_name => "#{described_class::FRIENDLY_NAME} creation",
:op_arg => "(name=My Credential)",
:tower => "EMS(manager_id=#{manager.id})"
}
}
end
it ".create_in_provider to succeed and send notification" do
expected_params[:organization] = 1 if described_class.name.include?("::EmbeddedAnsible::")
expect(Vmdb::Settings).to receive(:decrypt_passwords!).with(expected_params)
expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc)
store_new_credential(credential, manager)
expect(EmsRefresh).to receive(:queue_refresh_task).with(manager).and_return([finished_task.id])
expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager)
expect(credentials).to receive(:create!).with(expected_params)
expect(Notification).to receive(:create).with(expected_notify)
expect(described_class.create_in_provider(manager.id, params)).to be_a(described_class)
end
it ".create_in_provider to fail (not found during refresh) and send notification" do
expected_params[:organization] = 1 if described_class.name.include?("::EmbeddedAnsible::")
expect(Vmdb::Settings).to receive(:decrypt_passwords!).with(expected_params)
expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc)
expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task.id])
expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager)
expect(credentials).to receive(:create!).with(expected_params)
expected_notify[:type] = :tower_op_failure
expect(Notification).to receive(:create).with(expected_notify).and_return(double(Notification))
expect { described_class.create_in_provider(manager.id, params) }.to raise_error(ActiveRecord::RecordNotFound)
end
it ".create_in_provider_queue" do
expect(Vmdb::Settings).to receive(:encrypt_passwords!).with(params)
task_id = described_class.create_in_provider_queue(manager.id, params)
expect(MiqTask.find(task_id)).to have_attributes(:name => "Creating #{described_class::FRIENDLY_NAME} (name=#{params[:name]})")
expect(MiqQueue.first).to have_attributes(
:args => [manager.id, params],
:class_name => described_class.name,
:method_name => "create_in_provider",
:priority => MiqQueue::HIGH_PRIORITY,
:role => "ems_operations",
:zone => manager.my_zone
)
end
it ".create_in_provider_queue to fail with incompatible manager" do
wrong_manager = FactoryBot.create(:configuration_manager_foreman)
expect { described_class.create_in_provider_queue(wrong_manager.id, params) }.to raise_error(ActiveRecord::RecordNotFound)
end
def store_new_credential(credential, manager)
described_class.create!(
:resource => manager,
:manager_ref => credential.id.to_s,
:name => credential.name,
)
end
end
context "Delete through API" do
let(:credentials) { double("AnsibleTowerClient::Collection", :find => credential) }
let(:credential) { double("AnsibleTowerClient::Credential", :destroy! => nil, :id => '1') }
let(:ansible_cred) { described_class.create!(:resource => manager, :manager_ref => credential.id) }
let(:expected_notify) do
{
:type => :tower_op_success,
:options => {
:op_name => "#{described_class::FRIENDLY_NAME} deletion",
:op_arg => "(manager_ref=#{credential.id})",
:tower => "EMS(manager_id=#{manager.id})"
}
}
end
it "#delete_in_provider to succeed and send notification" do
expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc)
expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task.id])
expect(Notification).to receive(:create).with(expected_notify)
ansible_cred.delete_in_provider
end
it "#delete_in_provider to fail (finding credential) and send notification" do
expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc)
allow(credentials).to receive(:find).and_raise(AnsibleTowerClient::ClientError)
expected_notify[:type] = :tower_op_failure
expect(Notification).to receive(:create).with(expected_notify)
expect { ansible_cred.delete_in_provider }.to raise_error(AnsibleTowerClient::ClientError)
end
it "#delete_in_provider_queue" do
task_id = ansible_cred.delete_in_provider_queue
expect(MiqTask.find(task_id)).to have_attributes(:name => "Deleting #{described_class::FRIENDLY_NAME} (Tower internal reference=#{ansible_cred.manager_ref})")
expect(MiqQueue.first).to have_attributes(
:instance_id => ansible_cred.id,
:args => [],
:class_name => described_class.name,
:method_name => "delete_in_provider",
:priority => MiqQueue::HIGH_PRIORITY,
:role => "ems_operations",
:zone => manager.my_zone
)
end
end
context "Update through API" do
let(:credentials) { double("AnsibleTowerClient::Collection", :find => credential) }
let(:credential) { double("AnsibleTowerClient::Credential", :id => 1) }
let(:ansible_cred) { described_class.create!(:resource => manager, :manager_ref => credential.id) }
let(:params) { {:userid => 'john', :miq_task_id => 1, :task_id => 1} }
let(:expected_params) { {:username => 'john', :kind => described_class::TOWER_KIND} }
let(:expected_notify) do
{
:type => :tower_op_success,
:options => {
:op_name => "#{described_class::FRIENDLY_NAME} update",
:op_arg => "()",
:tower => "EMS(manager_id=#{manager.id})"
}
}
end
it "#update_in_provider to succeed and send notification" do
expect(Vmdb::Settings).to receive(:decrypt_passwords!).with(params)
expected_params[:organization] = 1 if described_class.name.include?("::EmbeddedAnsible::")
expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc)
expect(credential).to receive(:update_attributes!).with(expected_params)
expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task.id])
expect(Notification).to receive(:create).with(expected_notify)
expect(ansible_cred.update_in_provider(params)).to be_a(described_class)
end
it "#update_in_provider to fail (doing update_attributes!) and send notification" do
expected_params[:organization] = 1 if described_class.name.include?("::EmbeddedAnsible::")
expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc)
expect(credential).to receive(:update_attributes!).with(expected_params).and_raise(AnsibleTowerClient::ClientError)
expected_notify[:type] = :tower_op_failure
expect(Notification).to receive(:create).with(expected_notify).and_return(double(Notification))
expect { ansible_cred.update_in_provider(params) }.to raise_error(AnsibleTowerClient::ClientError)
end
it "#update_in_provider_queue" do
expect(Vmdb::Settings).to receive(:encrypt_passwords!).with(params)
task_id = ansible_cred.update_in_provider_queue(params)
expect(MiqTask.find(task_id)).to have_attributes(:name => "Updating #{described_class::FRIENDLY_NAME} (Tower internal reference=#{ansible_cred.manager_ref})")
params[:task_id] = task_id
expect(MiqQueue.first).to have_attributes(
:instance_id => ansible_cred.id,
:args => [params],
:class_name => described_class.name,
:method_name => "update_in_provider",
:priority => MiqQueue::HIGH_PRIORITY,
:role => "ems_operations",
:zone => manager.my_zone
)
end
end
end