mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Add support for quota limits
* feat: implemented SizeReporter and FileSizeReporter * test: FileSizeReporter tests * feat: added QuotedDataAccessor * test: added extra test to check recursiveness of filesizereporter * feat: added QuotaStrategy interface * feat: further progress in different files * feat: wrote doc, tests and improved code * feat: fixed bugs and code is now runnable and buildable * feat: finished implementation * fix: revert accidental chanegs * fix: fileSizeReported did not count container size * fix: bug calculating container sizes fixed * test: FileSizeReporter tests * test: QuotaDataValidator tests * test: QuotaError tests * fix: removed console.log * doc: added doc to several files * doc: changed doc for QuotaStrategy to new implementation * fix: improved content length regex * feat: improved GlobalQuotaStrategy code * fix: made FileSizeReported readonly * feat: added comments to quota-file.json * fix: changed default tempFilePath variable * test: included new tempFilePath variable in testing * chore: created seperate command for start:file:quota to pass tests * feat: removed all sync fs calls from FileSizeReporter * feat: minor changes in multple files * fix: changed function signatures to be in line with others * feat: optimized quota data validation * feat: improved FileSizeReporter code * fix: corrected calculation of containersizes and fixed erroring edgecase * feat: save content-length as number in metadata * feat: added comments and changed GlobalQuotaStrategy constructor * feat: changed file names and added small comment * test: AtomicFileDataAccessor tests * test: completed FileSizeReporter tests * fix: content-length is now saved correctly in RepresentationMetadata * feat: adapted content length metadata + tests * fix: removed tempFilePath variable * fix: reverted .gitignore * fix: forgot to remove tempFilePath variable from componentsjs config * test: GlobalQuotaStrategy tests * feat: replaced DataValidator with Validator * feat: reworked DataValidator * feat: added calcultateChunkSize() to SizeReporter * test: updated FileSizeReporter tests * fix: tempFile location now relative to rootFilePath * test: QuotaDataValidator tests * fix: corrected FileSizeReporter tests * fix: adapted FileSizeReporter tests * fix: FileSizeReporter bug on Windows * fix: regex linting error * feat: changed Validator class * feat: added PodQuotaStrategy to enable suota on a per pod basis * chore: bump context versions * fix: Capitalized comments in json file * chore: renamed ValidatorArgs to ValidatorInput * chore: order all exports * fix: made TODO comment clearer * chore: added seperated config files for global and pod based quota + fixed comments * chore: made minor changes to comments * feat: added PassthroughDataAccessor * feat: added PasstroughtDataAccessor + tests * fix: added invalid header check to ContentLengthParser * chore: improved mocks * chore: move quota limit higher up in config * fix: atomicity issue in AtomicFileDataAccessor * chore: moved .internal folder to config from FileSizeReporter * fix: improved algorithm to ignore folders while calculating file size in FileSizeReporter * fix: changes to support containers in the future * fix: added error handling to prevent reading of unexistent files * feat: added generic type to SizeReporter to calculate chunk sizes * test: use mocked DataAccessor * chore: added some comments to test and made minor improvement * fix: fs mock rename * chore: QuotaStrategy.estimateSize refactor * chore: move trackAvailableSpace to abstract class QuotaStrategy * fix: improved test case * test: quota integration tests * chore: edited some comments * chore: change lstat to stat * feat: moved estimateSize to SizeReporter to be consistent with calcultateChunkSize * test: finish up tests to reach coverage * fix: basic config * fix: minor changes to test CI run * fix: small fix for windows * fix: improved writing to file * chore: linting errors * chore: rename trackAvailableSpace * test: improved integration tests * test: logging info for test debugging * test: extra logging for debugging * test: logging for debugging * test: logging for debugging * test: logging for debugging * test: improved Quota integration test setup * test: improve quota tests for CI run * test: debugging Quota test * test: uncommented global quota test * test: changed global quota parameters * test: logging for debugging * test: logging cleanup * chore: minor changes, mostly typo fixes * chore: remove console.log * fix: getting inconsistent results * chore: try fix index.ts CI error * chore: try fix CI error * chore: try fix CI error * chore: revert last commits * chore: fix inconsistent files with origin * test: minor test improvements * chore: minor refactors and improvements * fix: added extra try catch for breaking bug * chore: improve config * chore: minor code improvements * test: use mockFs * feat: add extra check in podQuotaStrategy * chore: replace handle by handleSafe in ValidatingDataAccessor * chore: typo * test: improved Quota integration tests * test: made comment in test more correct * fix: rm -> rmdir for backwards compatibility * fix: fsPromises issue * chore: leave out irrelevant config * chore: removed start script from package.json * fix: Small fixes Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com>
This commit is contained in:
97
test/unit/storage/accessors/AtomicFileDataAccessor.test.ts
Normal file
97
test/unit/storage/accessors/AtomicFileDataAccessor.test.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import 'jest-rdf';
|
||||
import type { Readable } from 'stream';
|
||||
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
|
||||
import { AtomicFileDataAccessor } from '../../../../src/storage/accessors/AtomicFileDataAccessor';
|
||||
import { ExtensionBasedMapper } from '../../../../src/storage/mapping/ExtensionBasedMapper';
|
||||
import { APPLICATION_OCTET_STREAM } from '../../../../src/util/ContentTypes';
|
||||
import type { Guarded } from '../../../../src/util/GuardedStream';
|
||||
import { guardedStreamFrom } from '../../../../src/util/StreamUtil';
|
||||
import { CONTENT_TYPE } from '../../../../src/util/Vocabularies';
|
||||
import { mockFs } from '../../../util/Util';
|
||||
|
||||
jest.mock('fs');
|
||||
|
||||
describe('AtomicFileDataAccessor', (): void => {
|
||||
const rootFilePath = 'uploads';
|
||||
const base = 'http://test.com/';
|
||||
let accessor: AtomicFileDataAccessor;
|
||||
let cache: { data: any };
|
||||
let metadata: RepresentationMetadata;
|
||||
let data: Guarded<Readable>;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
cache = mockFs(rootFilePath, new Date());
|
||||
accessor = new AtomicFileDataAccessor(
|
||||
new ExtensionBasedMapper(base, rootFilePath),
|
||||
rootFilePath,
|
||||
'./.internal/tempFiles/',
|
||||
);
|
||||
// The 'mkdirSync' in AtomicFileDataAccessor's constructor does not seem to create the folder in the
|
||||
// cache object used for mocking fs.
|
||||
// This line creates what represents a folder in the cache object
|
||||
cache.data['.internal'] = { tempFiles: {}};
|
||||
metadata = new RepresentationMetadata(APPLICATION_OCTET_STREAM);
|
||||
data = guardedStreamFrom([ 'data' ]);
|
||||
});
|
||||
|
||||
describe('writing a document', (): void => {
|
||||
it('writes the data to the corresponding file.', async(): Promise<void> => {
|
||||
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)).resolves.toBeUndefined();
|
||||
expect(cache.data.resource).toBe('data');
|
||||
});
|
||||
|
||||
it('writes metadata to the corresponding metadata file.', async(): Promise<void> => {
|
||||
metadata = new RepresentationMetadata({ path: `${base}res.ttl` },
|
||||
{ [CONTENT_TYPE]: 'text/turtle', likes: 'apples' });
|
||||
await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).resolves.toBeUndefined();
|
||||
expect(cache.data['res.ttl']).toBe('data');
|
||||
expect(cache.data['res.ttl.meta']).toMatch(`<${base}res.ttl> <likes> "apples".`);
|
||||
});
|
||||
|
||||
it('should delete temp file when done writing.', async(): Promise<void> => {
|
||||
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)).resolves.toBeUndefined();
|
||||
expect(Object.keys(cache.data['.internal'].tempFiles)).toHaveLength(0);
|
||||
expect(cache.data.resource).toBe('data');
|
||||
});
|
||||
|
||||
it('should throw an error when writing the data goes wrong.', async(): Promise<void> => {
|
||||
data.read = jest.fn((): any => {
|
||||
data.emit('error', new Error('error'));
|
||||
return null;
|
||||
});
|
||||
jest.requireMock('fs').promises.stat = jest.fn((): any => ({
|
||||
isFile: (): boolean => false,
|
||||
}));
|
||||
await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error');
|
||||
});
|
||||
|
||||
it('should throw when renaming / moving the file goes wrong.', async(): Promise<void> => {
|
||||
jest.requireMock('fs').promises.rename = jest.fn((): any => {
|
||||
throw new Error('error');
|
||||
});
|
||||
jest.requireMock('fs').promises.stat = jest.fn((): any => ({
|
||||
isFile: (): boolean => true,
|
||||
}));
|
||||
await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error');
|
||||
});
|
||||
|
||||
it('should (on error) not unlink the temp file if it does not exist.', async(): Promise<void> => {
|
||||
jest.requireMock('fs').promises.rename = jest.fn((): any => {
|
||||
throw new Error('error');
|
||||
});
|
||||
jest.requireMock('fs').promises.stat = jest.fn((): any => ({
|
||||
isFile: (): boolean => false,
|
||||
}));
|
||||
await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error');
|
||||
});
|
||||
|
||||
it('should throw when renaming / moving the file goes wrong and the temp file does not exist.',
|
||||
async(): Promise<void> => {
|
||||
jest.requireMock('fs').promises.rename = jest.fn((): any => {
|
||||
throw new Error('error');
|
||||
});
|
||||
jest.requireMock('fs').promises.stat = jest.fn();
|
||||
await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error');
|
||||
});
|
||||
});
|
||||
});
|
||||
80
test/unit/storage/accessors/PassthroughDataAccessor.test.ts
Normal file
80
test/unit/storage/accessors/PassthroughDataAccessor.test.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
|
||||
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
|
||||
import type { DataAccessor } from '../../../../src/storage/accessors/DataAccessor';
|
||||
import { PassthroughDataAccessor } from '../../../../src/storage/accessors/PassthroughDataAccessor';
|
||||
import { guardedStreamFrom } from '../../../../src/util/StreamUtil';
|
||||
|
||||
describe('ValidatingDataAccessor', (): void => {
|
||||
let passthrough: PassthroughDataAccessor;
|
||||
let childAccessor: jest.Mocked<DataAccessor>;
|
||||
|
||||
const mockIdentifier = { path: 'http://localhost/test.txt' };
|
||||
const mockMetadata = new RepresentationMetadata();
|
||||
const mockData = guardedStreamFrom('test string');
|
||||
const mockRepresentation = new BasicRepresentation(mockData, mockMetadata);
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
jest.clearAllMocks();
|
||||
childAccessor = {
|
||||
canHandle: jest.fn(),
|
||||
writeDocument: jest.fn(),
|
||||
getData: jest.fn(),
|
||||
getChildren: jest.fn(),
|
||||
writeContainer: jest.fn(),
|
||||
deleteResource: jest.fn(),
|
||||
getMetadata: jest.fn(),
|
||||
};
|
||||
childAccessor.getChildren = jest.fn();
|
||||
passthrough = new PassthroughDataAccessor(childAccessor);
|
||||
});
|
||||
|
||||
describe('writeDocument()', (): void => {
|
||||
it('should call the accessors writeDocument() function.', async(): Promise<void> => {
|
||||
await passthrough.writeDocument(mockIdentifier, mockData, mockMetadata);
|
||||
expect(childAccessor.writeDocument).toHaveBeenCalledTimes(1);
|
||||
expect(childAccessor.writeDocument).toHaveBeenCalledWith(mockIdentifier, mockData, mockMetadata);
|
||||
});
|
||||
});
|
||||
describe('canHandle()', (): void => {
|
||||
it('should call the accessors canHandle() function.', async(): Promise<void> => {
|
||||
await passthrough.canHandle(mockRepresentation);
|
||||
expect(childAccessor.canHandle).toHaveBeenCalledTimes(1);
|
||||
expect(childAccessor.canHandle).toHaveBeenCalledWith(mockRepresentation);
|
||||
});
|
||||
});
|
||||
describe('getData()', (): void => {
|
||||
it('should call the accessors getData() function.', async(): Promise<void> => {
|
||||
await passthrough.getData(mockIdentifier);
|
||||
expect(childAccessor.getData).toHaveBeenCalledTimes(1);
|
||||
expect(childAccessor.getData).toHaveBeenCalledWith(mockIdentifier);
|
||||
});
|
||||
});
|
||||
describe('getMetadata()', (): void => {
|
||||
it('should call the accessors getMetadata() function.', async(): Promise<void> => {
|
||||
await passthrough.getMetadata(mockIdentifier);
|
||||
expect(childAccessor.getMetadata).toHaveBeenCalledTimes(1);
|
||||
expect(childAccessor.getMetadata).toHaveBeenCalledWith(mockIdentifier);
|
||||
});
|
||||
});
|
||||
describe('getChildren()', (): void => {
|
||||
it('should call the accessors getChildren() function.', async(): Promise<void> => {
|
||||
passthrough.getChildren(mockIdentifier);
|
||||
expect(childAccessor.getChildren).toHaveBeenCalledTimes(1);
|
||||
expect(childAccessor.getChildren).toHaveBeenCalledWith(mockIdentifier);
|
||||
});
|
||||
});
|
||||
describe('deleteResource()', (): void => {
|
||||
it('should call the accessors deleteResource() function.', async(): Promise<void> => {
|
||||
await passthrough.deleteResource(mockIdentifier);
|
||||
expect(childAccessor.deleteResource).toHaveBeenCalledTimes(1);
|
||||
expect(childAccessor.deleteResource).toHaveBeenCalledWith(mockIdentifier);
|
||||
});
|
||||
});
|
||||
describe('writeContainer()', (): void => {
|
||||
it('should call the accessors writeContainer() function.', async(): Promise<void> => {
|
||||
await passthrough.writeContainer(mockIdentifier, mockMetadata);
|
||||
expect(childAccessor.writeContainer).toHaveBeenCalledTimes(1);
|
||||
expect(childAccessor.writeContainer).toHaveBeenCalledWith(mockIdentifier, mockMetadata);
|
||||
});
|
||||
});
|
||||
});
|
||||
54
test/unit/storage/accessors/ValidatingDataAccessor.test.ts
Normal file
54
test/unit/storage/accessors/ValidatingDataAccessor.test.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import type { Validator, ValidatorInput } from '../../../../src/http/auxiliary/Validator';
|
||||
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
|
||||
import type { Representation } from '../../../../src/http/representation/Representation';
|
||||
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
|
||||
import type { DataAccessor } from '../../../../src/storage/accessors/DataAccessor';
|
||||
import { ValidatingDataAccessor } from '../../../../src/storage/accessors/ValidatingDataAccessor';
|
||||
import { guardedStreamFrom } from '../../../../src/util/StreamUtil';
|
||||
|
||||
describe('ValidatingDataAccessor', (): void => {
|
||||
let validatingAccessor: ValidatingDataAccessor;
|
||||
let childAccessor: jest.Mocked<DataAccessor>;
|
||||
let validator: jest.Mocked<Validator>;
|
||||
|
||||
const mockIdentifier = { path: 'http://localhost/test.txt' };
|
||||
const mockMetadata = new RepresentationMetadata();
|
||||
const mockData = guardedStreamFrom('test string');
|
||||
const mockRepresentation = new BasicRepresentation(mockData, mockMetadata);
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
jest.clearAllMocks();
|
||||
childAccessor = {
|
||||
writeDocument: jest.fn(),
|
||||
writeContainer: jest.fn(),
|
||||
} as any;
|
||||
childAccessor.getChildren = jest.fn();
|
||||
validator = {
|
||||
handleSafe: jest.fn(async(input: ValidatorInput): Promise<Representation> => input.representation),
|
||||
} as any;
|
||||
validatingAccessor = new ValidatingDataAccessor(childAccessor, validator);
|
||||
});
|
||||
|
||||
describe('writeDocument()', (): void => {
|
||||
it('should call the validator\'s handleSafe() function.', async(): Promise<void> => {
|
||||
await validatingAccessor.writeDocument(mockIdentifier, mockData, mockMetadata);
|
||||
expect(validator.handleSafe).toHaveBeenCalledTimes(1);
|
||||
expect(validator.handleSafe).toHaveBeenCalledWith({
|
||||
representation: mockRepresentation,
|
||||
identifier: mockIdentifier,
|
||||
});
|
||||
});
|
||||
it('should call the accessors writeDocument() function.', async(): Promise<void> => {
|
||||
await validatingAccessor.writeDocument(mockIdentifier, mockData, mockMetadata);
|
||||
expect(childAccessor.writeDocument).toHaveBeenCalledTimes(1);
|
||||
expect(childAccessor.writeDocument).toHaveBeenCalledWith(mockIdentifier, mockData, mockMetadata);
|
||||
});
|
||||
});
|
||||
describe('writeContainer()', (): void => {
|
||||
it('should call the accessors writeContainer() function.', async(): Promise<void> => {
|
||||
await validatingAccessor.writeContainer(mockIdentifier, mockMetadata);
|
||||
expect(childAccessor.writeContainer).toHaveBeenCalledTimes(1);
|
||||
expect(childAccessor.writeContainer).toHaveBeenCalledWith(mockIdentifier, mockMetadata);
|
||||
});
|
||||
});
|
||||
});
|
||||
132
test/unit/storage/size-reporter/FileSizeReporter.test.ts
Normal file
132
test/unit/storage/size-reporter/FileSizeReporter.test.ts
Normal file
@@ -0,0 +1,132 @@
|
||||
import { promises as fsPromises } from 'fs';
|
||||
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
|
||||
import type { ResourceIdentifier } from '../../../../src/http/representation/ResourceIdentifier';
|
||||
import type { FileIdentifierMapper, ResourceLink } from '../../../../src/storage/mapping/FileIdentifierMapper';
|
||||
import { FileSizeReporter } from '../../../../src/storage/size-reporter/FileSizeReporter';
|
||||
import { UNIT_BYTES } from '../../../../src/storage/size-reporter/Size';
|
||||
import { joinFilePath } from '../../../../src/util/PathUtil';
|
||||
import { mockFs } from '../../../util/Util';
|
||||
|
||||
jest.mock('fs');
|
||||
|
||||
describe('A FileSizeReporter', (): void => {
|
||||
// Folder size is fixed to 4 in the mock
|
||||
const folderSize = 4;
|
||||
const mapper: jest.Mocked<FileIdentifierMapper> = {
|
||||
mapFilePathToUrl: jest.fn(),
|
||||
mapUrlToFilePath: jest.fn().mockImplementation((id: ResourceIdentifier): ResourceLink => ({
|
||||
filePath: id.path,
|
||||
identifier: id,
|
||||
isMetadata: false,
|
||||
})),
|
||||
};
|
||||
const fileRoot = joinFilePath(process.cwd(), '/test-folder/');
|
||||
const fileSizeReporter = new FileSizeReporter(
|
||||
mapper,
|
||||
fileRoot,
|
||||
[ '^/\\.internal$' ],
|
||||
);
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
mockFs(fileRoot);
|
||||
});
|
||||
|
||||
it('should work without the ignoreFolders constructor parameter.', async(): Promise<void> => {
|
||||
const tempFileSizeReporter = new FileSizeReporter(
|
||||
mapper,
|
||||
fileRoot,
|
||||
);
|
||||
|
||||
const testFile = joinFilePath(fileRoot, '/test.txt');
|
||||
await fsPromises.writeFile(testFile, 'A'.repeat(20));
|
||||
|
||||
const result = tempFileSizeReporter.getSize({ path: testFile });
|
||||
await expect(result).resolves.toBeDefined();
|
||||
expect((await result).amount).toBe(20);
|
||||
});
|
||||
|
||||
it('should report the right file size.', async(): Promise<void> => {
|
||||
const testFile = joinFilePath(fileRoot, '/test.txt');
|
||||
await fsPromises.writeFile(testFile, 'A'.repeat(20));
|
||||
|
||||
const result = fileSizeReporter.getSize({ path: testFile });
|
||||
await expect(result).resolves.toBeDefined();
|
||||
expect((await result).amount).toBe(20);
|
||||
});
|
||||
|
||||
it('should work recursively.', async(): Promise<void> => {
|
||||
const containerFile = joinFilePath(fileRoot, '/test-folder-1/');
|
||||
await fsPromises.mkdir(containerFile, { recursive: true });
|
||||
const testFile = joinFilePath(containerFile, '/test.txt');
|
||||
await fsPromises.writeFile(testFile, 'A'.repeat(20));
|
||||
|
||||
const fileSize = fileSizeReporter.getSize({ path: testFile });
|
||||
const containerSize = fileSizeReporter.getSize({ path: containerFile });
|
||||
|
||||
await expect(fileSize).resolves.toEqual(expect.objectContaining({ amount: 20 }));
|
||||
await expect(containerSize).resolves.toEqual(expect.objectContaining({ amount: 20 + folderSize }));
|
||||
});
|
||||
|
||||
it('should not count files located in an ignored folder.', async(): Promise<void> => {
|
||||
const containerFile = joinFilePath(fileRoot, '/test-folder-2/');
|
||||
await fsPromises.mkdir(containerFile, { recursive: true });
|
||||
const testFile = joinFilePath(containerFile, '/test.txt');
|
||||
await fsPromises.writeFile(testFile, 'A'.repeat(20));
|
||||
|
||||
const internalContainerFile = joinFilePath(fileRoot, '/.internal/');
|
||||
await fsPromises.mkdir(internalContainerFile, { recursive: true });
|
||||
const internalTestFile = joinFilePath(internalContainerFile, '/test.txt');
|
||||
await fsPromises.writeFile(internalTestFile, 'A'.repeat(30));
|
||||
|
||||
const fileSize = fileSizeReporter.getSize({ path: testFile });
|
||||
const containerSize = fileSizeReporter.getSize({ path: containerFile });
|
||||
const rootSize = fileSizeReporter.getSize({ path: fileRoot });
|
||||
|
||||
const expectedFileSize = 20;
|
||||
const expectedContainerSize = 20 + folderSize;
|
||||
const expectedRootSize = expectedContainerSize + folderSize;
|
||||
|
||||
await expect(fileSize).resolves.toEqual(expect.objectContaining({ amount: expectedFileSize }));
|
||||
await expect(containerSize).resolves.toEqual(expect.objectContaining({ amount: expectedContainerSize }));
|
||||
await expect(rootSize).resolves.toEqual(expect.objectContaining({ amount: expectedRootSize }));
|
||||
});
|
||||
|
||||
it('should have the unit in its return value.', async(): Promise<void> => {
|
||||
const testFile = joinFilePath(fileRoot, '/test2.txt');
|
||||
await fsPromises.writeFile(testFile, 'A'.repeat(20));
|
||||
|
||||
const result = fileSizeReporter.getSize({ path: testFile });
|
||||
await expect(result).resolves.toBeDefined();
|
||||
expect((await result).unit).toBe(UNIT_BYTES);
|
||||
});
|
||||
|
||||
it('getUnit() should return UNIT_BYTES.', (): void => {
|
||||
expect(fileSizeReporter.getUnit()).toBe(UNIT_BYTES);
|
||||
});
|
||||
|
||||
it('should return 0 when the size of a non existent file is requested.', async(): Promise<void> => {
|
||||
const result = fileSizeReporter.getSize({ path: joinFilePath(fileRoot, '/test.txt') });
|
||||
await expect(result).resolves.toEqual(expect.objectContaining({ amount: 0 }));
|
||||
});
|
||||
|
||||
it('should calculate the chunk size correctly.', async(): Promise<void> => {
|
||||
const testString = 'testesttesttesttest==testtest';
|
||||
const result = fileSizeReporter.calculateChunkSize(testString);
|
||||
await expect(result).resolves.toEqual(testString.length);
|
||||
});
|
||||
|
||||
describe('estimateSize()', (): void => {
|
||||
it('should return the content-length.', async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata();
|
||||
metadata.contentLength = 100;
|
||||
await expect(fileSizeReporter.estimateSize(metadata)).resolves.toEqual(100);
|
||||
});
|
||||
it(
|
||||
'should return undefined if no content-length is present in the metadata.',
|
||||
async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata();
|
||||
await expect(fileSizeReporter.estimateSize(metadata)).resolves.toBeUndefined();
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
120
test/unit/storage/validators/QuotaValidator.test.ts
Normal file
120
test/unit/storage/validators/QuotaValidator.test.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
import type { Readable } from 'stream';
|
||||
import { PassThrough } from 'stream';
|
||||
import type { ValidatorInput } from '../../../../src/http/auxiliary/Validator';
|
||||
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
|
||||
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
|
||||
import type { ResourceIdentifier } from '../../../../src/http/representation/ResourceIdentifier';
|
||||
import type { QuotaStrategy } from '../../../../src/storage/quota/QuotaStrategy';
|
||||
import { UNIT_BYTES } from '../../../../src/storage/size-reporter/Size';
|
||||
import type { SizeReporter } from '../../../../src/storage/size-reporter/SizeReporter';
|
||||
import { QuotaValidator } from '../../../../src/storage/validators/QuotaValidator';
|
||||
import { guardStream } from '../../../../src/util/GuardedStream';
|
||||
import type { Guarded } from '../../../../src/util/GuardedStream';
|
||||
import { guardedStreamFrom, readableToString } from '../../../../src/util/StreamUtil';
|
||||
|
||||
describe('QuotaValidator', (): void => {
|
||||
let mockedStrategy: jest.Mocked<QuotaStrategy>;
|
||||
let validator: QuotaValidator;
|
||||
let identifier: ResourceIdentifier;
|
||||
let mockMetadata: RepresentationMetadata;
|
||||
let mockData: Guarded<Readable>;
|
||||
let mockInput: ValidatorInput;
|
||||
let mockReporter: jest.Mocked<SizeReporter<any>>;
|
||||
|
||||
beforeEach((): void => {
|
||||
jest.clearAllMocks();
|
||||
identifier = { path: 'http://localhost/' };
|
||||
mockMetadata = new RepresentationMetadata();
|
||||
mockData = guardedStreamFrom([ 'test string' ]);
|
||||
mockInput = {
|
||||
representation: new BasicRepresentation(mockData, mockMetadata),
|
||||
identifier,
|
||||
};
|
||||
mockReporter = {
|
||||
getSize: jest.fn(),
|
||||
getUnit: jest.fn(),
|
||||
calculateChunkSize: jest.fn(),
|
||||
estimateSize: jest.fn().mockResolvedValue(8),
|
||||
};
|
||||
mockedStrategy = {
|
||||
reporter: mockReporter,
|
||||
limit: { unit: UNIT_BYTES, amount: 8 },
|
||||
getAvailableSpace: jest.fn().mockResolvedValue({ unit: UNIT_BYTES, amount: 10 }),
|
||||
estimateSize: jest.fn().mockResolvedValue({ unit: UNIT_BYTES, amount: 8 }),
|
||||
createQuotaGuard: jest.fn().mockResolvedValue(guardStream(new PassThrough())),
|
||||
} as any;
|
||||
validator = new QuotaValidator(mockedStrategy);
|
||||
});
|
||||
|
||||
describe('handle()', (): void => {
|
||||
// Step 2
|
||||
it('should destroy the stream when estimated size is larger than the available size.', async(): Promise<void> => {
|
||||
mockedStrategy.estimateSize.mockResolvedValueOnce({ unit: UNIT_BYTES, amount: 11 });
|
||||
|
||||
const result = validator.handle(mockInput);
|
||||
await expect(result).resolves.toBeDefined();
|
||||
const awaitedResult = await result;
|
||||
|
||||
const prom = new Promise<void>((resolve, reject): void => {
|
||||
awaitedResult.data.on('error', (): void => resolve());
|
||||
awaitedResult.data.on('end', (): void => reject(new Error('reject')));
|
||||
});
|
||||
|
||||
// Consume the stream
|
||||
await expect(readableToString(awaitedResult.data))
|
||||
.rejects.toThrow('Quota exceeded: Advertised Content-Length is');
|
||||
await expect(prom).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
// Step 3
|
||||
it('should destroy the stream when quota is exceeded during write.', async(): Promise<void> => {
|
||||
mockedStrategy.createQuotaGuard.mockResolvedValueOnce(guardStream(new PassThrough({
|
||||
async transform(this): Promise<void> {
|
||||
this.destroy(new Error('error'));
|
||||
},
|
||||
})));
|
||||
|
||||
const result = validator.handle(mockInput);
|
||||
await expect(result).resolves.toBeDefined();
|
||||
const awaitedResult = await result;
|
||||
|
||||
const prom = new Promise<void>((resolve, reject): void => {
|
||||
awaitedResult.data.on('error', (): void => resolve());
|
||||
awaitedResult.data.on('end', (): void => reject(new Error('reject')));
|
||||
});
|
||||
|
||||
// Consume the stream
|
||||
await expect(readableToString(awaitedResult.data)).rejects.toThrow('error');
|
||||
expect(mockedStrategy.createQuotaGuard).toHaveBeenCalledTimes(1);
|
||||
await expect(prom).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
// Step 4
|
||||
it('should throw when quota were exceeded after stream was finished.', async(): Promise<void> => {
|
||||
const result = validator.handle(mockInput);
|
||||
|
||||
// Putting this after the handle / before consuming the stream will only effect
|
||||
// this function in the flush part of the code.
|
||||
mockedStrategy.getAvailableSpace.mockResolvedValueOnce({ unit: UNIT_BYTES, amount: -100 });
|
||||
|
||||
await expect(result).resolves.toBeDefined();
|
||||
const awaitedResult = await result;
|
||||
|
||||
const prom = new Promise<void>((resolve, reject): void => {
|
||||
awaitedResult.data.on('error', (): void => resolve());
|
||||
awaitedResult.data.on('end', (): void => reject(new Error('reject')));
|
||||
});
|
||||
|
||||
// Consume the stream
|
||||
await expect(readableToString(awaitedResult.data)).rejects.toThrow('Quota exceeded after write completed');
|
||||
await expect(prom).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return a stream that is consumable without error if quota isn\'t exceeded.', async(): Promise<void> => {
|
||||
const result = validator.handle(mockInput);
|
||||
await expect(result).resolves.toBeDefined();
|
||||
const awaitedResult = await result;
|
||||
await expect(readableToString(awaitedResult.data)).resolves.toBe('test string');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user