From cfd4501dccf77cffe941e70b73afbbfa4b6d1494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Wed, 8 Feb 2023 12:17:42 +0100 Subject: [PATCH] test: add a couple pattern unit tests (#6252) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test: add a couple pattern unit tests Signed-off-by: Charles-Edouard Brétéché * more Signed-off-by: Charles-Edouard Brétéché * more Signed-off-by: Charles-Edouard Brétéché --------- Signed-off-by: Charles-Edouard Brétéché --- pkg/engine/pattern/pattern.go | 2 +- pkg/engine/pattern/pattern_test.go | 574 +++++++++++++----- .../03-sleep.yaml | 5 + ...lete-policy.yaml => 04-delete-policy.yaml} | 0 .../{04-sleep.yaml => 05-sleep.yaml} | 0 .../{05-assert.yaml => 06-assert.yaml} | 0 6 files changed, 414 insertions(+), 167 deletions(-) create mode 100644 test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/03-sleep.yaml rename test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/{03-delete-policy.yaml => 04-delete-policy.yaml} (100%) rename test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/{04-sleep.yaml => 05-sleep.yaml} (100%) rename test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/{05-assert.yaml => 06-assert.yaml} (100%) diff --git a/pkg/engine/pattern/pattern.go b/pkg/engine/pattern/pattern.go index c7ee19b492..973bc0ff48 100644 --- a/pkg/engine/pattern/pattern.go +++ b/pkg/engine/pattern/pattern.go @@ -138,7 +138,7 @@ func validateNilPattern(log logr.Logger, value interface{}) bool { } } -func validateMapPattern(log logr.Logger, value interface{}, typedPattern map[string]interface{}) bool { +func validateMapPattern(log logr.Logger, value interface{}, _ map[string]interface{}) bool { // verify the type of the resource value is map[string]interface, // we only check for existence of object, not the equality of content and value _, ok := value.(map[string]interface{}) diff --git a/pkg/engine/pattern/pattern_test.go b/pkg/engine/pattern/pattern_test.go index fa7917d252..a8d9c47fc9 100644 --- a/pkg/engine/pattern/pattern_test.go +++ b/pkg/engine/pattern/pattern_test.go @@ -1,9 +1,9 @@ package pattern import ( - "encoding/json" "testing" + "github.com/go-logr/logr" "github.com/kyverno/kyverno/pkg/engine/operator" "github.com/kyverno/kyverno/pkg/logging" "gotest.tools/assert" @@ -11,13 +11,6 @@ import ( var logger = logging.GlobalLogger() -func TestValidateValueWithPattern_Bool(t *testing.T) { - assert.Assert(t, Validate(logger, true, true)) - assert.Assert(t, !Validate(logger, true, false)) - assert.Assert(t, !Validate(logger, false, true)) - assert.Assert(t, Validate(logger, false, false)) -} - func TestValidateString_AsteriskTest(t *testing.T) { pattern := "*" value := "anything" @@ -60,164 +53,6 @@ func TestValidateString_QuestionMark(t *testing.T) { assert.Assert(t, !compareString(logger, value, pattern, operator.Equal)) } -func TestValidateValueWithPattern_BoolInJson(t *testing.T) { - rawPattern := []byte(` - { - "key": true - } - `) - - rawValue := []byte(` - { - "key": true - } - `) - - var pattern, value map[string]interface{} - err := json.Unmarshal(rawPattern, &pattern) - assert.Assert(t, err) - err = json.Unmarshal(rawValue, &value) - assert.Assert(t, err) - - assert.Assert(t, Validate(logger, value["key"], pattern["key"])) -} - -func TestValidateValueWithPattern_NullPatternStringValue(t *testing.T) { - rawPattern := []byte(` - { - "key": null - } - `) - - rawValue := []byte(` - { - "key": "value" - } - `) - - var pattern, value map[string]interface{} - err := json.Unmarshal(rawPattern, &pattern) - assert.Assert(t, err) - err = json.Unmarshal(rawValue, &value) - assert.Assert(t, err) - - assert.Assert(t, !Validate(logger, value["key"], pattern["key"])) -} - -func TestValidateValueWithPattern_NullPatternDefaultString(t *testing.T) { - rawPattern := []byte(` - { - "key": null - } - `) - - rawValue := []byte(` - { - "key": "" - } - `) - - var pattern, value map[string]interface{} - err := json.Unmarshal(rawPattern, &pattern) - assert.Assert(t, err) - err = json.Unmarshal(rawValue, &value) - assert.Assert(t, err) - - assert.Assert(t, Validate(logger, value["key"], pattern["key"])) -} - -func TestValidateValueWithPattern_NullPatternDefaultFloat(t *testing.T) { - rawPattern := []byte(` - { - "key": null - } - `) - - rawValue := []byte(` - { - "key": 0.0 - } - `) - - var pattern, value map[string]interface{} - err := json.Unmarshal(rawPattern, &pattern) - assert.Assert(t, err) - err = json.Unmarshal(rawValue, &value) - assert.Assert(t, err) - - assert.Assert(t, Validate(logger, value["key"], pattern["key"])) -} - -func TestValidateValueWithPattern_NullPatternDefaultInt(t *testing.T) { - rawPattern := []byte(` - { - "key": null - } - `) - - rawValue := []byte(` - { - "key": 0 - } - `) - - var pattern, value map[string]interface{} - err := json.Unmarshal(rawPattern, &pattern) - assert.Assert(t, err) - err = json.Unmarshal(rawValue, &value) - assert.Assert(t, err) - - assert.Assert(t, Validate(logger, value["key"], pattern["key"])) -} - -func TestValidateValueWithPattern_NullPatternDefaultBool(t *testing.T) { - rawPattern := []byte(` - { - "key": null - } - `) - - rawValue := []byte(` - { - "key": false - } - `) - - var pattern, value map[string]interface{} - err := json.Unmarshal(rawPattern, &pattern) - assert.Assert(t, err) - err = json.Unmarshal(rawValue, &value) - assert.Assert(t, err) - - assert.Assert(t, Validate(logger, value["key"], pattern["key"])) -} - -func TestValidateValueWithPattern_StringsLogicalOr(t *testing.T) { - pattern := "192.168.88.1 | 10.100.11.*" - value := "10.100.11.54" - assert.Assert(t, Validate(logger, value, pattern)) -} - -func TestValidateValueWithPattern_StringsLogicalAnd(t *testing.T) { - pattern := ">1 & <20" - value := "10" - assert.Assert(t, Validate(logger, value, pattern)) -} - -func TestValidateValueWithPattern_StringsAllLogicalOperators(t *testing.T) { - pattern := ">1 & <20 | >31 & <33" - value := "10" - assert.Assert(t, Validate(logger, value, pattern)) - value = "32" - assert.Assert(t, Validate(logger, value, pattern)) - value = "21" - assert.Assert(t, !Validate(logger, value, pattern)) -} - -func TestValidateValueWithPattern_EqualTwoFloats(t *testing.T) { - assert.Assert(t, Validate(logger, 7.0, 7.000)) -} - func TestValidateValueWithNilPattern_NullPatternStringValue(t *testing.T) { assert.Assert(t, !validateNilPattern(logger, "value")) } @@ -404,3 +239,410 @@ func TestValidateKernelVersion_NotEquals(t *testing.T) { assert.Assert(t, !validateStringPatterns(logger, "5.10.84-1", "!5.10.84-1 & !5.15.2-1")) assert.Assert(t, !validateStringPatterns(logger, "5.15.2-1", "!5.10.84-1 & !5.15.2-1")) } + +func TestValidate(t *testing.T) { + type args struct { + value interface{} + pattern interface{} + } + tests := []struct { + name string + args args + want bool + }{{ + args: args{ + value: true, + pattern: true, + }, + want: true, + }, { + args: args{ + value: true, + pattern: false, + }, + want: false, + }, { + args: args{ + value: false, + pattern: true, + }, + want: false, + }, { + args: args{ + value: false, + pattern: false, + }, + want: true, + }, { + args: args{ + value: "value", + pattern: nil, + }, + want: false, + }, { + args: args{ + value: "", + pattern: nil, + }, + want: true, + }, { + args: args{ + value: 0.0, + pattern: nil, + }, + want: true, + }, { + args: args{ + value: 0, + pattern: nil, + }, + want: true, + }, { + args: args{ + value: false, + pattern: nil, + }, + want: true, + }, { + args: args{ + value: "10.100.11.54", + pattern: "192.168.88.1 | 10.100.11.*", + }, + want: true, + }, { + args: args{ + value: "10", + pattern: ">1 & <20", + }, + want: true, + }, { + args: args{ + value: "10", + pattern: ">1 & <20 | >31 & <33", + }, + want: true, + }, { + args: args{ + value: "32", + pattern: ">1 & <20 | >31 & <33", + }, + want: true, + }, { + args: args{ + value: "21", + pattern: ">1 & <20 | >31 & <33", + }, + want: false, + }, { + args: args{ + value: 7.0, + pattern: 7.000, + }, + want: true, + }, { + args: args{ + value: 10, + pattern: 10, + }, + want: true, + }, { + args: args{ + value: 8, + pattern: 10, + }, + want: false, + }, { + args: args{ + value: int64(10), + pattern: int64(10), + }, + want: true, + }, { + args: args{ + value: int64(8), + pattern: int64(10), + }, + want: false, + }, { + args: args{ + value: nil, + pattern: []interface{}{}, + }, + want: false, + }, { + args: args{ + value: nil, + pattern: []string{}, + }, + want: false, + }, { + args: args{ + value: map[string]interface{}{ + "a": true, + }, + pattern: map[string]interface{}{ + "a": true, + }, + }, + want: true, + }, { + args: args{ + value: map[string]interface{}{ + "a": true, + }, + pattern: map[string]interface{}{ + "b": false, + }, + }, + want: true, + }, { + args: args{ + value: nil, + pattern: false, + }, + want: false, + }, { + args: args{ + value: 8.0, + pattern: 8, + }, + want: true, + }, { + args: args{ + value: 8.1, + pattern: 8, + }, + want: false, + }, { + args: args{ + value: "8", + pattern: 8, + }, + want: true, + }, { + args: args{ + value: "8.1", + pattern: 8, + }, + want: false, + }, { + args: args{ + value: false, + pattern: 8, + }, + want: false, + }, + + { + args: args{ + value: 8, + pattern: 8.0, + }, + want: true, + }, { + args: args{ + value: 8, + pattern: 8.1, + }, + want: false, + }, { + args: args{ + value: int64(8), + pattern: 8.0, + }, + want: true, + }, { + args: args{ + value: int64(8), + pattern: 8.1, + }, + want: false, + }, { + args: args{ + value: "8", + pattern: 8.0, + }, + want: true, + }, { + args: args{ + value: "8.1", + pattern: 8.1, + }, + want: true, + }, { + args: args{ + value: "abc", + pattern: 8.1, + }, + want: false, + }, { + args: args{ + value: false, + pattern: 8.0, + }, + want: false, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Validate(logr.Discard(), tt.args.value, tt.args.pattern); got != tt.want { + t.Errorf("Validate() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_convertNumberToString(t *testing.T) { + type args struct { + value interface{} + } + tests := []struct { + name string + args args + want string + wantErr bool + }{{ + args: args{ + value: nil, + }, + want: "0", + }, { + args: args{ + value: "123", + }, + want: "123", + }, { + args: args{ + value: "", + }, + want: "", + }, { + args: args{ + value: "abc", + }, + want: "abc", + }, { + args: args{ + value: 0.0, + }, + want: "0.000000", + }, { + args: args{ + value: 3.10, + }, + want: "3.100000", + }, { + args: args{ + value: -3.10, + }, + want: "-3.100000", + }, { + args: args{ + value: -3, + }, + want: "-3", + }, { + args: args{ + value: 3, + }, + want: "3", + }, { + args: args{ + value: 0, + }, + want: "0", + }, { + args: args{ + value: int64(-3), + }, + want: "-3", + }, { + args: args{ + value: int64(3), + }, + want: "3", + }, { + args: args{ + value: int64(0), + }, + want: "0", + }, { + args: args{ + value: false, + }, + wantErr: true, + }, + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := convertNumberToString(tt.args.value) + if (err != nil) != tt.wantErr { + t.Errorf("convertNumberToString() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("convertNumberToString() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_validateMapPattern(t *testing.T) { + type args struct { + value interface{} + } + tests := []struct { + name string + args args + want bool + }{{ + args: args{ + value: nil, + }, + want: false, + }, { + args: args{ + value: true, + }, + want: false, + }, { + args: args{ + value: 8, + }, + want: false, + }, { + args: args{ + value: "", + }, + want: false, + }, { + args: args{ + value: "abc", + }, + want: false, + }, { + args: args{ + value: map[string]interface{}(nil), + }, + want: true, + }, { + args: args{ + value: map[string]interface{}{}, + }, + want: true, + }, { + args: args{ + value: map[string]interface{}{ + "a": true, + }, + }, + want: true, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := validateMapPattern(logr.Discard(), tt.args.value, nil); got != tt.want { + t.Errorf("validateMapPattern() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/03-sleep.yaml b/test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/03-sleep.yaml new file mode 100644 index 0000000000..fe3b8abbcb --- /dev/null +++ b/test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/03-sleep.yaml @@ -0,0 +1,5 @@ +# A command can only run a single command, not a pipeline and not a script. The program called must exist on the system where the test is run. +apiVersion: kuttl.dev/v1beta1 +kind: TestStep +commands: + - command: sleep 3 \ No newline at end of file diff --git a/test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/03-delete-policy.yaml b/test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/04-delete-policy.yaml similarity index 100% rename from test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/03-delete-policy.yaml rename to test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/04-delete-policy.yaml diff --git a/test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/04-sleep.yaml b/test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/05-sleep.yaml similarity index 100% rename from test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/04-sleep.yaml rename to test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/05-sleep.yaml diff --git a/test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/05-assert.yaml b/test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/06-assert.yaml similarity index 100% rename from test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/05-assert.yaml rename to test/conformance/kuttl/generate/policy/standard/clone/sync/pol-clone-sync-delete-policy/06-assert.yaml