|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package semantic |
| 6 | + |
| 7 | +import ( |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/elastic/package-spec/v3/code/go/internal/fspath" |
| 16 | +) |
| 17 | + |
| 18 | +func TestValidateInputPackagesPolicyTemplates(t *testing.T) { |
| 19 | + |
| 20 | + t.Run("policy_templates_have_template_path", func(t *testing.T) { |
| 21 | + d := t.TempDir() |
| 22 | + |
| 23 | + err := os.MkdirAll(filepath.Join(d, "agent", "input"), 0o755) |
| 24 | + require.NoError(t, err) |
| 25 | + err = os.WriteFile(filepath.Join(d, "manifest.yml"), []byte(` |
| 26 | +type: input |
| 27 | +policy_templates: |
| 28 | + - name: udp |
| 29 | + template_path: udp.yml.hbs |
| 30 | +`), 0o644) |
| 31 | + require.NoError(t, err) |
| 32 | + err = os.WriteFile(filepath.Join(d, "agent", "input", "udp.yml.hbs"), []byte("# UDP template"), 0o644) |
| 33 | + require.NoError(t, err) |
| 34 | + |
| 35 | + errs := ValidateInputPackagesPolicyTemplates(fspath.DirFS(d)) |
| 36 | + require.Empty(t, errs, "expected no validation errors") |
| 37 | + |
| 38 | + }) |
| 39 | + |
| 40 | + t.Run("policy_templates_empty_template_path", func(t *testing.T) { |
| 41 | + d := t.TempDir() |
| 42 | + |
| 43 | + err := os.MkdirAll(filepath.Join(d, "agent", "input"), 0o755) |
| 44 | + require.NoError(t, err) |
| 45 | + err = os.WriteFile(filepath.Join(d, "manifest.yml"), []byte(` |
| 46 | +type: input |
| 47 | +policy_templates: |
| 48 | + - name: udp |
| 49 | +`), 0o644) |
| 50 | + require.NoError(t, err) |
| 51 | + |
| 52 | + errs := ValidateInputPackagesPolicyTemplates(fspath.DirFS(d)) |
| 53 | + require.NotEmpty(t, errs, "expected no validation errors") |
| 54 | + |
| 55 | + assert.Len(t, errs, 1) |
| 56 | + assert.ErrorIs(t, errs[0], errRequiredTemplatePath) |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("policy_templates_missing_template_path", func(t *testing.T) { |
| 60 | + d := t.TempDir() |
| 61 | + |
| 62 | + err := os.MkdirAll(filepath.Join(d, "agent", "input"), 0o755) |
| 63 | + require.NoError(t, err) |
| 64 | + err = os.WriteFile(filepath.Join(d, "manifest.yml"), []byte(` |
| 65 | +type: input |
| 66 | +policy_templates: |
| 67 | + - name: udp |
| 68 | + template_path: missing.yml.hbs |
| 69 | +`), 0o644) |
| 70 | + require.NoError(t, err) |
| 71 | + |
| 72 | + errs := ValidateInputPackagesPolicyTemplates(fspath.DirFS(d)) |
| 73 | + require.NotEmpty(t, errs, "expected validation errors") |
| 74 | + assert.Len(t, errs, 1) |
| 75 | + assert.ErrorIs(t, errs[0], errTemplateNotFound) |
| 76 | + }) |
| 77 | + |
| 78 | + t.Run("not_input_package_type", func(t *testing.T) { |
| 79 | + d := t.TempDir() |
| 80 | + |
| 81 | + err := os.MkdirAll(filepath.Join(d, "agent", "input"), 0o755) |
| 82 | + require.NoError(t, err) |
| 83 | + err = os.WriteFile(filepath.Join(d, "manifest.yml"), []byte(` |
| 84 | +type: integration |
| 85 | +policy_templates: |
| 86 | + - name: udp |
| 87 | + template_path: missing.yml.hbs |
| 88 | +`), 0o644) |
| 89 | + require.NoError(t, err) |
| 90 | + |
| 91 | + errs := ValidateInputPackagesPolicyTemplates(fspath.DirFS(d)) |
| 92 | + require.NotEmpty(t, errs, "expected validation errors") |
| 93 | + assert.Len(t, errs, 1) |
| 94 | + assert.ErrorIs(t, errs[0], errInvalidPackageType) |
| 95 | + }) |
| 96 | + |
| 97 | +} |
0 commit comments