summaryrefslogtreecommitdiffstats
path: root/components/pm-subscription-handler/tests/test_exit_handler.py
blob: ac1e15c6a819919e595cc4adeb473475ae78b190 (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
# ============LICENSE_START===================================================
#  Copyright (C) 2020 Nordix Foundation.
# ============================================================================
# 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.
#
# SPDX-License-Identifier: Apache-2.0
# ============LICENSE_END=====================================================
import os
import signal
import threading
import time
from unittest import TestCase
from unittest.mock import patch, Mock, MagicMock

import pmsh_service_main
from mod.exit_handler import ExitHandler
from mod.pmsh_utils import PeriodicTask
from mod.subscription import AdministrativeState


class ExitHandlerTests(TestCase):

    @patch('pmsh_service_main.create_app')
    @patch('pmsh_service_main.db')
    @patch('pmsh_service_main.AppConfig')
    @patch('pmsh_service_main.Subscription')
    @patch('pmsh_service_main.launch_api_server')
    @patch('pmsh_service_main.SubscriptionHandler')
    @patch.object(PeriodicTask, 'start')
    @patch.object(PeriodicTask, 'cancel')
    def test_terminate_signal_success(self, mock_task_cancel, mock_task_start, mock_sub_handler,
                                      mock_launch_api_server, mock_sub, mock_app_conf,
                                      mock_db, mock_app):
        pid = os.getpid()
        mock_db.get_app.return_value = Mock()

        mock_sub.administrativeState = AdministrativeState.UNLOCKED.value
        mock_sub.process_subscription = Mock()
        mock_sub_handler_instance = MagicMock(execute=Mock(), current_sub=mock_sub)
        mock_sub_handler.side_effect = [mock_sub_handler_instance]

        def mock_api_server_run(param):
            while mock_sub.administrativeState == AdministrativeState.UNLOCKED.value:
                time.sleep(1)

        mock_launch_api_server.side_effect = mock_api_server_run

        def trigger_signal():
            time.sleep(1)
            os.kill(pid, signal.SIGTERM)

        thread = threading.Thread(target=trigger_signal)
        thread.start()

        pmsh_service_main.main()

        self.assertEqual(4, mock_task_cancel.call_count)
        self.assertTrue(ExitHandler.shutdown_signal_received)
        self.assertEqual(1, mock_sub.process_subscription.call_count)
        self.assertEqual(mock_sub.administrativeState, AdministrativeState.LOCKED.value)