-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from jcagarcia/rakes
feat(#4): Adding rake tasks for activating, deactivating and listing feature flags
- Loading branch information
Showing
8 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
namespace :rollout do | ||
desc "Activate a feature" | ||
task :on, [:feature, :percentage] => :environment do |task, args| | ||
if args.feature | ||
puts "Activating feature #{args.feature}..." | ||
if args.percentage | ||
activated = rollout.activate(args.feature, args.percentage.to_i) | ||
else | ||
activated = rollout.activate(args.feature) | ||
end | ||
|
||
if activated | ||
puts "Feature flag #{args.feature} has been activated! :)" | ||
else | ||
puts "Feature flag #{args.feature} has NOT been activated! :(" | ||
end | ||
end | ||
end | ||
|
||
desc "Deactivate a feature" | ||
task :off, [:feature] => :environment do |task, args| | ||
if args.feature | ||
puts "Deactivating feature #{args.feature}..." | ||
deactivated = rollout.deactivate(args.feature) | ||
if deactivated | ||
puts "Feature flag #{args.feature} has been deactivated! :)" | ||
else | ||
puts "Feature flag #{args.feature} has NOT been deactivated! :(" | ||
end | ||
end | ||
end | ||
|
||
desc "List features" | ||
task list: :environment do | ||
features = rollout.features | ||
puts "This is the list of all the available features:" | ||
puts "" | ||
if !features.empty? | ||
puts features | ||
else | ||
puts "- No feature flags stored" | ||
end | ||
end | ||
|
||
private | ||
|
||
def rollout | ||
@rollout ||= Rollout.new(storage) | ||
end | ||
|
||
def storage | ||
begin | ||
@storage ||= Redis.new( | ||
host: ENV.fetch('ROLLOUT_REDIS_HOST'), | ||
port: ENV.fetch('ROLLOUT_REDIS_PORT') | ||
) | ||
rescue KeyError => e | ||
puts "ROLLOUT_REDIS_HOST and ROLLOUT_REDIS_PORT are mandatory env variables to define in order to run rollout rake tasks" | ||
raise e | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class Rollout | ||
VERSION = '0.2.0' | ||
VERSION = '0.3.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require 'mock_redis' | ||
require 'rake' | ||
require 'rollout' | ||
|
||
RSpec.configure do |config| | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe "Rake tasks" do | ||
let(:storage) { MockRedis.new } | ||
let(:instance) { Rollout.new(storage) } | ||
let(:redis_host) { 'localhost' } | ||
let(:redis_port) { '6379' } | ||
let!(:original_stdout) { $stdout } | ||
let(:rake_output) { StringIO.new } | ||
|
||
before(:each) do | ||
$stdout = rake_output | ||
allow(Redis).to receive(:new).with(host: redis_host, port: redis_port).and_return(storage) | ||
ENV["ROLLOUT_REDIS_HOST"] = redis_host | ||
ENV["ROLLOUT_REDIS_PORT"] = redis_port | ||
end | ||
|
||
after(:each) do | ||
$stdout = original_stdout | ||
end | ||
|
||
it "performs all the operations" do | ||
Rake.application.rake_require "rollout/tasks/rollout" | ||
Rake::Task.define_task(:environment) | ||
|
||
Rake::Task["rollout:on"].reenable | ||
Rake.application.invoke_task "rollout:on[a-feature-flag]" | ||
Rake::Task["rollout:on"].reenable | ||
Rake.application.invoke_task "rollout:on[another-feature-flag,20]" | ||
Rake::Task["rollout:on"].reenable | ||
Rake.application.invoke_task "rollout:on[super-feature-flag,50]" | ||
|
||
expect(instance.active?('a-feature-flag')).to be true | ||
expect(instance.active?('another-feature-flag')).to be false | ||
expect(instance.active?('super-feature-flag')).to be false | ||
|
||
Rake::Task["rollout:list"].reenable | ||
Rake.application.invoke_task "rollout:list" | ||
|
||
Rake::Task["rollout:off"].reenable | ||
Rake.application.invoke_task "rollout:off[a-feature-flag]" | ||
Rake::Task["rollout:off"].reenable | ||
Rake.application.invoke_task "rollout:off[another-feature-flag]" | ||
Rake::Task["rollout:off"].reenable | ||
Rake.application.invoke_task "rollout:off[super-feature-flag]" | ||
|
||
expect(instance.active?('a-feature-flag')).to be false | ||
expect(instance.active?('another-feature-flag')).to be false | ||
expect(instance.active?('super-feature-flag')).to be false | ||
|
||
Rake::Task["rollout:list"].reenable | ||
Rake.application.invoke_task "rollout:list" | ||
|
||
output = rake_output.string | ||
expect(output).to include('Activating feature a-feature-flag...') | ||
expect(output).to include('Feature flag a-feature-flag has been activated! :)') | ||
expect(output).to include('Activating feature another-feature-flag...') | ||
expect(output).to include('Feature flag another-feature-flag has been activated! :)') | ||
expect(output).to include('Activating feature super-feature-flag...') | ||
expect(output).to include('Feature flag super-feature-flag has been activated! :)') | ||
expect(output).to include('This is the list of all the available features:') | ||
expect(output).to include('{:name=>"a-feature-flag", :percentage=>100, :data=>{:requests=>0, :errors=>0}}') | ||
expect(output).to include('{:name=>"another-feature-flag", :percentage=>20, :data=>{:requests=>0, :errors=>0}}') | ||
expect(output).to include('{:name=>"super-feature-flag", :percentage=>50, :data=>{:requests=>0, :errors=>0}}') | ||
expect(output).to include('Deactivating feature a-feature-flag...') | ||
expect(output).to include('Feature flag a-feature-flag has been deactivated! :)') | ||
expect(output).to include('Deactivating feature another-feature-flag...') | ||
expect(output).to include('Feature flag another-feature-flag has been deactivated! :)') | ||
expect(output).to include('Deactivating feature super-feature-flag...') | ||
expect(output).to include('Feature flag super-feature-flag has been deactivated! :)') | ||
expect(output).to include('This is the list of all the available features:') | ||
expect(output).to include('- No feature flags stored') | ||
end | ||
end |