-
Notifications
You must be signed in to change notification settings - Fork 1
/
loader.go
103 lines (84 loc) · 2.38 KB
/
loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package framework
import (
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"k8s.io/utils/strings/slices"
)
const (
testFileGlobPattern = "*.test.yaml"
)
// Loader loads a test suite.
type Loader struct {
rootDir string
additionalTestDirs []string
}
// NewLoader returns a loader and applies options to it.
func NewLoader(rootDir string, opts ...LoaderOpt) *Loader {
loader := Loader{
rootDir: rootDir,
}
for _, opt := range opts {
opt(&loader)
}
return &loader
}
// LoaderOpt allows setting custom options.
type LoaderOpt func(loader *Loader)
// WithAdditionalTestDirs adds additional test source directories which are scanned for tests.
func WithAdditionalTestDirs(path ...string) LoaderOpt {
return func(loader *Loader) {
loader.additionalTestDirs = append(loader.additionalTestDirs, path...)
}
}
// LoadSuite loads a helmtest suite from the given directory.
func (loader *Loader) LoadSuite() (*Test, error) {
var suite Test
if err := unmarshalYamlFromFileStrict(filepath.Join(loader.rootDir, "suite.yaml"), &suite); err != nil && !os.IsNotExist(err) {
return nil, errors.Wrap(err, "loading suite specification")
}
if suite.Name == "" {
suite.Name = strings.TrimRight(loader.rootDir, "/")
}
testYAMLFiles, err := loader.readTestYAMLFiles()
if err != nil {
return nil, err
}
for _, file := range testYAMLFiles {
test := Test{
parent: &suite,
}
if err := unmarshalYamlFromFileStrict(file, &test); err != nil {
return nil, errors.Wrapf(err, "loading test specification from file %s", file)
}
if test.Name == "" {
test.Name = filepath.Base(file)
}
suite.Tests = append(suite.Tests, &test)
}
if err := suite.initialize(); err != nil {
return nil, err
}
return &suite, nil
}
// readTestYAMLFiles locates test files, if any.
func (loader *Loader) readTestYAMLFiles() ([]string, error) {
var testYAMLFiles []string
var scannedDirs []string
dirs := []string{loader.rootDir}
dirs = append(dirs, loader.additionalTestDirs...)
for _, dir := range dirs {
if slices.Contains(scannedDirs, dir) {
continue
}
dirGlob := filepath.Join(dir, testFileGlobPattern)
yamlFiles, err := filepath.Glob(dirGlob)
if err != nil {
return nil, errors.Wrapf(err, "resolving files using wildcard pattern %s", dirGlob)
}
testYAMLFiles = append(testYAMLFiles, yamlFiles...)
scannedDirs = append(scannedDirs, dir)
}
return testYAMLFiles, nil
}