Debian Codegraph @

config_tempest/tests/test_credentials.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Red Hat, Inc.
4 # All Rights Reserved.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17
18 from unittest import mock
19
20 from fixtures import MonkeyPatch
21
22 from config_tempest.tests.base import BaseConfigTempestTest
23
24
25 class TestCredentials(BaseConfigTempestTest):
26
27 def setUp(self):
28 super(TestCredentials, self).setUp()
29 self.conf = self._get_conf("v2.0", "v3")
30 self.creds = self._get_creds(self.conf)
31 self.creds_v2 = self._get_creds(self.conf, v2=True)
32
33 def test_get_credential(self):
34 # set conf containing the newer values (admin creds in auth section)
35 self.creds._conf = self._get_alt_conf("v2.0", "v3")
36 resp = self.creds.get_credential("username")
37 self.assertEqual(resp, "demo")
38 # set admin credentials
39 self.creds.admin = True
40 resp = self.creds.get_credential("username")
41 self.assertEqual(resp, "admin")
42
43 def test_get_identity_version_v2(self):
44 resp = self.creds_v2._get_identity_version()
45 self.assertEqual(resp, 'v2')
46
47 def test_get_identity_version_v3(self):
48 conf = self._get_conf("v3", "v3") # uri has to be v3
49 creds = self._get_creds(conf)
50 resp = creds._get_identity_version()
51 self.assertEqual(resp, 'v3')
52
53 def test_get_creds_kwargs(self):
54 expected_resp = {
55 'username': 'demo',
56 'password': 'secret',
57 'project_name': 'demo'
58 }
59 self.assertEqual(self.creds_v2._get_creds_kwargs(), expected_resp)
60 self.creds_v2.identity_version = 'v3'
61 expected_resp = {
62 'username': 'demo',
63 'password': 'secret',
64 'project_name': 'demo',
65 'domain_name': 'Default',
66 'user_domain_name': 'Default',
67 'project_domain_name': 'Default',
68 }
69 self.assertEqual(self.creds._get_creds_kwargs(), expected_resp)
70
71 def test_set_credentials_v2(self):
72 mock_function = mock.Mock()
73 function2mock = 'config_tempest.credentials.auth.get_credentials'
74 self.useFixture(MonkeyPatch(function2mock, mock_function))
75 self.creds_v2.username = "name"
76 self.creds_v2.password = "pass"
77 self.creds_v2.project_name = "Tname"
78 self.creds_v2.set_credentials()
79 mock_function.assert_called_with(
80 auth_url=None, fill_in=False, identity_version='v2',
81 disable_ssl_certificate_validation='true',
82 ca_certs=None, password='pass', project_name='Tname',
83 username='name')
84
85 def test_set_credentials_v3(self):
86 mock_function = mock.Mock()
87 function2mock = 'config_tempest.credentials.auth.get_credentials'
88 self.useFixture(MonkeyPatch(function2mock, mock_function))
89 self.creds.username = "name"
90 self.creds.password = "pass"
91 self.creds.project_name = "project_name"
92 self.creds.identity_version = "v3"
93 self.creds.set_credentials()
94 mock_function.assert_called_with(
95 auth_url=None, fill_in=False, identity_version='v3',
96 disable_ssl_certificate_validation='true',
97 ca_certs=None, password='pass',
98 username='name',
99 project_name='project_name',
100 domain_name='Default',
101 user_domain_name='Default',
102 project_domain_name='Default')
103
104 def test_get_auth_provider_keystone_v2(self):
105 # check if method returns correct method - KeystoneV2AuthProvider
106 mock_function = mock.Mock()
107 # mock V2Provider, if other provider is called, it fails
108 func2mock = 'config_tempest.credentials.auth.KeystoneV2AuthProvider'
109 self.useFixture(MonkeyPatch(func2mock, mock_function))
110 resp = self.creds_v2.get_auth_provider()
111 self.assertEqual(resp, mock_function())
112 # check parameters of returned function
113 self.creds_v2.get_auth_provider()
114 mock_function.assert_called_with(self.creds_v2.tempest_creds,
115 'http://172.16.52.151:5000/v2.0',
116 'true', None)
117
118 def test_get_auth_provider_keystone_v3(self):
119 # check if method returns KeystoneV3AuthProvider
120 # make isinstance return True
121 mockIsInstance = mock.Mock(return_value=True)
122 self.useFixture(MonkeyPatch('config_tempest.credentials.isinstance',
123 mockIsInstance))
124 mock_function = mock.Mock()
125 # mock V3Provider, if other provider is called, it fails
126 func2mock = 'config_tempest.credentials.auth.KeystoneV3AuthProvider'
127 self.useFixture(MonkeyPatch(func2mock, mock_function))
128 resp = self.creds.get_auth_provider()
129 self.assertEqual(resp, mock_function())
130 # check parameters of returned function
131 self.creds.get_auth_provider()
132 mock_function.assert_called_with(self.creds.tempest_creds,
133 'http://172.16.52.151:5000/v3',
134 'true', None)