mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-30 19:35:06 +00:00
fix: replace mattbaird/jsonpatch with appscode/jsonpatch (#7401)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
c6fd0e7245
commit
07ef7da0c4
4 changed files with 24 additions and 275 deletions
5
go.mod
5
go.mod
|
@ -335,4 +335,7 @@ require (
|
|||
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
|
||||
)
|
||||
|
||||
replace github.com/jmespath/go-jmespath => github.com/kyverno/go-jmespath v0.4.1-0.20230204162932-3ee946b9433d
|
||||
replace (
|
||||
github.com/jmespath/go-jmespath => github.com/kyverno/go-jmespath v0.4.1-0.20230204162932-3ee946b9433d
|
||||
github.com/mattbaird/jsonpatch => gomodules.xyz/jsonpatch/v2 v2.2.0
|
||||
)
|
||||
|
|
5
go.sum
5
go.sum
|
@ -395,6 +395,7 @@ github.com/envoyproxy/protoc-gen-validate v0.0.14/go.mod h1:iSmxcyjqTsJpI2R4NaDN
|
|||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/esimonov/ifshort v1.0.2/go.mod h1:yZqNJUrNn20K8Q9n2CrjTKYyVEmX209Hgu+M1LBpeZE=
|
||||
github.com/ettle/strcase v0.1.1/go.mod h1:hzDLsPC7/lwKyBOywSHEP89nt2pDgdy+No1NBA9o9VY=
|
||||
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
|
||||
github.com/evanphx/json-patch v4.5.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
|
||||
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
|
||||
github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U=
|
||||
|
@ -938,8 +939,6 @@ github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kN
|
|||
github.com/matoous/godox v0.0.0-20210227103229-6504466cf951/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s=
|
||||
github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A=
|
||||
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
|
||||
github.com/mattbaird/jsonpatch v0.0.0-20200820163806-098863c1fc24 h1:uYuGXJBAi1umT+ZS4oQJUgKtfXCAYTR+n9zw1ViT0vA=
|
||||
github.com/mattbaird/jsonpatch v0.0.0-20200820163806-098863c1fc24/go.mod h1:M1qoD/MqPgTZIk0EWKB38wE28ACRfVcn+cU08jyArI0=
|
||||
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
|
@ -1933,6 +1932,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
|
|||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
|
||||
gomodules.xyz/jsonpatch/v2 v2.1.0/go.mod h1:IhYNNY4jnS53ZnfE4PAmpKtDpTCj1JFXc+3mwe7XcUU=
|
||||
gomodules.xyz/jsonpatch/v2 v2.2.0 h1:4pT439QV83L+G9FkcCriY6EkpcK6r6bK+A5FBUMI7qY=
|
||||
gomodules.xyz/jsonpatch/v2 v2.2.0/go.mod h1:WXp+iVDkoLQqPudfQ9GBlwB2eZ5DKOnjQZCYdOS8GPY=
|
||||
gomodules.xyz/jsonpatch/v2 v2.3.0 h1:8NFhfS6gzxNqjLIYnZxg319wZ5Qjnx4m/CcX+Klzazc=
|
||||
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
|
||||
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package patch
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/kyverno/kyverno/pkg/utils/wildcard"
|
||||
|
@ -23,91 +21,10 @@ func generatePatches(src, dst []byte) ([]jsonpatch.JsonPatchOperation, error) {
|
|||
if pp, err := jsonpatch.CreatePatch(src, dst); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return filterAndSortPatches(pp), err
|
||||
return filterInvalidPatches(pp), err
|
||||
}
|
||||
}
|
||||
|
||||
// filterAndSortPatches
|
||||
// 1. filters out patches with the certain paths
|
||||
// 2. sorts the removal patches(with same path) by the key of index
|
||||
// in descending order. The sort is required as when removing multiple
|
||||
// elements from an array, the elements must be removed in descending
|
||||
// order to preserve each index value.
|
||||
func filterAndSortPatches(originalPatches []jsonpatch.JsonPatchOperation) []jsonpatch.JsonPatchOperation {
|
||||
patches := filterInvalidPatches(originalPatches)
|
||||
|
||||
result := make([]jsonpatch.JsonPatchOperation, len(patches))
|
||||
index := getIndexToBeReversed(patches)
|
||||
|
||||
if len(index) == 0 {
|
||||
return patches
|
||||
}
|
||||
|
||||
start := 0
|
||||
for _, idx := range index {
|
||||
end := idx[0]
|
||||
copy(result[start:end], patches[start:end])
|
||||
reversedPatches := reverse(patches, idx)
|
||||
copy(result[end:], reversedPatches)
|
||||
start = idx[1] + 1
|
||||
}
|
||||
copy(result[start:], patches[start:])
|
||||
return result
|
||||
}
|
||||
|
||||
func getIndexToBeReversed(patches []jsonpatch.JsonPatchOperation) [][]int {
|
||||
removePaths := make([]string, len(patches))
|
||||
|
||||
for i, patch := range patches {
|
||||
if patch.Operation == "remove" {
|
||||
removePaths[i] = patch.Path
|
||||
}
|
||||
}
|
||||
return getRemoveInterval(removePaths)
|
||||
}
|
||||
|
||||
func getRemoveInterval(removePaths []string) [][]int {
|
||||
// get paths end with '/number'
|
||||
regex := regexp.MustCompile(`\/\d+$`)
|
||||
for i, path := range removePaths {
|
||||
if !regex.Match([]byte(path)) {
|
||||
removePaths[i] = ""
|
||||
}
|
||||
}
|
||||
|
||||
res := [][]int{}
|
||||
for i := 0; i < len(removePaths); {
|
||||
if removePaths[i] != "" {
|
||||
baseDir := filepath.Dir(removePaths[i])
|
||||
j := i + 1
|
||||
for ; j < len(removePaths); j++ {
|
||||
curDir := filepath.Dir(removePaths[j])
|
||||
if baseDir != curDir {
|
||||
break
|
||||
}
|
||||
}
|
||||
if i != j-1 {
|
||||
res = append(res, []int{i, j - 1})
|
||||
}
|
||||
i = j
|
||||
} else {
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func reverse(patches []jsonpatch.JsonPatchOperation, interval []int) []jsonpatch.JsonPatchOperation {
|
||||
res := make([]jsonpatch.JsonPatchOperation, interval[1]-interval[0]+1)
|
||||
j := 0
|
||||
for i := interval[1]; i >= interval[0]; i-- {
|
||||
res[j] = patches[i]
|
||||
j++
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// filterInvalidPatches filters out patch with the following path:
|
||||
// - not */metadata/name, */metadata/namespace, */metadata/labels, */metadata/annotations
|
||||
// - /status
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/mattbaird/jsonpatch"
|
||||
assertnew "github.com/stretchr/testify/assert"
|
||||
"gotest.tools/assert"
|
||||
)
|
||||
|
@ -13,16 +12,16 @@ import (
|
|||
func Test_GeneratePatches(t *testing.T) {
|
||||
out, err := strategicMergePatch(logr.Discard(), string(baseBytes), string(overlayBytes))
|
||||
assert.NilError(t, err)
|
||||
|
||||
expectedPatches := map[string]bool{
|
||||
`{"op":"remove","path":"/spec/template/spec/containers/0"}`: true,
|
||||
`{"op":"add","path":"/spec/template/spec/containers/0","value":{"image":"nginx","name":"nginx"}}`: true,
|
||||
`{"op":"add","path":"/spec/template/spec/containers/1","value":{"env":[{"name":"WORDPRESS_DB_HOST","value":"$(MYSQL_SERVICE)"},{"name":"WORDPRESS_DB_PASSWORD","valueFrom":{"secretKeyRef":{"key":"password","name":"mysql-pass"}}}],"image":"wordpress:4.8-apache","name":"wordpress","ports":[{"containerPort":80,"name":"wordpress"}],"volumeMounts":[{"mountPath":"/var/www/html","name":"wordpress-persistent-storage"}]}}`: true,
|
||||
`{"op":"add","path":"/spec/template/spec/initContainers","value":[{"command":["echo $(WORDPRESS_SERVICE)","echo $(MYSQL_SERVICE)"],"image":"debian","name":"init-command"}]}`: true,
|
||||
`{"op":"add","path":"/spec/template/spec/containers/1","value":{"env":[{"name":"WORDPRESS_DB_HOST","value":"$(MYSQL_SERVICE)"},{"name":"WORDPRESS_DB_PASSWORD","valueFrom":{"secretKeyRef":{"key":"password","name":"mysql-pass"}}}],"image":"wordpress:4.8-apache","name":"wordpress","ports":[{"containerPort":80,"name":"wordpress"}],"volumeMounts":[{"mountPath":"/var/www/html","name":"wordpress-persistent-storage"}]}}`: true,
|
||||
`{"op":"replace","path":"/spec/template/spec/containers/0/image","value":"nginx"}`: true,
|
||||
`{"op":"replace","path":"/spec/template/spec/containers/0/name","value":"nginx"}`: true,
|
||||
`{"op":"remove","path":"/spec/template/spec/containers/0/ports"}`: true,
|
||||
`{"op":"remove","path":"/spec/template/spec/containers/0/volumeMounts"}`: true,
|
||||
}
|
||||
patches, err := generatePatches(baseBytes, out)
|
||||
assert.NilError(t, err)
|
||||
|
||||
for _, p := range patches {
|
||||
assertnew.Equal(t, expectedPatches[p.Json()], true)
|
||||
}
|
||||
|
@ -219,189 +218,18 @@ func Test_ignorePath(t *testing.T) {
|
|||
func Test_GeneratePatches_sortRemovalPatches(t *testing.T) {
|
||||
base := []byte(`{"apiVersion": "apps/v1","kind": "Deployment","metadata": {"name": "nginx-deployment","labels": {"app": "nginx"}},"spec": {"selector": {"matchLabels": {"app": "nginx"}},"replicas": 1,"template": {"metadata": {"labels": {"app": "nginx"}},"spec": {"containers": [{"name": "nginx","image": "nginx:1.14.2","ports": [{"containerPort": 80}]}],"tolerations": [{"effect": "NoExecute","key": "node.kubernetes.io/not-ready","operator": "Exists","tolerationSeconds": 300},{"effect": "NoExecute","key": "node.kubernetes.io/unreachable","operator": "Exists","tolerationSeconds": 300}]}}}}`)
|
||||
patchedResource := []byte(`{"apiVersion": "apps/v1","kind": "Deployment","metadata": {"name": "nginx-deployment","labels": {"app": "nginx"}},"spec": {"selector": {"matchLabels": {"app": "nginx"}},"replicas": 1,"template": {"metadata": {"labels": {"app": "nginx"}},"spec": {"containers": [{"name": "nginx","image": "nginx:1.14.2","ports": [{"containerPort": 80}]}],"tolerations": [{"effect": "NoSchedule","key": "networkzone","operator": "Equal","value": "dmz"}]}}}}`)
|
||||
expectedPatches := [][]byte{[]byte(`{"op":"remove","path":"/spec/template/spec/tolerations/1"}`), []byte(`{"op":"remove","path":"/spec/template/spec/tolerations/0"}`), []byte(`{"op":"add","path":"/spec/template/spec/tolerations/0","value":{"effect":"NoSchedule","key":"networkzone","operator":"Equal","value":"dmz"}}`)}
|
||||
expectedPatches := [][]byte{
|
||||
[]byte(`{"op":"remove","path":"/spec/template/spec/tolerations/1"}`),
|
||||
[]byte(`{"op":"replace","path":"/spec/template/spec/tolerations/0/effect","value":"NoSchedule"}`),
|
||||
[]byte(`{"op":"replace","path":"/spec/template/spec/tolerations/0/key","value":"networkzone"}`),
|
||||
[]byte(`{"op":"replace","path":"/spec/template/spec/tolerations/0/operator","value":"Equal"}`),
|
||||
[]byte(`{"op":"add","path":"/spec/template/spec/tolerations/0/value","value":"dmz"}`),
|
||||
[]byte(`{"op":"remove","path":"/spec/template/spec/tolerations/0/tolerationSeconds"}`),
|
||||
}
|
||||
patches, err := generatePatches(base, patchedResource)
|
||||
fmt.Println(patches)
|
||||
assertnew.Nil(t, err)
|
||||
for _, patch := range patches {
|
||||
fmt.Println(patch.Json())
|
||||
}
|
||||
assertnew.Equal(t, expectedPatches, ConvertPatches(patches...))
|
||||
}
|
||||
|
||||
func Test_sortRemovalPatches(t *testing.T) {
|
||||
tests := []struct {
|
||||
patches []jsonpatch.JsonPatchOperation
|
||||
expected []jsonpatch.JsonPatchOperation
|
||||
}{
|
||||
{
|
||||
patches: []jsonpatch.JsonPatchOperation{{Operation: "add", Path: "/a"}},
|
||||
expected: []jsonpatch.JsonPatchOperation{{Operation: "add", Path: "/a"}},
|
||||
},
|
||||
{
|
||||
patches: []jsonpatch.JsonPatchOperation{{Operation: "add", Path: "/a"}, {Operation: "remove", Path: "/a"}},
|
||||
expected: []jsonpatch.JsonPatchOperation{{Operation: "add", Path: "/a"}, {Operation: "remove", Path: "/a"}},
|
||||
},
|
||||
{
|
||||
patches: []jsonpatch.JsonPatchOperation{{Operation: "remove", Path: "/a/0"}, {Operation: "add", Path: "/a/0"}},
|
||||
expected: []jsonpatch.JsonPatchOperation{{Operation: "remove", Path: "/a/0"}, {Operation: "add", Path: "/a/0"}},
|
||||
},
|
||||
{
|
||||
patches: []jsonpatch.JsonPatchOperation{{Operation: "remove", Path: "/a/0"}, {Operation: "remove", Path: "/a/1"}, {Operation: "remove", Path: "/a/2"}},
|
||||
expected: []jsonpatch.JsonPatchOperation{{Operation: "remove", Path: "/a/2"}, {Operation: "remove", Path: "/a/1"}, {Operation: "remove", Path: "/a/0"}},
|
||||
},
|
||||
{
|
||||
patches: []jsonpatch.JsonPatchOperation{{Operation: "remove", Path: "/a/0"}, {Operation: "remove", Path: "/b/0"}},
|
||||
expected: []jsonpatch.JsonPatchOperation{{Operation: "remove", Path: "/a/0"}, {Operation: "remove", Path: "/b/0"}},
|
||||
},
|
||||
{
|
||||
patches: []jsonpatch.JsonPatchOperation{{Operation: "remove", Path: "/a/0"}, {Operation: "remove", Path: "/b/0"}, {Operation: "remove", Path: "/b/1"}, {Operation: "remove", Path: "/c/0"}},
|
||||
expected: []jsonpatch.JsonPatchOperation{{Operation: "remove", Path: "/a/0"}, {Operation: "remove", Path: "/b/1"}, {Operation: "remove", Path: "/b/0"}, {Operation: "remove", Path: "/c/0"}},
|
||||
},
|
||||
{
|
||||
patches: []jsonpatch.JsonPatchOperation{{Operation: "remove", Path: "/a/0"}, {Operation: "add", Path: "/z"}, {Operation: "remove", Path: "/b/0"}, {Operation: "remove", Path: "/b/1"}, {Operation: "remove", Path: "/c/0"}},
|
||||
expected: []jsonpatch.JsonPatchOperation{{Operation: "remove", Path: "/a/0"}, {Operation: "add", Path: "/z"}, {Operation: "remove", Path: "/b/1"}, {Operation: "remove", Path: "/b/0"}, {Operation: "remove", Path: "/c/0"}},
|
||||
},
|
||||
{
|
||||
patches: []jsonpatch.JsonPatchOperation{{Operation: "remove", Path: "/a/0"}, {Operation: "remove", Path: "/b/0"}, {Operation: "add", Path: "/b/c/0"}, {Operation: "remove", Path: "/b/1"}, {Operation: "remove", Path: "/c/0"}},
|
||||
expected: []jsonpatch.JsonPatchOperation{{Operation: "remove", Path: "/a/0"}, {Operation: "remove", Path: "/b/0"}, {Operation: "add", Path: "/b/c/0"}, {Operation: "remove", Path: "/b/1"}, {Operation: "remove", Path: "/c/0"}},
|
||||
},
|
||||
{
|
||||
patches: []jsonpatch.JsonPatchOperation{
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/7"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/6"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/5"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/4"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/3"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/2"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/1"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/0"},
|
||||
{Operation: "add", Path: "/spec/containers/0/args/0", Value: "--logtostderr"},
|
||||
{Operation: "add", Path: "/spec/containers/0/args/1", Value: "--secure-listen-address=[$(IP)]:9100"},
|
||||
{Operation: "add", Path: "/spec/containers/0/args/2", Value: "--tls-cipher-suites=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"},
|
||||
{Operation: "add", Path: "/spec/containers/0/args/3", Value: "--upstream=http://127.0.0.1:9100/"},
|
||||
{Operation: "remove", Path: "/spec/containers/1/args/3"},
|
||||
{Operation: "remove", Path: "/spec/containers/1/args/2"},
|
||||
{Operation: "remove", Path: "/spec/containers/1/args/1"},
|
||||
{Operation: "remove", Path: "/spec/containers/1/args/0"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/0", Value: "--web.listen-address=127.0.0.1:9100"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/1", Value: "--Path.procfs=/host/proc"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/2", Value: "--Path.sysfs=/host/sys"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/3", Value: "--Path.rootfs=/host/root"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/4", Value: "--no-collector.wifi"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/5", Value: "--no-collector.hwmon"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/6", Value: "--collector.filesystem.ignored-mount-points=^/(dev|proc|sys|var/lib/docker/.+)($|/)"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/7", Value: "--collector.filesystem.ignored-fs-types=^(autofs|binfmt_misc|cgroup|tracefs)$"},
|
||||
},
|
||||
expected: []jsonpatch.JsonPatchOperation{
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/0"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/1"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/2"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/3"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/4"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/5"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/6"},
|
||||
{Operation: "remove", Path: "/spec/containers/0/args/7"},
|
||||
{Operation: "add", Path: "/spec/containers/0/args/0", Value: "--logtostderr"},
|
||||
{Operation: "add", Path: "/spec/containers/0/args/1", Value: "--secure-listen-address=[$(IP)]:9100"},
|
||||
{Operation: "add", Path: "/spec/containers/0/args/2", Value: "--tls-cipher-suites=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"},
|
||||
{Operation: "add", Path: "/spec/containers/0/args/3", Value: "--upstream=http://127.0.0.1:9100/"},
|
||||
{Operation: "remove", Path: "/spec/containers/1/args/0"},
|
||||
{Operation: "remove", Path: "/spec/containers/1/args/1"},
|
||||
{Operation: "remove", Path: "/spec/containers/1/args/2"},
|
||||
{Operation: "remove", Path: "/spec/containers/1/args/3"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/0", Value: "--web.listen-address=127.0.0.1:9100"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/1", Value: "--Path.procfs=/host/proc"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/2", Value: "--Path.sysfs=/host/sys"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/3", Value: "--Path.rootfs=/host/root"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/4", Value: "--no-collector.wifi"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/5", Value: "--no-collector.hwmon"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/6", Value: "--collector.filesystem.ignored-mount-points=^/(dev|proc|sys|var/lib/docker/.+)($|/)"},
|
||||
{Operation: "add", Path: "/spec/containers/1/args/7", Value: "--collector.filesystem.ignored-fs-types=^(autofs|binfmt_misc|cgroup|tracefs)$"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
sortedPatches := filterAndSortPatches(test.patches)
|
||||
assertnew.Equal(t, test.expected, sortedPatches, fmt.Sprintf("%dth test fails", i))
|
||||
}
|
||||
}
|
||||
|
||||
func Test_getRemoveInterval(t *testing.T) {
|
||||
tests := []struct {
|
||||
removalPaths []string
|
||||
expectedIndex [][]int
|
||||
}{
|
||||
{
|
||||
removalPaths: []string{"/a/0", "/b/0", "/b/1", "/c/0"},
|
||||
expectedIndex: [][]int{{1, 2}},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{},
|
||||
expectedIndex: [][]int{},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a"},
|
||||
expectedIndex: [][]int{},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a/0"},
|
||||
expectedIndex: [][]int{},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a/0", "/a/1"},
|
||||
expectedIndex: [][]int{{0, 1}},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a/0", "/a"},
|
||||
expectedIndex: [][]int{},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a", "/a/0"},
|
||||
expectedIndex: [][]int{},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a", "/a"},
|
||||
expectedIndex: [][]int{},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a/0", "/b/0"},
|
||||
expectedIndex: [][]int{},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a/0", "/a/1", "/a/2"},
|
||||
expectedIndex: [][]int{{0, 2}},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a/0", "/a/1", "/a/b"},
|
||||
expectedIndex: [][]int{{0, 1}},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a", "/a/0", "/a/1"},
|
||||
expectedIndex: [][]int{{1, 2}},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/", "/a", "/b/0"},
|
||||
expectedIndex: [][]int{},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a/b", "/a/c", "/b/0", "/b/1", "/c"},
|
||||
expectedIndex: [][]int{{2, 3}},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a/0", "/b/c", "/b/d", "/b/e", "/c/0"},
|
||||
expectedIndex: [][]int{},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a/0", "/a/1", "/b/z", "/c/0", "/c/1", "/c/2", "/d/z", "/e/0"},
|
||||
expectedIndex: [][]int{{0, 1}, {3, 5}},
|
||||
},
|
||||
{
|
||||
removalPaths: []string{"/a/0", "/a/1", "/a/2", "/a/3"},
|
||||
expectedIndex: [][]int{{0, 3}},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
res := getRemoveInterval(test.removalPaths)
|
||||
assertnew.Equal(t, test.expectedIndex, res, fmt.Sprintf("%d-th test fails at path %v", i, test.removalPaths))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue