Skip to content

Commit 47c338d

Browse files
author
Anna Kapuscinska
committed
crdutils: Move test helpers to dedicated file and export them
This is done to improve reusability - the helpers can now be imported from different packages. Signed-off-by: Anna Kapuscinska <[email protected]>
1 parent 92c9ea2 commit 47c338d

File tree

2 files changed

+67
-65
lines changed

2 files changed

+67
-65
lines changed

pkg/crdutils/crdutils_test.go

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,20 @@
44
package crdutils
55

66
import (
7-
"bytes"
87
_ "embed"
9-
"fmt"
108
"os"
119
"path/filepath"
1210
"reflect"
1311
"runtime"
1412
"strconv"
15-
"strings"
1613
"testing"
17-
"text/template"
1814

19-
"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/client"
20-
"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
21-
"github.com/cilium/tetragon/pkg/logger"
15+
"github.com/stretchr/testify/assert"
2216
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2317

24-
"github.com/stretchr/testify/assert"
18+
"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
2519
)
2620

27-
// TPContext and GenericTracingPolicy replicate definitions from tracingpolicy
28-
// package as examples to test generic functionality.
29-
30-
var TPContext, _ = NewCRDContext[*GenericTracingPolicy](&client.TracingPolicyCRD.Definition)
31-
32-
type GenericTracingPolicy struct {
33-
metav1.TypeMeta
34-
Metadata metav1.ObjectMeta `json:"metadata"`
35-
Spec v1alpha1.TracingPolicySpec `json:"spec"`
36-
}
37-
38-
func (gtp *GenericTracingPolicy) GetObjectMetaStruct() *metav1.ObjectMeta {
39-
return &gtp.Metadata
40-
}
41-
4221
var writev = `
4322
apiVersion: cilium.io/v1alpha1
4423
kind: TracingPolicy
@@ -408,55 +387,16 @@ func TestYamlData(t *testing.T) {
408387
}
409388
}
410389

411-
// Read a config file and sub in templated values
412-
func fileConfigWithTemplate(fileName string, data interface{}) (*GenericTracingPolicy, error) {
413-
templ, err := template.ParseFiles(fileName)
414-
if err != nil {
415-
return nil, err
416-
}
417-
418-
var buf bytes.Buffer
419-
templ.Execute(&buf, data)
420-
421-
pol, err := TPContext.FromYAML(buf.String())
422-
if err != nil {
423-
return nil, fmt.Errorf("TPContext.FromYAML error %w", err)
424-
}
425-
return pol, nil
426-
}
427-
428390
func TestExamplesSmoke(t *testing.T) {
429391
_, filename, _, _ := runtime.Caller(0)
430392
examplesDir := filepath.Join(filepath.Dir(filename), "../../examples/tracingpolicy")
431-
err := filepath.Walk(examplesDir, func(path string, info os.FileInfo, err error) error {
432-
if err != nil {
433-
return err
434-
}
435-
436-
// Skip non-directories
437-
if info.IsDir() {
438-
return nil
439-
}
440-
441-
// Skip non-yaml files with a warning
442-
if !strings.HasSuffix(info.Name(), "yaml") || strings.HasSuffix(info.Name(), "yml") {
443-
logger.GetLogger().WithField("path", path).Warn("skipping non-yaml file")
444-
return nil
445-
}
446-
447-
// Fill this in with template data as needed
393+
CheckPolicies(t, examplesDir, func(path string) error {
448394
data := map[string]string{
449395
"Pid": strconv.Itoa(os.Getpid()),
450396
}
451-
452-
// Attempt to parse the file
453-
_, err = fileConfigWithTemplate(path, data)
454-
assert.NoError(t, err, "example %s must parse correctly: %s", info.Name(), err)
455-
456-
return nil
397+
_, err := FileConfigWithTemplate(path, data)
398+
return err
457399
})
458-
459-
assert.NoError(t, err, "failed to walk examples directory")
460400
}
461401

462402
const invalidNameYaml = `apiVersion: cilium.io/v1alpha1

pkg/crdutils/testhelpers.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,72 @@
77
package crdutils
88

99
import (
10+
"bytes"
11+
"fmt"
1012
"os"
13+
"path/filepath"
14+
"strings"
1115
"testing"
16+
"text/template"
17+
18+
"github.com/stretchr/testify/assert"
19+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
21+
"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/client"
22+
"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
1223
)
1324

25+
// TPContext and GenericTracingPolicy replicate definitions from tracingpolicy
26+
// package as examples to test generic functionality.
27+
28+
var TPContext, _ = NewCRDContext[*GenericTracingPolicy](&client.TracingPolicyCRD.Definition)
29+
30+
type GenericTracingPolicy struct {
31+
metav1.TypeMeta
32+
Metadata metav1.ObjectMeta `json:"metadata"`
33+
Spec v1alpha1.TracingPolicySpec `json:"spec"`
34+
}
35+
36+
func (gtp *GenericTracingPolicy) GetObjectMetaStruct() *metav1.ObjectMeta {
37+
return &gtp.Metadata
38+
}
39+
40+
func FileConfigWithTemplate(fileName string, data any) (*GenericTracingPolicy, error) {
41+
templ, err := template.ParseFiles(fileName)
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
var buf bytes.Buffer
47+
templ.Execute(&buf, data)
48+
49+
pol, err := TPContext.FromYAML(buf.String())
50+
if err != nil {
51+
return nil, fmt.Errorf("TPContext.FromYAML error %w", err)
52+
}
53+
return pol, nil
54+
}
55+
56+
func CheckPolicies(t *testing.T, policiesDir string, fromFile func(string) error) {
57+
err := filepath.Walk(policiesDir, func(path string, info os.FileInfo, err error) error {
58+
if err != nil {
59+
return err
60+
}
61+
62+
// Skip directories and non-yaml files
63+
if info.IsDir() || (!strings.HasSuffix(info.Name(), "yaml") && !strings.HasSuffix(info.Name(), "yml")) {
64+
return nil
65+
}
66+
67+
// Attempt to parse the file
68+
err = fromFile(path)
69+
assert.NoError(t, err, "example %s must parse correctly: %s", info.Name(), err)
70+
71+
return nil
72+
})
73+
assert.NoError(t, err, "failed to walk examples directory")
74+
}
75+
1476
func CreateTempFile(t *testing.T, data string) string {
1577
file, err := os.CreateTemp(t.TempDir(), "tetragon-")
1678
if err != nil {

0 commit comments

Comments
 (0)