"Fossies" - the Fresh Open Source Software Archive

Member "magnum-8.1.0/magnum/tests/unit/common/cert_manager/test_x509keypair_cert_manager.py" (1 Oct 2019, 4399 Bytes) of package /linux/misc/openstack/magnum-8.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Copyright 2016 Intel, Inc
    2 #
    3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 #    not use this file except in compliance with the License. You may obtain
    5 #    a copy of the License at
    6 #
    7 #         http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 #    Unless required by applicable law or agreed to in writing, software
   10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 #    License for the specific language governing permissions and limitations
   13 #    under the License.
   14 import mock
   15 
   16 from magnum.common.cert_manager import x509keypair_cert_manager as x509_cm
   17 from magnum.common import context
   18 from magnum.tests import base
   19 from magnum.tests.unit.db import base as db_base
   20 from magnum.tests.unit.db import utils
   21 
   22 
   23 class TestX509keypairCert(base.BaseTestCase):
   24 
   25     def setUp(self):
   26         self.certificate = "My Certificate"
   27         self.intermediates = "My Intermediates"
   28         self.private_key = "My Private Key"
   29         self.private_key_passphrase = "My Private Key Passphrase"
   30 
   31         super(TestX509keypairCert, self).setUp()
   32 
   33     def test_x509keypair_cert(self):
   34         # Create a cert
   35         cert = x509_cm.Cert(
   36             certificate=self.certificate,
   37             intermediates=self.intermediates,
   38             private_key=self.private_key,
   39             private_key_passphrase=self.private_key_passphrase
   40         )
   41 
   42         # Validate the cert functions
   43         self.assertEqual(self.certificate, cert.get_certificate())
   44         self.assertEqual(self.intermediates, cert.get_intermediates())
   45         self.assertEqual(self.private_key, cert.get_private_key())
   46         self.assertEqual(self.private_key_passphrase,
   47                          cert.get_private_key_passphrase())
   48 
   49 
   50 class TestX509keypairManager(db_base.DbTestCase):
   51 
   52     def setUp(self):
   53         self.certificate = "My Certificate"
   54         self.intermediates = "My Intermediates"
   55         self.private_key = "My Private Key"
   56         self.private_key_passphrase = "My Private Key Passphrase"
   57         self.context = context.make_admin_context()
   58         super(TestX509keypairManager, self).setUp()
   59 
   60     def test_store_cert(self):
   61         x509keypair = utils.get_test_x509keypair()
   62         with mock.patch.object(self.dbapi, 'create_x509keypair',
   63                                autospec=True) as mock_create_x509keypair:
   64             mock_create_x509keypair.return_value = x509keypair
   65 
   66             uuid = x509_cm.CertManager.store_cert(context=self.context,
   67                                                   **x509keypair)
   68             self.assertEqual(uuid, '72625085-c507-4410-9b28-cd7cf1fbf1ad')
   69 
   70     def test_get_cert(self):
   71         x509keypair = utils.get_test_x509keypair(uuid='fake-uuid')
   72         with mock.patch.object(self.dbapi, 'get_x509keypair_by_uuid',
   73                                autospec=True) as mock_get_x509keypair:
   74             mock_get_x509keypair.return_value = x509keypair
   75             cert_obj = x509_cm.CertManager.get_cert('fake-uuid',
   76                                                     context=self.context)
   77             self.assertEqual(cert_obj.certificate, 'certificate')
   78             self.assertEqual(cert_obj.private_key, 'private_key')
   79             self.assertEqual(cert_obj.private_key_passphrase,
   80                              'private_key_passphrase')
   81             self.assertEqual(cert_obj.intermediates, 'intermediates')
   82             mock_get_x509keypair.assert_called_once_with(self.context,
   83                                                          'fake-uuid')
   84 
   85     def test_delete_cert(self):
   86         x509keypair = utils.get_test_x509keypair(uuid='fake-uuid')
   87         with mock.patch.object(self.dbapi, 'get_x509keypair_by_uuid',
   88                                autospec=True) as mock_get_x509keypair:
   89             mock_get_x509keypair.return_value = x509keypair
   90             with mock.patch.object(self.dbapi, 'destroy_x509keypair',
   91                                    autospec=True) as mock_destroy_x509keypair:
   92                 x509_cm.CertManager.delete_cert('fake-uuid',
   93                                                 context=self.context)
   94                 mock_get_x509keypair.assert_called_once_with(self.context,
   95                                                              'fake-uuid')
   96                 mock_destroy_x509keypair.assert_called_once_with('fake-uuid')