-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathcheck_api_deprecation_test.go
336 lines (263 loc) · 13.3 KB
/
check_api_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
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/diff"
"github.com/tufin/oasdiff/load"
)
func open(file string) (*load.SpecInfo, error) {
return load.NewSpecInfo(openapi3.NewLoader(), load.NewSource(file))
}
func getDeprecationFile(file string) string {
return fmt.Sprintf("../data/deprecation/%s", file)
}
func singleCheckConfig(c checker.BackwardCompatibilityCheck) *checker.Config {
return checker.NewConfig(checker.BackwardCompatibilityChecks{c}).WithSingleCheck(c)
}
func allChecksConfig() *checker.Config {
return checker.NewConfig(checker.GetAllChecks())
}
// 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(diff.NewConfig(), 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 and an invalid sunset date is breaking
func TestBreaking_DeprecationWithInvalidSunset(t *testing.T) {
s1, err := open(getDeprecationFile("base.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("deprecated-with-invalid-sunset.yaml"))
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
c := singleCheckConfig(checker.APIDeprecationCheck).WithDeprecation(0, 10)
errs := checker.CheckBackwardCompatibility(c, d, osm)
require.NotEmpty(t, errs)
require.Len(t, errs, 1)
require.Equal(t, checker.APIDeprecatedSunsetParseId, errs[0].GetId())
require.Equal(t, "failed to parse sunset date: 'sunset date doesn't conform with RFC3339: invalid'", errs[0].GetUncolorizedText(checker.NewDefaultLocalizer()))
}
// BC: deprecating an operation with a deprecation policy and an invalid stability level is breaking
func TestBreaking_DeprecationWithInvalidStabilityLevel(t *testing.T) {
s1, err := open(getDeprecationFile("base.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("deprecated-with-invalid-stability.yaml"))
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
c := singleCheckConfig(checker.APIDeprecationCheck).WithDeprecation(0, 10)
errs := checker.CheckBackwardCompatibility(c, d, osm)
require.NotEmpty(t, errs)
require.Len(t, errs, 1)
require.Equal(t, checker.APIInvalidStabilityLevelId, errs[0].GetId())
require.Equal(t, "failed to parse stability level: 'value is not one of draft, alpha, beta or stable: \"invalid\"'", errs[0].GetUncolorizedText(checker.NewDefaultLocalizer()))
require.Equal(t, "../data/deprecation/deprecated-with-invalid-stability.yaml", errs[0].GetSource())
}
// BC: deprecating an operation without a deprecation policy but without specifying sunset date is not breaking
func TestBreaking_DeprecationWithoutSunsetNoPolicy(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(diff.NewConfig(), s1, s2)
require.NoError(t, err)
c := singleCheckConfig(checker.APIDeprecationCheck).WithDeprecation(0, 0)
errs := checker.CheckBackwardCompatibility(c, d, osm)
require.Empty(t, errs)
}
// BC: deprecating an operation with a deprecation policy but without specifying sunset date is breaking
func TestBreaking_DeprecationWithoutSunsetWithPolicy(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(diff.NewConfig(), s1, s2)
require.NoError(t, err)
c := singleCheckConfig(checker.APIDeprecationCheck).WithDeprecation(30, 100)
errs := checker.CheckBackwardCompatibility(c, d, osm)
require.Len(t, errs, 1)
require.Equal(t, checker.APIDeprecatedSunsetMissingId, errs[0].GetId())
require.Equal(t, "sunset date is missing for deprecated API", errs[0].GetText(checker.NewDefaultLocalizer()))
}
// BC: deprecating an operation with a default deprecation policy but without specifying sunset date is not 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(diff.NewConfig(), s1, s2)
require.NoError(t, err)
c := singleCheckConfig(checker.APIDeprecationCheck)
errs := checker.CheckBackwardCompatibility(c, d, osm)
require.Empty(t, errs)
}
// BC: deprecating an operation without a deprecation policy and without specifying sunset date is not breaking for alpha level
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(diff.NewConfig(), 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, checker.STABILITY_ALPHA)
s1.Spec.Paths.Value("/api/test").Get.Extensions["x-stability-level"] = alpha
s1.Spec.Paths.Value("/api/test").Post.Extensions = map[string]interface{}{"x-stability-level": alpha}
s2, err := open(getDeprecationFile("base-alpha-stability.yaml"))
require.NoError(t, err)
s2.Spec.Paths.Delete("/api/test")
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIDeprecationCheck), d, osm)
require.Empty(t, errs)
}
// 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, checker.STABILITY_DRAFT)
s1.Spec.Paths.Value("/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.Value("/api/test").Get.Extensions["x-stability-level"] = draft
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), 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, checker.STABILITY_DRAFT)
s1.Spec.Paths.Value("/api/test").Get.Extensions["x-stability-level"] = draft
s1.Spec.Paths.Value("/api/test").Post.Extensions = map[string]interface{}{"x-stability-level": draft}
s2, err := open(getDeprecationFile("base-alpha-stability.yaml"))
require.NoError(t, err)
s2.Spec.Paths.Delete("/api/test")
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIDeprecationCheck), d, osm)
require.Empty(t, errs)
}
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)
sunsetDate := civil.DateOf(time.Now()).AddDays(9).String()
s2.Spec.Paths.Value("/api/test").Get.Extensions[diff.SunsetExtension] = toJson(t, sunsetDate)
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
c := singleCheckConfig(checker.APIDeprecationCheck).WithDeprecation(0, 10)
errs := checker.CheckBackwardCompatibility(c, d, osm)
require.NotEmpty(t, errs)
require.Len(t, errs, 1)
require.Equal(t, checker.APISunsetDateTooSmallId, errs[0].GetId())
require.Equal(t, fmt.Sprintf("sunset date '%s' is too small, must be at least '10' days from now", sunsetDate), errs[0].GetUncolorizedText(checker.NewDefaultLocalizer()))
}
// 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.Value("/api/test").Get.Extensions[diff.SunsetExtension] = toJson(t, civil.DateOf(time.Now()).AddDays(10).String())
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
c := singleCheckConfig(checker.APIDeprecationCheck).WithDeprecation(0, 10)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibilityUntilLevel(c, d, osm, checker.INFO)
require.Len(t, errs, 1)
// only a non-breaking change detected
require.Equal(t, checker.INFO, errs[0].GetLevel())
require.Equal(t, "endpoint deprecated", errs[0].GetUncolorizedText(checker.NewDefaultLocalizer()))
}
// 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(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIDeprecationCheck), d, osm)
require.Empty(t, errs)
}
// 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(diff.NewConfig(), 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, checker.EndpointDeprecatedId, e0.Id)
require.Equal(t, "GET", e0.Operation)
require.Equal(t, "/api/test", e0.Path)
require.Equal(t, "endpoint deprecated", e0.GetUncolorizedText(checker.NewDefaultLocalizer()))
}
// 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(diff.NewConfig(), 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, checker.EndpointReactivatedId, e0.Id)
require.Equal(t, "GET", e0.Operation)
require.Equal(t, "/api/test", e0.Path)
require.Equal(t, "endpoint reactivated", e0.GetUncolorizedText(checker.NewDefaultLocalizer()))
}
func TestBreaking_InvaidStability(t *testing.T) {
s1, err := open(getDeprecationFile("invalid-stability.yaml"))
require.NoError(t, err)
s2, err := open(getDeprecationFile("base-alpha-stability.yaml"))
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.APIDeprecationCheck), d, osm)
require.Len(t, errs, 1)
require.IsType(t, checker.ApiChange{}, errs[0])
e0 := errs[0].(checker.ApiChange)
require.Equal(t, checker.APIInvalidStabilityLevelId, e0.Id)
require.Equal(t, "GET", e0.Operation)
require.Equal(t, "/api/test", e0.Path)
require.Equal(t, "failed to parse stability level: 'value is not one of draft, alpha, beta or stable: \"ga\"'", e0.GetUncolorizedText(checker.NewDefaultLocalizer()))
require.Equal(t, "../data/deprecation/invalid-stability.yaml", errs[0].GetSource())
}