Skip to content

Commit

Permalink
feat: add config command (#1136)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan authored Jan 31, 2022
1 parent aa8c3b0 commit 258d9da
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compat.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
| [client] | :white_check_mark: | :x: |
| [cluster] | :white_check_mark: | :white_check_mark: |
| [command] | :white_check_mark: | :x: |
| [config] | :white_check_mark: | :x: |
| [config] | :white_check_mark: | :white_check_mark: |
| [copy] | :white_check_mark: | :x: |
| [dbsize] | :white_check_mark: | :white_check_mark: |
| [decr] | :white_check_mark: | :white_check_mark: |
Expand Down
51 changes: 51 additions & 0 deletions src/commands/config.js
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
}
1 change: 1 addition & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './bgrewriteaof'
export * from './bgsave'
export * from './brpoplpush'
export * from './cluster'
export * from './config'
export * from './connect'
export * from './dbsize'
export * from './decr'
Expand Down
160 changes: 160 additions & 0 deletions test/integration/commands/config.js
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.',
])
})
})
})
})

0 comments on commit 258d9da

Please sign in to comment.