]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/keystone/spec/unit/provider/keystone_spec.rb
Update to Kilo
[dsa-puppet.git] / 3rdparty / modules / keystone / spec / unit / provider / keystone_spec.rb
1 require 'puppet'
2 require 'spec_helper'
3 require 'puppet/provider/keystone'
4 require 'tempfile'
5
6 klass = Puppet::Provider::Keystone
7
8 class Puppet::Provider::Keystone
9   @credentials = Puppet::Provider::Openstack::CredentialsV3.new
10
11   def self.reset
12     @admin_endpoint = nil
13     @tenant_hash    = nil
14     @admin_token    = nil
15     @keystone_file  = nil
16     @domain_id_to_name = nil
17     @default_domain_id = nil
18     @domain_hash = nil
19   end
20 end
21
22 describe Puppet::Provider::Keystone do
23
24   after :each do
25     klass.reset
26   end
27
28   describe 'when retrieving the security token' do
29     it 'should return nothing if there is no keystone config file' do
30       File.expects(:exists?).with("/etc/keystone/keystone.conf").returns(false)
31       expect(klass.get_admin_token).to be_nil
32     end
33
34     it 'should return nothing if the keystone config file does not have a DEFAULT section' do
35       mock = {}
36       File.expects(:exists?).with("/etc/keystone/keystone.conf").returns(true)
37       Puppet::Util::IniConfig::File.expects(:new).returns(mock)
38       mock.expects(:read).with('/etc/keystone/keystone.conf')
39       expect(klass.get_admin_token).to be_nil
40     end
41
42     it 'should fail if the keystone config file does not contain an admin token' do
43       mock = {'DEFAULT' => {'not_a_token' => 'foo'}}
44       File.expects(:exists?).with("/etc/keystone/keystone.conf").returns(true)
45       Puppet::Util::IniConfig::File.expects(:new).returns(mock)
46       mock.expects(:read).with('/etc/keystone/keystone.conf')
47       expect(klass.get_admin_token).to be_nil
48     end
49
50     it 'should parse the admin token if it is in the config file' do
51       mock = {'DEFAULT' => {'admin_token' => 'foo'}}
52       File.expects(:exists?).with("/etc/keystone/keystone.conf").returns(true)
53       Puppet::Util::IniConfig::File.expects(:new).returns(mock)
54       mock.expects(:read).with('/etc/keystone/keystone.conf')
55       expect(klass.get_admin_token).to eq('foo')
56     end
57
58     it 'should use the specified bind_host in the admin endpoint' do
59       mock = {'DEFAULT' => {'admin_bind_host' => '192.168.56.210', 'admin_port' => '35357' }}
60       File.expects(:exists?).with("/etc/keystone/keystone.conf").returns(true)
61       Puppet::Util::IniConfig::File.expects(:new).returns(mock)
62       mock.expects(:read).with('/etc/keystone/keystone.conf')
63       expect(klass.get_admin_endpoint).to eq('http://192.168.56.210:35357/v3/')
64     end
65
66     it 'should use localhost in the admin endpoint if bind_host is 0.0.0.0' do
67       mock = {'DEFAULT' => { 'admin_bind_host' => '0.0.0.0', 'admin_port' => '35357' }}
68       File.expects(:exists?).with("/etc/keystone/keystone.conf").returns(true)
69       Puppet::Util::IniConfig::File.expects(:new).returns(mock)
70       mock.expects(:read).with('/etc/keystone/keystone.conf')
71       expect(klass.get_admin_endpoint).to eq('http://127.0.0.1:35357/v3/')
72     end
73
74     it 'should use [::1] in the admin endpoint if bind_host is ::0' do
75       mock = {'DEFAULT' => { 'admin_bind_host' => '::0', 'admin_port' => '35357' }}
76       File.expects(:exists?).with("/etc/keystone/keystone.conf").returns(true)
77       Puppet::Util::IniConfig::File.expects(:new).returns(mock)
78       mock.expects(:read).with('/etc/keystone/keystone.conf')
79       expect(klass.get_admin_endpoint).to eq('http://[::1]:35357/v3/')
80     end
81
82     it 'should use localhost in the admin endpoint if bind_host is unspecified' do
83       mock = {'DEFAULT' => { 'admin_port' => '35357' }}
84       File.expects(:exists?).with("/etc/keystone/keystone.conf").returns(true)
85       Puppet::Util::IniConfig::File.expects(:new).returns(mock)
86       mock.expects(:read).with('/etc/keystone/keystone.conf')
87       expect(klass.get_admin_endpoint).to eq('http://127.0.0.1:35357/v3/')
88     end
89
90     it 'should use https if ssl is enabled' do
91       mock = {'DEFAULT' => {'admin_bind_host' => '192.168.56.210', 'admin_port' => '35357' }, 'ssl' => {'enable' => 'True'}}
92       File.expects(:exists?).with("/etc/keystone/keystone.conf").returns(true)
93       Puppet::Util::IniConfig::File.expects(:new).returns(mock)
94       mock.expects(:read).with('/etc/keystone/keystone.conf')
95       expect(klass.get_admin_endpoint).to eq('https://192.168.56.210:35357/v3/')
96     end
97
98     it 'should use http if ssl is disabled' do
99       mock = {'DEFAULT' => {'admin_bind_host' => '192.168.56.210', 'admin_port' => '35357' }, 'ssl' => {'enable' => 'False'}}
100       File.expects(:exists?).with("/etc/keystone/keystone.conf").returns(true)
101       Puppet::Util::IniConfig::File.expects(:new).returns(mock)
102       mock.expects(:read).with('/etc/keystone/keystone.conf')
103       expect(klass.get_admin_endpoint).to eq('http://192.168.56.210:35357/v3/')
104     end
105
106     it 'should use the defined admin_endpoint if available' do
107       mock = {'DEFAULT' => {'admin_endpoint' => 'https://keystone.example.com' }, 'ssl' => {'enable' => 'False'}}
108       File.expects(:exists?).with("/etc/keystone/keystone.conf").returns(true)
109       Puppet::Util::IniConfig::File.expects(:new).returns(mock)
110       mock.expects(:read).with('/etc/keystone/keystone.conf')
111       expect(klass.get_admin_endpoint).to eq('https://keystone.example.com/v3/')
112     end
113
114     it 'should handle an admin_endpoint with a trailing slash' do
115       mock = {'DEFAULT' => {'admin_endpoint' => 'https://keystone.example.com/' }, 'ssl' => {'enable' => 'False'}}
116       File.expects(:exists?).with("/etc/keystone/keystone.conf").returns(true)
117       Puppet::Util::IniConfig::File.expects(:new).returns(mock)
118       mock.expects(:read).with('/etc/keystone/keystone.conf')
119       expect(klass.get_admin_endpoint).to eq('https://keystone.example.com/v3/')
120     end
121
122   end
123
124   describe 'when using domains' do
125     it 'name_and_domain should return the resource domain' do
126       expect(klass.name_and_domain('foo::in_name', 'from_resource', 'default')).to eq(['foo', 'from_resource'])
127     end
128     it 'name_and_domain should return the default domain' do
129       expect(klass.name_and_domain('foo', nil, 'default')).to eq(['foo', 'default'])
130     end
131     it 'name_and_domain should return the domain part of the name' do
132       expect(klass.name_and_domain('foo::in_name', nil, 'default')).to eq(['foo', 'in_name'])
133     end
134     it 'should return the default domain name using the default_domain_id from keystone.conf' do
135       ENV['OS_USERNAME']     = 'test'
136       ENV['OS_PASSWORD']     = 'abc123'
137       ENV['OS_PROJECT_NAME'] = 'test'
138       ENV['OS_AUTH_URL']     = 'http://127.0.0.1:35357/v3'
139       mock = {
140         'DEFAULT' => {
141           'admin_endpoint' => 'http://127.0.0.1:35357',
142           'admin_token'    => 'admin_token'
143         },
144         'identity' => {'default_domain_id' => 'somename'}
145       }
146       File.expects(:exists?).with('/etc/keystone/keystone.conf').returns(true)
147       Puppet::Util::IniConfig::File.expects(:new).returns(mock)
148       mock.expects(:read).with('/etc/keystone/keystone.conf')
149       klass.expects(:openstack)
150            .with('domain', 'list', '--quiet', '--format', 'csv', [])
151            .returns('"ID","Name","Enabled","Description"
152 "somename","SomeName",True,"default domain"
153 ')
154       expect(klass.name_and_domain('foo')).to eq(['foo', 'SomeName'])
155     end
156     it 'should return Default if default_domain_id is not configured' do
157       ENV['OS_USERNAME']     = 'test'
158       ENV['OS_PASSWORD']     = 'abc123'
159       ENV['OS_PROJECT_NAME'] = 'test'
160       ENV['OS_AUTH_URL']     = 'http://127.0.0.1:35357/v3'
161       mock = {}
162       Puppet::Util::IniConfig::File.expects(:new).returns(mock)
163       File.expects(:exists?).with('/etc/keystone/keystone.conf').returns(true)
164       mock.expects(:read).with('/etc/keystone/keystone.conf')
165       klass.expects(:openstack)
166            .with('domain', 'list', '--quiet', '--format', 'csv', [])
167            .returns('"ID","Name","Enabled","Description"
168 "default","Default",True,"default domain"
169 ')
170       expect(klass.name_and_domain('foo')).to eq(['foo', 'Default'])
171     end
172   end
173 end