Skip to content

Commit

Permalink
Merge pull request #38 from leventyalcin/fix/assert_file_owner_requir…
Browse files Browse the repository at this point in the history
…es_sudo

Fix/assert file owner requires sudo
  • Loading branch information
martin-schulze-vireso authored Mar 6, 2022
2 parents 55c72f2 + fdd71e8 commit bb53897
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vagrant
# vagrant virtualbox image creates this log file. so, better to ignore
*-cloudimg-console.log
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,50 @@ path : <temp>/path/to/non-existent-file
--
```

## **Development**

No one would want to develop piece of bash dependant libraries on their laptops due to single mistake (globbing for instance) can cause a disaster. In order to prevent this there is a Vagrantfile that you can use.

In order to start development environment, you have to take two steps;

```shell
user@localhost:~/bats-file$ vagrant up
```

The line above spins up a brand new virtualbox image and provisions with prerequisites.

However, as the tests require not to be on a network share due to running commands eg: `mknod`, the files are shared into the VM by `rsync` module. Rsync in vagrant only runs initialy and stops. During the active development, you regularly change files and might want to see the impact. To achive that, you have to use auto rsync.

> `auto-rsync` is a long running command. It means that it has to run on terminal screen as long as the VM is up and running. So, you have to keep this in a dedicated terminal screen.
After bringing up the VM, you can simply run following command;

```shell
user@localhost:~/bats-file$ vagrant rsync-auto
```

**WARNING! WARNING! WARNING!:** There will be a small delay between file save and sync. When you save a file please keep eye on the terminal window/pane and sync is triggered and finished. Simply run your next test with 5s-10s delay.

The repo files can be found under `/home/vagrant/bats-file` and you can run tests with

```shell
user@localhost:~/bats-file$ bats test
# or
user@localhost:~/bats-file$ bats test/on-particular-test.bats
```

Once you're done with development, you can simply turn the VM off with

```shell
user@localhost:~/bats-file$ vagrant halt
```

### Why?

- Why Vagrant, not Docker
- This replicates the 100% CI
- Why EoL Xenial image
- This replicates the 100% CI. Also, one step at a time. This wasn't in place at all. However, this change will bring up other concerns. So, that problems should be solved at another time.

<!-- REFERENCES -->

Expand Down
36 changes: 36 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "public_network"
config.vm.synced_folder ".", "/home/vagrant/bats-file/", type: "rsync",
rsync__exclude: [
".git/",
".gitignore",
"*.yml",
"*.md",
"LICENSE",
"*.json",
"*.log",
"Vagrantfile",
],
rsync__args: ["--verbose", "--archive", "--delete", "-z"]

config.vm.provider "virtualbox" do |vb|
# vb.gui = true
vb.memory = "1024"
end
config.vm.provision "shell", privileged: true, inline: <<-SHELL
apt-get update
apt-get install -y git vim curl
ln -s /usr/bin/python3 /usr/bin/python
SHELL

config.vm.provision "shell", privileged: false, inline: <<-SHELL
mkdir -p $HOME/.local/bin
echo 'export PATH="$PATH:$HOME/.local/bin"' >> /home/vagrant/.bashrc
git clone --depth 1 https://github.com/bats-core/bats-support /home/vagrant/bats-support
/home/vagrant/bats-file/script/install-bats.sh
SHELL
end
21 changes: 7 additions & 14 deletions src/file.bash
Original file line number Diff line number Diff line change
Expand Up @@ -283,27 +283,20 @@ assert_files_equal() {
assert_file_owner() {
local -r owner="$1"
local -r file="$2"
if [[ `uname` == "Darwin" ]]; then
sudo chown root ${TEST_FIXTURE_ROOT}/dir/owner
sudo chown daemon ${TEST_FIXTURE_ROOT}/dir/notowner
if [ `stat -f '%Su' "$file"` != "$owner" ]; then
local -r rem="$BATSLIB_FILE_PATH_REM"
local -r add="$BATSLIB_FILE_PATH_ADD"
batslib_print_kv_single 4 'path' "${file/$rem/$add}" \
| batslib_decorate "user $owner is not the owner of the file" \
| fail
if [[ "$(uname)" == "Darwin" ]]; then
__cmd_param="-f %Su"
elif [[ "$(uname)" == "Linux" ]]; then
__cmd_param="-c %U"
fi
elif [[ `uname` == "Linux" ]]; then
sudo chown root ${TEST_FIXTURE_ROOT}/dir/owner
sudo chown daemon ${TEST_FIXTURE_ROOT}/dir/notowner
if [ `stat -c "%U" "$file"` != "$owner" ]; then
__o=$(stat $__cmd_param "$file")

if [[ "$__o" != "$owner" ]]; then
local -r rem="$BATSLIB_FILE_PATH_REM"
local -r add="$BATSLIB_FILE_PATH_ADD"
batslib_print_kv_single 4 'path' "${file/$rem/$add}" \
| batslib_decorate "user $owner is not the owner of the file" \
| fail
fi
fi
}

# Fail if file does not have given permissions. This
Expand Down
25 changes: 21 additions & 4 deletions test/59-assert-10-assert_file_owner.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,38 @@ load 'test_helper'
fixtures 'exist'

setup () {
touch ${TEST_FIXTURE_ROOT}/dir/owner ${TEST_FIXTURE_ROOT}/dir/notowner
touch ${TEST_FIXTURE_ROOT}/dir/owner ${TEST_FIXTURE_ROOT}/dir/notowner
sudo_path=$(command -v sudo 2>/dev/null)
# There is PATH addition to /usr/sbin which won't come by default on bash3
# on macOS
chown_path=$(PATH="$PATH:/usr/sbin" command -v chown 2>/dev/null)
if [[ "$(whoami)" != 'root' ]] && [ -x "$sudo_path" ]; then
__cmd="$sudo_path $chown_path"
else
__cmd="$chown_path"
fi
$__cmd root ${TEST_FIXTURE_ROOT}/dir/owner
$__cmd daemon ${TEST_FIXTURE_ROOT}/dir/notowner
}

teardown () {
rm -f ${TEST_FIXTURE_ROOT}/dir/owner ${TEST_FIXTURE_ROOT}/dir/notowner
sudo_path=$(command -v sudo 2>/dev/null)
if [[ "$(whoami)" != 'root' ]] && [ -x "$sudo_path" ]; then
__cmd="$sudo_path rm -f"
else
__cmd="rm -f"
fi
$__cmd ${TEST_FIXTURE_ROOT}/dir/owner ${TEST_FIXTURE_ROOT}/dir/notowner
}


# Correctness
@test 'assert_file_owner() <file>: returns 0 if <file> user root is the owner of the file' {
local -r owner="root"
local -r file="${TEST_FIXTURE_ROOT}/dir/owner"
run assert_file_owner "$owner" "$file"
[ "$status" -eq 0 ]
echo ${#lines[@]}
[ "${#lines[@]}" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]

}

Expand Down
22 changes: 20 additions & 2 deletions test/59-assert-11-assert_not_file_owner.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,28 @@ load 'test_helper'
fixtures 'exist'

setup () {
touch ${TEST_FIXTURE_ROOT}/dir/owner ${TEST_FIXTURE_ROOT}/dir/notowner
touch ${TEST_FIXTURE_ROOT}/dir/owner ${TEST_FIXTURE_ROOT}/dir/notowner
sudo_path=$(command -v sudo 2>/dev/null)
# There is PATH addition to /usr/sbin which won't come by default on bash3
# on macOS
chown_path=$(PATH="$PATH:/usr/sbin" command -v chown 2>/dev/null)
if [[ "$(whoami)" != 'root' ]] && [ -x "$sudo_path" ]; then
__cmd="$sudo_path $chown_path"
else
__cmd="$chown_path"
fi
$__cmd root ${TEST_FIXTURE_ROOT}/dir/owner
$__cmd daemon ${TEST_FIXTURE_ROOT}/dir/notowner
}

teardown () {
rm -f ${TEST_FIXTURE_ROOT}/dir/owner ${TEST_FIXTURE_ROOT}/dir/notowner
sudo_path=$(command -v sudo 2>/dev/null)
if [[ "$(whoami)" != 'root' ]] && [ -x "$sudo_path" ]; then
__cmd="$sudo_path rm -f"
else
__cmd="rm -f"
fi
$__cmd ${TEST_FIXTURE_ROOT}/dir/owner ${TEST_FIXTURE_ROOT}/dir/notowner
}

# Correctness
Expand Down

0 comments on commit bb53897

Please sign in to comment.