Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 5df3550ac5
Fetching contributors…

Cannot retrieve contributors at this time

file 147 lines (131 sloc) 6.764 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
#--
# Copyright: Copyright (c) 2010-2011 RightScale, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# 'Software'), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++

require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
require File.expand_path(File.join(File.dirname(__FILE__), 'download', 'download_retriever_spec_helper'))
require 'tmpdir'
require 'flexmock'

describe RightScraper::Scraper do
  include RightScraper::SpecHelpers::DevelopmentModeEnvironment

  include RightScraper::SharedExamples

  shared_examples_for 'scrapes to given base dir' do
    before(:each) do
      @scraper = RightScraper::Scraper.new(:basedir => @tmpdir, :kind => :cookbook)
    end

    after(:each) do
      FileUtils.remove_entry_secure @tmpdir unless @tmpdir.nil?
    end

    it 'starts out successful' do
      @scraper.succeeded?.should be_true
      @scraper.errors.should == []
    end

    context 'given a legal download repository' do
      before(:each) do
        @helper = RightScraper::DownloadRetrieverSpecHelper.new
        @repo = @helper.repo
      end

      after(:each) do
        @helper.close
      end

      it_should_behave_like "a normal repository"

      it 'should log correctly as it scrapes' do
        callback = flexmock("callback")
        callback.should_receive(:call).with(:begin, :retrieving, "from #{@repo}", nil).once.ordered
        callback.should_receive(:call).with(:begin, :initialize, String, nil).once.ordered
        callback.should_receive(:call).with(:commit, :initialize, String, nil).once.ordered
        callback.should_receive(:call).with(:begin, :downloading, "", nil).once.ordered
        callback.should_receive(:call).with(:begin, :running_command, String, nil).once.ordered
        callback.should_receive(:call).with(:commit, :running_command, String, nil).once.ordered
        callback.should_receive(:call).with(:commit, :downloading, "", nil).once.ordered
        callback.should_receive(:call).with(:begin, :unpacking, "", nil).once.ordered
        callback.should_receive(:call).with(:begin, :running_command, String, nil).once.ordered
        callback.should_receive(:call).with(:commit, :running_command, String, nil).once.ordered
        callback.should_receive(:call).with(:commit, :unpacking, "", nil).once.ordered
        callback.should_receive(:call).with(:commit, :retrieving, "from #{@repo}", nil).once.ordered
        callback.should_receive(:call).with(:begin, :scraping, String, nil).once.ordered
        callback.should_receive(:call).with(:begin, :finding_next_cookbook, String, nil).once.ordered
        callback.should_receive(:call).with(:begin, :reading_cookbook, String, nil).once.ordered
        callback.should_receive(:call).with(:begin, :scanning_filesystem, String, nil).once.ordered
        callback.should_receive(:call).with(:begin, :metadata_parsing, "", nil).once.ordered
        callback.should_receive(:call).with(:commit, :metadata_parsing, "", nil).once.ordered
        callback.should_receive(:call).with(:commit, :scanning_filesystem, String, nil).once.ordered
        callback.should_receive(:call).with(:commit, :reading_cookbook, String, nil).once.ordered
        callback.should_receive(:call).with(:commit, :finding_next_cookbook, String, nil).once.ordered
        callback.should_receive(:call).with(:begin, :next, "", nil).once.ordered
        callback.should_receive(:call).with(:begin, :searching, "", nil).once.ordered
        callback.should_receive(:call).with(:commit, :searching, "", nil).once.ordered
        callback.should_receive(:call).with(:commit, :next, "", nil).once.ordered
        callback.should_receive(:call).with(:begin, :next, "", nil).once.ordered
        callback.should_receive(:call).with(:commit, :next, "", nil).once.ordered
        callback.should_receive(:call).with(:commit, :scraping, String, nil).once.ordered
        @scraper.scrape(@repo) do |phase, operation, explanation, exception|
          callback.call(phase, operation, explanation, exception)
        end
        @scraper.errors.should == []
        @scraper.succeeded?.should be_true
        @scraper.resources.size.should == 1
      end
    end

    context 'given several repositories' do
      it 'should continue to scrape even if errors occur' do
        GC.start
        repo = RightScraper::Repositories::Base.from_hash(:display_name => 'illegal repo',
                                                          :repo_type => :download,
                                                          :url => "http://example.com/foo")
        @scraper.scrape(repo)
        helpers = [RightScraper::DownloadRetrieverSpecHelper,
                   RightScraper::DownloadRetrieverSpecHelper,
                   RightScraper::DownloadRetrieverSpecHelper]
        helpers.each do |klass|
          helper = klass.new
          @scraper.scrape(helper.repo)
          helper.close
        end
        @scraper.succeeded?.should be_false
        @scraper.resources.size.should == 3
        @scraper.errors.size.should == 1
      end
    end

    it 'catches normal logging behavior' do
      logger = @scraper.instance_variable_get(:@logger)
      logger.should_not be_nil
      logger.info("foo")
      logger.error("foo")
      @scraper.succeeded?.should be_false
      @scraper.errors.should == [[nil, :log, {:severity => Logger::ERROR,
                                       :message => "foo",
                                       :progname => nil}]]
    end
  end

  context 'when base dir is not provided' do
    before(:each) do
      @tmpdir = nil
    end

    it_should_behave_like 'scrapes to given base dir'
  end

  context 'when base dir is provided' do
    before(:each) do
      @tmpdir = Dir.mktmpdir
    end

    it_should_behave_like 'scrapes to given base dir'
  end
end
Something went wrong with that request. Please try again.