| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
import collections |
| 13 |
from unittest import mock |
| 14 |
|
| 15 |
import keystoneauth1 |
| 16 |
from magnum.common import clients |
| 17 |
from magnum.common import utils |
| 18 |
from magnum.tests.unit.db import base |
| 19 |
from magnum.tests.unit.objects import utils as obj_utils |
| 20 |
|
| 21 |
from magnum_capi_helm.common import app_creds |
| 22 |
|
| 23 |
|
| 24 |
class TestAppCreds(base.DbTestCase): |
| 25 |
def setUp(self): |
| 26 |
super().setUp() |
| 27 |
self.cluster_obj = obj_utils.create_test_cluster( |
| 28 |
self.context, |
| 29 |
name="cluster_example_$A", |
| 30 |
master_flavor_id="flavor_small", |
| 31 |
flavor_id="flavor_medium", |
| 32 |
) |
| 33 |
|
| 34 |
@mock.patch.object(utils, "get_openstack_ca") |
| 35 |
def test_get_openstack_ca_certificate(self, mock_ca): |
| 36 |
mock_ca.return_value = "cert" |
| 37 |
|
| 38 |
cert = app_creds._get_openstack_ca_certificate() |
| 39 |
|
| 40 |
self.assertEqual("cert", cert) |
| 41 |
|
| 42 |
@mock.patch.object(utils, "get_openstack_ca") |
| 43 |
def test_get_openstack_ca_certificate_get_certify(self, mock_ca): |
| 44 |
mock_ca.return_value = None |
| 45 |
|
| 46 |
cert = app_creds._get_openstack_ca_certificate() |
| 47 |
|
| 48 |
self.assertIsNotNone(cert) |
| 49 |
|
| 50 |
@mock.patch.object(clients, "OpenStackClients") |
| 51 |
def test_create_app_cred(self, mock_client): |
| 52 |
mock_client().cinder_region_name.return_value = "cinder" |
| 53 |
mock_client().url_for.return_value = "http://keystone" |
| 54 |
mock_app_cred = mock_client().keystone().client.application_credentials |
| 55 |
app_cred = collections.namedtuple("appcred", ["id", "secret"]) |
| 56 |
mock_app_cred.create.return_value = app_cred("id", "pass") |
| 57 |
context = mock.MagicMock() |
| 58 |
context.roles = ["member", "foo", "admin"] |
| 59 |
|
| 60 |
app_cred = app_creds._create_app_cred(context, self.cluster_obj) |
| 61 |
|
| 62 |
expected = { |
| 63 |
"clouds": { |
| 64 |
"openstack": { |
| 65 |
"auth": { |
| 66 |
"application_credential_id": "id", |
| 67 |
"application_credential_secret": "pass", |
| 68 |
"auth_url": "http://keystone", |
| 69 |
}, |
| 70 |
"auth_type": "v3applicationcredential", |
| 71 |
"identity_api_version": 3, |
| 72 |
"interface": "public", |
| 73 |
"region_name": "cinder", |
| 74 |
"verify": True, |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
self.assertEqual(expected, app_cred) |
| 79 |
mock_client().url_for.assert_called_once_with( |
| 80 |
service_type="identity", interface="public" |
| 81 |
) |
| 82 |
mock_app_cred.create.assert_called_once_with( |
| 83 |
user="fake_user", |
| 84 |
name=f"magnum-{self.cluster_obj.uuid}", |
| 85 |
description=f"Magnum cluster ({self.cluster_obj.uuid})", |
| 86 |
# roles=["member", "foo"], |
| 87 |
) |
| 88 |
|
| 89 |
@mock.patch.object(app_creds, "_get_openstack_ca_certificate") |
| 90 |
@mock.patch.object(app_creds, "_create_app_cred") |
| 91 |
def test_get_app_cred_yaml(self, mock_create, mock_ca): |
| 92 |
mock_ca.return_value = "cacert" |
| 93 |
mock_create.return_value = { |
| 94 |
"clouds": { |
| 95 |
"openstack": {"auth": {"application_credential_id": "id"}} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
app_cred = app_creds.get_app_cred_string_data("context", "cluster") |
| 100 |
|
| 101 |
expected = { |
| 102 |
"cacert": "cacert", |
| 103 |
"clouds.yaml": """\ |
| 104 |
clouds: |
| 105 |
openstack: |
| 106 |
auth: |
| 107 |
application_credential_id: id |
| 108 |
""", |
| 109 |
} |
| 110 |
self.assertEqual(expected, app_cred) |
| 111 |
|
| 112 |
@mock.patch.object(clients, "OpenStackClients") |
| 113 |
def test_delete_app_cred(self, mock_client): |
| 114 |
mock_app_cred = mock_client().keystone().client.application_credentials |
| 115 |
mock_find = mock.MagicMock() |
| 116 |
mock_app_cred.find.return_value = mock_find |
| 117 |
|
| 118 |
app_creds.delete_app_cred("context", self.cluster_obj) |
| 119 |
|
| 120 |
mock_find.delete.assert_called_once_with() |
| 121 |
mock_app_cred.find.assert_called_once_with( |
| 122 |
name=f"magnum-{self.cluster_obj.uuid}", |
| 123 |
user="fake_user", |
| 124 |
) |
| 125 |
|
| 126 |
@mock.patch.object(clients, "OpenStackClients") |
| 127 |
def test_delete_app_cred_not_found(self, mock_client): |
| 128 |
mock_app_cred = mock_client().keystone().client.application_credentials |
| 129 |
mock_app_cred.find.side_effect = keystoneauth1.exceptions.http.NotFound |
| 130 |
|
| 131 |
app_creds.delete_app_cred("context", self.cluster_obj) |
| 132 |
|
| 133 |
mock_app_cred.find.assert_called_once_with( |
| 134 |
name=f"magnum-{self.cluster_obj.uuid}", |
| 135 |
user="fake_user", |
| 136 |
) |