File: test_credentials.py

package info (click to toggle)
python-tempestconf 3.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: python: 4,530; makefile: 18; sh: 9
file content (134 lines) | stat: -rw-r--r-- 5,590 bytes parent folder | download
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
# -*- coding: utf-8 -*-

# Copyright 2018 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from unittest import mock

from fixtures import MonkeyPatch

from config_tempest.tests.base import BaseConfigTempestTest


class TestCredentials(BaseConfigTempestTest):

    def setUp(self):
        super(TestCredentials, self).setUp()
        self.conf = self._get_conf("v2.0", "v3")
        self.creds = self._get_creds(self.conf)
        self.creds_v2 = self._get_creds(self.conf, v2=True)

    def test_get_credential(self):
        # set conf containing the newer values (admin creds in auth section)
        self.creds._conf = self._get_alt_conf("v2.0", "v3")
        resp = self.creds.get_credential("username")
        self.assertEqual(resp, "demo")
        # set admin credentials
        self.creds.admin = True
        resp = self.creds.get_credential("username")
        self.assertEqual(resp, "admin")

    def test_get_identity_version_v2(self):
        resp = self.creds_v2._get_identity_version()
        self.assertEqual(resp, 'v2')

    def test_get_identity_version_v3(self):
        conf = self._get_conf("v3", "v3")  # uri has to be v3
        creds = self._get_creds(conf)
        resp = creds._get_identity_version()
        self.assertEqual(resp, 'v3')

    def test_get_creds_kwargs(self):
        expected_resp = {
            'username': 'demo',
            'password': 'secret',
            'project_name': 'demo'
        }
        self.assertEqual(self.creds_v2._get_creds_kwargs(), expected_resp)
        self.creds_v2.identity_version = 'v3'
        expected_resp = {
            'username': 'demo',
            'password': 'secret',
            'project_name': 'demo',
            'domain_name': 'Default',
            'user_domain_name': 'Default',
            'project_domain_name': 'Default',
        }
        self.assertEqual(self.creds._get_creds_kwargs(), expected_resp)

    def test_set_credentials_v2(self):
        mock_function = mock.Mock()
        function2mock = 'config_tempest.credentials.auth.get_credentials'
        self.useFixture(MonkeyPatch(function2mock, mock_function))
        self.creds_v2.username = "name"
        self.creds_v2.password = "pass"
        self.creds_v2.project_name = "Tname"
        self.creds_v2.set_credentials()
        mock_function.assert_called_with(
            auth_url=None, fill_in=False, identity_version='v2',
            disable_ssl_certificate_validation='true',
            ca_certs=None, password='pass', project_name='Tname',
            username='name')

    def test_set_credentials_v3(self):
        mock_function = mock.Mock()
        function2mock = 'config_tempest.credentials.auth.get_credentials'
        self.useFixture(MonkeyPatch(function2mock, mock_function))
        self.creds.username = "name"
        self.creds.password = "pass"
        self.creds.project_name = "project_name"
        self.creds.identity_version = "v3"
        self.creds.set_credentials()
        mock_function.assert_called_with(
            auth_url=None, fill_in=False, identity_version='v3',
            disable_ssl_certificate_validation='true',
            ca_certs=None, password='pass',
            username='name',
            project_name='project_name',
            domain_name='Default',
            user_domain_name='Default',
            project_domain_name='Default')

    def test_get_auth_provider_keystone_v2(self):
        # check if method returns correct method - KeystoneV2AuthProvider
        mock_function = mock.Mock()
        # mock V2Provider, if other provider is called, it fails
        func2mock = 'config_tempest.credentials.auth.KeystoneV2AuthProvider'
        self.useFixture(MonkeyPatch(func2mock, mock_function))
        resp = self.creds_v2.get_auth_provider()
        self.assertEqual(resp, mock_function())
        # check parameters of returned function
        self.creds_v2.get_auth_provider()
        mock_function.assert_called_with(self.creds_v2.tempest_creds,
                                         'http://172.16.52.151:5000/v2.0',
                                         'true', None)

    def test_get_auth_provider_keystone_v3(self):
        # check if method returns KeystoneV3AuthProvider
        # make isinstance return True
        mockIsInstance = mock.Mock(return_value=True)
        self.useFixture(MonkeyPatch('config_tempest.credentials.isinstance',
                                    mockIsInstance))
        mock_function = mock.Mock()
        # mock V3Provider, if other provider is called, it fails
        func2mock = 'config_tempest.credentials.auth.KeystoneV3AuthProvider'
        self.useFixture(MonkeyPatch(func2mock, mock_function))
        resp = self.creds.get_auth_provider()
        self.assertEqual(resp, mock_function())
        # check parameters of returned function
        self.creds.get_auth_provider()
        mock_function.assert_called_with(self.creds.tempest_creds,
                                         'http://172.16.52.151:5000/v3',
                                         'true', None)