mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
scan examples folder to build TS
This commit is contained in:
parent
711a892c12
commit
6b91ce18d2
14 changed files with 230 additions and 30 deletions
40
examples/cli/output/ghost.yaml
Normal file
40
examples/cli/output/ghost.yaml
Normal file
|
@ -0,0 +1,40 @@
|
|||
kind: Deployment
|
||||
apiVersion: apps/v1
|
||||
metadata:
|
||||
name: ghost
|
||||
creationTimestamp:
|
||||
labels:
|
||||
app: nginx_is_mutated
|
||||
cli: test
|
||||
isMutated: 'true'
|
||||
nirmata.io/application.name: ghost
|
||||
nirmata.io/component: ghost
|
||||
nirmata.io/deployment.name: ghost
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
nirmata.io/application.name: ghost
|
||||
nirmata.io/component: ghost
|
||||
template:
|
||||
metadata:
|
||||
creationTimestamp:
|
||||
labels:
|
||||
nirmata.io/application.name: ghost
|
||||
nirmata.io/component: ghost
|
||||
nirmata.io/deployment.name: ghost
|
||||
spec:
|
||||
containers:
|
||||
- name: ghost
|
||||
image: nginx:latest
|
||||
ports:
|
||||
- containerPort: 8080
|
||||
protocol: TCP
|
||||
resources: {}
|
||||
strategy:
|
||||
type: RollingUpdate
|
||||
rollingUpdate:
|
||||
maxUnavailable: 0
|
||||
maxSurge: 1
|
||||
revisionHistoryLimit: 5
|
||||
status: {}
|
29
examples/cli/output/nginx.yaml
Normal file
29
examples/cli/output/nginx.yaml
Normal file
|
@ -0,0 +1,29 @@
|
|||
kind: Deployment
|
||||
apiVersion: apps/v1
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
creationTimestamp:
|
||||
labels:
|
||||
app: nginx_is_mutated
|
||||
cli: test
|
||||
isMutated: 'true'
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
template:
|
||||
metadata:
|
||||
creationTimestamp:
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.7.9
|
||||
ports:
|
||||
- containerPort: 80
|
||||
resources: {}
|
||||
imagePullPolicy: Always
|
||||
strategy: {}
|
||||
status: {}
|
21
examples/cli/testScenarios.yaml
Normal file
21
examples/cli/testScenarios.yaml
Normal file
|
@ -0,0 +1,21 @@
|
|||
# input
|
||||
policy: policy-deployment
|
||||
resource: nginx-deployment
|
||||
initResources:
|
||||
# expected
|
||||
mutation:
|
||||
mPatchedResource: nginx-deployment
|
||||
reason: Success
|
||||
validation:
|
||||
reason: Success
|
||||
---
|
||||
# input
|
||||
policy: policy-deployment
|
||||
resource: ghost
|
||||
initResources:
|
||||
# expected
|
||||
mutation:
|
||||
mPatchedResource: ghost
|
||||
reason: Success
|
||||
validation:
|
||||
reason: Failed
|
36
examples/generate/policy_basic.yaml
Normal file
36
examples/generate/policy_basic.yaml
Normal file
|
@ -0,0 +1,36 @@
|
|||
apiVersion : kyverno.io/v1alpha1
|
||||
kind : Policy
|
||||
metadata :
|
||||
name : basic-policy
|
||||
spec :
|
||||
rules:
|
||||
- name: "Basic config generator for all namespaces"
|
||||
resource:
|
||||
kinds:
|
||||
- Namespace
|
||||
selector:
|
||||
matchLabels:
|
||||
LabelForSelector : "namespace2"
|
||||
generate:
|
||||
kind: ConfigMap
|
||||
name: default-config
|
||||
clone:
|
||||
namespace: default
|
||||
name: config-template
|
||||
- name: "Basic config generator for all namespaces"
|
||||
resource:
|
||||
kinds:
|
||||
- Namespace
|
||||
selector:
|
||||
matchLabels:
|
||||
LabelForSelector : "namespace2"
|
||||
generate:
|
||||
kind: Secret
|
||||
name: mongo-creds
|
||||
data:
|
||||
data:
|
||||
DB_USER: YWJyYWthZGFicmE=
|
||||
DB_PASSWORD: YXBwc3dvcmQ=
|
||||
metadata:
|
||||
labels:
|
||||
purpose: mongo
|
31
examples/mutate/overlay/output/nginx.yaml
Normal file
31
examples/mutate/overlay/output/nginx.yaml
Normal file
|
@ -0,0 +1,31 @@
|
|||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
creationTimestamp:
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
template:
|
||||
metadata:
|
||||
creationTimestamp:
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:latest
|
||||
ports:
|
||||
- containerPort: 80
|
||||
resources: {}
|
||||
imagePullPolicy: Always
|
||||
- name: ghost
|
||||
image: ghost:latest
|
||||
resources: {}
|
||||
imagePullPolicy: Always
|
||||
strategy: {}
|
||||
status: {}
|
|
@ -2,6 +2,7 @@ apiVersion: apps/v1
|
|||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
creationTimestamp:
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
|
@ -11,6 +12,7 @@ spec:
|
|||
app: nginx
|
||||
template:
|
||||
metadata:
|
||||
creationTimestamp:
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
10
examples/mutate/overlay/testScenarios.yaml
Normal file
10
examples/mutate/overlay/testScenarios.yaml
Normal file
|
@ -0,0 +1,10 @@
|
|||
# input
|
||||
policy: set-image-pull-policy
|
||||
resource: nginx-deployment
|
||||
initResources:
|
||||
# expected
|
||||
mutation:
|
||||
mPatchedResource: nginx-deployment
|
||||
reason: Success
|
||||
validation:
|
||||
reason: Success
|
|
@ -1,13 +1,20 @@
|
|||
apiVersion: v1
|
||||
kind: Endpoints
|
||||
apiVersion: v1
|
||||
metadata:
|
||||
name: test-endpoint
|
||||
creationTimestamp:
|
||||
labels:
|
||||
label : test
|
||||
label: test
|
||||
subsets:
|
||||
- addresses:
|
||||
- ip: 192.168.10.172
|
||||
ports:
|
||||
- name: load-balancer-connection
|
||||
port: 80
|
||||
protocol: UDP
|
||||
- addresses:
|
||||
- ip: 192.168.10.171
|
||||
ports:
|
||||
- name: secure-connection
|
||||
port: 443
|
||||
port: 9663
|
||||
protocol: TCP
|
||||
|
|
|
@ -9,7 +9,13 @@ mutation:
|
|||
validation:
|
||||
reason: Success
|
||||
---
|
||||
# input
|
||||
policy: policy-endpoints
|
||||
resource: test-endpoint
|
||||
initResources:
|
||||
mPatchedResource: test-endpoint
|
||||
# expected
|
||||
mutation:
|
||||
mPatchedResource: test-endpoint
|
||||
reason: Success
|
||||
validation:
|
||||
reason: Success
|
|
@ -90,13 +90,14 @@ func loadScenarios(tbPath string, file string) ([]*tScenario, error) {
|
|||
glog.Warningf("Error while decoding YAML object, err: %s", err)
|
||||
continue
|
||||
}
|
||||
fmt.Println(s.Policy)
|
||||
fmt.Println(s.Resource)
|
||||
ts = append(ts, s)
|
||||
}
|
||||
return ts, nil
|
||||
}
|
||||
|
||||
// Load test structure folder
|
||||
func (tb *testBundle) load() error {
|
||||
// scenario file defines the mapping of resources and policies
|
||||
scenarios, err := loadScenarios(tb.path, tScenarioFile)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -116,8 +117,7 @@ func (tb *testBundle) load() error {
|
|||
}
|
||||
tb.policies[policy.GetName()] = policy
|
||||
}
|
||||
|
||||
// load resources
|
||||
// load trigger resources
|
||||
loadResources(tb.path, tb.resources, resourcesFolder)
|
||||
// load output resources
|
||||
loadResources(tb.path, tb.output, outputFolder)
|
||||
|
@ -144,8 +144,10 @@ type testBundle struct {
|
|||
}
|
||||
|
||||
func (tb *testBundle) run(t *testing.T, testingapplyTest IApplyTest) {
|
||||
glog.Infof("Start: test on test bundles %s", tb.path)
|
||||
// run each scenario
|
||||
for _, ts := range tb.scenarios {
|
||||
fmt.Println(tb.path)
|
||||
// get policy
|
||||
p, ok := tb.policies[ts.Policy]
|
||||
if !ok {
|
||||
|
@ -167,6 +169,7 @@ func (tb *testBundle) run(t *testing.T, testingapplyTest IApplyTest) {
|
|||
tb.checkMutationResult(t, ts.Mutation, mPatchedResource, mResult)
|
||||
tb.checkValidationResult(t, ts.Validation, vResult)
|
||||
}
|
||||
glog.Infof("Done: test on test bundles %s", tb.path)
|
||||
}
|
||||
|
||||
func (tb *testBundle) checkValidationResult(t *testing.T, expect *tValidation, vResult result.Result) {
|
||||
|
@ -191,13 +194,17 @@ func (tb *testBundle) checkMutationResult(t *testing.T, expect *tMutation, pr *r
|
|||
return
|
||||
}
|
||||
// get expected patched resource
|
||||
er, ok := tb.resources[expect.MPatchedResource]
|
||||
er, ok := tb.output[expect.MPatchedResource]
|
||||
if !ok {
|
||||
glog.Warningf("Resource %s not found", expect.MPatchedResource)
|
||||
return
|
||||
}
|
||||
// compare patched resources
|
||||
if !checkMutationRPatches(pr, er) {
|
||||
fmt.Printf("Expected Resource %s \n", string(er.rawResource))
|
||||
fmt.Printf("Patched Resource %s \n", string(pr.rawResource))
|
||||
|
||||
glog.Warningf("Expected resource %s ", string(pr.rawResource))
|
||||
t.Error("Patched resources not as expected")
|
||||
}
|
||||
// compare result
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package testutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
@ -41,20 +40,20 @@ type testSuite struct {
|
|||
|
||||
func (ts *testSuite) buildTestSuite() error {
|
||||
// loading test bundles for test suite
|
||||
fmt.Println(ts.path)
|
||||
err := filepath.Walk(ts.path, func(path string, info os.FileInfo, err error) error {
|
||||
if info.IsDir() {
|
||||
glog.Infof("searching for test files at %s", path)
|
||||
// check if there are resources dir and policies yaml
|
||||
tb := NewTestBundle(path)
|
||||
if tb != nil {
|
||||
// load resources
|
||||
// try to load the test folder structure
|
||||
err := tb.load()
|
||||
if err != nil {
|
||||
// glog.Error(err)
|
||||
glog.Warningf("no supported test structure avaialbe at path %s", path)
|
||||
return nil
|
||||
}
|
||||
glog.Infof("loading test suite at path %s", path)
|
||||
ts.tb = append(ts.tb, tb)
|
||||
// fmt.Println(path)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
package testutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
ospath "path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
// Load policy & resource files
|
||||
|
@ -19,6 +20,7 @@ const outputFolder string = "output"
|
|||
|
||||
//LoadTestSuite reads the resource, policy and scenario files
|
||||
func LoadTestSuite(t *testing.T, path string) *testSuite {
|
||||
glog.Infof("loading test suites at %s", path)
|
||||
// gp := os.Getenv("GOPATH")
|
||||
// ap := ospath.Join(gp, "src/github.com/nirmata/kyverno")
|
||||
// build test suite
|
||||
|
@ -26,6 +28,7 @@ func LoadTestSuite(t *testing.T, path string) *testSuite {
|
|||
// ts := NewTestSuite(t, ospath.Join(ap, examplesPath))
|
||||
ts := NewTestSuite(t, path)
|
||||
ts.buildTestSuite()
|
||||
glog.Infof("done loading test suite at %s", path)
|
||||
return ts
|
||||
}
|
||||
|
||||
|
@ -43,7 +46,6 @@ func getYAMLfiles(path string) (yamls []string) {
|
|||
return nil
|
||||
}
|
||||
for _, file := range fileInfo {
|
||||
fmt.Println(filepath.Ext(file.Name()))
|
||||
if file.Name() == tScenarioFile {
|
||||
continue
|
||||
}
|
||||
|
|
|
@ -1,20 +1,36 @@
|
|||
package testutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
func TestUtils(t *testing.T) {
|
||||
file := "/Users/shiv/nirmata/code/go/src/github.com/nirmata/kyverno/examples/mutate/patches"
|
||||
ts := LoadTestSuite(t, file)
|
||||
func runTest(t *testing.T, path string) {
|
||||
// Load test suites at specified path
|
||||
ts := LoadTestSuite(t, path)
|
||||
|
||||
// policy application logic
|
||||
tp := &testPolicy{}
|
||||
ts.setApplyTest(tp)
|
||||
|
||||
// run the tests for each test bundle
|
||||
ts.runTests()
|
||||
if ts != nil {
|
||||
fmt.Println("Done building the test bundles")
|
||||
glog.Infof("Done running the test at %s", path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExamples(t *testing.T) {
|
||||
// folders := []string{
|
||||
// "/Users/shiv/nirmata/code/go/src/github.com/nirmata/kyverno/examples/mutate/patches",
|
||||
// "/Users/shiv/nirmata/code/go/src/github.com/nirmata/kyverno/examples/mutate/overlay",
|
||||
// "/Users/shiv/nirmata/code/go/src/github.com/nirmata/kyverno/examples/cli",
|
||||
// }
|
||||
folders := []string{
|
||||
"/Users/shiv/nirmata/code/go/src/github.com/nirmata/kyverno/examples",
|
||||
}
|
||||
for _, folder := range folders {
|
||||
runTest(t, folder)
|
||||
}
|
||||
// run the tests against the policy engine
|
||||
}
|
||||
|
|
|
@ -61,7 +61,7 @@ type resourceInfo struct {
|
|||
|
||||
func (ri resourceInfo) isSame(other resourceInfo) bool {
|
||||
// compare gvk
|
||||
if ri.gvk != other.gvk {
|
||||
if *ri.gvk != *other.gvk {
|
||||
return false
|
||||
}
|
||||
// compare rawResource
|
||||
|
@ -69,7 +69,7 @@ func (ri resourceInfo) isSame(other resourceInfo) bool {
|
|||
}
|
||||
|
||||
func getResourceYAML(d []byte) {
|
||||
fmt.Println(string(d))
|
||||
// fmt.Println(string(d))
|
||||
// convert json to yaml
|
||||
// print the result for reference
|
||||
// can be used as a dry run the get the expected result
|
||||
|
@ -154,20 +154,16 @@ func (tp *testPolicy) applyPolicy(policy *policytypes.Policy, resource *resource
|
|||
err := mResult.ToError()
|
||||
if err == nil && len(mPatches) != 0 {
|
||||
patchedResource, err = engine.ApplyPatches(resource.rawResource, mPatches)
|
||||
fmt.Println(len(resource.rawResource))
|
||||
fmt.Println(len(patchedResource))
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
// Validate
|
||||
vResult = engine.Validate(*policy, patchedResource, *resource.gvk)
|
||||
fmt.Println(len(patchedResource))
|
||||
}
|
||||
// Generate
|
||||
if client == nil {
|
||||
glog.Warning("Client is required to test generate")
|
||||
}
|
||||
fmt.Println(len(patchedResource))
|
||||
|
||||
// transform the patched Resource into resource Info
|
||||
_, ri := extractResourceRaw(patchedResource)
|
||||
|
@ -213,8 +209,6 @@ func LoadScenarios(file string) ([]*tScenario, error) {
|
|||
glog.Warningf("Error while decoding YAML object, err: %s", err)
|
||||
continue
|
||||
}
|
||||
fmt.Println(s.Policy)
|
||||
fmt.Println(s.Resource)
|
||||
ts = append(ts, s)
|
||||
}
|
||||
return ts, nil
|
||||
|
|
Loading…
Add table
Reference in a new issue