Skip to content

Commit

Permalink
Check for missing app code before upgrade #26227
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Apr 20, 2017
1 parent 631af7a commit 933d7d0
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/Command/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCP\ILogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -87,6 +88,13 @@ protected function execute(InputInterface $input, OutputInterface $output) {

$skip3rdPartyAppsDisable = false;

$preUpgradeCommand = $this->getApplication()->find('upgrade:checkapps');

$exitCode = $preUpgradeCommand->run(new ArrayInput([]), $output);
if ($exitCode !==0 ){
return 1;
}

if ($input->getOption('no-app-disable')) {
$skip3rdPartyAppsDisable = true;
}
Expand Down
72 changes: 72 additions & 0 deletions core/Command/Upgrade/CheckApps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Copyright (c) 2017, ownCloud GmbH.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Core\Command\Upgrade;

use OC\Updater\PreUpdate;
use OCP\App\IAppManager;
use OCP\ILogger;
use OC\Core\Command\Base;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CheckApps extends Base {
/** @var PreUpdate */
private $preUpdate;

/** @var ILogger */
private $logger;

/**
* @param IAppManager $appManager
* @param ILogger $logger
*/
public function __construct(PreUpdate $preUpdate, ILogger $logger) {
parent::__construct();
$this->preUpdate = $preUpdate;
$this->logger = $logger;
}


protected function configure() {
parent::configure();
$this
->setName('upgrade:checkapps')
->setDescription('Check if there are apps with missing code.')
;
}

/**
* Execute the command
*
* @param InputInterface $input input interface
* @param OutputInterface $output output interface
* @return int|null
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$missingApps = $this->preUpdate->getMissingApps();
if (count($missingApps) !==0 ){
$this->writeArrayInOutputFormat($input, $output, $missingApps);
return 1;
}
return 0;
}
}
8 changes: 8 additions & 0 deletions core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@
$application->add(new OC\Core\Command\Security\ListCertificates(\OC::$server->getCertificateManager(null), \OC::$server->getL10N('core')));
$application->add(new OC\Core\Command\Security\ImportCertificate(\OC::$server->getCertificateManager(null)));
$application->add(new OC\Core\Command\Security\RemoveCertificate(\OC::$server->getCertificateManager(null)));

$application->add(
new OC\Core\Command\Upgrade\CheckApps(
new \OC\Updater\PreUpdate(\OC::$server->getAppManager()),
\OC::$server->getLogger()
)
);

} else {
$application->add(new OC\Core\Command\Maintenance\Install(\OC::$server->getConfig()));
}
48 changes: 48 additions & 0 deletions lib/private/Updater/PreUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Copyright (c) 2017, ownCloud GmbH.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Updater;

use OCP\App\IAppManager;

class PreUpdate {
/** @var IAppManager */
private $appManager;

public function __construct(IAppManager $appManager) {
$this->appManager = $appManager;
}

/**
* @return array
*/
public function getMissingApps(){
$installedApps = $this->appManager->getInstalledApps();
$missingApps = [];
foreach ($installedApps as $appId){
$info = $this->appManager->getAppInfo($appId);
if (!isset($info['id'])){
$missingApps[] = $appId;
}
}
return $missingApps;
}
}
89 changes: 89 additions & 0 deletions tests/Core/Command/Upgrade/CheckAppsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Copyright (c) 2017, ownCloud GmbH.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace Tests\Core\Command\Upgrade;


use OC\Core\Command\Upgrade\CheckApps;
use OC\Updater\PreUpdate;
use OCP\ILogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Test\TestCase;

class CheckAppsTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $consoleInput;
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $consoleOutput;

/** @var Command */
protected $command;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $preUpdate;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $logger;


protected function setUp() {
parent::setUp();

$this->preUpdate = $this->getMockBuilder(PreUpdate::class)
->disableOriginalConstructor()
->getMock();
$this->logger = $this->getMockBuilder(ILogger::class)
->disableOriginalConstructor()
->getMock();


$this->consoleInput = $this->createMock(InputInterface::class);
$this->consoleOutput = $this->createMock(OutputInterface::class);

$this->command = new CheckApps($this->preUpdate, $this->logger);
}

public function testCheckAppsData(){
return [
[
[], 0,
],
[
['foo_app'], 1,
],
];
}

/** @dataProvider testCheckAppsData
* @param [] $appIds
* @param int $expected
*/
public function testCheckApps($appIds, $expected) {
$this->preUpdate->expects($this->any())
->method('getMissingApps')
->willReturn($appIds);

$exitCode = $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
$this->assertEquals($expected, $exitCode);
}
}
87 changes: 87 additions & 0 deletions tests/lib/Updater/PreUpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Copyright (c) 2017, ownCloud GmbH.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace Tests\Lib\Updater;

use OC\Updater\PreUpdate;
use OCP\App\IAppManager;
use Test\TestCase;

class PreUpdateTest extends TestCase {

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $appManager;

/** @var PreUpdate */
protected $instance;

protected function setUp() {
parent::setUp();
$this->appManager = $this->getMockBuilder(IAppManager::class)
->disableOriginalConstructor()
->getMock();

$this->instance = new PreUpdate($this->appManager);
}

public function testGetMissingAppsData(){
return [
[
['firstapp', 'secondapp', 'thirdapp'],
[
['firstapp', [ 'id' => 'firstapp' ]],
['secondapp', [ 'id' => 'secondapp' ]],
['thirdapp', [ 'id' => 'thirdapp' ]],
],
[]
],

[
['firstapp', 'secondapp', 'thirdapp'],
[
['firstapp', [ 'id' => 'firstapp' ]],
['secondapp', [] ],
['thirdapp', [ 'id' => 'thirdapp' ]],
],
['secondapp']
],

];
}

/** @dataProvider testGetMissingAppsData
* @param string[] $installedApps
* @param [] $appMap
* @param [] $expected
*/
public function testGetMissingApps($installedApps, $appMap, $expected) {
$this->appManager->expects($this->any())
->method('getInstalledApps')
->willReturn($installedApps);

$this->appManager->expects($this->any())
->method('getAppInfo')
->willReturnMap($appMap);

$actual = $this->instance->getMissingApps();
$this->assertEquals($expected, $actual);
}
}

0 comments on commit 933d7d0

Please sign in to comment.