Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quadlet - treat paths starting with systemd specifiers as absolute #17930

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion pkg/systemd/quadlet/quadlet.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,8 +968,22 @@ func addNetworks(quadletUnitFile *parser.UnitFile, groupName string, serviceUnit
}
}

// Systemd Specifiers start with % with the exception of %%
func startsWithSystemdSpecifier(filePath string) bool {
if len(filePath) == 0 || filePath[0] != '%' {
return false
}

if len(filePath) > 1 && filePath[1] == '%' {
return false
}

return true
}

func getAbsolutePath(quadletUnitFile *parser.UnitFile, filePath string) (string, error) {
if !filepath.IsAbs(filePath) {
// When the path starts with a Systemd specifier do not resolve what looks like a relative address
if !startsWithSystemdSpecifier(filePath) && !filepath.IsAbs(filePath) {
if len(quadletUnitFile.Path) > 0 {
filePath = filepath.Join(filepath.Dir(quadletUnitFile.Path), filePath)
} else {
Expand Down
5 changes: 4 additions & 1 deletion test/e2e/quadlet/env-file.container
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
## assert-podman-final-args localhost/imagename
## assert-podman-args --env-file /opt/env/abs-1 --env-file /opt/env/abs-2
## assert-podman-args --env-file /opt/env/abs-1
## assert-podman-args --env-file /opt/env/abs-2
## assert-podman-args-regex --env-file /.*/podman_test.*/quadlet/rel-1
## assert-podman-args --env-file %h/env

[Container]
Image=localhost/imagename
EnvironmentFile=/opt/env/abs-1
EnvironmentFile=/opt/env/abs-2
EnvironmentFile=rel-1
EnvironmentFile=%h/env
4 changes: 3 additions & 1 deletion test/e2e/quadlet/volume.container
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
## assert-podman-args -v /host/dir2:/container/volume2:Z
## assert-podman-args-regex -v .*/podman_test.*/quadlet/host/dir3:/container/volume3
## assert-podman-args -v named:/container/named
## assert-podman-args -v systemd-quadlet:/container/quadlet localhost/imagename
## assert-podman-args -v systemd-quadlet:/container/quadlet
## assert-podman-args -v %h/container:/container/volume4

[Container]
Image=localhost/imagename
Expand All @@ -12,3 +13,4 @@ Volume=./host/dir3:/container/volume3
Volume=/container/empty
Volume=named:/container/named
Volume=quadlet.volume:/container/quadlet
Volume=%h/container:/container/volume4
24 changes: 24 additions & 0 deletions test/system/252-quadlet.bats
Original file line number Diff line number Diff line change
Expand Up @@ -558,4 +558,28 @@ EOF
remove_secret $SECRET_NAME
}

@test "quadlet - volume path using specifier" {
local tmp_path=$(mktemp -d --tmpdir=$PODMAN_TMPDIR quadlet.volume.XXXXXX)
local tmp_dir=${tmp_path#/tmp/}
local file_name="f$(random_string 10).txt"
local file_content="data_$(random_string 15)"
echo $file_content > $tmp_path/$file_name

local quadlet_file=$PODMAN_TMPDIR/basic_$(random_string).container
cat > $quadlet_file <<EOF
[Container]
Image=$IMAGE
Volume=%T/$tmp_dir:/test_content:Z
Exec=sh -c "echo STARTED CONTAINER; echo "READY=1" | socat -u STDIN unix-sendto:\$NOTIFY_SOCKET; top"
EOF

run_quadlet "$quadlet_file"
service_setup $QUADLET_SERVICE_NAME

run_podman exec $QUADLET_CONTAINER_NAME /bin/sh -c "cat /test_content/$file_name"
is "$output" $file_content

rm -rf $tmp_path
}

# vim: filetype=sh