def test_local_file_already_exists( self, dcos_node: Node, tmp_path: Path, ) -> None: """ Downloading a file raises a ``ValueError`` if the local file path already exists. """ content = str(uuid.uuid4()) random = uuid.uuid4().hex local_file_name = 'local_file_{random}.txt'.format(random=random) local_file_path = tmp_path / local_file_name local_file_path.write_text(content) remote_file_name = 'remote_file_{random}.txt'.format(random=random) remote_file_path = Path('/etc/') / remote_file_name dcos_node.send_file( local_path=local_file_path, remote_path=remote_file_path, ) message = ('Failed to download a file to "{file}". ' 'A file already exists in that location.').format( file=local_file_path) with pytest.raises(ValueError) as exc: dcos_node.download_file( remote_path=remote_file_path, local_path=local_file_path, ) assert str(exc.value) == message
def test_custom_user( self, dcos_node: Node, tmpdir: local, ) -> None: """ It is possible to send a file to a cluster node as a custom user. """ testuser = str(uuid.uuid4().hex) dcos_node.run(args=['useradd', testuser]) dcos_node.run( args=['cp', '-R', '$HOME/.ssh', '/home/{}/'.format(testuser)], shell=True, ) random = str(uuid.uuid4()) local_file = tmpdir.join('example_file.txt') local_file.write(random) master_destination_dir = '/home/{testuser}/{random}'.format( testuser=testuser, random=random, ) master_destination_path = Path(master_destination_dir) / 'file.txt' dcos_node.send_file( local_path=Path(str(local_file)), remote_path=master_destination_path, user=testuser, ) args = ['stat', '-c', '"%U"', str(master_destination_path)] result = dcos_node.run(args=args, shell=True) assert result.stdout.decode().strip() == testuser # Implicitly asserts SSH connection closed by ``send_file``. dcos_node.run(args=['userdel', '-r', testuser])
def test_send_file_to_directory( self, dcos_node: Node, tmpdir: local, ) -> None: """ It is possible to send a file to a cluster node to a directory that is mounted as tmpfs. See ``DockerExecTransport.send_file`` for details. """ content = str(uuid.uuid4()) file_name = 'example_file.txt' local_file = tmpdir.join(file_name) local_file.write(content) master_destination_path = Path( '/etc/{random}'.format(random=uuid.uuid4().hex), ) dcos_node.run(args=['mkdir', '--parent', str(master_destination_path)]) dcos_node.send_file( local_path=Path(str(local_file)), remote_path=master_destination_path, ) args = ['cat', str(master_destination_path / file_name)] result = dcos_node.run(args=args) assert result.stdout.decode() == content
def test_file_to_file( self, dcos_node: Node, tmp_path: Path, ) -> None: """ It is possible to download a file from a node to a file path. """ content = str(uuid.uuid4()) random = uuid.uuid4().hex local_file_name = 'local_file_{random}.txt'.format(random=random) remote_file_name = 'remote_file_{random}.txt'.format(random=random) remote_file_path = Path('/etc/') / remote_file_name downloaded_file_name = 'downloaded_file_{random}.txt'.format( random=random, ) downloaded_file_path = tmp_path / downloaded_file_name local_file = tmp_path / local_file_name local_file.write_text(content) dcos_node.send_file( local_path=local_file, remote_path=remote_file_path, ) dcos_node.download_file( remote_path=remote_file_path, local_path=downloaded_file_path, ) assert downloaded_file_path.read_text() == content
def test_sudo( self, dcos_node: Node, tmpdir: local, ) -> None: """ It is possible to use sudo to send a file to a directory which the user does not have access to. """ testuser = str(uuid.uuid4().hex) dcos_node.run(args=['useradd', testuser]) dcos_node.run( args=['cp', '-R', '$HOME/.ssh', '/home/{}/'.format(testuser)], shell=True, ) sudoers_line = '{user} ALL=(ALL) NOPASSWD: ALL'.format(user=testuser) dcos_node.run( args=['echo "' + sudoers_line + '">> /etc/sudoers'], shell=True, ) random = str(uuid.uuid4()) local_file = tmpdir.join('example_file.txt') local_file.write(random) master_destination_dir = '/etc/{testuser}/{random}'.format( testuser=testuser, random=random, ) master_destination_path = Path(master_destination_dir) / 'file.txt' with pytest.raises(CalledProcessError): dcos_node.send_file( local_path=Path(str(local_file)), remote_path=master_destination_path, user=testuser, ) dcos_node.send_file( local_path=Path(str(local_file)), remote_path=master_destination_path, user=testuser, sudo=True, ) args = ['stat', '-c', '"%U"', str(master_destination_path)] result = dcos_node.run(args=args, shell=True) assert result.stdout.decode().strip() == 'root' # Implicitly asserts SSH connection closed by ``send_file``. dcos_node.run(args=['userdel', '-r', testuser])
def test_send_file( self, dcos_node: Node, tmpdir: local, ) -> None: """ It is possible to send a file to a cluster node as the default user. """ content = str(uuid.uuid4()) local_file = tmpdir.join('example_file.txt') local_file.write(content) master_destination_path = Path('/etc/new_dir/on_master_node.txt') dcos_node.send_file( local_path=Path(str(local_file)), remote_path=master_destination_path, ) args = ['cat', str(master_destination_path)] result = dcos_node.run(args=args) assert result.stdout.decode() == content
def test_send_symlink(self, dcos_node: Node, tmpdir: local) -> None: """ If sending the path to a symbolic link, the link's target is sent. """ random = str(uuid.uuid4()) dir_containing_real_file = tmpdir.mkdir(uuid.uuid4().hex) dir_containing_symlink = tmpdir.mkdir(uuid.uuid4().hex) local_file = dir_containing_real_file.join('example_file.txt') local_file.write(random) symlink_file = dir_containing_symlink.join('symlink.txt') symlink_file_path = Path(str(symlink_file)) symlink_file_path.symlink_to(target=Path(str(local_file))) master_destination_dir = '/etc/{random}'.format(random=random) master_destination_path = Path(master_destination_dir) / 'file.txt' dcos_node.send_file( local_path=symlink_file_path, remote_path=master_destination_path, ) args = ['cat', str(master_destination_path)] result = dcos_node.run(args=args) assert result.stdout.decode() == random
def test_send_file_to_tmp_directory( self, dcos_node: Node, tmpdir: local, ) -> None: """ It is possible to send a file to a cluster node to a directory that is mounted as tmpfs. See ``DockerExecTransport.send_file`` for details. """ content = str(uuid.uuid4()) local_file = tmpdir.join('example_file.txt') local_file.write(content) master_destination_path = Path('/tmp/mydir/on_master_node.txt') dcos_node.send_file( local_path=Path(str(local_file)), remote_path=master_destination_path, ) args = ['cat', str(master_destination_path)] result = dcos_node.run(args=args) assert result.stdout.decode() == content
def _send_tarstream_to_node_and_extract( tarstream: io.BytesIO, node: Node, remote_path: Path, ) -> None: """ Given a tarstream, send the contents to a remote path. """ tar_path = Path('/tmp/dcos_e2e_tmp.tar') with tempfile.NamedTemporaryFile() as tmp_file: tmp_file.write(tarstream.getvalue()) tmp_file.flush() node.send_file( local_path=Path(tmp_file.name), remote_path=tar_path, ) tar_args = ['tar', '-C', str(remote_path), '-xvf', str(tar_path)] node.run(args=tar_args) node.run(args=['rm', str(tar_path)])
def test_send_file( self, dcos_node: Node, tmp_path: Path, ) -> None: """ It is possible to send a file to a cluster node as the default user. """ content = str(uuid.uuid4()) local_file = tmp_path / 'example_file.txt' local_file.write_text(content) random = uuid.uuid4().hex master_destination_dir = '/etc/{random}'.format(random=random) master_destination_path = Path(master_destination_dir) / 'file.txt' dcos_node.send_file( local_path=local_file, remote_path=master_destination_path, ) args = ['cat', str(master_destination_path)] result = dcos_node.run(args=args) assert result.stdout.decode() == content
def test_send_directory( self, dcos_node: Node, tmp_path: Path, ) -> None: """ It is possible to send a directory to a cluster node as the default user. """ original_content = str(uuid.uuid4()) dir_name = 'directory' file_name = 'example_file.txt' dir_path = tmp_path / dir_name dir_path.mkdir() local_file_path = dir_path / file_name local_file_path.write_text(original_content) random = uuid.uuid4().hex master_base_dir = '/etc/{random}'.format(random=random) master_destination_dir = Path(master_base_dir) dcos_node.send_file( local_path=local_file_path, remote_path=master_destination_dir / dir_name / file_name, ) args = ['cat', str(master_destination_dir / dir_name / file_name)] result = dcos_node.run(args=args) assert result.stdout.decode() == original_content new_content = str(uuid.uuid4()) local_file_path.write_text(new_content) dcos_node.send_file( local_path=dir_path, remote_path=master_destination_dir, ) args = ['cat', str(master_destination_dir / dir_name / file_name)] result = dcos_node.run(args=args) assert result.stdout.decode() == new_content