Merge "data extraction service"
[dcaegen2/services.git] / components / pm-subscription-handler / tests / test_exit_handler.py
1 # ============LICENSE_START===================================================
2 #  Copyright (C) 2020 Nordix Foundation.
3 # ============================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # SPDX-License-Identifier: Apache-2.0
17 # ============LICENSE_END=====================================================
18 import os
19 from signal import SIGTERM, signal
20 from unittest.mock import patch, Mock
21
22 from mod.exit_handler import ExitHandler
23 from mod.subscription import Subscription
24 from tests.base_setup import BaseClassSetup
25
26
27 class ExitHandlerTests(BaseClassSetup):
28
29     @classmethod
30     def setUpClass(cls):
31         super().setUpClass()
32
33     @patch('mod.pmsh_utils.PeriodicTask')
34     def setUp(self, mock_periodic_task):
35         super().setUp()
36         self.mock_aai_event_thread = mock_periodic_task
37         self.sub = self.app_conf.subscription
38
39     def tearDown(self):
40         super().tearDown()
41
42     @classmethod
43     def tearDownClass(cls):
44         super().tearDownClass()
45
46     @patch.object(Subscription, 'update_sub_nf_status')
47     @patch.object(Subscription, 'update_subscription_status')
48     def test_terminate_signal_successful(self, mock_upd_sub_status,
49                                          mock_upd_subnf_status):
50         handler = ExitHandler(periodic_tasks=[self.mock_aai_event_thread],
51                               app_conf=self.app_conf,
52                               subscription_handler=Mock())
53         signal(SIGTERM, handler)
54         os.kill(os.getpid(), SIGTERM)
55         self.assertTrue(ExitHandler.shutdown_signal_received)