forked from tilt-dev/tilt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_path_test.go
82 lines (69 loc) · 2.53 KB
/
json_path_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package k8s
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime"
"github.com/tilt-dev/tilt/internal/k8s/jsonpath"
"github.com/tilt-dev/tilt/internal/k8s/testyaml"
)
func TestJSONPathOneMatch(t *testing.T) {
entities := MustParseYAMLFromString(t, testyaml.SanchoYAML)
deployment := entities[0]
path, err := NewJSONPath("{.spec.template.spec.containers[].image}")
assert.NoError(t, err)
result, err := path.FindStrings(deployment.Obj)
assert.NoError(t, err)
assert.Equal(t, []string{"gcr.io/some-project-162817/sancho"}, result)
}
func TestJSONPathReplace(t *testing.T) {
entities := MustParseYAMLFromString(t, testyaml.SanchoYAML)
deployment := entities[0]
path, err := NewJSONPath("{.spec.template.spec.containers[].image}")
assert.NoError(t, err)
err = path.VisitStrings(deployment.Obj, func(val jsonpath.Value, s string) error {
val.Set(reflect.ValueOf("injected-image"))
return nil
})
assert.NoError(t, err)
result, err := path.FindStrings(deployment.Obj)
assert.NoError(t, err)
assert.Equal(t, []string{"injected-image"}, result)
}
func TestJSONPathMultipleMatches(t *testing.T) {
entities := MustParseYAMLFromString(t, testyaml.SanchoSidecarYAML)
deployment := entities[0]
path, err := NewJSONPath("{.spec.template.spec.containers[*].image}")
assert.NoError(t, err)
result, err := path.FindStrings(deployment.Obj)
assert.NoError(t, err)
assert.Equal(t, []string{
"gcr.io/some-project-162817/sancho",
"gcr.io/some-project-162817/sancho-sidecar",
}, result)
}
func TestJSONPathCRD(t *testing.T) {
entities := MustParseYAMLFromString(t, testyaml.CRDYAML)
crd := entities[0]
path, err := NewJSONPath("{.spec.validation.openAPIV3Schema.properties.spec.properties.image}")
assert.NoError(t, err)
content := crd.Obj.(runtime.Unstructured).UnstructuredContent()
result, err := path.FindStrings(content)
assert.NoError(t, err)
assert.Equal(t, []string{"docker.io/bitnami/minideb:latest"}, result)
}
func TestJSONPathCRDReplace(t *testing.T) {
entities := MustParseYAMLFromString(t, testyaml.CRDYAML)
crd := entities[0]
path, err := NewJSONPath("{.spec.validation.openAPIV3Schema.properties.spec.properties.image}")
assert.NoError(t, err)
content := crd.Obj.(runtime.Unstructured).UnstructuredContent()
err = path.VisitStrings(content, func(val jsonpath.Value, s string) error {
val.Set(reflect.ValueOf("injected-image"))
return nil
})
assert.NoError(t, err)
result, err := path.FindStrings(content)
assert.NoError(t, err)
assert.Equal(t, []string{"injected-image"}, result)
}