forked from oasdiff/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker_deprecation_test.go
366 lines (287 loc) · 13.4 KB
/
checker_deprecation_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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package checker_test
import (
"encoding/json"
"fmt"
"testing"
"time"
"cloud.google.com/go/civil"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/checker/localizations"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
)
func open(file string) (*load.SpecInfo, error) {
return load.LoadSpecInfoFromFile(openapi3.NewLoader(), file)
}
func getDeprecationFile(file string) string {
return fmt.Sprintf("../data/deprecation/%s", file)
}
func singleCheckConfig(c checker.BackwardCompatibilityCheck) checker.Config {
return checker.Config{
Checks: []checker.BackwardCompatibilityCheck{c},
MinSunsetBetaDays: 31,
MinSunsetStableDays: 180,
Localizer: *localizations.New("en", "en"),
}
}
// BC: deleting an operation before sunset date is breaking
func TestBreaking_RemoveBeforeSunset(t *testing.T) {
s1, err := open(getDeprecationFile("deprecated-future.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("sunset.yaml"))
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIRemovedCheck), d, osm)
require.NotEmpty(t, errs)
require.Len(t, errs, 1)
require.Equal(t, "api-removed-before-sunset", errs[0].GetId())
}
// BC: deleting an operation without sunset date is breaking
func TestBreaking_DeprecationNoSunset(t *testing.T) {
s1, err := open(getDeprecationFile("deprecated-no-sunset.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("sunset.yaml"))
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIRemovedCheck), d, osm)
require.NoError(t, err)
require.NotEmpty(t, errs)
require.Len(t, errs, 1)
require.Equal(t, "api-path-sunset-parse", errs[0].GetId())
}
// BC: deleting an operation after sunset date is not breaking
func TestBreaking_DeprecationPast(t *testing.T) {
s1, err := open(getDeprecationFile("deprecated-past.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("sunset.yaml"))
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIDeprecationCheck), d, osm)
require.Empty(t, errs)
}
// BC: deprecating an operation with a deprecation policy but without specifying sunset date is breaking
func TestBreaking_DeprecationWithoutSunset(t *testing.T) {
s1, err := open(getDeprecationFile("base.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("deprecated-no-sunset.yaml"))
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
c := singleCheckConfig(checker.APIDeprecationCheck)
c.MinSunsetStableDays = 10
errs := checker.CheckBackwardCompatibility(c, d, osm)
require.NotEmpty(t, errs)
require.Len(t, errs, 1)
require.Equal(t, "api-deprecated-sunset-parse", errs[0].GetId())
}
// BC: deprecating an operation without a deprecation policy and without specifying sunset date is not breaking
func TestBreaking_DeprecationForAlpha(t *testing.T) {
s1, err := open(getDeprecationFile("base-alpha-stability.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("deprecated-no-sunset-alpha-stability.yaml"))
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIDeprecationCheck), d, osm)
require.Empty(t, errs)
}
// BC: removing the path without a deprecation policy and without specifying sunset date is not breaking for alpha level
func TestBreaking_RemovedPathForAlpha(t *testing.T) {
s1, err := open(getDeprecationFile("base-alpha-stability.yaml"))
require.NoError(t, err)
alpha := toJson(t, "alpha")
s1.Spec.Paths["/api/test"].Get.Extensions["x-stability-level"] = alpha
s1.Spec.Paths["/api/test"].Post.Extensions["x-stability-level"] = alpha
s2, err := open(getDeprecationFile("base-alpha-stability.yaml"))
require.NoError(t, err)
delete(s2.Spec.Paths, "/api/test")
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIDeprecationCheck), d, osm)
require.Empty(t, errs)
}
// BC: removing the path without a deprecation policy and without specifying sunset date is breaking if some APIs are not alpha stability level
func TestBreaking_RemovedPathForAlphaBreaking(t *testing.T) {
s1, err := open(getDeprecationFile("base-alpha-stability.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("base-alpha-stability.yaml"))
require.NoError(t, err)
delete(s2.Spec.Paths, "/api/test")
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIRemovedCheck), d, osm)
require.Len(t, errs, 2)
require.Equal(t, errs[0].GetId(), "api-path-removed-without-deprecation")
require.Equal(t, errs[1].GetId(), "api-path-removed-without-deprecation")
}
// BC: deprecating an operation without a deprecation policy and without specifying sunset date is not breaking for draft level
func TestBreaking_DeprecationForDraft(t *testing.T) {
s1, err := open(getDeprecationFile("base-alpha-stability.yaml"))
require.NoError(t, err)
draft := toJson(t, "draft")
s1.Spec.Paths["/api/test"].Get.Extensions["x-stability-level"] = draft
s2, err := open(getDeprecationFile("deprecated-no-sunset-alpha-stability.yaml"))
require.NoError(t, err)
s2.Spec.Paths["/api/test"].Get.Extensions["x-stability-level"] = draft
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIDeprecationCheck), d, osm)
require.Empty(t, errs)
}
// BC: removing the path without a deprecation policy and without specifying sunset date is not breaking for draft level
func TestBreaking_RemovedPathForDraft(t *testing.T) {
s1, err := open(getDeprecationFile("base-alpha-stability.yaml"))
require.NoError(t, err)
draft := toJson(t, "draft")
s1.Spec.Paths["/api/test"].Get.Extensions["x-stability-level"] = draft
s1.Spec.Paths["/api/test"].Post.Extensions["x-stability-level"] = draft
s2, err := open(getDeprecationFile("base-alpha-stability.yaml"))
require.NoError(t, err)
delete(s2.Spec.Paths, "/api/test")
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIDeprecationCheck), d, osm)
require.Empty(t, errs)
}
// BC: removing the path without a deprecation policy and without specifying sunset date is breaking if some APIs are not draft stability level
func TestBreaking_RemovedPathForDraftBreaking(t *testing.T) {
s1, err := open(getDeprecationFile("base-alpha-stability.yaml"))
require.NoError(t, err)
draft := toJson(t, "draft")
s1.Spec.Paths["/api/test"].Get.Extensions["x-stability-level"] = draft
s2, err := open(getDeprecationFile("base-alpha-stability.yaml"))
require.NoError(t, err)
delete(s2.Spec.Paths, "/api/test")
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIRemovedCheck), d, osm)
require.Len(t, errs, 2)
require.Equal(t, errs[0].GetId(), "api-path-removed-without-deprecation")
require.Equal(t, errs[1].GetId(), "api-path-removed-without-deprecation")
}
func toJson(t *testing.T, value string) json.RawMessage {
t.Helper()
data, err := json.Marshal(value)
require.NoError(t, err)
return data
}
// BC: deprecating an operation with a deprecation policy and sunset date before required deprecation period is breaking
func TestBreaking_DeprecationWithEarlySunset(t *testing.T) {
s1, err := open(getDeprecationFile("base.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("deprecated-future.yaml"))
require.NoError(t, err)
s2.Spec.Paths["/api/test"].Get.Extensions[diff.SunsetExtension] = toJson(t, civil.DateOf(time.Now()).AddDays(9).String())
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
c := singleCheckConfig(checker.APIDeprecationCheck)
c.MinSunsetStableDays = 10
errs := checker.CheckBackwardCompatibility(c, d, osm)
require.NotEmpty(t, errs)
require.Len(t, errs, 1)
require.Equal(t, "api-sunset-date-too-small", errs[0].GetId())
}
// BC: deprecating an operation with a deprecation policy and sunset date after required deprecation period is not breaking
func TestBreaking_DeprecationWithProperSunset(t *testing.T) {
s1, err := open(getDeprecationFile("base.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("deprecated-future.yaml"))
require.NoError(t, err)
s2.Spec.Paths["/api/test"].Get.Extensions[diff.SunsetExtension] = toJson(t, civil.DateOf(time.Now()).AddDays(10).String())
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
c := singleCheckConfig(checker.APIDeprecationCheck)
c.MinSunsetStableDays = 10
errs := checker.CheckBackwardCompatibilityUntilLevel(c, d, osm, checker.INFO)
require.Len(t, errs, 1)
// only a non-breaking change detected
require.Equal(t, errs[0].GetLevel(), checker.INFO)
}
// BC: deleting a path after sunset date of all contained operations is not breaking
func TestBreaking_DeprecationPathPast(t *testing.T) {
s1, err := open(getDeprecationFile("deprecated-path-past.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("sunset-path.yaml"))
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIDeprecationCheck), d, osm)
require.Empty(t, errs)
}
// BC: deleting a path with some operations having sunset date in the future is breaking
func TestBreaking_DeprecationPathMixed(t *testing.T) {
s1, err := open(getDeprecationFile("deprecated-path-mixed.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("sunset-path.yaml"))
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIRemovedCheck), d, osm)
require.NotEmpty(t, errs)
require.Len(t, errs, 1)
require.Equal(t, "api-path-removed-before-sunset", errs[0].GetId())
}
// BC: deleting sunset header for a deprecated endpoint is breaking
func TestBreaking_SunsetDeletedForDeprecatedEndpoint(t *testing.T) {
s1, err := open(getDeprecationFile("deprecated-with-sunset.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("deprecated-no-sunset.yaml"))
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APISunsetChangedCheck), d, osm)
require.NotEmpty(t, errs)
require.Len(t, errs, 1)
require.Equal(t, "sunset-deleted", errs[0].GetId())
}
// test sunset date without double quotes, see https://github.com/Tufin/oasdiff/pull/198/files
func TestBreaking_DeprecationPathMixed_RFC3339_Sunset(t *testing.T) {
s1, err := open(getDeprecationFile("deprecated-path-mixed-rfc3339-sunset.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("sunset-path.yaml"))
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIRemovedCheck), d, osm)
require.NotEmpty(t, errs)
require.Len(t, errs, 1)
require.Equal(t, "api-path-removed-before-sunset", errs[0].GetId())
}
// CL: path operations that became deprecated
func TestApiDeprecated_DetectsDeprecatedOperations(t *testing.T) {
s1, err := open("../data/deprecation/base.yaml")
require.NoError(t, err)
s2, err := open("../data/deprecation/deprecated-future.yaml")
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.APIDeprecationCheck), d, osm, checker.INFO)
require.NotEmpty(t, errs)
require.Len(t, errs, 1)
require.IsType(t, checker.ApiChange{}, errs[0])
e0 := errs[0].(checker.ApiChange)
require.Equal(t, "endpoint-deprecated", e0.Id)
require.Equal(t, "GET", e0.Operation)
require.Equal(t, "/api/test", e0.Path)
}
// CL: path operations that were re-activated
func TestApiDeprecated_DetectsReactivatedOperations(t *testing.T) {
s1, err := open("../data/deprecation/deprecated-future.yaml")
require.NoError(t, err)
s2, err := open("../data/deprecation/base.yaml")
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.APIDeprecationCheck), d, osm, checker.INFO)
require.NotEmpty(t, errs)
require.Len(t, errs, 1)
require.IsType(t, checker.ApiChange{}, errs[0])
e0 := errs[0].(checker.ApiChange)
require.Equal(t, "endpoint-reactivated", e0.Id)
require.Equal(t, "GET", e0.Operation)
require.Equal(t, "/api/test", e0.Path)
}