|
4 | 4 | package yaml_test
|
5 | 5 |
|
6 | 6 | import (
|
7 |
| - "bytes" |
8 | 7 | "os"
|
9 | 8 | "path/filepath"
|
10 | 9 | "runtime"
|
11 | 10 | "strconv"
|
12 | 11 | "strings"
|
13 | 12 | "testing"
|
14 |
| - "text/template" |
15 | 13 |
|
16 | 14 | "github.com/cilium/tetragon/api/v1/tetragon/codegen/eventchecker/yaml"
|
| 15 | + "github.com/cilium/tetragon/pkg/crdutils" |
17 | 16 | "github.com/cilium/tetragon/pkg/eventcheckertests/yamlhelpers"
|
18 |
| - "github.com/cilium/tetragon/pkg/logger" |
19 | 17 | "github.com/stretchr/testify/assert"
|
20 | 18 | )
|
21 | 19 |
|
22 |
| -var examplesDir string |
23 |
| - |
24 |
| -func init() { |
| 20 | +func TestExamplesSmoke(t *testing.T) { |
25 | 21 | _, filename, _, _ := runtime.Caller(0)
|
26 |
| - examplesDir = filepath.Join(filepath.Dir(filename), "../../../examples/eventchecker") |
27 |
| -} |
28 |
| - |
29 |
| -// Read a template file and apply data to it, returning the restulting string |
30 |
| -func readFileTemplate(fileName string, data interface{}) (string, error) { |
31 |
| - templ, err := template.ParseFiles(fileName) |
32 |
| - if err != nil { |
33 |
| - return "", err |
34 |
| - } |
35 |
| - |
36 |
| - var buf bytes.Buffer |
37 |
| - err = templ.Execute(&buf, data) |
38 |
| - if err != nil { |
39 |
| - return "", err |
40 |
| - } |
41 |
| - |
42 |
| - return buf.String(), nil |
43 |
| -} |
| 22 | + examplesDir := filepath.Join(filepath.Dir(filename), "../../../examples/eventchecker") |
44 | 23 |
|
45 |
| -func TestExamplesSmoke(t *testing.T) { |
46 | 24 | err := filepath.Walk(examplesDir, func(path string, info os.FileInfo, err error) error {
|
47 | 25 | if err != nil {
|
48 | 26 | return err
|
49 | 27 | }
|
50 | 28 |
|
51 |
| - // Skip directories |
52 |
| - if info.IsDir() { |
53 |
| - return nil |
54 |
| - } |
55 |
| - |
56 |
| - // Skip non-yaml files with a warning |
57 |
| - if !strings.HasSuffix(info.Name(), "yaml") || strings.HasSuffix(info.Name(), "yml") { |
58 |
| - logger.GetLogger().WithField("path", path).Warn("skipping non-yaml file") |
| 29 | + // Skip directories and non-yaml files |
| 30 | + if info.IsDir() || (!strings.HasSuffix(info.Name(), "yaml") && !strings.HasSuffix(info.Name(), "yml")) { |
59 | 31 | return nil
|
60 | 32 | }
|
61 | 33 |
|
62 |
| - logger.GetLogger().WithField("path", path).Info("verifying file") |
63 |
| - |
64 | 34 | // Fill this in with template data as needed
|
65 | 35 | templateData := map[string]string{
|
66 | 36 | "Pid": strconv.Itoa(os.Getpid()),
|
67 | 37 | }
|
68 | 38 |
|
69 | 39 | // Attempt to parse the file
|
70 |
| - data, err := readFileTemplate(path, templateData) |
| 40 | + data, err := crdutils.ReadFileTemplate(path, templateData) |
71 | 41 | assert.NoError(t, err, "example %s must parse correctly", info.Name())
|
72 | 42 |
|
73 |
| - assert.NoError(t, err) |
74 |
| - |
75 | 43 | var conf yaml.EventCheckerConf
|
76 | 44 | yamlhelpers.AssertUnmarshalRoundTrip(t, []byte(data), &conf)
|
77 | 45 |
|
|
0 commit comments