From 9e59c5ac19851464e8561180eb2a1e5be69676e4 Mon Sep 17 00:00:00 2001 From: uu59 Date: Thu, 15 May 2014 16:44:34 +0900 Subject: [PATCH] Add all methods of Plugin model specs --- Gemfile | 1 + Gemfile.lock | 8 +++ app/models/plugin.rb | 8 +-- spec/models/plugin_spec.rb | 100 +++++++++++++++++++++++++++++++++++-- spec/spec_helper.rb | 1 + 5 files changed, 107 insertions(+), 11 deletions(-) diff --git a/Gemfile b/Gemfile index 6105d88..bac613b 100644 --- a/Gemfile +++ b/Gemfile @@ -12,6 +12,7 @@ group :development, :test do gem "database_cleaner", "~> 1.2.0" gem "capybara", "~> 2.2.1" gem "simplecov", "~> 0.7.1", require: false + gem "webmock", "~> 1.18.0" end eval File.read "Gemfile.plugins" if File.exist?("Gemfile.plugins") diff --git a/Gemfile.lock b/Gemfile.lock index 0d0e7ca..9691d5c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -45,6 +45,7 @@ GEM minitest (~> 5.1) thread_safe (~> 0.1) tzinfo (~> 1.1) + addressable (2.3.6) arel (5.0.1.20140414130214) bcrypt (3.1.7) builder (3.2.2) @@ -63,6 +64,8 @@ GEM execjs coffee-script-source (1.7.0) cool.io (1.2.4) + crack (0.4.2) + safe_yaml (~> 1.0.0) database_cleaner (1.2.0) diff-lcs (1.2.5) domain_name (0.5.18) @@ -163,6 +166,7 @@ GEM rspec-core (~> 2.14.0) rspec-expectations (~> 2.14.0) rspec-mocks (~> 2.14.0) + safe_yaml (1.0.3) sass (3.2.19) sass-rails (4.0.3) railties (>= 4.0.0, < 5.0) @@ -199,6 +203,9 @@ GEM unf (0.1.4) unf_ext unf_ext (0.0.6) + webmock (1.18.0) + addressable (>= 2.3.6) + crack (>= 0.3.2) webrobots (0.1.1) xpath (2.0.0) nokogiri (~> 1.3) @@ -216,3 +223,4 @@ DEPENDENCIES rake rspec-rails (~> 2.0) simplecov (~> 0.7.1) + webmock (~> 1.18.0) diff --git a/app/models/plugin.rb b/app/models/plugin.rb index c100803..7101539 100644 --- a/app/models/plugin.rb +++ b/app/models/plugin.rb @@ -93,11 +93,7 @@ class Plugin end def self.gemfile_path - if Rails.env == "test" - gemfile_path = "/tmp/fluentd-ui-test-Gemfile.plugins" # can't create a file under Rails.root directory on Circle CI - else - gemfile_path = Rails.root + "Gemfile.plugins" - end + Rails.root + "Gemfile.plugins" end def self.pristine! @@ -118,7 +114,7 @@ class Plugin def fluent_gem(*commands) unless system(*%W(bundle exec fluent-gem) + commands) # TODO: should grab stdout/stderr - raise GemError, "failed command #{commands}" + raise GemError, "failed command #{commands.join(" ")}" end true end diff --git a/spec/models/plugin_spec.rb b/spec/models/plugin_spec.rb index 6b7f3ed..e16dd3b 100644 --- a/spec/models/plugin_spec.rb +++ b/spec/models/plugin_spec.rb @@ -2,9 +2,11 @@ require 'spec_helper' describe Plugin do let(:plugin) { FactoryGirl.build(:plugin) } + before do - Kernel.stub(:system) # do not call `system('fluent-gem install ..')` on CI + Plugin.stub(:gemfile_path).and_return { "/tmp/fluentd-ui-test-gemfile.plugins" } # NOTE: can't create a file under Rails.root directory on Circle CI end + after do File.unlink Plugin.gemfile_path if File.exist?(Plugin.gemfile_path) Plugin.pristine! @@ -42,6 +44,10 @@ describe Plugin do end end + describe "#format_gemfile" do + it { plugin.format_gemfile.should == %Q|gem "#{plugin.gem_name}", "#{plugin.version}"| } + end + describe "#install!" do describe "invoke fluent_gem" do after do @@ -79,6 +85,19 @@ describe Plugin do end end + context "system command error" do + before { plugin.should_receive(:system).and_return { false } } + subject { expect { plugin.install! } } + + it "raise GemError" do + subject.to raise_error(Plugin::GemError) + end + + it "error message contains gem name" do + subject.to raise_error(/#{plugin.gem_name}/) + end + end + describe "after install succeed" do before do plugin.stub(:fluent_gem).and_return { true } @@ -90,7 +109,7 @@ describe Plugin do end end - describe "uninstall!" do + describe "#uninstall!" do let(:installed_plugin) { FactoryGirl.build(:plugin, gem_name: "fluent-plugin-foobar") } before do @@ -103,12 +122,83 @@ describe Plugin do installed_plugin.uninstall! end - it do - installed_plugin.should_not be_installed + it { installed_plugin.should_not be_installed } + it { Plugin.should be_gemfile_changed } + end + + describe "#upgrade!" do + let(:installed_plugin) { FactoryGirl.build(:plugin, gem_name: "fluent-plugin-foobar", version: current_version) } + let(:current_version) { "1.0.0" } + let(:target_version) { "1.2.0" } + + before do + Plugin.any_instance.stub(:fluent_gem).and_return { true } # NOTE: not `plugin.stub` because upgrade! creates new Plugin instance internally + installed_plugin.install! + Plugin.pristine! + installed_plugin.upgrade!(target_version) + end + + it { installed_plugin.should be_installed } + it { Plugin.should be_gemfile_changed } + it { installed_plugin.installed_version.should == target_version } + end + + describe ".installed" do + before do + plugin.stub(:fluent_gem).and_return { true } + plugin.install! end it do - Plugin.should be_gemfile_changed + Plugin.installed.map(&:format_gemfile).should =~ [plugin].map(&:format_gemfile) end end + + describe "#latest_version?" do + let(:plugin) { FactoryGirl.build(:plugin, version: gem_version.to_s) } + let(:gem_version) { Gem::Version.new("1.0.0") } + + before do + plugin.stub(:installed_version).and_return { gem_version.to_s } + stub_request(:get, /rubygems.org/).to_return(body: JSON.dump(api_response)) + end + + subject { plugin.latest_version? } + + context "available updates" do + let(:api_response) do + [{number: gem_version.bump}, {number: gem_version}] + end + + it { subject.should be_false } + end + + context "unavailable updates" do + let(:api_response) do + [{number: gem_version}] + end + + it { subject.should be_true } + end + end + + describe "#installed_version" do + before do + Plugin.any_instance.stub(:fluent_gem).and_return { true } # NOTE: not `plugin.stub` because upgrade! creates new Plugin instance internally + plugin.install! + end + + it { plugin.installed_version.should == plugin.version } + + context "upgrade to x.y.z" do + before { plugin.upgrade!(target_version) } + let(:target_version) { "3.3.3" } + + it { plugin.installed_version.should == target_version } + end + end + + describe "#to_param" do + it { plugin.to_param.should == plugin.gem_name } + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 618932f..7191752 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,6 +13,7 @@ ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' +require 'webmock/rspec' # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories.