|
4 | 4 | package crdutils
|
5 | 5 |
|
6 | 6 | import (
|
7 |
| - "bytes" |
8 | 7 | _ "embed"
|
9 |
| - "fmt" |
10 | 8 | "os"
|
11 | 9 | "path/filepath"
|
12 | 10 | "reflect"
|
13 | 11 | "runtime"
|
14 | 12 | "strconv"
|
15 |
| - "strings" |
16 | 13 | "testing"
|
17 |
| - "text/template" |
18 | 14 |
|
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" |
22 | 16 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
23 | 17 |
|
24 |
| - "github.com/stretchr/testify/assert" |
| 18 | + "github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1" |
25 | 19 | )
|
26 | 20 |
|
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 >p.Metadata |
40 |
| -} |
41 |
| - |
42 | 21 | var writev = `
|
43 | 22 | apiVersion: cilium.io/v1alpha1
|
44 | 23 | kind: TracingPolicy
|
@@ -408,55 +387,16 @@ func TestYamlData(t *testing.T) {
|
408 | 387 | }
|
409 | 388 | }
|
410 | 389 |
|
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 |
| - |
428 | 390 | func TestExamplesSmoke(t *testing.T) {
|
429 | 391 | _, filename, _, _ := runtime.Caller(0)
|
430 | 392 | 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 { |
448 | 394 | data := map[string]string{
|
449 | 395 | "Pid": strconv.Itoa(os.Getpid()),
|
450 | 396 | }
|
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 |
457 | 399 | })
|
458 |
| - |
459 |
| - assert.NoError(t, err, "failed to walk examples directory") |
460 | 400 | }
|
461 | 401 |
|
462 | 402 | const invalidNameYaml = `apiVersion: cilium.io/v1alpha1
|
|
0 commit comments