Skip to content

Commit 9986f32

Browse files
add cli tests for listing stale upload
1 parent 6a1a2f3 commit 9986f32

File tree

3 files changed

+245
-7
lines changed

3 files changed

+245
-7
lines changed

.drone.star

+14-7
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,12 @@ pipelineVolumeGo = \
363363
"temp": {},
364364
}
365365

366+
storageVolume = \
367+
{
368+
"name": "storage",
369+
"path": "/root/.ocis",
370+
}
371+
366372
# minio mc environment variables
367373
MINIO_MC_ENV = {
368374
"CACHE_BUCKET": S3_CACHE_BUCKET,
@@ -1030,7 +1036,7 @@ def localApiTestPipeline(ctx):
10301036
restoreBuildArtifactCache(ctx, "ocis-binary-amd64", "ocis/bin") +
10311037
(tikaService() if params["tikaNeeded"] else []) +
10321038
(waitForServices("online-offices", ["collabora:9980", "onlyoffice:443", "fakeoffice:8080"]) if params["collaborationServiceNeeded"] else []) +
1033-
ocisServer(storage, params["accounts_hash_difficulty"], extra_server_environment = params["extraServerEnvironment"], with_wrapper = True, tika_enabled = params["tikaNeeded"]) +
1039+
ocisServer(storage, params["accounts_hash_difficulty"], extra_server_environment = params["extraServerEnvironment"], with_wrapper = True, tika_enabled = params["tikaNeeded"], volumes = ([storageVolume] if name.startswith("cli") else [])) +
10341040
(waitForClamavService() if params["antivirusNeeded"] else []) +
10351041
(waitForEmailService() if params["emailNeeded"] else []) +
10361042
(ocisServer(storage, params["accounts_hash_difficulty"], deploy_type = "federation", extra_server_environment = params["extraServerEnvironment"]) if params["federationServer"] else []) +
@@ -1049,6 +1055,10 @@ def localApiTestPipeline(ctx):
10491055
"refs/pull/**",
10501056
],
10511057
},
1058+
"volumes": [{
1059+
"name": "storage",
1060+
"temp": {},
1061+
}],
10521062
}
10531063
pipelines.append(pipeline)
10541064
return pipelines
@@ -1120,6 +1130,7 @@ def localApiTests(ctx, name, suites, storage = "ocis", extra_environment = {}, w
11201130
"" if with_remote_php else "cat %s/expected-failures-without-remotephp.md >> %s" % (test_dir, expected_failures_file),
11211131
"make -C %s test-acceptance-api" % (dirs["base"]),
11221132
],
1133+
"volumes": [storageVolume],
11231134
}]
11241135

11251136
def cs3ApiTests(ctx, storage, accounts_hash_difficulty = 4):
@@ -1525,12 +1536,8 @@ def multiServiceE2ePipeline(ctx):
15251536
for item in storage_users_environment:
15261537
storage_users2_environment[item] = storage_users_environment[item]
15271538

1528-
storage_volume = [{
1529-
"name": "storage",
1530-
"path": "/root/.ocis",
1531-
}]
1532-
storage_users_services = startOcisService("storage-users", "storageusers1", storage_users1_environment, storage_volume) + \
1533-
startOcisService("storage-users", "storageusers2", storage_users2_environment, storage_volume) + \
1539+
storage_users_services = startOcisService("storage-users", "storageusers1", storage_users1_environment, [storageVolume]) + \
1540+
startOcisService("storage-users", "storageusers2", storage_users2_environment, [storageVolume]) + \
15341541
ocisHealthCheck("storage-users", ["storageusers1:9159", "storageusers2:9159"])
15351542

15361543
for _, suite in config["e2eMultiService"].items():

tests/acceptance/bootstrap/CliContext.php

+155
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
2424
use Behat\Behat\Context\Context;
25+
use Behat\Gherkin\Node\PyStringNode;
2526
use Behat\Gherkin\Node\TableNode;
2627
use GuzzleHttp\Exception\GuzzleException;
2728
use PHPUnit\Framework\Assert;
@@ -480,6 +481,16 @@ public function cleanUploadsSessions(): void {
480481
Assert::assertEquals("200", $response->getStatusCode(), "Failed to clean upload sessions");
481482
}
482483

