Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 5 additions & 65 deletions pkg/crdutils/crdutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,20 @@
package crdutils

import (
"bytes"
_ "embed"
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
"text/template"

"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/client"
"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
"github.com/cilium/tetragon/pkg/logger"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/stretchr/testify/assert"
"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
)

// TPContext and GenericTracingPolicy replicate definitions from tracingpolicy
// package as examples to test generic functionality.

var TPContext, _ = NewCRDContext[*GenericTracingPolicy](&client.TracingPolicyCRD.Definition)

type GenericTracingPolicy struct {
metav1.TypeMeta
Metadata metav1.ObjectMeta `json:"metadata"`
Spec v1alpha1.TracingPolicySpec `json:"spec"`
}

func (gtp *GenericTracingPolicy) GetObjectMetaStruct() *metav1.ObjectMeta {
return &gtp.Metadata
}

var writev = `
apiVersion: cilium.io/v1alpha1
kind: TracingPolicy
Expand Down Expand Up @@ -408,55 +387,16 @@ func TestYamlData(t *testing.T) {
}
}

// Read a config file and sub in templated values
func fileConfigWithTemplate(fileName string, data interface{}) (*GenericTracingPolicy, error) {
templ, err := template.ParseFiles(fileName)
if err != nil {
return nil, err
}

var buf bytes.Buffer
templ.Execute(&buf, data)

pol, err := TPContext.FromYAML(buf.String())
if err != nil {
return nil, fmt.Errorf("TPContext.FromYAML error %w", err)
}
return pol, nil
}

func TestExamplesSmoke(t *testing.T) {
_, filename, _, _ := runtime.Caller(0)
examplesDir := filepath.Join(filepath.Dir(filename), "../../examples/tracingpolicy")
err := filepath.Walk(examplesDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// Skip non-directories
if info.IsDir() {
return nil
}

// Skip non-yaml files with a warning
if !strings.HasSuffix(info.Name(), "yaml") || strings.HasSuffix(info.Name(), "yml") {
logger.GetLogger().WithField("path", path).Warn("skipping non-yaml file")
return nil
}

// Fill this in with template data as needed
CheckPolicies(t, examplesDir, func(path string) error {
data := map[string]string{
"Pid": strconv.Itoa(os.Getpid()),
}

// Attempt to parse the file
_, err = fileConfigWithTemplate(path, data)
assert.NoError(t, err, "example %s must parse correctly: %s", info.Name(), err)

return nil
_, err := FileConfigWithTemplate(path, data)
return err
})

assert.NoError(t, err, "failed to walk examples directory")
}

const invalidNameYaml = `apiVersion: cilium.io/v1alpha1
Expand Down
75 changes: 75 additions & 0 deletions pkg/crdutils/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,85 @@
package crdutils

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"text/template"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/client"
"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
)

// TPContext and GenericTracingPolicy replicate definitions from tracingpolicy
// package as examples to test generic functionality.

var TPContext, _ = NewCRDContext[*GenericTracingPolicy](&client.TracingPolicyCRD.Definition)

type GenericTracingPolicy struct {
metav1.TypeMeta
Metadata metav1.ObjectMeta `json:"metadata"`
Spec v1alpha1.TracingPolicySpec `json:"spec"`
}

func (gtp *GenericTracingPolicy) GetObjectMetaStruct() *metav1.ObjectMeta {
return &gtp.Metadata
}

// Read a template file and apply data to it, returning the resulting string
func ReadFileTemplate(fileName string, data any) (string, error) {
templ, err := template.ParseFiles(fileName)
if err != nil {
return "", err
}

var buf bytes.Buffer
err = templ.Execute(&buf, data)
if err != nil {
return "", err
}

return buf.String(), nil
}

func FileConfigWithTemplate(fileName string, data any) (*GenericTracingPolicy, error) {
polyaml, err := ReadFileTemplate(fileName, data)
if err != nil {
return nil, err
}

pol, err := TPContext.FromYAML(polyaml)
if err != nil {
return nil, fmt.Errorf("TPContext.FromYAML error %w", err)
}
return pol, nil
}

func CheckPolicies(t *testing.T, policiesDir string, fromFile func(string) error) {
err := filepath.Walk(policiesDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// Skip directories and non-yaml files
if info.IsDir() || (!strings.HasSuffix(info.Name(), "yaml") && !strings.HasSuffix(info.Name(), "yml")) {
return nil
}

// Attempt to parse the file
err = fromFile(path)
assert.NoError(t, err, "example %s must parse correctly: %s", info.Name(), err)

return nil
})
assert.NoError(t, err, "failed to walk examples directory")
}

func CreateTempFile(t *testing.T, data string) string {
file, err := os.CreateTemp(t.TempDir(), "tetragon-")
if err != nil {
Expand Down
44 changes: 6 additions & 38 deletions pkg/eventcheckertests/yaml/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,42 @@
package yaml_test

import (
"bytes"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"text/template"

"github.com/cilium/tetragon/api/v1/tetragon/codegen/eventchecker/yaml"
"github.com/cilium/tetragon/pkg/crdutils"
"github.com/cilium/tetragon/pkg/eventcheckertests/yamlhelpers"
"github.com/cilium/tetragon/pkg/logger"
"github.com/stretchr/testify/assert"
)

var examplesDir string

func init() {
func TestExamplesSmoke(t *testing.T) {
_, filename, _, _ := runtime.Caller(0)
examplesDir = filepath.Join(filepath.Dir(filename), "../../../examples/eventchecker")
}

// Read a template file and apply data to it, returning the restulting string
func readFileTemplate(fileName string, data interface{}) (string, error) {
templ, err := template.ParseFiles(fileName)
if err != nil {
return "", err
}

var buf bytes.Buffer
err = templ.Execute(&buf, data)
if err != nil {
return "", err
}

return buf.String(), nil
}
examplesDir := filepath.Join(filepath.Dir(filename), "../../../examples/eventchecker")

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

// Skip directories
if info.IsDir() {
return nil
}

// Skip non-yaml files with a warning
if !strings.HasSuffix(info.Name(), "yaml") || strings.HasSuffix(info.Name(), "yml") {
logger.GetLogger().WithField("path", path).Warn("skipping non-yaml file")
// Skip directories and non-yaml files
if info.IsDir() || (!strings.HasSuffix(info.Name(), "yaml") && !strings.HasSuffix(info.Name(), "yml")) {
return nil
}

logger.GetLogger().WithField("path", path).Info("verifying file")

// Fill this in with template data as needed
templateData := map[string]string{
"Pid": strconv.Itoa(os.Getpid()),
}

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

assert.NoError(t, err)

var conf yaml.EventCheckerConf
yamlhelpers.AssertUnmarshalRoundTrip(t, []byte(data), &conf)

Expand Down
Loading