]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/keystone/spec/unit/provider/keystone_user/openstack_spec.rb
1a74c636cd2e868f12f6c1732aa49d94642e10fb
[dsa-puppet.git] / 3rdparty / modules / keystone / spec / unit / provider / keystone_user / openstack_spec.rb
1 require 'puppet'
2 require 'spec_helper'
3 require 'puppet/provider/keystone_user/openstack'
4
5 provider_class = Puppet::Type.type(:keystone_user).provider(:openstack)
6
7 describe provider_class do
8
9   let(:user_attrs) do
10     {
11       :name         => 'foo',
12       :ensure       => 'present',
13       :enabled      => 'True',
14       :password     => 'foo',
15       :tenant       => 'foo',
16       :email        => 'foo@example.com',
17       :auth         => {
18         'username'    => 'test',
19         'password'    => 'abc123',
20         'tenant_name' => 'foo',
21         'auth_url'    => 'http://127.0.0.1:5000/v2.0',
22       }
23     }
24   end
25
26   let(:resource) do
27     Puppet::Type::Keystone_user.new(user_attrs)
28   end
29
30   let(:provider) do
31     provider_class.new(resource)
32   end
33
34   describe 'when updating a user' do
35
36     describe '#create' do
37       it 'creates a user' do
38         provider.class.stubs(:openstack)
39                       .with('user', 'list', '--quiet', '--format', 'csv', [['--long', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
40                       .returns('"ID","Name","Project","Email","Enabled"
41 "1cb05cfed7c24279be884ba4f6520262","foo","foo","foo@example.com",True
42 ')
43         provider.class.stubs(:openstack)
44                       .with('user', 'create', '--format', 'shell', [['foo', '--enable', '--password', 'foo', '--project', 'foo', '--email', 'foo@example.com', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
45                       .returns('email="foo@example.com"
46 enabled="True"
47 id="12b23f07d4a3448d8189521ab09610b0"
48 name="foo"
49 project_id="5e2001b2248540f191ff22627dc0c2d7"
50 username="foo"
51 ')
52         provider.create
53         expect(provider.exists?).to be_truthy
54       end
55     end
56
57     describe '#destroy' do
58       it 'destroys a user' do
59         provider.class.stubs(:openstack)
60                       .with('user', 'list', '--quiet', '--format', 'csv', [['--long', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
61                       .returns('"ID","Name","Project","Email","Enabled"')
62         provider.class.stubs(:openstack)
63                       .with('user', 'delete', [['foo', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
64         provider.destroy
65         expect(provider.exists?).to be_falsey
66       end
67
68     end
69
70     describe '#exists' do
71       context 'when user exists' do
72
73         subject(:response) do
74           provider.class.stubs(:openstack)
75                         .with('user', 'list', '--quiet', '--format', 'csv', [['--long', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
76                         .returns('"ID","Name","Project","Email","Enabled"
77 "1cb05cfed7c24279be884ba4f6520262","foo","foo","foo@example.com",True
78 ')
79           response = provider.exists?
80         end
81
82         it { is_expected.to be_truthy }
83       end
84
85       context 'when user does not exist' do
86
87         subject(:response) do
88           provider.class.stubs(:openstack)
89                         .with('user', 'list', '--quiet', '--format', 'csv', [['--long', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
90                         .returns('"ID","Name","Project","Email","Enabled"')
91           response = provider.exists?
92         end
93
94         it { is_expected.to be_falsey }
95       end
96     end
97
98     describe '#instances' do
99       it 'finds every user' do
100         provider.class.stubs(:openstack)
101                       .with('user', 'list', '--quiet', '--format', 'csv', [['--long', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
102                       .returns('"ID","Name","Project","Email","Enabled"
103 "1cb05cfed7c24279be884ba4f6520262","foo","foo","foo@example.com",True
104 ')
105         instances = provider.instances
106         expect(instances.count).to eq(1)
107       end
108     end
109
110     describe '#tenant' do
111       it 'gets the tenant with default backend' do
112         provider.class.stubs(:openstack)
113                       .with('user', 'list', '--quiet', '--format', 'csv', [['--long', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
114                       .returns('"ID","Name","Project","Email","Enabled"
115 "1cb05cfed7c24279be884ba4f6520262","foo","foo","foo@example.com",True
116 ')
117         tenant = provider.tenant
118         expect(tenant).to eq('foo')
119       end
120       it 'gets the tenant with LDAP backend' do
121         provider.class.stubs(:openstack)
122                       .with('user', 'list', '--quiet', '--format', 'csv', [['--long', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
123                       .returns('"ID","Name","Project","Email","Enabled"
124 "1cb05cfed7c24279be884ba4f6520262","foo","","foo@example.com",True
125 ')
126         provider.class.expects(:openstack)
127                       .with('user role', 'list', '--quiet', '--format', 'csv', [['foo', '--project', 'foo', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
128                       .returns('"ID","Name","Project","User"
129 "1cb05cfed7c24279be884ba4f6520262","foo","foo","foo"
130 ')
131         tenant = provider.tenant
132         expect(tenant).to eq('foo')
133       end
134     end
135     describe '#tenant=' do
136       context 'when using default backend' do
137         it 'sets the tenant' do
138           provider.class.expects(:openstack)
139                         .with('user', 'set', [['foo', '--project', 'bar', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
140            provider.class.expects(:openstack)
141                          .with('user role', 'list', '--quiet', '--format', 'csv', [['foo', '--project', 'bar', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
142                          .returns('"ID","Name","Project","User"
143 "1cb05cfed7c24279be884ba4f6520262","foo","foo","foo"
144 ')
145           provider.tenant=('bar')
146         end
147       end
148       context 'when using LDAP read-write backend' do
149         it 'sets the tenant when _member_ role exists' do
150           provider.class.expects(:openstack)
151                         .with('user', 'set', [['foo', '--project', 'bar', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
152            provider.class.expects(:openstack)
153                          .with('user role', 'list', '--quiet', '--format', 'csv', [['foo', '--project', 'bar', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
154                          .returns('')
155           provider.class.expects(:openstack)
156                         .with('role', 'show', '--format', 'shell', [['_member_', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
157                         .returns('name="_member_"')
158           provider.class.expects(:openstack)
159                         .with('role', 'add', [['_member_', '--project', 'bar', '--user', 'foo', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
160           provider.tenant=('bar')
161         end
162         it 'sets the tenant when _member_ role does not exist' do
163           provider.class.expects(:openstack)
164                         .with('user', 'set', [['foo', '--project', 'bar', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
165            provider.class.expects(:openstack)
166                          .with('user role', 'list', '--quiet', '--format', 'csv', [['foo', '--project', 'bar', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
167                          .returns('')
168           provider.class.expects(:openstack)
169                         .with('role', 'show', '--format', 'shell', [['_member_', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
170                         .raises(Puppet::ExecutionFailure, 'no such role _member_')
171           provider.class.expects(:openstack)
172                         .with('role', 'create', '--format', 'shell', [['_member_', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])                      
173                         .returns('name="_member_"')
174           provider.class.expects(:openstack)
175                         .with('role', 'add', [['_member_', '--project', 'bar', '--user', 'foo', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
176           provider.tenant=('bar')
177         end
178       end
179       context 'when using LDAP read-only backend' do
180         it 'sets the tenant when _member_ role exists' do
181           provider.class.expects(:openstack)
182                         .with('user', 'set', [['foo', '--project', 'bar', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
183                         .raises(Puppet::ExecutionFailure, 'You are not authorized to perform the requested action: LDAP user update')
184            provider.class.expects(:openstack)
185                          .with('user role', 'list', '--quiet', '--format', 'csv', [['foo', '--project', 'bar', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
186                          .returns('')
187           provider.class.expects(:openstack)
188                         .with('role', 'show', '--format', 'shell', [['_member_', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
189                         .returns('name="_member_"')
190           provider.class.expects(:openstack)
191                         .with('role', 'add', [['_member_', '--project', 'bar', '--user', 'foo', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
192           provider.tenant=('bar')
193         end
194         it 'sets the tenant and gets an unexpected exception message' do
195           provider.class.expects(:openstack)
196                         .with('user', 'set', [['foo', '--project', 'bar', '--os-username', 'test', '--os-password', 'abc123', '--os-tenant-name', 'foo', '--os-auth-url', 'http://127.0.0.1:5000/v2.0']])
197                         .raises(Puppet::ExecutionFailure, 'unknown error message')
198           expect{ provider.tenant=('bar') }.to raise_error(Puppet::ExecutionFailure, /unknown error message/)
199         end
200       end
201     end
202
203   end
204
205   describe "#password" do
206     let(:user_attrs) do
207       {
208         :name         => 'foo',
209         :ensure       => 'present',
210         :enabled      => 'True',
211         :password     => 'foo',
212         :tenant       => 'foo',
213         :email        => 'foo@example.com',
214         :auth         => {
215           'username'    => 'test',
216           'password'    => 'abc123',
217           'tenant_name' => 'foo',
218           'auth_url'    => 'https://127.0.0.1:5000/v2.0',
219         }
220       }
221     end
222
223     it 'checks the password with HTTPS' do
224       httpobj = mock('Net::HTTP')
225       httpobj.stubs(:use_ssl=).with(true)
226       httpobj.stubs(:verify_mode=)
227       Net::HTTP.stubs(:start).returns(httpobj)
228       reqobj = mock('Net::HTTP::Post')
229       reqobj.stubs(:body=)
230       reqobj.stubs(:content_type=)
231       Net::HTTP::Post.stubs(:start).returns(reqobj)
232       respobj = mock('Net::HTTPResponse')
233       respobj.stubs(:code).returns('200')
234       httpobj.stubs(:request).returns(respobj)
235       password = provider.password
236       expect(password).to eq('foo')
237     end
238     it 'fails the password check with HTTPS' do
239       httpobj = mock('Net::HTTP')
240       httpobj.stubs(:use_ssl=).with(true)
241       httpobj.stubs(:verify_mode=)
242       Net::HTTP.stubs(:start).returns(httpobj)
243       reqobj = mock('Net::HTTP::Post')
244       reqobj.stubs(:body=)
245       reqobj.stubs(:content_type=)
246       Net::HTTP::Post.stubs(:start).returns(reqobj)
247       respobj = mock('Net::HTTPResponse')
248       respobj.stubs(:code).returns('401')
249       httpobj.stubs(:request).returns(respobj)
250       password = provider.password
251       expect(password).to eq(nil)
252     end
253
254     describe 'when updating a user with unmanaged password' do
255
256       let(:user_attrs) do
257         {
258           :name             => 'foo',
259           :ensure           => 'present',
260           :enabled          => 'True',
261           :password         => 'foo',
262           :replace_password => 'False',
263           :tenant           => 'foo',
264           :email            => 'foo@example.com',
265           :auth             => {
266             'username'      => 'test',
267             'password'      => 'abc123',
268             'tenant_name'   => 'foo',
269             'auth_url'      => 'http://127.0.0.1:5000/v2.0',
270           }
271         }
272       end
273
274       let(:resource) do
275         Puppet::Type::Keystone_user.new(user_attrs)
276       end
277
278       let :provider do
279         provider_class.new(resource)
280       end
281
282       it 'should not try to check password' do
283         expect(provider.password).to eq('foo')
284       end
285     end
286
287   end
288 end