コード例 #1
0
def test_id_generator():
    # basic checks on size
    assert int(util.id_generator(size=1, chars=string.digits)) < 10
    assert len(util.id_generator(size=5)) == 5
    # basic checks on character types
    assert util.id_generator(size=5).isalnum()
    assert util.id_generator(size=8, chars=string.ascii_letters).isalpha()
    assert util.id_generator(size=8, chars=string.digits).isdigit()
コード例 #2
0
ファイル: util.py プロジェクト: randylerma/JumpSSH
def create_random_json(size=1000):
    random.seed()
    dummy_json = {}
    for i in range(size):
        random_key = jumpssh_util.id_generator(size=15)
        random_value = jumpssh_util.id_generator(size=100)
        dummy_json[random_key] = random_value
    return dummy_json
コード例 #3
0
def test_run_cmd_retry(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')

    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()

    with pytest.raises(exception.RunCmdError) as exc_info:
        gateway_session.run_cmd('dummy commmand', retry=2, retry_interval=1)
    assert exc_info.value.runs_nb == 3

    # prepare command that append a character in file at each new run
    temporary_filename1 = util.id_generator(size=7)
    cmd = "echo -n 'p' >> {0} && grep 'pppp' {0}".format(temporary_filename1)

    # command should still raise exception after 2 retries(=3 runs) as we except 4 p
    with pytest.raises(exception.RunCmdError) as exc_info:
        gateway_session.run_cmd(cmd, retry=2, retry_interval=1)
    assert exc_info.value.runs_nb == 3

    # same command should work fine with 3 retries(=4 runs)
    temporary_filename2 = util.id_generator(size=8)
    cmd = cmd.replace(temporary_filename1, temporary_filename2)
    result = gateway_session.run_cmd(cmd, retry=3, retry_interval=1)

    # by default no history kept
    assert len(result.result_list) == 0

    # check history is kept when requested
    temporary_filename3 = util.id_generator(size=8)
    cmd = cmd.replace(temporary_filename2, temporary_filename3)
    result = gateway_session.run_cmd(cmd,
                                     retry=3,
                                     retry_interval=1,
                                     keep_retry_history=True)
    assert len(result.result_list) == 4
コード例 #4
0
def test_get(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')
    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()

    remotehost_ip, remotehost_port = docker_env.get_host_ip_port('remotehost')
    remotehost_session = gateway_session.get_remote_session(
        host=tests_util.get_host_ip(),
        port=remotehost_port,
        username='******',
        password='******')

    # create random file on remote host and ensure it is properly there
    remote_path = "remote_file"
    remotehost_session.file(remote_path=remote_path,
                            content=json.dumps(
                                tests_util.create_random_json()))
    assert remotehost_session.exists(remote_path)

    # download that file in local folder
    local_folder = '/tmp/'
    remotehost_session.get(remote_path=remote_path, local_path=local_folder)
    local_file_path = os.path.join(local_folder, os.path.basename(remote_path))
    assert os.path.isfile(local_file_path)
    os.remove(local_file_path)

    # download that file locally specifying local filename
    local_file_path = '/tmp/downloaded_file_' + util.id_generator(size=20)
    remotehost_session.get(remote_path=remote_path, local_path=local_file_path)
    os.remove(local_file_path)

    # get remote file from location not accessible from current user and owned by root
    local_folder = '/tmp/'
    restricted_remote_path = os.path.join('/etc', remote_path)
    remotehost_session.run_cmd([
        'sudo mv %s %s' % (remote_path, restricted_remote_path),
        'sudo chown root:root %s' % restricted_remote_path,
        'sudo chmod 700 %s' % restricted_remote_path,
    ])
    remotehost_session.get(remote_path=restricted_remote_path,
                           local_path=local_folder,
                           use_sudo=True)
    local_file_path = os.path.join(local_folder, os.path.basename(remote_path))
    assert os.path.isfile(local_file_path)
    os.remove(local_file_path)