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