1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/engine/jmespath/utils_test.go
Andreas Brehmer a1ae86cdbe
Add JMESPath function for dynamic object/array lookup (#7136)
* Fix JMESPath functions error message

JMESPath functions `parse_yaml`, `items` and `object_from_lists` use
wrong format string arguments for an error message and count the
argument from 0 instead of 1.

Fix the format string args and add 1 to the argument index.

Also improve the error message itself.

Signed-off-by: Andreas Brehmer <andreas.brehmer@sap.com>

* Add JMESPath function `lookup`

`lookup` allows for dynamic lookups of objects and arrays, i.e. where
the key/index to look up is determined during the JMESPath query and
thus cannot be injected upfront.

Signed-off-by: Andreas Brehmer <andreas.brehmer@sap.com>

---------

Signed-off-by: Andreas Brehmer <andreas.brehmer@sap.com>
Co-authored-by: shuting <shuting@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-06-19 13:45:13 +00:00

80 lines
1.5 KiB
Go

package jmespath
import (
"fmt"
"math"
"testing"
"gotest.tools/assert"
)
func Test_intNumber(t *testing.T) {
testCases := []struct {
number float64
expectedResult int
}{
{
number: 0.0,
expectedResult: 0,
},
{
number: 1.0,
expectedResult: 1,
},
{
number: -1.0,
expectedResult: -1,
},
{
number: math.MaxInt32,
expectedResult: math.MaxInt32,
},
{
number: math.MinInt32,
expectedResult: math.MinInt32,
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
result, resultErr := intNumber(tc.number)
assert.NilError(t, resultErr)
assert.Equal(t, result, tc.expectedResult)
})
}
}
func Test_intNumber_Error(t *testing.T) {
testCases := []struct {
number float64
expectedMsg string
}{
{
number: 1.5,
expectedMsg: `expected an integer number but got: 1.5`,
},
{
number: math.NaN(),
expectedMsg: `expected an integer number but got: NaN`,
},
{
number: math.Inf(1),
expectedMsg: `expected an integer number but got: +Inf`,
},
{
number: math.Inf(-1),
expectedMsg: `expected an integer number but got: -Inf`,
},
{
number: math.MaxFloat64,
expectedMsg: `number is outside the range of integer numbers: 1.7976931348623157e+308`,
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
_, resultErr := intNumber(tc.number)
assert.Error(t, resultErr, tc.expectedMsg)
})
}
}