Recommended refactors

- make sure spec sets new cache dir
- move clean out of api
- capture warewulfconf.Paths.Cachedir+"/warewulf" in once place
- split Clean() into multiple functions
- remove WriteFileAbs

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2024-10-25 17:46:42 -06:00
parent ff84974506
commit fbd5ca9895
11 changed files with 54 additions and 64 deletions

View File

@@ -9,7 +9,6 @@ import (
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"
@@ -42,7 +41,7 @@ const Systemddir = "usr/lib/systemd/system"
const WWOverlaydir = "var/lib/warewulf/overlays"
const WWChrootdir = "var/lib/warewulf/chroots"
const WWProvisiondir = "srv/warewulf"
const Cachedir = "cache"
const Cachedir = "var/cache"
// New creates a test environment in a temporary directory and configures
// Warewulf to use it.
@@ -164,26 +163,3 @@ func (env *TestEnv) RemoveAll(t *testing.T) {
err := os.RemoveAll(env.BaseDir)
assert.NoError(t, err)
}
// Writes to absolute path, but checks if given file name
// is within testenv.
//
// Asserts no errors occur.
func (env *TestEnv) WriteFileAbs(t *testing.T, fileName string, content string) {
ok := strings.HasPrefix(fileName, env.BaseDir)
if !ok {
assert.Fail(t, "given filename is not in testenv")
}
dirName := filepath.Dir(fileName)
err := os.MkdirAll(dirName, 0755)
assert.NoError(t, err)
f, err := os.Create(fileName)
assert.NoError(t, err)
defer f.Close()
_, err = f.WriteString(content)
assert.NoError(t, err)
err = os.Chtimes(fileName,
time.Date(2006, time.February, 1, 3, 4, 5, 0, time.UTC),
time.Date(2006, time.February, 1, 3, 4, 5, 0, time.UTC))
assert.NoError(t, err)
}