diff options
author | Jason Goodman <jgoodman@gitlab.com> | 2019-08-23 22:44:15 +0300 |
---|---|---|
committer | Jason Goodman <jgoodman@gitlab.com> | 2019-08-27 23:48:34 +0300 |
commit | 77d6ef672dd786d8210d257923d995458788910a (patch) | |
tree | 9e697dafa8fb64b25f1c014ad10d1aee41f8882d | |
parent | dafc6ce7a92dfcf18a025ae05803f584bffaca09 (diff) |
Refactor to use singletonunleash-initializer
Remove logging
-rw-r--r-- | config/initializers/unleash.rb | 11 | ||||
-rw-r--r-- | lib/feature_flag_client.rb | 19 | ||||
-rw-r--r-- | lib/gitlab/unleash_client/logger.rb | 11 | ||||
-rw-r--r-- | lib/running_web_server.rb | 11 | ||||
-rw-r--r-- | spec/initializers/unleash_spec.rb | 62 | ||||
-rw-r--r-- | spec/lib/feature_flag_client_spec.rb | 54 |
6 files changed, 73 insertions, 95 deletions
diff --git a/config/initializers/unleash.rb b/config/initializers/unleash.rb deleted file mode 100644 index 4ece403a338..00000000000 --- a/config/initializers/unleash.rb +++ /dev/null @@ -1,11 +0,0 @@ -server_url = ENV['GITLAB_FEATURE_FLAG_SERVER_URL'] -instance_id = ENV['GITLAB_FEATURE_FLAG_INSTANCE_ID'] - -if RunningWebServer.unicorn? && server_url && instance_id - Unleash.configure do |config| - config.url = server_url - config.instance_id = instance_id - config.app_name = Rails.env - config.logger = Gitlab::UnleashClient::Logger.build - end -end diff --git a/lib/feature_flag_client.rb b/lib/feature_flag_client.rb new file mode 100644 index 00000000000..a87f59bfd37 --- /dev/null +++ b/lib/feature_flag_client.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class FeatureFlagClient + def self.enabled?(key) + server_url = ENV['GITLAB_FEATURE_FLAG_SERVER_URL'] + instance_id = ENV['GITLAB_FEATURE_FLAG_INSTANCE_ID'] + + if server_url && instance_id + @client ||= Unleash::Client.new( + url: server_url, + instance_id: instance_id, + app_name: Rails.env + ) + end + + # TODO: Implement call to server to check feature flag + false + end +end diff --git a/lib/gitlab/unleash_client/logger.rb b/lib/gitlab/unleash_client/logger.rb deleted file mode 100644 index 982cce20073..00000000000 --- a/lib/gitlab/unleash_client/logger.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -module Gitlab - module UnleashClient - class Logger < ::Gitlab::Logger - def self.file_name_noext - 'unleash_client' - end - end - end -end diff --git a/lib/running_web_server.rb b/lib/running_web_server.rb deleted file mode 100644 index 9de196f8210..00000000000 --- a/lib/running_web_server.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -class RunningWebServer - def self.unicorn? - !!defined?(::Unicorn) - end - - def self.puma? - !!defined?(::Puma) - end -end diff --git a/spec/initializers/unleash_spec.rb b/spec/initializers/unleash_spec.rb deleted file mode 100644 index 6be760595ea..00000000000 --- a/spec/initializers/unleash_spec.rb +++ /dev/null @@ -1,62 +0,0 @@ -require 'spec_helper' -require_relative '../../lib/running_web_server' - -describe 'Unleash initializer' do - def load_initializer - load Rails.root.join('config/initializers/unleash.rb') - end - - before do - allow(ENV).to receive(:[]).and_call_original - Unleash.configuration = nil - end - - context 'when the web server is unicorn' do - before do - allow(RunningWebServer).to receive(:unicorn?).and_return(true) - end - - it 'sets configuration based on environment variables' do - allow(ENV).to receive(:[]).with('GITLAB_FEATURE_FLAG_SERVER_URL').and_return('some server url') - allow(ENV).to receive(:[]).with('GITLAB_FEATURE_FLAG_INSTANCE_ID').and_return('some instance id') - - load_initializer - - expect(Unleash.configuration.url).to eq('some server url') - expect(Unleash.configuration.instance_id).to eq('some instance id') - expect(Unleash.configuration.app_name).to eq(Rails.env) - expect(Unleash.configuration.logger).to be_an_instance_of(Gitlab::UnleashClient::Logger) - end - - it 'does not set the configuration without a server url' do - allow(ENV).to receive(:[]).with('GITLAB_FEATURE_FLAG_INSTANCE_ID').and_return('some instance id') - - load_initializer - - expect(Unleash.configuration).to be_nil - end - - it 'does not set the configuration without an instance id' do - allow(ENV).to receive(:[]).with('GITLAB_FEATURE_FLAG_SERVER_URL').and_return('some server url') - - load_initializer - - expect(Unleash.configuration).to be_nil - end - end - - context 'when the web server is not unicorn' do - before do - allow(RunningWebServer).to receive(:unicorn?).and_return(false) - end - - it 'does not load the config' do - allow(ENV).to receive(:[]).with('GITLAB_FEATURE_FLAG_SERVER_URL').and_return('some server url') - allow(ENV).to receive(:[]).with('GITLAB_FEATURE_FLAG_INSTANCE_ID').and_return('some instance id') - - load_initializer - - expect(Unleash.configuration).to be_nil - end - end -end diff --git a/spec/lib/feature_flag_client_spec.rb b/spec/lib/feature_flag_client_spec.rb new file mode 100644 index 00000000000..5962ab5a985 --- /dev/null +++ b/spec/lib/feature_flag_client_spec.rb @@ -0,0 +1,54 @@ +require 'spec_helper' + +describe FeatureFlagClient do + before do + allow(ENV).to receive(:[]).and_call_original + allow(Unleash::Client).to receive(:new).and_return("fake client instance") + described_class.instance_variable_set(:@client, nil) + end + + describe '.enabled?' do + it 'returns false' do + expect(described_class.enabled?(:feature_flag)).to eq(false) + end + + it 'sets configuration based on environment variables' do + allow(ENV).to receive(:[]).with('GITLAB_FEATURE_FLAG_SERVER_URL').and_return('some server url') + allow(ENV).to receive(:[]).with('GITLAB_FEATURE_FLAG_INSTANCE_ID').and_return('some instance id') + expect(Unleash::Client).to receive(:new).with( + url: 'some server url', + instance_id: 'some instance id', + app_name: Rails.env + ) + + described_class.enabled?(:my_feature) + end + + it 'is a singleton' do + allow(ENV).to receive(:[]).with('GITLAB_FEATURE_FLAG_SERVER_URL').and_return('some server url') + allow(ENV).to receive(:[]).with('GITLAB_FEATURE_FLAG_INSTANCE_ID').and_return('some instance id') + expect(Unleash::Client).to receive(:new).with( + url: 'some server url', + instance_id: 'some instance id', + app_name: Rails.env + ).once + + described_class.enabled?(:my_feature) + described_class.enabled?(:my_feature) + end + + it 'does not set the configuration without a server url' do + allow(ENV).to receive(:[]).with('GITLAB_FEATURE_FLAG_INSTANCE_ID').and_return('some instance id') + expect(Unleash::Client).not_to receive(:new) + + described_class.enabled?(:my_feature) + end + + it 'does not set the configuration without an instance id' do + allow(ENV).to receive(:[]).with('GITLAB_FEATURE_FLAG_SERVER_URL').and_return('some server url') + expect(Unleash::Client).not_to receive(:new) + + described_class.enabled?(:my_feature) + end + end +end |