Skip to content

Conversation

@pfuntner
Copy link

@pfuntner pfuntner commented Dec 19, 2023

On Linux systems, if File.mode is used on a file that's a symlink, it will return 0x777 because that's the mode of the symlink but mode is pretty meaningless for a symlink. By doing stat -L ... the command will automatically resolve symlinks.

I know there is the BSDFile implementation too but I don't believe I have access to a BSD system to try things out.

This addresses a problem where testinfra is used to get information on a target file but you don't want to worry about whether a symbolic link is involved - it's irrelevant. There are examples of symlinks in the Amazon Linux 2023 container:

$ docker run -it --rm amazonlinux:2023 Unable to find image 'amazonlinux:2023' locally 2023: Pulling from library/amazonlinux 1db371c0a72a: Pull complete Digest: sha256:355f1638075375e4db3a0f7aa9cd73f79fb1a738b72035bc66a3cfda30e0053b Status: Downloaded newer image for amazonlinux:2023 bash-5.2# ls -ld /etc/issue lrwxrwxrwx 1 root root 16 Dec 13 00:00 /etc/issue -> ../usr/lib/issue bash-5.2# stat /etc/issue File: /etc/issue -> ../usr/lib/issue Size: 16 Blocks: 0 IO Block: 4096 symbolic link Device: 37h/55d Inode: 4903000 Links: 1 Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2023-12-13 00:00:03.000000000 +0000 Modify: 2023-12-13 00:00:03.000000000 +0000 Change: 2023-12-20 11:54:00.744190148 +0000 Birth: 2023-12-20 11:54:00.744190148 +0000 bash-5.2# stat -c %a /etc/issue 777 bash-5.2# ls -Lld /etc/issue -rw-r--r-- 1 root root 28 Dec 13 00:00 /etc/issue bash-5.2# stat -L /etc/issue File: /etc/issue Size: 28 Blocks: 8 IO Block: 4096 regular file Device: 37h/55d Inode: 4904219 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2023-12-13 00:00:03.000000000 +0000 Modify: 2023-12-13 00:00:03.000000000 +0000 Change: 2023-12-20 11:54:00.992194752 +0000 Birth: 2023-12-20 11:54:00.992194752 +0000 bash-5.2# stat -Lc %a /etc/issue 644 bash-5.2# 

I ran the tests via tox and they all passed.

This is my first pytest-testinfra pull request and I would be happy to rework it however it needs to be done.

@pfuntner pfuntner marked this pull request as ready for review December 19, 2023 17:45
On Linux systems, if File.mode is used on a file that's a symlink, it will return 0x777 because that's the "mode" of the symlink but mode is pretty meaningless for a symlink. By doing `stat -L ...` the command will automatically resolve symlinks.
@philpep philpep merged commit d2bcd9f into pytest-dev:main Feb 15, 2024
@philpep
Copy link
Contributor

philpep commented Feb 15, 2024

Merged, thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants