summaryrefslogtreecommitdiff
path: root/heat/tests/clients/test_barbican_client.py
blob: 4f07dc681734242497b04743f0eca02859e38442 (plain)
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
#
#    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.

import collections
from unittest import mock

from barbicanclient import exceptions

from heat.common import exception
from heat.engine.clients.os import barbican
from heat.tests import common
from heat.tests import utils


class BarbicanClientPluginTest(common.HeatTestCase):

    def setUp(self):
        super(BarbicanClientPluginTest, self).setUp()
        self.barbican_client = mock.MagicMock()
        con = utils.dummy_context()
        c = con.clients
        self.barbican_plugin = c.client_plugin('barbican')
        self.barbican_plugin.client = lambda: self.barbican_client

    @mock.patch('keystoneauth1.discover.get_version_data',
                mock.MagicMock(return_value=[{'status': "STABLE"}]))
    def test_create(self):
        context = utils.dummy_context()
        plugin = context.clients.client_plugin('barbican')
        client = plugin.client()
        self.assertIsNotNone(client.orders)

    def test_get_secret_by_ref(self):
        secret = collections.namedtuple('Secret', ['name'])('foo')
        self.barbican_client.secrets.get.return_value = secret
        self.assertEqual(secret,
                         self.barbican_plugin.get_secret_by_ref("secret"))

    def test_get_secret_payload_by_ref(self):
        payload_content = 'payload content'
        secret = collections.namedtuple(
            'Secret', ['name', 'payload'])('foo', payload_content)
        self.barbican_client.secrets.get.return_value = secret
        expect = payload_content
        self.assertEqual(expect,
                         self.barbican_plugin.get_secret_payload_by_ref(
                             "secret"))

    def test_get_secret_payload_by_ref_not_found(self):
        exc = exceptions.HTTPClientError(message="Not Found", status_code=404)
        self.barbican_client.secrets.get.side_effect = exc
        self.assertRaises(
            exception.EntityNotFound,
            self.barbican_plugin.get_secret_payload_by_ref,
            "secret")

    def test_get_secret_by_ref_not_found(self):
        exc = exceptions.HTTPClientError(message="Not Found", status_code=404)
        self.barbican_client.secrets.get.side_effect = exc
        self.assertRaises(
            exception.EntityNotFound,
            self.barbican_plugin.get_secret_by_ref,
            "secret")


class SecretConstraintTest(common.HeatTestCase):

    def setUp(self):
        super(SecretConstraintTest, self).setUp()
        self.ctx = utils.dummy_context()
        self.mock_get_secret_by_ref = mock.Mock()
        self.ctx.clients.client_plugin(
            'barbican').get_secret_by_ref = self.mock_get_secret_by_ref
        self.constraint = barbican.SecretConstraint()

    def test_validation(self):
        self.mock_get_secret_by_ref.return_value = {}
        self.assertTrue(self.constraint.validate("foo", self.ctx))

    def test_validation_error(self):
        self.mock_get_secret_by_ref.side_effect = exception.EntityNotFound(
            entity='Secret', name='bar')
        self.assertFalse(self.constraint.validate("bar", self.ctx))


class ContainerConstraintTest(common.HeatTestCase):

    def setUp(self):
        super(ContainerConstraintTest, self).setUp()
        self.ctx = utils.dummy_context()
        self.mock_get_container_by_ref = mock.Mock()
        self.ctx.clients.client_plugin(
            'barbican').get_container_by_ref = self.mock_get_container_by_ref
        self.constraint = barbican.ContainerConstraint()

    def test_validation(self):
        self.mock_get_container_by_ref.return_value = {}
        self.assertTrue(self.constraint.validate("foo", self.ctx))

    def test_validation_error(self):
        self.mock_get_container_by_ref.side_effect = exception.EntityNotFound(
            entity='Container', name='bar')
        self.assertFalse(self.constraint.validate("bar", self.ctx))