]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Handle broken plugin install
authorChocobozzz <me@florianbigard.com>
Wed, 30 Jun 2021 09:45:06 +0000 (11:45 +0200)
committerChocobozzz <me@florianbigard.com>
Wed, 30 Jun 2021 09:45:20 +0000 (11:45 +0200)
server/lib/plugins/plugin-manager.ts
server/tests/api/server/plugins.ts
server/tests/fixtures/peertube-plugin-test-broken/main.js [new file with mode: 0644]
server/tests/fixtures/peertube-plugin-test-broken/package.json [new file with mode: 0644]

index 6b9a255a4bd2d182fce90031ad3eb06e53439aa0..6599bcccaf28f95f70eb936a88a66a7c4e8e2bb4 100644 (file)
@@ -304,22 +304,28 @@ export class PluginManager implements ServerHook {
         uninstalled: false,
         peertubeEngine: packageJSON.engine.peertube
       }, { returning: true })
-    } catch (err) {
-      logger.error('Cannot install plugin %s, removing it...', toInstall, { err })
+
+      logger.info('Successful installation of plugin %s.', toInstall)
+
+      await this.registerPluginOrTheme(plugin)
+    } catch (rootErr) {
+      logger.error('Cannot install plugin %s, removing it...', toInstall, { err: rootErr })
 
       try {
-        await removeNpmPlugin(npmName)
+        await this.uninstall(npmName)
       } catch (err) {
-        logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err })
+        logger.error('Cannot uninstall plugin %s after failed installation.', toInstall, { err })
+
+        try {
+          await removeNpmPlugin(npmName)
+        } catch (err) {
+          logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err })
+        }
       }
 
-      throw err
+      throw rootErr
     }
 
-    logger.info('Successful installation of plugin %s.', toInstall)
-
-    await this.registerPluginOrTheme(plugin)
-
     return plugin
   }
 
@@ -425,8 +431,7 @@ export class PluginManager implements ServerHook {
 
     await ensureDir(registerOptions.peertubeHelpers.plugin.getDataDirectoryPath())
 
-    library.register(registerOptions)
-           .catch(err => logger.error('Cannot register plugin %s.', npmName, { err }))
+    await library.register(registerOptions)
 
     logger.info('Add plugin %s CSS to global file.', npmName)
 
index 6046ab97e21edc858adfcc1d7c35447c4d272ca9..6b61c7c33705384733584872d4fe6d7d7daa62f0 100644 (file)
@@ -2,6 +2,7 @@
 
 import 'mocha'
 import * as chai from 'chai'
+import { HttpStatusCode } from '@shared/core-utils'
 import {
   cleanupTests,
   closeAllSequelize,
@@ -10,6 +11,7 @@ import {
   getMyUserInformation,
   getPlugin,
   getPluginPackageJSON,
+  getPluginTestPath,
   getPublicSettings,
   installPlugin,
   killallServers,
@@ -400,6 +402,36 @@ describe('Test plugins', function () {
     expect((res.body as User).theme).to.equal('instance-default')
   })
 
+  it('Should not install a broken plugin', async function () {
+    this.timeout(60000)
+
+    async function check () {
+      const res = await listPlugins({
+        url: server.url,
+        accessToken: server.accessToken,
+        pluginType: PluginType.PLUGIN
+      })
+
+      const plugins: PeerTubePlugin[] = res.body.data
+
+      expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
+    }
+
+    await installPlugin({
+      url: server.url,
+      accessToken: server.accessToken,
+      path: getPluginTestPath('-broken'),
+      expectedStatus: HttpStatusCode.BAD_REQUEST_400
+    })
+
+    await check()
+
+    killallServers([ server ])
+    await reRunServer(server)
+
+    await check()
+  })
+
   after(async function () {
     await closeAllSequelize([ server ])
     await cleanupTests([ server ])
diff --git a/server/tests/fixtures/peertube-plugin-test-broken/main.js b/server/tests/fixtures/peertube-plugin-test-broken/main.js
new file mode 100644 (file)
index 0000000..afdb6f7
--- /dev/null
@@ -0,0 +1,12 @@
+async function register (options) {
+  options.unknownFunction()
+}
+
+async function unregister () {
+  return
+}
+
+module.exports = {
+  register,
+  unregister
+}
diff --git a/server/tests/fixtures/peertube-plugin-test-broken/package.json b/server/tests/fixtures/peertube-plugin-test-broken/package.json
new file mode 100644 (file)
index 0000000..fd03df2
--- /dev/null
@@ -0,0 +1,20 @@
+{
+  "name": "peertube-plugin-test-broken",
+  "version": "0.0.1",
+  "description": "Plugin test broken",
+  "engine": {
+    "peertube": ">=1.3.0"
+  },
+  "keywords": [
+    "peertube",
+    "plugin"
+  ],
+  "homepage": "https://github.com/Chocobozzz/PeerTube",
+  "author": "Chocobozzz",
+  "bugs": "https://github.com/Chocobozzz/PeerTube/issues",
+  "library": "./main.js",
+  "staticDirs": {},
+  "css": [],
+  "clientScripts": [],
+  "translations": {}
+}