Skip to content

Commit 4e7e54c

Browse files
author
Anna Kapuscinska
committed
eventcheckertests: Reuse helpers from crdutils package
Just to deduplicate the code a bit and improve reusability. Signed-off-by: Anna Kapuscinska <[email protected]>
1 parent 47c338d commit 4e7e54c

File tree

2 files changed

+23
-42
lines changed

2 files changed

+23
-42
lines changed

pkg/crdutils/testhelpers.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,29 @@ func (gtp *GenericTracingPolicy) GetObjectMetaStruct() *metav1.ObjectMeta {
3737
return &gtp.Metadata
3838
}
3939

40-
func FileConfigWithTemplate(fileName string, data any) (*GenericTracingPolicy, error) {
40+
// Read a template file and apply data to it, returning the resulting string
41+
func ReadFileTemplate(fileName string, data any) (string, error) {
4142
templ, err := template.ParseFiles(fileName)
4243
if err != nil {
43-
return nil, err
44+
return "", err
4445
}
4546

4647
var buf bytes.Buffer
47-
templ.Execute(&buf, data)
48+
err = templ.Execute(&buf, data)
49+
if err != nil {
50+
return "", err
51+
}
52+
53+
return buf.String(), nil
54+
}
55+
56+
func FileConfigWithTemplate(fileName string, data any) (*GenericTracingPolicy, error) {
57+
polyaml, err := ReadFileTemplate(fileName, data)
58+
if err != nil {
59+
return nil, err
60+
}
4861

49-
pol, err := TPContext.FromYAML(buf.String())
62+
pol, err := TPContext.FromYAML(polyaml)
5063
if err != nil {
5164
return nil, fmt.Errorf("TPContext.FromYAML error %w", err)
5265
}

pkg/eventcheckertests/yaml/yaml_test.go

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,42 @@
44
package yaml_test
55

66
import (
7-
"bytes"
87
"os"
98
"path/filepath"
109
"runtime"
1110
"strconv"
1211
"strings"
1312
"testing"
14-
"text/template"
1513

1614
"github.com/cilium/tetragon/api/v1/tetragon/codegen/eventchecker/yaml"
15+
"github.com/cilium/tetragon/pkg/crdutils"
1716
"github.com/cilium/tetragon/pkg/eventcheckertests/yamlhelpers"
18-
"github.com/cilium/tetragon/pkg/logger"
1917
"github.com/stretchr/testify/assert"
2018
)
2119

22-
var examplesDir string
23-
24-
func init() {
20+
func TestExamplesSmoke(t *testing.T) {
2521
_, 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")
4423

45-
func TestExamplesSmoke(t *testing.T) {
4624
err := filepath.Walk(examplesDir, func(path string, info os.FileInfo, err error) error {
4725
if err != nil {
4826
return err
4927
}
5028

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")) {
5931
return nil
6032
}
6133

62-
logger.GetLogger().WithField("path", path).Info("verifying file")
63-
6434
// Fill this in with template data as needed
6535
templateData := map[string]string{
6636
"Pid": strconv.Itoa(os.Getpid()),
6737
}
6838

6939
// Attempt to parse the file
70-
data, err := readFileTemplate(path, templateData)
40+
data, err := crdutils.ReadFileTemplate(path, templateData)
7141
assert.NoError(t, err, "example %s must parse correctly", info.Name())
7242

73-
assert.NoError(t, err)
74-
7543
var conf yaml.EventCheckerConf
7644
yamlhelpers.AssertUnmarshalRoundTrip(t, []byte(data), &conf)
7745

0 commit comments

Comments
 (0)