Home | History | Annotate | Download | only in tests
      1 #!/usr/bin/env python3
      2 
      3 # Copyright 2016 - The Android Open Source Project
      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 inspect
     18 import io
     19 import os
     20 import sys
     21 import time
     22 import unittest
     23 import re
     24 
     25 from acts.controllers.utils_lib.ssh import connection
     26 from acts.controllers.utils_lib.ssh import settings
     27 
     28 SSH_HOST = None
     29 SSH_USER = None
     30 
     31 
     32 class SshTestCases(unittest.TestCase):
     33     def setUp(self):
     34         self.settings = settings.SshSettings(SSH_HOST, SSH_USER)
     35 
     36     def test_ssh_run(self):
     37         """Test running an ssh command.
     38 
     39         Test that running an ssh command works.
     40         """
     41         conn = connection.SshConnection(self.settings)
     42 
     43         result = conn.run('echo "Hello World"')
     44         self.assertTrue(result.stdout.find('Hello World') > -1)
     45 
     46     def test_ssh_env(self):
     47         """Test that special envirment variables get sent over ssh.
     48 
     49         Test that given a dictonary of enviroment variables they will be sent
     50         to the remote host.
     51         """
     52         conn = connection.SshConnection(self.settings)
     53 
     54         result = conn.run('printenv', env={'MYSPECIALVAR': 20})
     55         self.assertTrue(result.stdout.find('MYSPECIALVAR=20'))
     56 
     57     def test_ssh_permission_denied(self):
     58         """Test that permission denied works.
     59 
     60         Connect to a remote host using an invalid username and see if we are
     61         rejected.
     62         """
     63         with self.assertRaises(connection.Error):
     64             bad_settings = settings.SshSettings(SSH_HOST, SSH_USER + 'BAD')
     65             conn = connection.SshConnection(bad_settings)
     66             result = conn.run('echo "Hello World"')
     67 
     68     def test_ssh_unknown_host(self):
     69         """Test that permission denied works.
     70 
     71         Connect to a remote host using an invalid username and see if we are
     72         rejected.
     73         """
     74         with self.assertRaises(connection.Error):
     75             bad_settings = settings.SshSettings('BADHOSTNAME', SSH_USER)
     76             conn = connection.SshConnection(bad_settings)
     77             result = conn.run('echo "Hello World"')
     78 
     79 
     80 if __name__ == '__main__':
     81     # Get host info from command line arguments.
     82     if len(sys.argv) < 3:
     83         raise ValueError("usage: %s <username> <hostname to ssh to>" %
     84                          sys.argv[0])
     85     SSH_HOST = sys.argv[2]
     86     SSH_USER = sys.argv[1]
     87     unittest.main(argv=sys.argv[0:1] + sys.argv[3:])
     88