From 57451f52cdbd527a980c0df75e1ee8bb7897d2e9 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 9 Aug 2016 11:29:32 +0200 Subject: Memoize CI config node validator to prevent leaks --- lib/gitlab/ci/config/node/validatable.rb | 10 ++++------ spec/lib/gitlab/ci/config/node/validatable_spec.rb | 4 ++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/gitlab/ci/config/node/validatable.rb b/lib/gitlab/ci/config/node/validatable.rb index f6e2896dfb2..085e6e988d1 100644 --- a/lib/gitlab/ci/config/node/validatable.rb +++ b/lib/gitlab/ci/config/node/validatable.rb @@ -7,13 +7,11 @@ module Gitlab class_methods do def validator - validator = Class.new(Node::Validator) - - if defined?(@validations) - @validations.each { |rules| validator.class_eval(&rules) } + @validator ||= Class.new(Node::Validator).tap do |validator| + if defined?(@validations) + @validations.each { |rules| validator.class_eval(&rules) } + end end - - validator end private diff --git a/spec/lib/gitlab/ci/config/node/validatable_spec.rb b/spec/lib/gitlab/ci/config/node/validatable_spec.rb index 10cd01afcd1..64b77fd6e03 100644 --- a/spec/lib/gitlab/ci/config/node/validatable_spec.rb +++ b/spec/lib/gitlab/ci/config/node/validatable_spec.rb @@ -23,6 +23,10 @@ describe Gitlab::Ci::Config::Node::Validatable do .to be Gitlab::Ci::Config::Node::Validator end + it 'returns only one validator to mitigate leaks' do + expect { node.validator }.not_to change { node.validator } + end + context 'when validating node instance' do let(:node_instance) { node.new } -- cgit v1.2.1