-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
export function config(_subcommand, ...args) { | ||
if (!_subcommand) { | ||
throw new Error("ERR wrong number of arguments for 'config' command") | ||
} | ||
|
||
const subcommand = _subcommand.toUpperCase() | ||
|
||
if (subcommand === 'HELP' && args.length === 0) { | ||
return [ | ||
'CONFIG <subcommand> [<arg> [value] [opt] ...]. Subcommands are:', | ||
'GET <pattern>', | ||
' Return parameters matching the glob-like <pattern> and their values.', | ||
'SET <directive> <value>', | ||
' Set the configuration <directive> to <value>.', | ||
'RESETSTAT', | ||
' Reset statistics reported by the INFO command.', | ||
'REWRITE', | ||
' Rewrite the configuration file.', | ||
'HELP', | ||
' Prints this help.', | ||
] | ||
} | ||
|
||
if (subcommand === 'GET' && args.length > 0) { | ||
return [] | ||
} | ||
|
||
if (subcommand === 'SET' && args.length > 1) { | ||
throw new Error(`ERR Unsupported CONFIG parameter: ${args[0]}`) | ||
} | ||
|
||
if (subcommand === 'RESETSTAT' && args.length === 0) { | ||
return 'OK' | ||
} | ||
|
||
if (subcommand === 'REWRITE' && args.length === 0) { | ||
throw new Error('ERR The server is running without a config file') | ||
} | ||
|
||
throw new Error( | ||
`ERR Unknown subcommand or wrong number of arguments for '${_subcommand}'. Try CONFIG HELP.` | ||
) | ||
} | ||
|
||
export function configBuffer(...args) { | ||
const val = config.apply(this, args) | ||
if (val?.map) { | ||
return val.map(payload => (payload ? Buffer.from(payload) : payload)) | ||
} | ||
return val ? Buffer.from(val) : val | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import Redis from 'ioredis' | ||
|
||
// eslint-disable-next-line import/no-relative-parent-imports | ||
import { runTwinSuite } from '../../../test-utils' | ||
|
||
runTwinSuite('config', (command, equals) => { | ||
describe(command, () => { | ||
const redis = new Redis() | ||
|
||
afterAll(() => { | ||
redis.disconnect() | ||
}) | ||
|
||
it('should throw on too few arguments', async () => { | ||
expect.hasAssertions() | ||
|
||
try { | ||
await redis[command]() | ||
} catch (err) { | ||
expect(err.message).toMatch('wrong number of arguments') | ||
} | ||
}) | ||
|
||
it('should throw on unknown subcommand', async () => { | ||
expect.hasAssertions() | ||
|
||
try { | ||
await redis[command]('foobar') | ||
} catch (err) { | ||
expect(err.message).toMatch('Unknown subcommand') | ||
} | ||
}) | ||
|
||
describe('get', () => { | ||
it('should throw on wrong number of arguments', async () => { | ||
expect.hasAssertions() | ||
|
||
try { | ||
await redis[command]('get') | ||
} catch (err) { | ||
expect(err.message).toMatch('wrong number of arguments') | ||
} | ||
}) | ||
|
||
it('returns a list over config options', async () => { | ||
expect(await redis[command]('GET', '*')).toEqual(expect.any(Array)) | ||
}) | ||
}) | ||
|
||
describe('set', () => { | ||
it('should throw on wrong number of arguments', async () => { | ||
expect.assertions(2) | ||
|
||
try { | ||
await redis[command]('set') | ||
} catch (err) { | ||
expect(err.message).toMatch('wrong number of arguments') | ||
} | ||
|
||
try { | ||
await redis[command]('SET', 'foo') | ||
} catch (err) { | ||
expect(err.message).toMatch('wrong number of arguments') | ||
} | ||
}) | ||
|
||
it('should throw as we actually do not support setting the config', async () => { | ||
expect.hasAssertions() | ||
|
||
try { | ||
await redis[command]('SET', 'maxmockmemory', '1000000') | ||
} catch (err) { | ||
expect(err.message).toMatch( | ||
'ERR Unsupported CONFIG parameter: maxmockmemory' | ||
) | ||
} | ||
}) | ||
}) | ||
|
||
describe('resetstat', () => { | ||
it('should throw on wrong number of arguments', async () => { | ||
expect.hasAssertions() | ||
|
||
try { | ||
await redis[command]('RESETSTAT', 'foo') | ||
} catch (err) { | ||
expect(err.message).toMatch('wrong number of arguments') | ||
} | ||
}) | ||
|
||
it('returns OK', async () => { | ||
expect(equals(await redis[command]('RESETSTAT'), 'OK')).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('rewrite', () => { | ||
it('should throw on wrong number of arguments', async () => { | ||
expect.hasAssertions() | ||
|
||
try { | ||
await redis[command]('REWRITE', 'foo') | ||
} catch (err) { | ||
expect(err.message).toMatch('wrong number of arguments') | ||
} | ||
}) | ||
|
||
it('throws as redis is running without a config file', async () => { | ||
expect.hasAssertions() | ||
|
||
try { | ||
await redis[command]('REWRITE') | ||
} catch (err) { | ||
expect(err.message).toMatch( | ||
'ERR The server is running without a config file' | ||
) | ||
} | ||
}) | ||
}) | ||
|
||
describe('help', () => { | ||
it('should throw on wrong number of arguments', async () => { | ||
expect.assertions(2) | ||
|
||
try { | ||
await redis[command]('HELP', 'foo') | ||
} catch (err) { | ||
expect(err.message).toMatch('wrong number of arguments') | ||
} | ||
|
||
try { | ||
await redis[command]('HELP', 'foo', 'bar') | ||
} catch (err) { | ||
expect(err.message).toMatch('wrong number of arguments') | ||
} | ||
}) | ||
|
||
it('prints a list over available subcommands', async () => { | ||
const result = await redis[command]('HELP') | ||
|
||
expect( | ||
result.map(val => { | ||
return Buffer.isBuffer(val) ? val.toString() : val | ||
}) | ||
).toEqual([ | ||
'CONFIG <subcommand> [<arg> [value] [opt] ...]. Subcommands are:', | ||
'GET <pattern>', | ||
' Return parameters matching the glob-like <pattern> and their values.', | ||
'SET <directive> <value>', | ||
' Set the configuration <directive> to <value>.', | ||
'RESETSTAT', | ||
' Reset statistics reported by the INFO command.', | ||
'REWRITE', | ||
' Rewrite the configuration file.', | ||
'HELP', | ||
' Prints this help.', | ||
]) | ||
}) | ||
}) | ||
}) | ||
}) |