forked from microsoft/AzurePolicyTestFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_test.go
53 lines (47 loc) · 1.16 KB
/
policy_test.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
package tester
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"testing"
"github.com/ContainerSolutions/AzurePolicyTestFramework/pkg/runner"
"github.com/stretchr/testify/require"
"sigs.k8s.io/yaml"
)
func TestPolicies(t *testing.T) {
configPath := os.Getenv("TEST_CONFIG_PATH")
if configPath == "" {
configPath = "./"
}
testPattern := os.Getenv("TEST_PATTERN")
if testPattern == "" {
testPattern = ".*"
}
re := regexp.MustCompile(testPattern)
walkFn := func(path string, info fs.FileInfo, err error) error {
if filepath.Ext(path) == ".yaml" {
yamlFile, err := os.Open(path)
if err != nil {
return err
}
defer yamlFile.Close()
byteValue, _ := io.ReadAll(yamlFile)
var testConfig runner.TestConfig
if err := yaml.Unmarshal(byteValue, &testConfig); err != nil {
return fmt.Errorf("Could not unmarshal file %s: %w", path, err)
}
for _, tc := range testConfig.Cases {
testRunner := runner.NewTestRunner(tc)
if re.Match([]byte(testRunner.TestCase.Name)) {
t.Run(testRunner.TestCase.Name, testRunner.Test)
}
}
}
return nil
}
err := filepath.Walk(configPath, walkFn)
require.Nil(t, err)
}