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
135
136
|
#
# 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 json
from unittest import mock
from novaclient import exceptions
from oslo_utils import excutils
from heat.common import template_format
from heat.engine import scheduler
from heat.tests import common
from heat.tests import utils
sg_template = {
"heat_template_version": "2013-05-23",
"resources": {
"ServerGroup": {
"type": "OS::Nova::ServerGroup",
"properties": {
"name": "test",
"policies": ["anti-affinity"]
}
}
}
}
class FakeGroup(object):
def __init__(self, name):
self.id = name
self.name = name
class NovaServerGroupTest(common.HeatTestCase):
def setUp(self):
super(NovaServerGroupTest, self).setUp()
def _init_template(self, sg_template):
template = template_format.parse(json.dumps(sg_template))
self.stack = utils.parse_stack(template)
self.sg = self.stack['ServerGroup']
# create mock clients and objects
nova = mock.MagicMock()
self.sg.client = mock.MagicMock(return_value=nova)
class FakeNovaPlugin(object):
@excutils.exception_filter
def ignore_not_found(self, ex):
if not isinstance(ex, exceptions.NotFound):
raise ex
def is_conflict(self, ex):
return False
self.patchobject(excutils.exception_filter, '__exit__')
self.patchobject(self.sg, 'client_plugin',
return_value=FakeNovaPlugin())
self.sg_mgr = nova.server_groups
def _create_sg(self, name):
if name:
sg = sg_template['resources']['ServerGroup']
sg['properties']['name'] = name
self._init_template(sg_template)
self.sg_mgr.create.return_value = FakeGroup(name)
else:
try:
sg = sg_template['resources']['ServerGroup']
del sg['properties']['name']
except Exception:
pass
self._init_template(sg_template)
name = 'test'
n = name
def fake_create(name, policies):
self.assertGreater(len(name), 1)
return FakeGroup(n)
self.sg_mgr.create = fake_create
scheduler.TaskRunner(self.sg.create)()
self.assertEqual((self.sg.CREATE, self.sg.COMPLETE),
self.sg.state)
self.assertEqual(name, self.sg.resource_id)
def test_sg_create(self):
self._create_sg('test')
expected_args = ()
expected_kwargs = {'name': 'test',
'policies': ["anti-affinity"],
}
self.sg_mgr.create.assert_called_once_with(*expected_args,
**expected_kwargs)
def test_sg_create_no_name(self):
self._create_sg(None)
def test_sg_show_resource(self):
self._create_sg('test')
self.sg.client = mock.MagicMock()
s_groups = mock.MagicMock()
sg = mock.MagicMock()
sg.to_dict.return_value = {'server_gr': 'info'}
s_groups.get.return_value = sg
self.sg.client().server_groups = s_groups
self.assertEqual({'server_gr': 'info'}, self.sg.FnGetAtt('show'))
s_groups.get.assert_called_once_with('test')
def test_needs_replace_failed(self):
self._create_sg('test')
self.sg.state_set(self.sg.CREATE, self.sg.FAILED)
mock_show_resource = self.patchobject(self.sg, '_show_resource')
mock_show_resource.side_effect = [exceptions.NotFound(404), None]
self.sg.resource_id = None
self.assertTrue(self.sg.needs_replace_failed())
self.assertEqual(0, mock_show_resource.call_count)
self.sg.resource_id = 'sg_id'
self.assertTrue(self.sg.needs_replace_failed())
self.assertEqual(1, mock_show_resource.call_count)
mock_show_resource.return_value = None
self.assertFalse(self.sg.needs_replace_failed())
self.assertEqual(2, mock_show_resource.call_count)
|