summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Goodman <jgoodman@gitlab.com>2019-08-22 16:33:46 -0400
committerJason Goodman <jgoodman@gitlab.com>2019-08-27 16:48:34 -0400
commitdafc6ce7a92dfcf18a025ae05803f584bffaca09 (patch)
treee3264f608205ad62f3365aa6c893b1a43795d8ce
parenta58f4f00cf7cc8b4755d0d20094cbbd547d31d2b (diff)
downloadgitlab-ce-dafc6ce7a92dfcf18a025ae05803f584bffaca09.tar.gz
Add initializer for unleash client
Add logger for unleash client
-rw-r--r--config/initializers/unleash.rb11
-rw-r--r--lib/gitlab/unleash_client/logger.rb11
-rw-r--r--lib/running_web_server.rb11
-rw-r--r--spec/initializers/unleash_spec.rb62
4 files changed, 95 insertions, 0 deletions
diff --git a/config/initializers/unleash.rb b/config/initializers/unleash.rb
new file mode 100644
index 00000000000..4ece403a338
--- /dev/null
+++ b/config/initializers/unleash.rb
@@ -0,0 +1,11 @@
+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/gitlab/unleash_client/logger.rb b/lib/gitlab/unleash_client/logger.rb
new file mode 100644
index 00000000000..982cce20073
--- /dev/null
+++ b/lib/gitlab/unleash_client/logger.rb
@@ -0,0 +1,11 @@
+# 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
new file mode 100644
index 00000000000..9de196f8210
--- /dev/null
+++ b/lib/running_web_server.rb
@@ -0,0 +1,11 @@
+# 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
new file mode 100644
index 00000000000..6be760595ea
--- /dev/null
+++ b/spec/initializers/unleash_spec.rb
@@ -0,0 +1,62 @@
+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