use tempdir pkg to manage test files
Some checks failed
Lint / golangci-lint (push) Failing after 10m24s
Declarative Tests / test (push) Failing after 13s
Declarative Tests / build-fedora (push) Successful in 5m5s
Declarative Tests / build-ubuntu-focal (push) Successful in 1m54s

This commit is contained in:
Matthew Rich 2024-09-27 00:54:27 +00:00
parent 204ba078a0
commit 2fd1764cce
3 changed files with 39 additions and 36 deletions

View File

@ -195,8 +195,10 @@ func (f *File) Notify(m *machine.EventMessage) {
if triggerErr := f.StateMachine().Trigger("created"); triggerErr == nil { if triggerErr := f.StateMachine().Trigger("created"); triggerErr == nil {
return return
} }
} else {
f.State = "absent"
panic(e)
} }
f.State = "absent"
case "start_delete": case "start_delete":
if deleteErr := f.Delete(ctx); deleteErr == nil { if deleteErr := f.Delete(ctx); deleteErr == nil {
if triggerErr := f.StateMachine().Trigger("deleted"); triggerErr == nil { if triggerErr := f.StateMachine().Trigger("deleted"); triggerErr == nil {
@ -417,7 +419,7 @@ func (f *File) Create(ctx context.Context) error {
} }
gid, gidErr := LookupGID(f.Group) gid, gidErr := LookupGID(f.Group)
if gidErr != nil { if gidErr != nil {
return gidErr return fmt.Errorf("%w: unkwnon group %d", ErrInvalidFileGroup, gid)
} }
mode, modeErr := f.Mode.GetMode() mode, modeErr := f.Mode.GetMode()

View File

@ -32,16 +32,17 @@ func TestNewFileResource(t *testing.T) {
} }
func TestNewFileNormalized(t *testing.T) { func TestNewFileNormalized(t *testing.T) {
file := fmt.Sprintf("%s/%s", TempDir, "bar/../fooread.txt") indirectFile := fmt.Sprintf("%s/%s", string(TempDir), "bar/../fooread.txt")
absFilePath,_ := filepath.Abs(file) absFilePath,_ := filepath.Abs(indirectFile)
f := NewNormalizedFile() f := NewNormalizedFile()
assert.NotNil(t, f) assert.NotNil(t, f)
assert.Nil(t, f.SetURI("file://" + file)) assert.Nil(t, f.SetURI("file://" + indirectFile))
assert.NotEqual(t, file, f.Path) assert.NotEqual(t, indirectFile, f.Path)
assert.Equal(t, absFilePath, f.Path) assert.Equal(t, absFilePath, f.Path)
assert.NotEqual(t, "file://" + file, f.URI()) assert.NotEqual(t, "file://" + indirectFile, f.URI())
assert.Equal(t, "file://" + absFilePath, f.URI()) assert.Equal(t, "file://" + absFilePath, f.URI())
} }
@ -55,7 +56,7 @@ func TestApplyResourceTransformation(t *testing.T) {
func TestReadFile(t *testing.T) { func TestReadFile(t *testing.T) {
ctx := context.Background() ctx := context.Background()
file, _ := filepath.Abs(filepath.Join(TempDir, "fooread.txt")) file, _ := filepath.Abs(TempDir.FilePath("fooread.txt"))
expectedTime, timeErr := time.Parse(time.RFC3339Nano, "2001-12-15T01:01:01.000000001Z") expectedTime, timeErr := time.Parse(time.RFC3339Nano, "2001-12-15T01:01:01.000000001Z")
assert.Nil(t, timeErr) assert.Nil(t, timeErr)
@ -106,7 +107,7 @@ func TestReadFile(t *testing.T) {
func TestUseConfig(t *testing.T) { func TestUseConfig(t *testing.T) {
file, _ := filepath.Abs(filepath.Join(TempDir, "missingfile.txt")) file, _ := filepath.Abs(TempDir.FilePath("missingfile.txt"))
f := NewFile() f := NewFile()
assert.NotNil(t, f) assert.NotNil(t, f)
@ -123,7 +124,7 @@ func TestUseConfig(t *testing.T) {
func TestReadFileError(t *testing.T) { func TestReadFileError(t *testing.T) {
ctx := context.Background() ctx := context.Background()
file, _ := filepath.Abs(filepath.Join(TempDir, "missingfile.txt")) file, _ := filepath.Abs(TempDir.FilePath("missingfile.txt"))
f := NewFile() f := NewFile()
assert.NotNil(t, f) assert.NotNil(t, f)
@ -134,7 +135,7 @@ func TestReadFileError(t *testing.T) {
} }
func TestCreateFile(t *testing.T) { func TestCreateFile(t *testing.T) {
file, _ := filepath.Abs(filepath.Join(TempDir, "foo.txt")) file, _ := filepath.Abs(TempDir.FilePath("foo.txt"))
decl := fmt.Sprintf(` decl := fmt.Sprintf(`
path: "%s" path: "%s"
@ -166,7 +167,7 @@ func TestCreateFile(t *testing.T) {
} }
func TestLoadFile(t *testing.T) { func TestLoadFile(t *testing.T) {
file, _ := filepath.Abs(filepath.Join(TempDir, "foo.txt")) file, _ := filepath.Abs(TempDir.FilePath("foo.txt"))
decl := fmt.Sprintf(` decl := fmt.Sprintf(`
path: "%s" path: "%s"
@ -210,7 +211,7 @@ filetype: "directory"
} }
func TestFileDirectory(t *testing.T) { func TestFileDirectory(t *testing.T) {
file, _ := filepath.Abs(filepath.Join(TempDir, "testdir")) file, _ := filepath.Abs(TempDir.FilePath("testdir"))
decl := fmt.Sprintf(` decl := fmt.Sprintf(`
path: "%s" path: "%s"
@ -237,7 +238,7 @@ func TestFileDirectory(t *testing.T) {
} }
func TestFileTimes(t *testing.T) { func TestFileTimes(t *testing.T) {
file, _ := filepath.Abs(filepath.Join(TempDir, "testtimes.txt")) file, _ := filepath.Abs(TempDir.FilePath("testtimes.txt"))
decl := fmt.Sprintf(` decl := fmt.Sprintf(`
path: "%s" path: "%s"
owner: "%s" owner: "%s"
@ -259,7 +260,7 @@ func TestFileTimes(t *testing.T) {
} }
func TestFileSetURI(t *testing.T) { func TestFileSetURI(t *testing.T) {
file, _ := filepath.Abs(filepath.Join(TempDir, "testuri.txt")) file, _ := filepath.Abs(TempDir.FilePath("testuri.txt"))
f := NewFile() f := NewFile()
assert.NotNil(t, f) assert.NotNil(t, f)
e := f.SetURI("file://" + file) e := f.SetURI("file://" + file)
@ -269,10 +270,10 @@ func TestFileSetURI(t *testing.T) {
} }
func TestFileNormalizePath(t *testing.T) { func TestFileNormalizePath(t *testing.T) {
absFile, absFilePathErr := filepath.Abs(filepath.Join(TempDir, "./testuri.txt")) absFile, absFilePathErr := filepath.Abs(TempDir.FilePath("./testuri.txt"))
assert.Nil(t, absFilePathErr) assert.Nil(t, absFilePathErr)
file := filepath.Join(TempDir, "./testuri.txt") file := TempDir.FilePath("./testuri.txt")
f := NewFile() f := NewFile()
assert.NotNil(t, f) assert.NotNil(t, f)
@ -288,7 +289,7 @@ func TestFileUpdateAttributesFromFileInfo(t *testing.T) {
f := NewFile() f := NewFile()
assert.NotNil(t, f) assert.NotNil(t, f)
info, e := os.Lstat(TempDir) info, e := os.Lstat(string(TempDir))
assert.Nil(t, e) assert.Nil(t, e)
updateAttributesErr := f.UpdateAttributesFromFileInfo(info) updateAttributesErr := f.UpdateAttributesFromFileInfo(info)
@ -301,8 +302,8 @@ func TestFileUpdateAttributesFromFileInfo(t *testing.T) {
func TestFileReadStat(t *testing.T) { func TestFileReadStat(t *testing.T) {
ctx := context.Background() ctx := context.Background()
link := filepath.Join(TempDir, "link.txt") link := TempDir.FilePath("link.txt")
linkTargetFile := filepath.Join(TempDir, "testuri.txt") linkTargetFile := TempDir.FilePath("testuri.txt")
f := NewFile() f := NewFile()
assert.NotNil(t, f) assert.NotNil(t, f)
@ -345,7 +346,7 @@ func TestFileReadStat(t *testing.T) {
} }
func TestFileResourceFileInfo(t *testing.T) { func TestFileResourceFileInfo(t *testing.T) {
testFile := filepath.Join(TempDir, "testuri.txt") testFile := TempDir.FilePath("testuri.txt")
f := NewFile() f := NewFile()
assert.NotNil(t, f) assert.NotNil(t, f)
@ -370,8 +371,8 @@ func TestFileResourceFileInfo(t *testing.T) {
func TestFileClone(t *testing.T) { func TestFileClone(t *testing.T) {
ctx := context.Background() ctx := context.Background()
testFile := filepath.Join(TempDir, "testorig.txt") testFile := TempDir.FilePath("testorig.txt")
testCloneFile := filepath.Join(TempDir, "testclone.txt") testCloneFile := TempDir.FilePath("testclone.txt")
f := NewFile() f := NewFile()
assert.NotNil(t, f) assert.NotNil(t, f)
@ -404,7 +405,7 @@ func TestFileClone(t *testing.T) {
func TestFileErrors(t *testing.T) { func TestFileErrors(t *testing.T) {
//ctx := context.Background() //ctx := context.Background()
testFile := filepath.Join(TempDir, "testerr.txt") testFile := TempDir.FilePath("testerr.txt")
f := NewFile() f := NewFile()
assert.NotNil(t, f) assert.NotNil(t, f)
@ -435,7 +436,7 @@ func TestFileErrors(t *testing.T) {
} }
func TestFileDelete(t *testing.T) { func TestFileDelete(t *testing.T) {
file, _ := filepath.Abs(filepath.Join(TempDir, "foo.txt")) file, _ := filepath.Abs(TempDir.FilePath("foo.txt"))
decl := fmt.Sprintf(` decl := fmt.Sprintf(`
path: "%s" path: "%s"
@ -466,8 +467,8 @@ func TestFileDelete(t *testing.T) {
} }
func TestFileContentRef(t *testing.T) { func TestFileContentRef(t *testing.T) {
file, _ := filepath.Abs(filepath.Join(TempDir, "src.txt")) file, _ := filepath.Abs(TempDir.FilePath("src.txt"))
copyFile, _ := filepath.Abs(filepath.Join(TempDir, "copy.txt")) copyFile, _ := filepath.Abs(TempDir.FilePath("copy.txt"))
decl := fmt.Sprintf(` decl := fmt.Sprintf(`
path: "%s" path: "%s"
@ -487,7 +488,7 @@ func TestFileContentRef(t *testing.T) {
mode: "0600" mode: "0600"
sourceref: "file://%s" sourceref: "file://%s"
state: present state: present
`, file, ProcessTestUserName, ProcessTestGroupName, copyFile) `, copyFile, ProcessTestUserName, ProcessTestGroupName, file)
f := NewFile() f := NewFile()
stater := f.StateMachine() stater := f.StateMachine()
@ -537,7 +538,7 @@ func TestFilePathURI(t *testing.T) {
} }
func TestFileAbsent(t *testing.T) { func TestFileAbsent(t *testing.T) {
file, _ := filepath.Abs(filepath.Join(TempDir, "testabsentstate.txt")) file, _ := filepath.Abs(TempDir.FilePath("testabsentstate.txt"))
decl := fmt.Sprintf(` decl := fmt.Sprintf(`
path: "%s" path: "%s"
@ -563,7 +564,7 @@ func TestFileAbsent(t *testing.T) {
func TestFileReader(t *testing.T) { func TestFileReader(t *testing.T) {
expected := "datatoreadusinganio.readers" expected := "datatoreadusinganio.readers"
dataReader := strings.NewReader(expected) dataReader := strings.NewReader(expected)
file, _ := filepath.Abs(filepath.Join(TempDir, "testabsentstate.txt")) file, _ := filepath.Abs(TempDir.FilePath("testabsentstate.txt"))
decl := fmt.Sprintf(` decl := fmt.Sprintf(`
path: "%s" path: "%s"
owner: "%s" owner: "%s"
@ -589,7 +590,7 @@ func TestFileReader(t *testing.T) {
} }
func TestFileSetURIError(t *testing.T) { func TestFileSetURIError(t *testing.T) {
file := fmt.Sprintf("%s/%s", TempDir, "fooread.txt") file := TempDir.FilePath("fooread.txt")
f := NewFile() f := NewFile()
assert.NotNil(t, f) assert.NotNil(t, f)
e := f.SetURI("foo://" + file) e := f.SetURI("foo://" + file)
@ -598,7 +599,7 @@ func TestFileSetURIError(t *testing.T) {
} }
func TestFileContentType(t *testing.T) { func TestFileContentType(t *testing.T) {
file := fmt.Sprintf("%s/%s", TempDir, "fooread.txt") file := TempDir.FilePath("fooread.txt")
f := NewFile() f := NewFile()
assert.NotNil(t, f) assert.NotNil(t, f)
e := f.SetURI("file://" + file) e := f.SetURI("file://" + file)

View File

@ -12,16 +12,16 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"testing" "testing"
"decl/internal/tempdir"
) )
var TempDir string var TempDir tempdir.Path = "testresourcefile"
var ProcessTestUserName string var ProcessTestUserName string
var ProcessTestGroupName string var ProcessTestGroupName string
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
var err error err := TempDir.Create()
TempDir, err = os.MkdirTemp("", "testresourcefile")
if err != nil || TempDir == "" { if err != nil || TempDir == "" {
log.Fatal(err) log.Fatal(err)
} }
@ -31,7 +31,7 @@ func TestMain(m *testing.M) {
ProcessTestUserName, ProcessTestGroupName = ProcessUserName() ProcessTestUserName, ProcessTestGroupName = ProcessUserName()
rc := m.Run() rc := m.Run()
os.RemoveAll(TempDir) TempDir.Remove()
os.Exit(rc) os.Exit(rc)
} }