]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/keystone/spec/unit/provider/keystone_tenant/openstack_spec.rb
Update to Kilo
[dsa-puppet.git] / 3rdparty / modules / keystone / spec / unit / provider / keystone_tenant / openstack_spec.rb
1 require 'puppet'
2 require 'spec_helper'
3 require 'puppet/provider/keystone_tenant/openstack'
4
5 provider_class = Puppet::Type.type(:keystone_tenant).provider(:openstack)
6
7 class Puppet::Provider::Keystone
8   def self.reset
9     @admin_endpoint = nil
10     @tenant_hash    = nil
11     @admin_token    = nil
12     @keystone_file  = nil
13     @domain_id_to_name = nil
14     @default_domain_id = nil
15     @domain_hash = nil
16   end
17 end
18
19 describe provider_class do
20
21   after :each do
22     provider_class.reset
23   end
24
25   let(:tenant_attrs) do
26     {
27       :name         => 'foo',
28       :description  => 'foo',
29       :ensure       => 'present',
30       :enabled      => 'True',
31     }
32   end
33
34   let(:resource) do
35     Puppet::Type::Keystone_tenant.new(tenant_attrs)
36   end
37
38   let(:provider) do
39     provider_class.new(resource)
40   end
41
42   def before_hook(domainlist)
43     if domainlist
44       provider.class.expects(:openstack).once
45                     .with('domain', 'list', '--quiet', '--format', 'csv', [])
46                     .returns('"ID","Name","Enabled","Description"
47 "foo_domain_id","foo_domain",True,"foo domain"
48 "bar_domain_id","bar_domain",True,"bar domain"
49 "another_domain_id","another_domain",True,"another domain"
50 "disabled_domain_id","disabled_domain",False,"disabled domain"
51 "default","Default",True,"the default domain"
52 ')
53     end
54   end
55
56   before :each, :domainlist => true do
57     before_hook(true)
58   end
59
60   before :each, :domainlist => false do
61     before_hook(false)
62   end
63
64   shared_examples 'authenticated with environment variables' do
65     ENV['OS_USERNAME']     = 'test'
66     ENV['OS_PASSWORD']     = 'abc123'
67     ENV['OS_PROJECT_NAME'] = 'test'
68     ENV['OS_AUTH_URL']     = 'http://127.0.0.1:35357/v3'
69   end
70
71   describe 'when managing a tenant' do
72
73     it_behaves_like 'authenticated with environment variables' do
74       describe '#create', :domainlist => true do
75         it 'creates a tenant' do
76           provider.class.expects(:openstack)
77                         .with('project', 'create', '--format', 'shell', ['foo', '--enable', '--description', 'foo', '--domain', 'Default'])
78                         .returns('description="foo"
79 enabled="True"
80 name="foo"
81 id="foo"
82 domain_id="foo_domain_id"
83 ')
84           provider.create
85           expect(provider.exists?).to be_truthy
86         end
87       end
88
89       describe '#destroy', :domainlist => false do
90         it 'destroys a tenant' do
91           provider.instance_variable_get('@property_hash')[:id] = 'my-project-id'
92           provider.class.expects(:openstack)
93                         .with('project', 'delete', 'my-project-id')
94           provider.destroy
95           expect(provider.exists?).to be_falsey
96         end
97       end
98
99       context 'when tenant does not exist', :domainlist => false do
100         subject(:response) do
101           response = provider.exists?
102         end
103
104         it { expect(response).to be_falsey }
105       end
106
107       describe '#instances', :domainlist => true do
108         it 'finds every tenant' do
109           provider.class.expects(:openstack)
110                         .with('project', 'list', '--quiet', '--format', 'csv', '--long')
111                        .returns('"ID","Name","Domain ID","Description","Enabled"
112 "1cb05cfed7c24279be884ba4f6520262","foo","foo_domain_id","foo",True
113 "2cb05cfed7c24279be884ba4f6520262","foo","bar_domain_id","foo",True
114 ')
115           instances = provider.class.instances
116           expect(instances[0].name).to eq('foo')
117           expect(instances[0].domain).to eq('bar_domain')
118           expect(instances[1].name).to eq('foo::foo_domain')
119         end
120       end
121     end
122
123     describe 'v3 domains with no domain in resource', :domainlist => true do
124
125       let(:tenant_attrs) do
126         {
127           :name         => 'foo',
128           :description  => 'foo',
129           :ensure       => 'present',
130           :enabled      => 'True'
131         }
132       end
133
134       it 'adds default domain to commands' do
135         mock = {
136           'identity' => {'default_domain_id' => 'foo_domain_id'}
137         }
138         Puppet::Util::IniConfig::File.expects(:new).returns(mock)
139         File.expects(:exists?).with('/etc/keystone/keystone.conf').returns(true)
140         mock.expects(:read).with('/etc/keystone/keystone.conf')
141         provider.class.expects(:openstack)
142           .with('project', 'create', '--format', 'shell', ['foo', '--enable', '--description', 'foo', '--domain', 'foo_domain'])
143           .returns('description="foo"
144 enabled="True"
145 name="foo"
146 id="project-id"
147 domain_id="foo_domain_id"
148 ')
149         provider.create
150         expect(provider.exists?).to be_truthy
151         expect(provider.id).to eq("project-id")
152       end
153
154     end
155
156     describe 'v3 domains with domain in resource', :domainlist => false do
157
158       let(:tenant_attrs) do
159         {
160           :name         => 'foo',
161           :description  => 'foo',
162           :ensure       => 'present',
163           :enabled      => 'True',
164           :domain       => 'foo_domain'
165         }
166       end
167
168       it 'uses given domain in commands' do
169         provider.class.expects(:openstack)
170           .with('project', 'create', '--format', 'shell', ['foo', '--enable', '--description', 'foo', '--domain', 'foo_domain'])
171           .returns('description="foo"
172 enabled="True"
173 name="foo"
174 id="project-id"
175 domain_id="foo_domain_id"
176 ')
177         provider.create
178         expect(provider.exists?).to be_truthy
179         expect(provider.id).to eq("project-id")
180       end
181     end
182
183     describe 'v3 domains with domain in name/title', :domainlist => false do
184
185       let(:tenant_attrs) do
186         {
187           :name         => 'foo::foo_domain',
188           :description  => 'foo',
189           :ensure       => 'present',
190           :enabled      => 'True'
191         }
192       end
193
194       it 'uses given domain in commands' do
195         provider.class.expects(:openstack)
196           .with('project', 'create', '--format', 'shell', ['foo', '--enable', '--description', 'foo', '--domain', 'foo_domain'])
197           .returns('description="foo"
198 enabled="True"
199 name="foo"
200 id="project-id"
201 domain_id="foo_domain_id"
202 ')
203         provider.create
204         expect(provider.exists?).to be_truthy
205         expect(provider.id).to eq("project-id")
206       end
207     end
208
209     describe 'v3 domains with domain in name/title and in resource', :domainlist => false do
210
211       let(:tenant_attrs) do
212         {
213           :name         => 'foo::bar_domain',
214           :description  => 'foo',
215           :ensure       => 'present',
216           :enabled      => 'True',
217           :domain       => 'foo_domain'
218         }
219       end
220
221       it 'uses given domain in commands' do
222         provider.class.expects(:openstack)
223           .with('project', 'create', '--format', 'shell', ['foo', '--enable', '--description', 'foo', '--domain', 'foo_domain'])
224           .returns('description="foo"
225 enabled="True"
226 name="foo"
227 id="project-id"
228 domain_id="foo_domain_id"
229 ')
230         provider.create
231         expect(provider.exists?).to be_truthy
232         expect(provider.id).to eq("project-id")
233       end
234     end
235   end
236 end