blob: f987882c012cc6ba36d15e50f19d0e5f2f666464 [file] [log] [blame]
tyagi2cbc0a82015-05-21 02:53:14 -07001# 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
13import eventlet
14
15from oslo_concurrency import processutils
16from six.moves import configparser
17
18from heat_integrationtests.common import test
19
20
21class ReloadOnSighupTest(test.HeatIntegrationTest):
22
23 def setUp(self):
24 self.config_file = "/etc/heat/heat.conf"
25 super(ReloadOnSighupTest, self).setUp()
26
27 def _set_config_value(self, service, key, value):
28 config = configparser.ConfigParser()
29 config.read(self.config_file)
30 config.set(service, key, value)
31 with open(self.config_file, 'wb') as f:
32 config.write(f)
33
34 def _get_config_value(self, service, key):
35 config = configparser.ConfigParser()
36 config.read(self.config_file)
37 val = config.get(service, key)
38 return val
39
40 def _get_heat_api_pids(self, service):
41 # get the pids of all heat-api processes
42 if service == "heat_api":
43 process = "heat-api|grep -Ev 'grep|cloudwatch|cfn'"
44 else:
45 process = "%s|grep -Ev 'grep'" % service.replace('_', '-')
46 cmd = "ps -ef|grep %s|awk '{print $2}'" % process
47 out, err = processutils.execute(cmd, shell=True)
48 self.assertIsNotNone(out, "heat-api service not running. %s" % err)
49 pids = filter(None, out.split('\n'))
50
51 # get the parent pids of all heat-api processes
52 cmd = "ps -ef|grep %s|awk '{print $3}'" % process
53 out, _ = processutils.execute(cmd, shell=True)
54 parent_pids = filter(None, out.split('\n'))
55
56 heat_api_parent = list(set(pids) & set(parent_pids))[0]
57 heat_api_children = list(set(pids) - set(parent_pids))
58
59 return heat_api_parent, heat_api_children
60
61 def _change_config(self, service, old_workers, new_workers):
62 pre_reload_parent, pre_reload_children = self._get_heat_api_pids(
63 service)
64 self.assertEqual(old_workers, len(pre_reload_children))
65
66 # change the config values
67 self._set_config_value(service, 'workers', new_workers)
68 cmd = "kill -HUP %s" % pre_reload_parent
69 processutils.execute(cmd, shell=True)
70 # wait till heat-api reloads
71 eventlet.sleep(2)
72
73 post_reload_parent, post_reload_children = self._get_heat_api_pids(
74 service)
75 self.assertEqual(pre_reload_parent, post_reload_parent)
76 self.assertEqual(new_workers, len(post_reload_children))
77 # test if all child processes are newly created
78 self.assertEqual(set(post_reload_children) & set(pre_reload_children),
79 set())
80
81 def _reload(self, service):
82 old_workers = int(self._get_config_value(service, 'workers'))
83 new_workers = old_workers + 1
84 self.addCleanup(self._set_config_value, service, 'workers',
85 old_workers)
86
87 self._change_config(service, old_workers, new_workers)
88 # revert all the changes made
89 self._change_config(service, new_workers, old_workers)
90
91 def test_api_reload_on_sighup(self):
92 self._reload('heat_api')
93
94 def test_api_cfn_reload_on_sighup(self):
95 self._reload('heat_api_cfn')
96
97 def test_api_cloudwatch_on_sighup(self):
98 self._reload('heat_api_cloudwatch')