Skip to content

Commit

Permalink
packer_test: add FileGlob checker
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lbajolet-hashicorp committed Dec 16, 2024
1 parent 44a9491 commit 11c238b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packer_test/common/check/file_gadgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package check
import (
"fmt"
"os"
"path/filepath"
)

type fileExists struct {
Expand Down Expand Up @@ -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,
}
}

0 comments on commit 11c238b

Please sign in to comment.