1# Copyright 2024 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5from __future__ import annotations 6 7from crossbench import path as pth 8from crossbench import plt 9from tests import test_helper 10from tests.crossbench.plt.helper import BasePosixMockPlatformTestCase 11 12 13class LinuxSshMockPlatformTestCase(BasePosixMockPlatformTestCase): 14 __test__ = True 15 HOST = "host" 16 PORT = 9515 17 SSH_PORT = 22 18 SSH_USER = "user" 19 platform: plt.LinuxSshPlatform 20 21 def setUp(self) -> None: 22 super().setUp() 23 self.platform = plt.LinuxSshPlatform( 24 self.mock_platform, 25 host=self.HOST, 26 port=self.PORT, 27 ssh_port=self.SSH_PORT, 28 ssh_user=self.SSH_USER) 29 30 def test_is_linux(self): 31 self.assertTrue(self.platform.is_linux) 32 33 def test_is_remote_ssh(self): 34 self.assertTrue(self.platform.is_remote_ssh) 35 36 def test_basic_properties(self): 37 self.assertTrue(self.platform.is_remote) 38 self.assertEqual(self.platform.host, self.HOST) 39 self.assertEqual(self.platform.port, self.PORT) 40 self.assertIs(self.platform.host_platform, self.mock_platform) 41 self.assertTrue(self.platform.is_posix) 42 43 def test_name(self): 44 self.assertEqual(self.platform.name, "linux_ssh") 45 46 def test_version(self): 47 self._expect_sh_ssh("uname -r", result="999") 48 self.assertEqual(self.platform.version, "999") 49 # Subsequent calls are cached. 50 self.assertEqual(self.platform.version, "999") 51 52 53 def test_iterdir(self): 54 self._expect_sh_ssh("'[' -d parent_dir/child_dir ']'") 55 self._expect_sh_ssh("ls -1 parent_dir/child_dir", result="file1\nfile2\n") 56 57 self.assertSetEqual( 58 set(self.platform.iterdir(pth.AnyWindowsPath("parent_dir\\child_dir"))), 59 { 60 pth.AnyPosixPath("parent_dir/child_dir/file1"), 61 pth.AnyPosixPath("parent_dir/child_dir/file2") 62 }) 63 64 def _expect_sh_ssh(self, *args, result=""): 65 self.mock_platform.expect_sh( 66 "ssh", 67 "-p", 68 str(self.SSH_PORT), 69 f"{self.SSH_USER}@{self.HOST}", 70 *args, 71 result=result) 72 73 74if __name__ == "__main__": 75 test_helper.run_pytest(__file__) 76