484+
/**
485+
* @AfterScenario @cli-stale-uploads
486+
*
487+
* @return void
488+
*/
489+
public function cleanUpStaleUploads(): void {
490+
$response = $this->runDeleteStaleUploadsCommand(false);
491+
$this->featureContext->theHTTPStatusCodeShouldBe(200, "Failed to cleanup stale upload", $response);
492+
}
493+
483494
/**
484495
* @When /^the administrator triggers "([^"]*)" email notifications using the CLI$/
485496
*
@@ -496,4 +507,148 @@ public function theAdministratorTriggersEmailNotificationsUsingTheCLI(string $in
496507

497508
$this->featureContext->setResponse(CliHelper::runCommand($body));
498509
}
510+
511+
/**
512+
* @Given the administrator has created stale upload
513+
*
514+
* @return void
515+
*/
516+
public function theAdministratorHasCreatedStaleUpload(): void {
517+
$folderPath = $this->featureContext->getStorageUsersRoot() . "/uploads";
518+
$infoFiles = glob($folderPath . '/*.info');
519+
foreach ($infoFiles as $file) {
520+
if (!unlink($file)) {
521+
Assert::fail("Fail to delete info file");
522+
}
523+
}
524+
}
525+
526+
/**
527+
* Runs the stale uploads CLI command
528+
*
529+
* @param bool $dryRun
530+
* @param string|null $spaceId
531+
*
532+
* @return ResponseInterface
533+
*/
534+
protected function runDeleteStaleUploadsCommand(bool $dryRun=true, ?string $spaceId = null): ResponseInterface {
535+
$command = "storage-users uploads delete-stale-nodes";
536+
$command .= " --dry-run=" . ($dryRun ? "true" : "false");
537+
538+
if ($spaceId !== null) {
539+
$command .= " --spaceid=$spaceId";
540+
}
541+
542+
$body = [
543+
"command" => $command
544+
];
545+
return CliHelper::runCommand($body);
546+
}
547+
548+
/**
549+
* @When the administrator lists all the stale uploads
550+
*
551+
* @return void
552+
*/
553+
public function theAdministratorListsAllTheStaleUploads(): void {
554+
$this->featureContext->setResponse($this->runDeleteStaleUploadsCommand(true));
555+
}
556+
557+
/**
558+
* @When the administrator lists all the stale uploads of space :space owned by user :user
559+
*
560+
* @param string $spaceName
561+
* @param string $user
562+
*
563+
* @return void
564+
*/
565+
public function theAdministratorListsTheStaleUploadsOfSpace(string $spaceName, string $user): void {
566+
$space = $this->spacesContext->getSpaceByName(
567+
$user,
568+
$spaceName
569+
);
570+
$spaceOwnerId = $space["owner"]["user"]["id"];
571+
$this->featureContext->setResponse($this->runDeleteStaleUploadsCommand(true, $spaceOwnerId));
572+
}
573+
574+
/**
575+
* @Then the CLI response should contain the following message:
576+
*
577+
* @param PyStringNode $content
578+
*
579+
* @return void
580+
*/
581+
public function theCLIResponseShouldContainTheseMessage(PyStringNode $content): void {
582+
$response = $this->featureContext->getJsonDecodedResponseBodyContent();
583+
$this->featureContext->theHTTPStatusCodeShouldBe(200);
584+
$expectedMessage = str_replace("\r\n", "\n", trim($content->getRaw()));
585+
$actualMessage = str_replace("\r\n", "\n", trim($response->message ?? ''));
586+
587+
Assert::assertSame(
588+
$expectedMessage,
589+
$actualMessage,
590+
"Expected cli output to be $expectedMessage but found $actualMessage"
591+
);
592+
}
593+
594+
/**
595+
* @When the administrator deletes all the stale uploads
596+
*
597+
* @return void
598+
*/
599+
public function theAdministratorDeletesAllTheStaleUploads(): void {
600+
$this->featureContext->setResponse($this->runDeleteStaleUploadsCommand(false));
601+
}
602+
603+
/**
604+
* @When the administrator deletes all the stale uploads of space :spaceName owned by user :user
605+
*
606+
* @param string $spaceName
607+
* @param string $user
608+
*
609+
* @return void
610+
*/
611+
public function theAdministratorDeletesTheStaleUploadsOfSpaceOwnedByUser(string $spaceName, string $user): void {
612+
$space = $this->spacesContext->getSpaceByName(
613+
$user,
614+
$spaceName
615+
);
616+
$spaceOwnerId = $space["owner"]["user"]["id"];
617+
$this->featureContext->setResponse($this->runDeleteStaleUploadsCommand(false, $spaceOwnerId));
618+
}
619+
620+
/**
621+
* @Then there should be :number stale uploads
622+
* @Then there should be :number stale uploads of space :spaceName owned by user :user
623+
*
624+
* @param int $number
625+
* @param string|null $spaceName
626+
* @param string|null $user
627+
*
628+
* @return void
629+
* @throws GuzzleException
630+
*/
631+
public function thereShouldBeStaleUploadsOfSpaceOwnedByUser(int $number, string $spaceName='', string $user=''): void {
632+
$spaceOwnerId = null;
633+
if ($spaceName !== '' && $user !== '') {
634+
$space = $this->spacesContext->getSpaceByName(
635+
$user,
636+
$spaceName
637+
);
638+
$spaceOwnerId = $space["owner"]["user"]["id"];
639+
}
640+
641+
$response = $this->runDeleteStaleUploadsCommand(true, $spaceOwnerId);
642+
$jsonDecodedResponse = $this->featureContext->getJsonDecodedResponseBodyContent($response);
643+
$this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response);
644+
645+
$expectedMessage = "Total stale nodes: $number";
646+
647+
Assert::assertStringContainsString(
648+
$expectedMessage,
649+
$jsonDecodedResponse->message ?? '',
650+
"Expected message to contain '$expectedMessage', but got: " . ($jsonDecodedResponse->message ?? 'null')
651+
);
652+
653+
}
499654
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
@env-config @cli-stale-uploads
2+
Feature: stale upload via CLI command
3+
As a user
4+
I want to manage stale upload
5+
So that I clean up stale uploads on storage
6+
7+
Background:
8+
Given user "Alice" has been created with default attributes
9+
And user "Brian" has been created with default attributes
10+
11+
12+
Scenario: list and delete all stale uploads
13+
Given the config "POSTPROCESSING_DELAY" has been set to "10s"
14+
And the administrator has assigned the role "Space Admin" to user "Alice" using the Graph API
15+
And user "Alice" has created a space "staleuploads" with the default quota using the Graph API
16+
And user "Alice" has uploaded a file "filesForUpload/testavatar.jpg" to "/testavatar.jpg" in space "staleuploads"
17+
And user "Brian" has uploaded file "filesForUpload/textfile.txt" to "textfile.txt"
18+
And the administrator has stopped the server
19+
And the administrator has created stale upload
20+
And the administrator has started the server
21+
When the administrator lists all the stale uploads
22+
Then the CLI response should contain the following message:
23+
"""
24+
Scanning all spaces for stale processing nodes...
25+
Total stale nodes: 2
26+
"""
27+
When the administrator deletes all the stale uploads
28+
Then the CLI response should contain the following message:
29+
"""
30+
Scanning all spaces for stale processing nodes...
31+
Total stale nodes: 2
32+
"""
33+
And there should be "0" stale uploads
34+
35+
36+
Scenario: list and delete all stale upload of specific space
37+
Given user "Alice" has created folder "FolderToShare"
38+
And using spaces DAV path
39+
And the administrator has assigned the role "Space Admin" to user "Alice" using the Graph API
40+
And user "Alice" has created a space "staleuploads" with the default quota using the Graph API
41+
And using SharingNG
42+
And the config "POSTPROCESSING_DELAY" has been set to "10s"
43+
And user "Alice" has sent the following resource share invitation:
44+
| resource | FolderToShare |
45+
| space | Personal |
46+
| sharee | Brian |
47+
| shareType | user |
48+
| permissionsRole | Uploader |
49+
And user "Brian" has uploaded a file "filesForUpload/testavatar.png" to "FolderToShare/testavatar.png" in space "Shares"
50+
And user "Alice" has uploaded file "filesForUpload/testavatar.png" to "testavatar.png"
51+
And user "Alice" has uploaded a file "filesForUpload/testavatar.jpg" to "/testavatar.jpg" in space "staleuploads"
52+
And the administrator has stopped the server
53+
And the administrator has created stale upload
54+
And the administrator has started the server
55+
When the administrator lists all the stale uploads of space "Personal" owned by user "Alice"
56+
Then the CLI response should contain the following message:
57+
"""
58+
Total stale nodes: 2
59+
"""
60+
When the administrator lists all the stale uploads of space "staleuploads" owned by user "Alice"
61+
Then the CLI response should contain the following message:
62+
"""
63+
Total stale nodes: 1
64+
"""
65+
When the administrator deletes all the stale uploads of space "Personal" owned by user "Alice"
66+
Then the CLI response should contain the following message:
67+
"""
68+
Total stale nodes: 2
69+
"""
70+
And there should be "0" stale uploads of space "Personal" owned by user "Alice"
71+
When the administrator deletes all the stale uploads of space "staleuploads" owned by user "Alice"
72+
Then the CLI response should contain the following message:
73+
"""
74+
Total stale nodes: 1
75+
"""
76+
And there should be "0" stale uploads of space "Personal" owned by user "Alice"

0 commit comments

Comments
 (0)