From ef5351af9899b6a77f3096af1d8203b8f8bc1156 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 1 Oct 2024 10:55:32 -0400 Subject: [PATCH 1/2] packer_test: add FileExists checker Some tests will create files and directories as part of the execution path for Packer, and we need a way to check this, so this commit adds a new file gadget to do those checks after a command executes. --- packer_test/common/check/file_gadgets.go | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 packer_test/common/check/file_gadgets.go diff --git a/packer_test/common/check/file_gadgets.go b/packer_test/common/check/file_gadgets.go new file mode 100644 index 00000000000..cb15f94e02d --- /dev/null +++ b/packer_test/common/check/file_gadgets.go @@ -0,0 +1,35 @@ +package check + +import ( + "fmt" + "os" +) + +type fileExists struct { + filepath string + isDir bool +} + +func (fe fileExists) Check(_, _ string, _ error) error { + st, err := os.Stat(fe.filepath) + if err != nil { + return fmt.Errorf("failed to stat %q: %s", fe.filepath, err) + } + + if st.IsDir() && !fe.isDir { + return fmt.Errorf("file %q is a directory, wasn't supposed to be", fe.filepath) + } + + if !st.IsDir() && fe.isDir { + return fmt.Errorf("file %q is not a directory, was supposed to be", fe.filepath) + } + + return nil +} + +func FileExists(filePath string, isDir bool) Checker { + return fileExists{ + filepath: filePath, + isDir: isDir, + } +} From e3d160635da280077bfcda781632d5d6b7174e1b Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 16 Dec 2024 11:53:14 -0500 Subject: [PATCH 2/2] packer_test: add FileGlob checker When trying to validate that a particular file exists after a run of Packer in a test suite, we can use the FileExists checker that we provide as part of the gadgets we added for the acceptance test suites. This approach works well, but only if we can extract a file name reliably from the output of Packer core, or if we know what to look for exactly beforehand. For other cases with a generated name however, the FileExists checker is not enough, and therefore to accomodate for those cases, we are introducing a new checker for this purpose: FileGlob. FileGlob, as its name suggests, runs a glob expression on the filesystem, and returns an error if no match was found regarding this glob expression. --- packer_test/common/check/file_gadgets.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packer_test/common/check/file_gadgets.go b/packer_test/common/check/file_gadgets.go index cb15f94e02d..83d507c4e62 100644 --- a/packer_test/common/check/file_gadgets.go +++ b/packer_test/common/check/file_gadgets.go @@ -3,6 +3,7 @@ package check import ( "fmt" "os" + "path/filepath" ) type fileExists struct { @@ -33,3 +34,26 @@ func FileExists(filePath string, isDir bool) Checker { isDir: isDir, } } + +type fileGlob struct { + filepath string +} + +func (fe fileGlob) Check(_, _ string, _ error) error { + matches, err := filepath.Glob(fe.filepath) + if err != nil { + return fmt.Errorf("error evaluating file glob pattern %q: %v", fe.filepath, err) + } + + if len(matches) == 0 { + return fmt.Errorf("no matches found for file glob pattern %q", fe.filepath) + } + + return nil +} + +func FileGlob(filename string) Checker { + return fileGlob{ + filepath: filename, + } +}