Files
dokku/plugins/storage/storage_test.go
T
Jose Diaz-Gonzalez cbde55a4a0 fix: respect DOKKU_LIB_HOST_ROOT for storage bind mounts
When Dokku runs in Docker with a shared socket, create/chown still use
DOKKU_LIB_ROOT inside the container, but Docker -v flags for storage:exec
and deploy must use DOKKU_LIB_HOST_ROOT so the daemon mounts the host data
volume instead of the real host's /var/lib/dokku.

Closes #9126.
2026-10-16 11:52:01 -04:00

153 lines
4.9 KiB
Go

package storage
import (
"testing"
. "github.com/onsi/gomega"
)
func TestVerifyPathsAbsolutePath(t *testing.T) {
RegisterTestingT(t)
Expect(VerifyPaths("/host/path:/container/path")).To(Succeed())
Expect(VerifyPaths("/var/lib/dokku/data/storage/test:/app/data")).To(Succeed())
}
func TestVerifyPathsNamedVolume(t *testing.T) {
RegisterTestingT(t)
Expect(VerifyPaths("volume_name:/container/path")).To(Succeed())
Expect(VerifyPaths("my-volume:/app/data")).To(Succeed())
Expect(VerifyPaths("my.volume:/app/data")).To(Succeed())
}
func TestVerifyPathsInvalid(t *testing.T) {
RegisterTestingT(t)
err := VerifyPaths("/host/path")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Storage path must be two valid paths divided by colon"))
err = VerifyPaths("a:/container")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Volume name must be two characters or more"))
err = VerifyPaths("-invalid:/container")
Expect(err).To(HaveOccurred())
}
func TestValidateDirectoryName(t *testing.T) {
RegisterTestingT(t)
Expect(ValidateDirectoryName("myapp")).To(Succeed())
Expect(ValidateDirectoryName("my-app")).To(Succeed())
Expect(ValidateDirectoryName("my_app")).To(Succeed())
Expect(ValidateDirectoryName("MyApp123")).To(Succeed())
}
func TestValidateDirectoryNameInvalid(t *testing.T) {
RegisterTestingT(t)
err := ValidateDirectoryName("")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Please specify a directory"))
err = ValidateDirectoryName("@invalid")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Directory can only contain"))
err = ValidateDirectoryName("my/app")
Expect(err).To(HaveOccurred())
err = ValidateDirectoryName("my app")
Expect(err).To(HaveOccurred())
}
func TestParseMountPath(t *testing.T) {
RegisterTestingT(t)
entry := ParseMountPath("/host/path:/container/path")
Expect(entry.HostPath).To(Equal("/host/path"))
Expect(entry.ContainerPath).To(Equal("/container/path"))
Expect(entry.Readonly).To(BeFalse())
Expect(entry.VolumeOptions).To(BeEmpty())
entry = ParseMountPath("/host/path:/container/path:ro")
Expect(entry.HostPath).To(Equal("/host/path"))
Expect(entry.ContainerPath).To(Equal("/container/path"))
Expect(entry.Readonly).To(BeTrue())
Expect(entry.VolumeOptions).To(BeEmpty())
entry = ParseMountPath("/host/path:/container/path:Z")
Expect(entry.HostPath).To(Equal("/host/path"))
Expect(entry.ContainerPath).To(Equal("/container/path"))
Expect(entry.Readonly).To(BeFalse())
Expect(entry.VolumeOptions).To(Equal("Z"))
entry = ParseMountPath("/host/path:/container/path:ro,Z")
Expect(entry.HostPath).To(Equal("/host/path"))
Expect(entry.ContainerPath).To(Equal("/container/path"))
Expect(entry.Readonly).To(BeTrue())
Expect(entry.VolumeOptions).To(Equal("Z"))
entry = ParseMountPath("/host/path:/container/path:noexec,nosuid,ro")
Expect(entry.Readonly).To(BeTrue())
Expect(entry.VolumeOptions).To(Equal("noexec,nosuid"))
entry = ParseMountPath("volume_name:/container/path")
Expect(entry.HostPath).To(Equal("volume_name"))
Expect(entry.ContainerPath).To(Equal("/container/path"))
Expect(entry.Readonly).To(BeFalse())
Expect(entry.VolumeOptions).To(BeEmpty())
}
func TestGetStorageDirectory(t *testing.T) {
RegisterTestingT(t)
dir := GetStorageDirectory()
Expect(dir).To(ContainSubstring("data/storage"))
}
func TestHostPathForDocker(t *testing.T) {
RegisterTestingT(t)
t.Setenv("DOKKU_LIB_ROOT", "/var/lib/dokku")
t.Run("unset host root leaves path unchanged", func(t *testing.T) {
RegisterTestingT(t)
t.Setenv("DOKKU_LIB_HOST_ROOT", "")
Expect(HostPathForDocker("/var/lib/dokku/data/storage/foo")).To(Equal("/var/lib/dokku/data/storage/foo"))
})
t.Run("equal host root leaves path unchanged", func(t *testing.T) {
RegisterTestingT(t)
t.Setenv("DOKKU_LIB_HOST_ROOT", "/var/lib/dokku")
Expect(HostPathForDocker("/var/lib/dokku/data/storage/foo")).To(Equal("/var/lib/dokku/data/storage/foo"))
})
t.Run("rewrites paths under lib root", func(t *testing.T) {
RegisterTestingT(t)
t.Setenv("DOKKU_LIB_HOST_ROOT", "/host/var/lib/dokku")
Expect(HostPathForDocker("/var/lib/dokku/data/storage/foo")).To(Equal("/host/var/lib/dokku/data/storage/foo"))
Expect(HostPathForDocker("/var/lib/dokku")).To(Equal("/host/var/lib/dokku"))
})
t.Run("leaves named volumes unchanged", func(t *testing.T) {
RegisterTestingT(t)
t.Setenv("DOKKU_LIB_HOST_ROOT", "/host/var/lib/dokku")
Expect(HostPathForDocker("myvolume")).To(Equal("myvolume"))
})
t.Run("leaves custom absolute paths unchanged", func(t *testing.T) {
RegisterTestingT(t)
t.Setenv("DOKKU_LIB_HOST_ROOT", "/host/var/lib/dokku")
Expect(HostPathForDocker("/srv/demo")).To(Equal("/srv/demo"))
})
t.Run("does not false-match similar prefixes", func(t *testing.T) {
RegisterTestingT(t)
t.Setenv("DOKKU_LIB_HOST_ROOT", "/host/var/lib/dokku")
Expect(HostPathForDocker("/var/lib/dokku-extra/data")).To(Equal("/var/lib/dokku-extra/data"))
})
}