forked from cppforlife/go-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
229 lines (174 loc) · 4.01 KB
/
integration_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package patch_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"gopkg.in/yaml.v2"
. "github.com/cppforlife/go-patch/patch"
)
var _ = Describe("Integration", func() {
It("works in a basic way", func() {
inStr := `
releases:
- name: capi
version: 0.1
instance_groups:
- name: foo
- name: cloud_controller
instances: 0
jobs:
- name: cloud_controller
release: capi
- name: uaa
instances: 0
`
var in interface{}
err := yaml.Unmarshal([]byte(inStr), &in)
Expect(err).ToNot(HaveOccurred())
ops1Str := `
- type: replace
path: /instance_groups/name=cloud_controller/instances
value: 1
- type: replace
path: /instance_groups/name=cloud_controller/jobs/name=cloud_controller/consumes?/db
value:
instances:
- address: some-db.local
properties:
username: user
password: pass
- type: replace
path: /instance_groups/name=uaa/instances
value: 1
- type: replace
path: /instance_groups/-
value:
name: uaadb
instances: 2
- type: replace
path: /instance_groups/name=cloud_controller:before
value:
name: cc-before
instances: 2
- type: replace
path: /instance_groups/name=cloud_controller:after
value:
name: cc-after
instances: 1
- type: replace
path: /instance_groups/name=uaadb:after
value:
name: uaadb-after
instances: 1
- type: replace
path: /instance_groups/name=uaadb:next/instances
value: 4
`
var opDefs1 []OpDefinition
err = yaml.Unmarshal([]byte(ops1Str), &opDefs1)
Expect(err).ToNot(HaveOccurred())
ops1, err := NewOpsFromDefinitions(opDefs1)
Expect(err).ToNot(HaveOccurred())
ops2Str := `
- type: replace
path: /releases/name=capi/version
value: latest
`
var opDefs2 []OpDefinition
err = yaml.Unmarshal([]byte(ops2Str), &opDefs2)
Expect(err).ToNot(HaveOccurred())
ops2, err := NewOpsFromDefinitions(opDefs2)
Expect(err).ToNot(HaveOccurred())
ops := append(ops1, ops2...)
res, err := ops.Apply(in)
Expect(err).ToNot(HaveOccurred())
outStr := `
releases:
- name: capi
version: latest
instance_groups:
- name: foo
- name: cc-before
instances: 2
- name: cloud_controller
instances: 1
jobs:
- name: cloud_controller
release: capi
consumes:
db:
instances:
- address: some-db.local
properties:
username: user
password: pass
- name: cc-after
instances: 1
- name: uaa
instances: 1
- name: uaadb
instances: 2
- name: uaadb-after
instances: 4
`
var out interface{}
err = yaml.Unmarshal([]byte(outStr), &out)
Expect(err).ToNot(HaveOccurred())
Expect(res).To(Equal(out))
})
It("works with find op", func() {
inStr := `
releases:
- name: capi
version: 0.1
instance_groups:
- name: cloud_controller
instances: 0
jobs:
- name: cloud_controller
release: capi
- name: uaa
instances: 0
`
var in interface{}
err := yaml.Unmarshal([]byte(inStr), &in)
Expect(err).ToNot(HaveOccurred())
path := MustNewPointerFromString("/instance_groups/name=cloud_controller")
res, err := FindOp{Path: path}.Apply(in)
Expect(err).ToNot(HaveOccurred())
outStr := `
name: cloud_controller
instances: 0
jobs:
- name: cloud_controller
release: capi
`
var out interface{}
err = yaml.Unmarshal([]byte(outStr), &out)
Expect(err).ToNot(HaveOccurred())
Expect(res).To(Equal(out))
})
It("shows custom error messages", func() {
inStr := `
releases:
- name: capi
version: 0.1
`
var in interface{}
err := yaml.Unmarshal([]byte(inStr), &in)
Expect(err).ToNot(HaveOccurred())
opsStr := `
- type: remove
path: /releases/0/not-there
error: "Custom error message"
`
var opDefs []OpDefinition
err = yaml.Unmarshal([]byte(opsStr), &opDefs)
Expect(err).ToNot(HaveOccurred())
ops, err := NewOpsFromDefinitions(opDefs)
Expect(err).ToNot(HaveOccurred())
_, err = ops.Apply(in)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal(
"Error 'Custom error message': Expected to find a map key 'not-there' for path '/releases/0/not-there' (found map keys: 'name', 'version')"))
})
})