flamenco/internal/manager/local_storage/local_storage_test.go
Sybren A. Stüvel 27a6dde708 Manager: add local_storage package for managing storage locations
Add a `local_storage` package that finds a suitable place to put files.
Currently it just looks at the location of the currently running
executable; it can later do other things. It can be queried for directory
to put job-specific files.

It is intended to be used by the under-development "last rendered output"
processing system, to store an image file per job. Later we should also
refactor the task log handling system to use this.
2022-06-23 16:45:38 +02:00

59 lines
1.8 KiB
Go

package local_storage
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewNextToExe(t *testing.T) {
si := NewNextToExe("nø ASCïÏ")
// Unit test executables typically are in `/tmp/go-build{random number}`.
assert.Contains(t, si.rootPath, "go-build")
assert.Equal(t, filepath.Base(si.rootPath), "nø ASCïÏ",
"the real path should end in the given directory name")
}
func TestNewNextToExe_noSubdir(t *testing.T) {
exePath, err := os.Executable()
if !assert.NoError(t, err) {
t.FailNow()
}
exeName := filepath.Base(exePath)
// The filesystem in an empty "subdirectory" next to the executable should
// contain the executable.
si := NewNextToExe("")
_, err = os.Stat(filepath.Join(si.rootPath, exeName))
assert.NoErrorf(t, err, "should be able to stat this executable %s", exeName)
}
func TestForJob(t *testing.T) {
si := NewNextToExe("task-logs")
jobPath := si.ForJob("08e126ef-d773-468b-8bab-19a8213cf2ff")
expectedSuffix := filepath.Join("task-logs", "job-08e1", "08e126ef-d773-468b-8bab-19a8213cf2ff")
hasSuffix := strings.HasSuffix(jobPath, expectedSuffix)
assert.Truef(t, hasSuffix, "expected %s to have suffix %s", jobPath, expectedSuffix)
}
func TestErase(t *testing.T) {
si := NewNextToExe("task-logs")
assert.NoDirExists(t, si.rootPath, "creating a StorageInfo should not create the directory")
jobPath := si.ForJob("08e126ef-d773-468b-8bab-19a8213cf2ff")
assert.NoDirExists(t, jobPath, "getting a path should not create it")
assert.NoError(t, os.MkdirAll(jobPath, os.ModePerm))
assert.DirExists(t, jobPath, "os.MkdirAll is borked")
assert.NoError(t, si.Erase())
assert.NoDirExists(t, si.rootPath, "Erase() should erase the root path, and everything in it")
}