1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/documentation/testing-policies.md

71 lines
2.1 KiB
Markdown
Raw Normal View History

2019-05-21 15:50:36 -07:00
<small>*[documentation](/README.md#documentation) / Testing Policies*</small>
2019-05-21 11:06:03 -07:00
# Testing Policies
2019-05-22 18:14:10 +03:00
The resources definitions for testing are located in [/test](/test) directory. Each test contains a pair of files: one is the resource definition, and the second is the kyverno policy for this definition.
2019-05-21 15:50:36 -07:00
## Test using kubectl
2019-05-22 18:14:10 +03:00
To do this you should [install kyverno to the cluster](/documentation/installation.md).
For example, to test the simplest kyverno policy for ConfigMap, create the policy and then the resource itself via kubectl:
2019-05-22 20:41:33 -07:00
2019-05-22 18:14:10 +03:00
````bash
cd test/ConfigMap
kubectl create -f policy-CM.yaml
kubectl create -f CM.yaml
````
Then compare the original resource definition in CM.yaml with the actual one:
2019-05-22 20:41:33 -07:00
2019-05-22 18:14:10 +03:00
````bash
kubectl get -f CM.yaml -o yaml
````
2019-05-21 15:50:36 -07:00
## Test using the Kyverno CLI
2019-06-11 18:46:35 -07:00
The Kyverno Command Line Interface (CLI) tool allows writing and testing policies without having to apply local policy changes to a cluster. You can also test policies without a Kubernetes clusters, but results may vary as default values will not be filled in.
2019-05-21 15:50:36 -07:00
2019-05-22 20:02:19 -07:00
### Building the CLI
2019-05-21 15:50:36 -07:00
2019-05-22 20:02:19 -07:00
You will need a [Go environment](https://golang.org/doc/install) setup.
1. Clone the Kyverno repo
````bash
git clone https://github.com/nirmata/kyverno/
````
2. Build the CLI
````bash
2019-05-22 20:41:33 -07:00
cd kyverno/cmd/kyverno
2019-05-22 20:02:19 -07:00
go build
````
Or, you can directly build and install the CLI using `go get`:
````bash
2019-05-22 20:41:33 -07:00
go get -u https://github.com/nirmata/kyverno/cmd/kyverno
2019-05-22 20:02:19 -07:00
````
### Using the CLI
2019-06-12 16:33:12 -07:00
The CLI loads default kubeconfig ($HOME/.kube/config) to test policies in Kubernetes cluster. If no kubeconfig is found, the CLI will test policies on raw resources.
2019-05-22 20:02:19 -07:00
To test a policy using the CLI type:
2019-06-12 16:33:12 -07:00
`kyverno apply @<policy> @<resource YAML file or folder>`
2019-05-22 20:02:19 -07:00
For example:
```bash
2019-06-12 16:33:12 -07:00
kyverno apply @../../examples/cli/policy-deployment.yaml @../../examples/cli/resources
```
To test a policy with the specific kubeconfig:
```bash
kyverno apply @../../examples/cli/policy-deployment.yaml @../../examples/cli/resources --kubeconfig $PATH_TO_KUBECONFIG_FILE
2019-05-22 20:02:19 -07:00
```
2019-06-12 16:33:12 -07:00
In future releases, the CLI will support complete validation and generation of policies.