File: credentials_spec.rb

package info (click to toggle)
puppet-module-openstacklib 25.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 968 kB
  • sloc: ruby: 4,500; python: 38; sh: 22; makefile: 10
file content (241 lines) | stat: -rw-r--r-- 8,025 bytes parent folder | download | duplicates (2)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
require 'puppet'
require 'spec_helper'
require 'puppet/provider/openstack'
require 'puppet/provider/openstack/credentials'


describe Puppet::Provider::Openstack::Credentials do

  let(:creds) do
    creds = Puppet::Provider::Openstack::CredentialsV3.new
  end

  describe "#set with valid value" do
    it 'works with valid value' do
      expect(creds.class.defined?('auth_url')).to be_truthy
      creds.set('auth_url', 'http://localhost:5000/v2.0')
      expect(creds.auth_url).to eq('http://localhost:5000/v2.0')
    end
  end

  describe "#set with invalid value" do
    it 'works with invalid value' do
      expect(creds.class.defined?('foo')).to be_falsey
      creds.set('foo', 'junk')
      expect(creds.respond_to?(:foo)).to be_falsey
      expect(creds.instance_variable_defined?(:@foo)).to be_falsey
      expect { creds.foo }.to raise_error(NoMethodError, /undefined method/)
    end
  end

  describe '#service_token_set?' do
    context "with service credentials" do
      it 'is successful' do
        creds.token = 'token'
        creds.endpoint = 'endpoint'
        expect(creds.service_token_set?).to be_truthy
        expect(creds.user_password_set?).to be_falsey
      end

      it 'fails' do
        creds.token = 'token'
        expect(creds.service_token_set?).to be_falsey
        expect(creds.user_password_set?).to be_falsey
      end
    end
  end

  describe '#password_set?' do
    context "with user credentials" do
      it 'is successful with project scope credential' do
        creds.auth_url = 'auth_url'
        creds.password = 'password'
        creds.project_name = 'project_name'
        creds.username = 'username'
        expect(creds.user_password_set?).to be_truthy
        expect(creds.service_token_set?).to be_falsey
      end

      it 'is successful with project scope credential' do
        creds.auth_url = 'auth_url'
        creds.password = 'password'
        creds.domain_name = 'domain_name'
        creds.username = 'username'
        expect(creds.user_password_set?).to be_truthy
        expect(creds.service_token_set?).to be_falsey
      end

      it 'is successful with system scope credential' do
        creds.auth_url = 'auth_url'
        creds.password = 'password'
        creds.system_scope = 'all'
        creds.username = 'username'
        expect(creds.user_password_set?).to be_truthy
        expect(creds.service_token_set?).to be_falsey
      end

      it 'is successful with cloud' do
        creds.cloud = 'openstack'
        expect(creds.user_password_set?).to be_truthy
        expect(creds.service_token_set?).to be_falsey
      end

      it 'fails' do
        creds.auth_url = 'auth_url'
        creds.password = 'password'
        creds.project_name = 'project_name'
        expect(creds.user_password_set?).to be_falsey
        expect(creds.service_token_set?).to be_falsey
      end
    end
  end

  describe '#set?' do
    context "without any credential" do
      it 'fails' do
        expect(creds.set?).to be_falsey
      end
    end
  end

  describe '#version' do
    it 'is version 3' do
      expect(creds.version).to eq('3')
    end
  end

  describe '#unset' do
    context "with all instance variables set" do
      it 'resets all but the identity_api_version' do
        creds.auth_url = 'auth_url'
        creds.password = 'password'
        creds.project_name = 'project_name'
        creds.domain_name = 'domain_name'
        creds.system_scope = 'system_scope'
        creds.username = 'username'
        creds.token = 'token'
        creds.endpoint = 'endpoint'
        creds.region_name = 'region_name'
        creds.identity_api_version = 'identity_api_version'
        creds.cloud = 'openstack'
        creds.client_config_file = '/etc/openstack/clouds.yaml'
        creds.unset
        expect(creds.auth_url).to eq(nil)
        expect(creds.password).to eq(nil)
        expect(creds.project_name).to eq(nil)
        expect(creds.domain_name).to eq(nil)
        expect(creds.system_scope).to eq(nil)
        expect(creds.username).to eq(nil)
        expect(creds.token).to eq(nil)
        expect(creds.endpoint).to eq(nil)
        expect(creds.region_name).to eq(nil)
        expect(creds.identity_api_version).to eq('identity_api_version')
        expect(creds.cloud).to eq(nil)
        expect(creds.client_config_file).to eq(nil)
        newcreds = Puppet::Provider::Openstack::CredentialsV3.new
        expect(newcreds.identity_api_version).to eq('3')
      end
    end
  end

  describe '#to_env' do
    context "with an exhaustive data set" do
      it 'successfully returns content' do
        creds.auth_url = 'auth_url'
        creds.password = 'password'
        creds.project_name = 'project_name'
        creds.domain_name = 'domain_name'
        creds.system_scope = 'all'
        creds.username = 'username'
        creds.token = 'token'
        creds.endpoint = 'endpoint'
        creds.region_name = 'Region1'
        creds.identity_api_version = 'identity_api_version'
        creds.cloud = 'openstack'
        creds.client_config_file = '/etc/openstack/clouds.yaml'
        expect(creds.to_env).to eq({
          'OS_USERNAME'             => 'username',
          'OS_PASSWORD'             => 'password',
          'OS_PROJECT_NAME'         => 'project_name',
          'OS_DOMAIN_NAME'          => 'domain_name',
          'OS_SYSTEM_SCOPE'         => 'all',
          'OS_AUTH_URL'             => 'auth_url',
          'OS_TOKEN'                => 'token',
          'OS_ENDPOINT'             => 'endpoint',
          'OS_REGION_NAME'          => 'Region1',
          'OS_IDENTITY_API_VERSION' => 'identity_api_version',
          'OS_CLOUD'                => 'openstack',
          'OS_CLIENT_CONFIG_FILE'   => '/etc/openstack/clouds.yaml',
        })
      end
    end
  end

  describe 'using v3' do
    let(:creds) do
      creds = Puppet::Provider::Openstack::CredentialsV3.new
    end
    describe 'with v3' do
      it 'uses v3 identity api' do
        creds.identity_api_version == '3'
      end
    end
    describe '#password_set? with username and project_name' do
      it 'is successful' do
        creds.auth_url = 'auth_url'
        creds.password = 'password'
        creds.project_name = 'project_name'
        creds.username = 'username'
        expect(creds.user_password_set?).to be_truthy
        expect(creds.scope).to eq('project')
      end
    end
    describe '#password_set? with username and domain_name' do
      it 'is successful' do
        creds.auth_url = 'auth_url'
        creds.password = 'password'
        creds.domain_name = 'domain_name'
        creds.username = 'username'
        expect(creds.user_password_set?).to be_truthy
        expect(creds.scope).to eq('domain')
      end
    end
    describe '#password_set? with username and system_scope' do
      it 'is successful' do
        creds.auth_url = 'auth_url'
        creds.password = 'password'
        creds.system_scope = 'all'
        creds.username = 'username'
        expect(creds.user_password_set?).to be_truthy
        expect(creds.scope).to eq('system')
      end
    end
    describe '#password_set? with cloud' do
      it 'is successful' do
        creds.cloud = 'openstack'
        expect(creds.user_password_set?).to be_truthy
        expect(creds.scope).to eq(nil)
      end
    end
    describe '#password_set? with user_id and project_id' do
      it 'is successful' do
        creds.auth_url = 'auth_url'
        creds.password = 'password'
        creds.project_id = 'projid'
        creds.user_id = 'userid'
        expect(creds.user_password_set?).to be_truthy
        expect(creds.scope).to eq('project')
      end
    end
    describe '#password_set? with user_id and domain_id' do
      it 'is successful' do
        creds.auth_url = 'auth_url'
        creds.password = 'password'
        creds.domain_id = 'domid'
        creds.user_id = 'userid'
        expect(creds.user_password_set?).to be_truthy
        expect(creds.scope).to eq('domain')
      end
    end
  end
end