forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredential.rb
131 lines (116 loc) · 5.47 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
require 'ansible_tower_client'
shared_examples_for "ansible credential" do
let(:finished_task) { FactoryGirl.create(:miq_task, :state => "Finished") }
let(:atc) { double("AnsibleTowerClient::Connection", :api => api) }
let(:api) { double("AnsibleTowerClient::Api", :credentials => credentials) }
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
it ".create_in_provider" do
expected_params = {
:description => "Description",
:name => "My Credential",
:related => {},
:username => "john",
:kind => described_class::TOWER_KIND
}
expected_params[:organization_id] = 1 if described_class.name.include?("::EmbeddedAnsible::")
expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc)
store_new_credential(credential, manager)
expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task])
expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager)
expect(credentials).to receive(:create!).with(expected_params)
expect(described_class.create_in_provider(manager.id, params)).to be_a(described_class)
end
it "not found during refresh" do
expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc)
expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task])
expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager)
expect { described_class.create_in_provider(manager.id, params) }.to raise_error(ActiveRecord::RecordNotFound)
end
it ".create_in_provider_queue" do
task_id = described_class.create_in_provider_queue(manager.id, params)
expect(MiqTask.find(task_id)).to have_attributes(:name => "Creating #{described_class.name} with 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
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) }
it "#delete_in_provider" do
expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc)
expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task])
ansible_cred.delete_in_provider
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.name} with manager_ref=#{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", :update_attributes! => {}, :id => 1) }
let(:ansible_cred) { described_class.create!(:resource => manager, :manager_ref => credential.id) }
it "#update_in_provider" do
expected_params = {
:username => 'john',
:kind => described_class::TOWER_KIND
}
expected_params[:organization_id] = 1 if described_class.name.include?("::EmbeddedAnsible::")
expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc)
expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task])
expect(credential).to receive(:update_attributes!).with(expected_params)
expect(ansible_cred.update_in_provider('userid' => 'john')).to be_a(described_class)
end
it "#update_in_provider_queue" do
task_id = ansible_cred.update_in_provider_queue({})
expect(MiqTask.find(task_id)).to have_attributes(:name => "Updating #{described_class.name} with manager_ref=#{ansible_cred.manager_ref}")
expect(MiqQueue.first).to have_attributes(
:instance_id => ansible_cred.id,
:args => [{:task_id => task_id}],
:class_name => described_class.name,
:method_name => "update_in_provider",
:priority => MiqQueue::HIGH_PRIORITY,
:role => "ems_operations",
:zone => manager.my_zone
)
end
end
end