@@ -19,6 +19,7 @@ package cli
19
19
import (
20
20
"errors"
21
21
"fmt"
22
+ "io/fs"
22
23
"os"
23
24
"path/filepath"
24
25
"runtime"
@@ -220,13 +221,15 @@ var _ = Describe("Discover external plugins", func() {
220
221
pluginFilePath string
221
222
pluginFileName string
222
223
pluginPath string
224
+ pluginsRoot string
225
+ plugins []plugin.Plugin
223
226
f afero.File
224
- fs machinery.Filesystem
227
+ filesystem machinery.Filesystem
225
228
err error
226
229
)
227
230
228
231
BeforeEach (func () {
229
- fs = machinery.Filesystem {
232
+ filesystem = machinery.Filesystem {
230
233
FS : afero .NewMemMapFs (),
231
234
}
232
235
@@ -236,14 +239,14 @@ var _ = Describe("Discover external plugins", func() {
236
239
pluginFileName = "externalPlugin.sh"
237
240
pluginFilePath = filepath .Join (pluginPath , "externalPlugin" , "v1" , pluginFileName )
238
241
239
- err = fs .FS .MkdirAll (filepath .Dir (pluginFilePath ), 0o700 )
242
+ err = filesystem .FS .MkdirAll (filepath .Dir (pluginFilePath ), 0o700 )
240
243
Expect (err ).ToNot (HaveOccurred ())
241
244
242
- f , err = fs .FS .Create (pluginFilePath )
245
+ f , err = filesystem .FS .Create (pluginFilePath )
243
246
Expect (err ).ToNot (HaveOccurred ())
244
247
Expect (f ).ToNot (BeNil ())
245
248
246
- _ , err = fs .FS .Stat (pluginFilePath )
249
+ _ , err = filesystem .FS .Stat (pluginFilePath )
247
250
Expect (err ).ToNot (HaveOccurred ())
248
251
})
249
252
@@ -253,56 +256,56 @@ var _ = Describe("Discover external plugins", func() {
253
256
_ , err = f .WriteString (testPluginScript )
254
257
Expect (err ).To (Not (HaveOccurred ()))
255
258
256
- err = fs .FS .Chmod (pluginFilePath , filePermissions )
259
+ err = filesystem .FS .Chmod (pluginFilePath , filePermissions )
257
260
Expect (err ).To (Not (HaveOccurred ()))
258
261
259
- _ , err = fs .FS .Stat (pluginFilePath )
262
+ _ , err = filesystem .FS .Stat (pluginFilePath )
260
263
Expect (err ).ToNot (HaveOccurred ())
261
264
262
- ps , err : = DiscoverExternalPlugins (fs .FS )
265
+ plugins , err = DiscoverExternalPlugins (filesystem .FS )
263
266
Expect (err ).ToNot (HaveOccurred ())
264
- Expect (ps ).NotTo (BeNil ())
265
- Expect (ps ).To (HaveLen (1 ))
266
- Expect (ps [0 ].Name ()).To (Equal ("externalPlugin" ))
267
- Expect (ps [0 ].Version ().Number ).To (Equal (1 ))
267
+ Expect (plugins ).NotTo (BeNil ())
268
+ Expect (plugins ).To (HaveLen (1 ))
269
+ Expect (plugins [0 ].Name ()).To (Equal ("externalPlugin" ))
270
+ Expect (plugins [0 ].Version ().Number ).To (Equal (1 ))
268
271
})
269
272
270
273
It ("should discover multiple external plugins and return the plugins without any errors" , func () {
271
274
// set the execute permissions on the first plugin executable
272
- err = fs .FS .Chmod (pluginFilePath , filePermissions )
275
+ err = filesystem .FS .Chmod (pluginFilePath , filePermissions )
273
276
274
277
pluginFileName = "myotherexternalPlugin.sh"
275
278
pluginFilePath = filepath .Join (pluginPath , "myotherexternalPlugin" , "v1" , pluginFileName )
276
279
277
- f , err = fs .FS .Create (pluginFilePath )
280
+ f , err = filesystem .FS .Create (pluginFilePath )
278
281
Expect (err ).ToNot (HaveOccurred ())
279
282
Expect (f ).ToNot (BeNil ())
280
283
281
- _ , err = fs .FS .Stat (pluginFilePath )
284
+ _ , err = filesystem .FS .Stat (pluginFilePath )
282
285
Expect (err ).ToNot (HaveOccurred ())
283
286
284
287
_ , err = f .WriteString (testPluginScript )
285
288
Expect (err ).To (Not (HaveOccurred ()))
286
289
287
290
// set the execute permissions on the second plugin executable
288
- err = fs .FS .Chmod (pluginFilePath , filePermissions )
291
+ err = filesystem .FS .Chmod (pluginFilePath , filePermissions )
289
292
Expect (err ).To (Not (HaveOccurred ()))
290
293
291
- _ , err = fs .FS .Stat (pluginFilePath )
294
+ _ , err = filesystem .FS .Stat (pluginFilePath )
292
295
Expect (err ).ToNot (HaveOccurred ())
293
296
294
- ps , err : = DiscoverExternalPlugins (fs .FS )
297
+ plugins , err = DiscoverExternalPlugins (filesystem .FS )
295
298
Expect (err ).ToNot (HaveOccurred ())
296
- Expect (ps ).NotTo (BeNil ())
297
- Expect (ps ).To (HaveLen (2 ))
299
+ Expect (plugins ).NotTo (BeNil ())
300
+ Expect (plugins ).To (HaveLen (2 ))
298
301
299
- Expect (ps [0 ].Name ()).To (Equal ("externalPlugin" ))
300
- Expect (ps [1 ].Name ()).To (Equal ("myotherexternalPlugin" ))
302
+ Expect (plugins [0 ].Name ()).To (Equal ("externalPlugin" ))
303
+ Expect (plugins [1 ].Name ()).To (Equal ("myotherexternalPlugin" ))
301
304
})
302
305
303
306
Context ("that are invalid" , func () {
304
307
BeforeEach (func () {
305
- fs = machinery.Filesystem {
308
+ filesystem = machinery.Filesystem {
306
309
FS : afero .NewMemMapFs (),
307
310
}
308
311
@@ -314,47 +317,48 @@ var _ = Describe("Discover external plugins", func() {
314
317
pluginFileName = "externalPlugin.sh"
315
318
pluginFilePath = filepath .Join (pluginPath , "externalPlugin" , "v1" , pluginFileName )
316
319
317
- err = fs .FS .MkdirAll (filepath .Dir (pluginFilePath ), 0o700 )
320
+ err = filesystem .FS .MkdirAll (filepath .Dir (pluginFilePath ), 0o700 )
318
321
Expect (err ).ToNot (HaveOccurred ())
319
322
320
- f , err := fs .FS .Create (pluginFilePath )
323
+ var file fs.File
324
+ file , err = filesystem .FS .Create (pluginFilePath )
321
325
Expect (err ).ToNot (HaveOccurred ())
322
- Expect (f ).ToNot (BeNil ())
326
+ Expect (file ).ToNot (BeNil ())
323
327
324
- _ , err = fs .FS .Stat (pluginFilePath )
328
+ _ , err = filesystem .FS .Stat (pluginFilePath )
325
329
Expect (err ).ToNot (HaveOccurred ())
326
330
327
331
// set the plugin file permissions to read-only
328
- err = fs .FS .Chmod (pluginFilePath , 0o444 )
332
+ err = filesystem .FS .Chmod (pluginFilePath , 0o444 )
329
333
Expect (err ).To (Not (HaveOccurred ()))
330
334
331
- ps , err : = DiscoverExternalPlugins (fs .FS )
335
+ plugins , err = DiscoverExternalPlugins (filesystem .FS )
332
336
Expect (err ).To (HaveOccurred ())
333
337
Expect (err .Error ()).To (ContainSubstring ("not an executable" ))
334
- Expect (ps ).To (BeEmpty ())
338
+ Expect (plugins ).To (BeEmpty ())
335
339
})
336
340
337
341
It ("should error if the plugin found has an invalid plugin name" , func () {
338
342
pluginFileName = ".sh"
339
343
pluginFilePath = filepath .Join (pluginPath , "externalPlugin" , "v1" , pluginFileName )
340
344
341
- err = fs .FS .MkdirAll (filepath .Dir (pluginFilePath ), 0o700 )
345
+ err = filesystem .FS .MkdirAll (filepath .Dir (pluginFilePath ), 0o700 )
342
346
Expect (err ).ToNot (HaveOccurred ())
343
347
344
- f , err = fs .FS .Create (pluginFilePath )
348
+ f , err = filesystem .FS .Create (pluginFilePath )
345
349
Expect (err ).ToNot (HaveOccurred ())
346
350
Expect (f ).ToNot (BeNil ())
347
351
348
- ps , err : = DiscoverExternalPlugins (fs .FS )
352
+ plugins , err = DiscoverExternalPlugins (filesystem .FS )
349
353
Expect (err ).To (HaveOccurred ())
350
354
Expect (err .Error ()).To (ContainSubstring ("Invalid plugin name found" ))
351
- Expect (ps ).To (BeEmpty ())
355
+ Expect (plugins ).To (BeEmpty ())
352
356
})
353
357
})
354
358
355
359
Context ("that does not match the plugin root directory name" , func () {
356
360
BeforeEach (func () {
357
- fs = machinery.Filesystem {
361
+ filesystem = machinery.Filesystem {
358
362
FS : afero .NewMemMapFs (),
359
363
}
360
364
@@ -366,17 +370,18 @@ var _ = Describe("Discover external plugins", func() {
366
370
pluginFileName = "random.sh"
367
371
pluginFilePath = filepath .Join (pluginPath , "externalPlugin" , "v1" , pluginFileName )
368
372
369
- err = fs .FS .MkdirAll (filepath .Dir (pluginFilePath ), 0o700 )
373
+ err = filesystem .FS .MkdirAll (filepath .Dir (pluginFilePath ), 0o700 )
370
374
Expect (err ).ToNot (HaveOccurred ())
371
375
372
- f , err = fs .FS .Create (pluginFilePath )
376
+ f , err = filesystem .FS .Create (pluginFilePath )
373
377
Expect (err ).ToNot (HaveOccurred ())
374
378
Expect (f ).ToNot (BeNil ())
375
379
376
- err = fs .FS .Chmod (pluginFilePath , filePermissions )
380
+ err = filesystem .FS .Chmod (pluginFilePath , filePermissions )
377
381
Expect (err ).ToNot (HaveOccurred ())
378
382
379
- ps , err := DiscoverExternalPlugins (fs .FS )
383
+ var ps []plugin.Plugin
384
+ ps , err = DiscoverExternalPlugins (filesystem .FS )
380
385
Expect (err ).ToNot (HaveOccurred ())
381
386
Expect (ps ).To (BeEmpty ())
382
387
})
@@ -387,7 +392,7 @@ var _ = Describe("Discover external plugins", func() {
387
392
return "" , errPluginsRoot
388
393
}
389
394
390
- _ , err : = DiscoverExternalPlugins (fs .FS )
395
+ _ , err = DiscoverExternalPlugins (filesystem .FS )
391
396
Expect (err ).To (HaveOccurred ())
392
397
393
398
Expect (err ).To (Equal (errPluginsRoot ))
@@ -398,7 +403,7 @@ var _ = Describe("Discover external plugins", func() {
398
403
return "externalplugin.sh" , nil
399
404
}
400
405
401
- _ , err : = DiscoverExternalPlugins (fs .FS )
406
+ _ , err = DiscoverExternalPlugins (filesystem .FS )
402
407
Expect (err ).ToNot (HaveOccurred ())
403
408
})
404
409
@@ -410,7 +415,7 @@ var _ = Describe("Discover external plugins", func() {
410
415
411
416
home := os .Getenv ("HOME" )
412
417
413
- pluginsRoot , err : = getPluginsRoot ("darwin" )
418
+ pluginsRoot , err = getPluginsRoot ("darwin" )
414
419
Expect (err ).ToNot (HaveOccurred ())
415
420
expected := filepath .Join (home , "Library" , "Application Support" , "kubebuilder" , "plugins" )
416
421
Expect (pluginsRoot ).To (Equal (expected ))
@@ -425,7 +430,7 @@ var _ = Describe("Discover external plugins", func() {
425
430
err = os .Setenv ("XDG_CONFIG_HOME" , "/some/random/path" )
426
431
Expect (err ).ToNot (HaveOccurred ())
427
432
428
- pluginsRoot , err : = getPluginsRoot (runtime .GOOS )
433
+ pluginsRoot , err = getPluginsRoot (runtime .GOOS )
429
434
Expect (err ).ToNot (HaveOccurred ())
430
435
Expect (pluginsRoot ).To (Equal ("/some/random/path/kubebuilder/plugins" ))
431
436
})
@@ -443,9 +448,9 @@ var _ = Describe("Discover external plugins", func() {
443
448
Expect (err ).ToNot (HaveOccurred ())
444
449
}
445
450
446
- pluginsroot , err : = getPluginsRoot (runtime .GOOS )
451
+ pluginsRoot , err = getPluginsRoot (runtime .GOOS )
447
452
Expect (err ).To (HaveOccurred ())
448
- Expect (pluginsroot ).To (Equal ("" ))
453
+ Expect (pluginsRoot ).To (Equal ("" ))
449
454
Expect (err .Error ()).To (ContainSubstring ("error retrieving home dir" ))
450
455
})
451
456
})
0 commit comments