|
| 1 | +# |
| 2 | +# Copyright (c) 2020-2024 NVIDIA CORPORATION & AFFILIATES. |
| 3 | +# Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +import os |
| 18 | +import pytest |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | +from mock import patch, mock_open |
| 22 | + |
| 23 | +test_path = os.path.dirname(os.path.abspath(__file__)) |
| 24 | +modules_path = os.path.dirname(test_path) |
| 25 | +sys.path.insert(0, modules_path) |
| 26 | + |
| 27 | +from sonic_platform import utils |
| 28 | +from sonic_platform.local_users_passwords_reset import LocalUsersConfigurationReset |
| 29 | + |
| 30 | + |
| 31 | +DEFAULT_USERS_JSON_EXAMPLE_OUTPUT = ''' |
| 32 | +{ |
| 33 | + "admin": { |
| 34 | + "expire": "false", |
| 35 | + "password": "HASHED_PASSWORD_123" |
| 36 | + } |
| 37 | +} |
| 38 | +''' |
| 39 | + |
| 40 | + |
| 41 | +class TestLocalUsersConfigurationReset: |
| 42 | + @patch('sonic_platform.utils.read_int_from_file') |
| 43 | + def test_should_trigger_method(self, mock_read_int): |
| 44 | + ''' |
| 45 | + Validate should_trigger() method |
| 46 | + ''' |
| 47 | + local_users_reset_class = LocalUsersConfigurationReset() |
| 48 | + |
| 49 | + mock_read_int.return_value = int(1) |
| 50 | + assert local_users_reset_class.should_trigger() == True |
| 51 | + mock_read_int.return_value = int(0) |
| 52 | + assert local_users_reset_class.should_trigger() == False |
| 53 | + mock_read_int.return_value = int(2) |
| 54 | + assert local_users_reset_class.should_trigger() == False |
| 55 | + mock_read_int.side_effect = ValueError() |
| 56 | + assert local_users_reset_class.should_trigger() == False |
| 57 | + |
| 58 | + @patch('subprocess.call') |
| 59 | + @patch('sonic_platform.utils.read_int_from_file') |
| 60 | + @patch("builtins.open", new_callable=mock_open, read_data=DEFAULT_USERS_JSON_EXAMPLE_OUTPUT) |
| 61 | + def test_basic_flow_resetting_users_triggered(self, mock_read_int, mock_subproc_call): |
| 62 | + ''' |
| 63 | + Test the basic flow of resetting local users when long button press is detected |
| 64 | + ''' |
| 65 | + # Mock long reset button press |
| 66 | + mock_read_int.return_value = int(1) |
| 67 | + LocalUsersConfigurationReset().start() |
| 68 | + mock_subproc_call.assert_called_with(['echo', 'admin:HASHED_PASSWORD_123', '|', 'sudo', 'chpasswd', '-e']) |
| 69 | + mock_subproc_call.assert_called_with(['sudo', 'passwd', '-e', 'admin']) |
| 70 | + |
| 71 | + @patch('subprocess.call') |
| 72 | + @patch('sonic_platform.utils.read_int_from_file') |
| 73 | + @patch("builtins.open", new_callable=mock_open, read_data=DEFAULT_USERS_JSON_OUTPUT) |
| 74 | + def test_basic_flow_resetting_users_not_triggered(self, mock_read_int, mock_subproc_call): |
| 75 | + ''' |
| 76 | + Test the basic flow of resetting local users when long button press is not detected |
| 77 | + ''' |
| 78 | + # Mock long reset button NOT pressed |
| 79 | + mock_read_int.return_value = int(0) |
| 80 | + LocalUsersConfigurationReset().start() |
| 81 | + mock_subproc_call.assert_not_called() |
0 commit comments