1+ # Copyright 2025 Microsoft Corporation
2+ #
3+ # Licensed under the Apache License, Version 2.0 (the "License");
4+ # you may not use this file except in compliance with the License.
5+ # You may obtain a copy of the License at
6+ #
7+ # http://www.apache.org/licenses/LICENSE-2.0
8+ #
9+ # Unless required by applicable law or agreed to in writing, software
10+ # distributed under the License is distributed on an "AS IS" BASIS,
11+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ # See the License for the specific language governing permissions and
13+ # limitations under the License.
14+ #
15+ # Requires Python 2.7+
16+ import platform
17+ import unittest
18+ from core .src .bootstrap .EnvLayer import EnvLayer
19+ from core .src .bootstrap .Constants import Constants
20+
21+
22+ class TestExecutionConfig (unittest .TestCase ):
23+ def setUp (self ):
24+ self .envlayer = EnvLayer ()
25+
26+ def tearDown (self ):
27+ pass
28+
29+ # region setup mocks
30+ def mock_platform_system (self ):
31+ return 'Linux'
32+
33+ def mock_linux_distribution (self ):
34+ return ['test' , 'test' , 'test' ]
35+
36+ def mock_linux_distribution_to_return_azure_linux (self ):
37+ return ['Microsoft Azure Linux' , '3.0' , '' ]
38+
39+ def mock_run_command_for_apt (self , cmd , no_output = False , chk_err = False ):
40+ if cmd .find ("which apt-get" ) > - 1 :
41+ return 0 , ''
42+ return - 1 , ''
43+
44+ def mock_run_command_for_yum (self , cmd , no_output = False , chk_err = False ):
45+ if cmd .find ("which yum" ) > - 1 :
46+ return 0 , ''
47+ return - 1 , ''
48+
49+ def mock_run_command_for_zypper (self , cmd , no_output = False , chk_err = False ):
50+ if cmd .find ("which zypper" ) > - 1 :
51+ return 0 , ''
52+ return - 1 , ''
53+
54+ def mock_run_command_for_tdnf (self , cmd , no_output = False , chk_err = False ):
55+ if cmd .find ("which tdnf" ) > - 1 :
56+ return 0 , ''
57+ return - 1 , ''
58+ # endregion
59+
60+ def test_get_package_manager (self ):
61+ self .backup_platform_system = platform .system ()
62+ platform .system = self .mock_platform_system
63+ self .backup_linux_distribution = self .envlayer .platform .linux_distribution
64+ self .envlayer .platform .linux_distribution = self .mock_linux_distribution
65+ self .backup_run_command_output = self .envlayer .run_command_output
66+
67+ test_input_output_table = [
68+ [self .mock_run_command_for_apt , self .mock_linux_distribution , Constants .APT ],
69+ [self .mock_run_command_for_tdnf , self .mock_linux_distribution_to_return_azure_linux , Constants .TDNF ],
70+ [self .mock_run_command_for_yum , self .mock_linux_distribution_to_return_azure_linux , None ], # check for Azure Linux machine which does not have tdnf
71+ [self .mock_run_command_for_yum , self .mock_linux_distribution , Constants .YUM ],
72+ [self .mock_run_command_for_zypper , self .mock_linux_distribution , Constants .ZYPPER ],
73+ ]
74+
75+ for row in test_input_output_table :
76+ self .envlayer .run_command_output = row [0 ]
77+ self .envlayer .platform .linux_distribution = row [1 ]
78+ package_manager = self .envlayer .get_package_manager ()
79+ self .assertTrue (package_manager is row [2 ])
80+
81+ self .envlayer .run_command_output = self .backup_run_command_output
82+ self .envlayer .platform .linux_distribution = self .backup_linux_distribution
83+ platform .system = self .backup_platform_system
84+
85+
86+ if __name__ == '__main__' :
87+ unittest .main ()
0 commit comments