Commit 2040d70f authored by alinamihaila's avatar alinamihaila

Fix regex for key name format

parent c27be87a
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
module Gitlab module Gitlab
module Redis module Redis
KeyFormatError = Class.new(StandardError)
class HLL class HLL
KeyFormatError = Class.new(StandardError)
def self.count(params) def self.count(params)
self.new.count(params) self.new.count(params)
end end
...@@ -20,7 +20,7 @@ module Gitlab ...@@ -20,7 +20,7 @@ module Gitlab
end end
def add(key:, value:, expiry:) def add(key:, value:, expiry:)
unless %r{\A.*\{.*\}.*\z}.match?(key) unless %r{\A(\w|-|:)*\{\w*\}(\w|-|:)*\z}.match?(key)
raise KeyFormatError.new("Invalid key format. #{key} key should have changeable parts in curly braces. See https://docs.gitlab.com/ee/development/redis.html#multi-key-commands") raise KeyFormatError.new("Invalid key format. #{key} key should have changeable parts in curly braces. See https://docs.gitlab.com/ee/development/redis.html#multi-key-commands")
end end
......
...@@ -5,13 +5,15 @@ require 'spec_helper' ...@@ -5,13 +5,15 @@ require 'spec_helper'
RSpec.describe Gitlab::Redis::HLL, :clean_gitlab_redis_shared_state do RSpec.describe Gitlab::Redis::HLL, :clean_gitlab_redis_shared_state do
describe '.add' do describe '.add' do
it 'raise an error when using an invalid key format' do it 'raise an error when using an invalid key format' do
expect { described_class.add(key: 'test', value: 1, expiry: 1.day) }.to raise_error(Gitlab::Redis::KeyFormatError) expect { described_class.add(key: 'test', value: 1, expiry: 1.day) }.to raise_error(Gitlab::Redis::HLL::KeyFormatError)
expect { described_class.add(key: 'test-{metric', value: 1, expiry: 1.day) }.to raise_error(Gitlab::Redis::KeyFormatError) expect { described_class.add(key: 'test-{metric', value: 1, expiry: 1.day) }.to raise_error(Gitlab::Redis::HLL::KeyFormatError)
expect { described_class.add(key: 'test-{metric}}', value: 1, expiry: 1.day) }.to raise_error(Gitlab::Redis::HLL::KeyFormatError)
end end
it "doesn't raise error when having correct format" do it "doesn't raise error when having correct format" do
expect { described_class.add(key: 'test-{metric}', value: 1, expiry: 1.day) }.not_to raise_error expect { described_class.add(key: 'test-{metric}', value: 1, expiry: 1.day) }.not_to raise_error
expect { described_class.add(key: 'test-{metric}-1', value: 1, expiry: 1.day) }.not_to raise_error expect { described_class.add(key: 'test-{metric}-1', value: 1, expiry: 1.day) }.not_to raise_error
expect { described_class.add(key: 'test:{metric}-1', value: 1, expiry: 1.day) }.not_to raise_error
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment