|
| 1 | +# HOWTO: Writing policy tests for a package |
| 2 | + |
| 3 | +## Introduction |
| 4 | +Elastic Packages support a variety of configuration variables as defined in |
| 5 | +their manifest files. Policy tests allow to check that specific sets of |
| 6 | +variables are accepted by Fleet, and they produce an expected agent policy. |
| 7 | + |
| 8 | +## Defining policy tests |
| 9 | + |
| 10 | +Policy tests can be defined in data streams in integration packages, or at the |
| 11 | +package level in input packages. |
| 12 | + |
| 13 | +Each test is composed of two files, one for the configuration of the policy, and |
| 14 | +another one for the expected result. |
| 15 | + |
| 16 | +When defining the tests at the data stream level, they must be defined following |
| 17 | +this structure. |
| 18 | +``` |
| 19 | +<package root>/ |
| 20 | + data_stream/ |
| 21 | + <data stream>/ |
| 22 | + _dev/ |
| 23 | + test/ |
| 24 | + policy/ |
| 25 | + test-<test name>.yml |
| 26 | + test-<test name>.expected |
| 27 | +``` |
| 28 | + |
| 29 | +When defining the tests at the package level, in input packages, they must be |
| 30 | +defined following this structure: |
| 31 | +``` |
| 32 | +<package root>/ |
| 33 | + _dev/ |
| 34 | + test/ |
| 35 | + policy/ |
| 36 | + test-<test name>.yml |
| 37 | + test-<test name>.expected |
| 38 | +``` |
| 39 | + |
| 40 | +It is possible, and encouraged, to define multiple policy tests for each package |
| 41 | +or data stream. |
| 42 | + |
| 43 | + |
| 44 | +### Defining the configuration of the policy |
| 45 | + |
| 46 | +Test configuration for the policy is defined in a YAML file prefixed with |
| 47 | +`test-`. |
| 48 | + |
| 49 | +In these configuration files it is possible to define: |
| 50 | +- Values for the variables of the package manifest. |
| 51 | +- Values for the variables of the data stream manifests (only used for |
| 52 | + integration packages). |
| 53 | +- Input to use, for packages supporting multiple input types. |
| 54 | + |
| 55 | +For example, the following configuration tells Fleet to create a policy using an |
| 56 | +specific input, and some variables at the package level: |
| 57 | +``` |
| 58 | +input: httpjson |
| 59 | +vars: |
| 60 | + url: http://localhost:1234/api/v1/logs |
| 61 | + username: test |
| 62 | + password: test |
| 63 | +``` |
| 64 | + |
| 65 | +The following configuration would set a value for a variable defined at the data |
| 66 | +stream level: |
| 67 | +``` |
| 68 | +data_stream: |
| 69 | + vars: |
| 70 | + paths: |
| 71 | + - "/var/logs/apache/access.log*" |
| 72 | +``` |
| 73 | + |
| 74 | +This configuration may look familiar to you if you are used to define system |
| 75 | +tests. The main difference is that policy tests are not going to be executed, so |
| 76 | +anything can be configured there, without expectations on having running |
| 77 | +services or reachable services. Also, no placeholders are expected to be found |
| 78 | +in policy tests. |
| 79 | + |
| 80 | + |
| 81 | +### Defining the expected policy |
| 82 | + |
| 83 | +Once you have decided the policy settings you would like to test, you should |
| 84 | +define the expected resulting policy. In principle it is possible to define it |
| 85 | +manually given that most of the information is included in the package, but it |
| 86 | +can be quite cumbersome. `elastic-package` is able to generate this file for |
| 87 | +you, using the `--generate` flag. |
| 88 | + |
| 89 | +If you run the policy tests with the `--generate` flag, `elastic-package` will |
| 90 | +write the found policy in the expected place. |
| 91 | +``` |
| 92 | +$ elastic-package test policy --generate |
| 93 | +``` |
| 94 | + |
| 95 | +Then check that the generated content is what you would expect to have. |
| 96 | + |
| 97 | + |
| 98 | +## Running policy tests |
| 99 | + |
| 100 | +You can run policy tests with the `elastic-package test` command. If a package |
| 101 | +includes policy tests, they will be executed if no test type is specified. You |
| 102 | +can also run the policy tests only indicating its type: |
| 103 | +``` |
| 104 | +$ elastic-package test policy |
| 105 | +``` |
| 106 | + |
| 107 | +With integration packages, you can run the policy tests for a single data |
| 108 | +stream, for example: |
| 109 | +``` |
| 110 | +$ elastic-package test policy --data-streams access |
| 111 | +``` |
| 112 | + |
| 113 | +Results are displayed using the usual format options. When the test fail, |
| 114 | +`elastic-package` shows the differences between the expected and found policy. |
0 commit comments