Project

General

Profile

Download (6.14 KB) Statistics
| Branch: | Tag: | Revision:

runcible / test / test_runner.rb @ master

1 c22b9740 David Davis
if RUBY_VERSION > "2.2"
2
  # Coverage - Keep these two lines at the top of this file
3
  require 'simplecov'
4
  require 'coveralls'
5
6
  SimpleCov.formatters = [SimpleCov::Formatter::HTMLFormatter,
7
                          Coveralls::SimpleCov::Formatter]
8
  SimpleCov.start do
9
    minimum_coverage 90
10
    refuse_coverage_drop
11
    track_files "lib/**/*.rb"
12
    add_filter '/test/'
13
  end
14
end
15
16 8db5edf8 Eric D Helms
require 'rubygems'
17 b1d71310 Eric D. Helms
require 'logger'
18 8db5edf8 Eric D Helms
require 'minitest/unit'
19
require 'minitest/autorun'
20 926b9789 Justin Sherrill
require 'mocha/setup'
21 8db5edf8 Eric D Helms
22 4f2ec2b8 Eric D. Helms
require './test/vcr_setup'
23 661ca339 Justin Sherrill
require './lib/runcible'
24
25 7de64685 David Davis
begin
26
  require 'debugger'
27
rescue LoadError
28 3e3ad61c Partha Aji
  puts 'Debugging not enabled.'
29 7de64685 David Davis
end
30 8db5edf8 Eric D Helms
31 661ca339 Justin Sherrill
class TestRuncible
32
  def self.server=(instance)
33
    @@instance = instance
34
  end
35
36
  def self.server
37
    @@instance
38
  end
39
end
40
41 f8bc785b Partha Aji
module MiniTest
42
  class Unit
43
    class TestCase
44
      def cassette_name
45
        test_name = self.__name__.gsub('test_', '')
46
        parent = (self.class.name.split('::')[-2] || '').underscore
47
        self_class = self.class.name.split('::')[-1].underscore.gsub('test_', '')
48
        "#{parent}/#{self_class}/#{test_name}"
49
      end
50 0e737b69 David Davis
51 f8bc785b Partha Aji
      def run_with_vcr(args)
52 926b9789 Justin Sherrill
        options = self.class.respond_to?(:cassette_options) ? self.class.cassette_options : {}
53
        VCR.insert_cassette(cassette_name, options)
54 f8bc785b Partha Aji
        to_ret = run_without_vcr(args)
55
        VCR.eject_cassette
56
        to_ret
57
      end
58 ae35dcd9 Justin Sherrill
59 f8bc785b Partha Aji
      alias_method_chain :run, :vcr
60 54addc4c Justin Sherrill
61 f8bc785b Partha Aji
      class << self
62
        attr_accessor :support
63 ae35dcd9 Justin Sherrill
64 f8bc785b Partha Aji
        def suite_cassette_name
65
          parent = (self.name.split('::')[-2] || '').underscore
66
          self_class = self.name.split('::')[-1].underscore.gsub('test_', '')
67
          "#{parent}/#{self_class}/suite"
68
        end
69
      end
70 54addc4c Justin Sherrill
71 f8bc785b Partha Aji
      def assert_async_response(response)
72
        support = @support || self.class.support
73 ddfc4136 Partha Aji
        fail '@support or @@support not defined' unless support
74 54addc4c Justin Sherrill
75 ddfc4136 Partha Aji
        if response.key? "group_id"
76
          assert_async_task_groups(response, support)
77
        else
78
          assert_async_tasks(response, support)
79
        end
80
      end
81
82
      def assert_async_tasks(response, support)
83 f8bc785b Partha Aji
        assert_equal 202, response.code
84
        tasks = support.wait_on_response(response)
85
        tasks.each do |task|
86
          assert task['state'], 'finished'
87
        end
88
      end
89 ddfc4136 Partha Aji
90
      def assert_async_task_groups(response, support)
91
        assert_equal 202, response.code
92
        summary = support.wait_on_task_group(response)
93
        assert_equal summary["total"], summary["finished"]
94
        summary
95
      end
96 54addc4c Justin Sherrill
    end
97
  end
98 0e737b69 David Davis
end
99 661ca339 Justin Sherrill
100 24595c26 Eric D. Helms
class CustomMiniTestRunner
101 8db5edf8 Eric D Helms
  class Unit < MiniTest::Unit
102
    def before_suites
103
      # code to run before the first test
104
    end
105
106
    def after_suites
107
      # code to run after the last test
108
    end
109
110
    def _run_suites(suites, type)
111 3e3ad61c Partha Aji
      if ENV['suite']
112
        suites = suites.select do |suite|
113 f8bc785b Partha Aji
          suite.name == ENV['suite']
114
        end
115 8db5edf8 Eric D Helms
      end
116 3e3ad61c Partha Aji
      before_suites
117
      super(suites, type)
118
    ensure
119
      after_suites
120 8db5edf8 Eric D Helms
    end
121
122
    def _run_suite(suite, type)
123 926b9789 Justin Sherrill
      options = suite.respond_to?(:cassette_options) ? suite.cassette_options : {}
124 3e3ad61c Partha Aji
      if logging?
125
        puts "Running Suite #{suite.inspect} - #{type.inspect} "
126
      end
127
      if suite.respond_to?(:before_suite)
128 926b9789 Justin Sherrill
        VCR.use_cassette(suite.suite_cassette_name, options) do
129 f8bc785b Partha Aji
          suite.before_suite
130
        end
131
      end
132 3e3ad61c Partha Aji
      super(suite, type)
133
    ensure
134
      if suite.respond_to?(:after_suite)
135 926b9789 Justin Sherrill
        VCR.use_cassette(suite.suite_cassette_name, options) do
136 3e3ad61c Partha Aji
          suite.after_suite
137 5354f21d Partha Aji
        end
138 8db5edf8 Eric D Helms
      end
139 3e3ad61c Partha Aji
      if logging?
140
        puts "Completed Running Suite #{suite.inspect} - #{type.inspect} "
141 f8bc785b Partha Aji
      end
142 8db5edf8 Eric D Helms
    end
143
144 375c3469 Partha Aji
    def logging?
145
      ENV['logging']
146
    end
147 8db5edf8 Eric D Helms
  end
148
end
149
150 24595c26 Eric D. Helms
class PulpMiniTestRunner
151 3e3ad61c Partha Aji
  def run_tests(suite, options = {})
152
    mode      = options[:mode] || 'none'
153 24595c26 Eric D. Helms
    test_name = options[:test_name] || nil
154 3e3ad61c Partha Aji
    auth_type = options[:auth_type] || 'http'
155 c3ba2452 Eric D. Helms
    logging   = options[:logging] || false
156 24595c26 Eric D. Helms
157
    MiniTest::Unit.runner = CustomMiniTestRunner::Unit.new
158
159 3e3ad61c Partha Aji
    if mode == 'all'
160 f8bc785b Partha Aji
      runcible_config(:auth_type => auth_type, :logging => logging)
161 c3ba2452 Eric D. Helms
    else
162 f8bc785b Partha Aji
      runcible_config(:logging => logging)
163 c3ba2452 Eric D. Helms
    end
164
165 f8bc785b Partha Aji
    vcr_config(mode)
166 24595c26 Eric D. Helms
167 3e3ad61c Partha Aji
    if test_name && File.exist?(test_name)
168 b4a37745 Justin Sherrill
      require test_name
169
    elsif test_name
170 4f2ec2b8 Eric D. Helms
      require "./test/#{test_name}_test.rb"
171 24595c26 Eric D. Helms
    else
172 3e3ad61c Partha Aji
      Dir["./test/#{suite}/*_test.rb"].each { |file| require file }
173 24595c26 Eric D. Helms
    end
174
  end
175
176 f8bc785b Partha Aji
  def runcible_config(options)
177
    config = { :api_path   => '/pulp/api/v2/',
178
               :http_auth  => {}}
179 c3ba2452 Eric D. Helms
180 3e3ad61c Partha Aji
    if options[:logging] == 'true'
181 b1d71310 Eric D. Helms
      log = ::Logger.new(STDOUT)
182
      log.level = Logger::DEBUG
183 f8bc785b Partha Aji
      config[:logging] = { :logger => log,
184
                           :debug  => true,
185
                           :stdout => true}
186 c3ba2452 Eric D. Helms
    end
187
188 3e3ad61c Partha Aji
    if options[:auth_type] == 'http'
189 24595c26 Eric D. Helms
190
      File.open('/etc/pulp/server.conf') do |f|
191
        f.each_line do |line|
192
          if line.start_with?('default_password')
193 661ca339 Justin Sherrill
            config[:http_auth][:password] = line.split(':')[1].strip
194 24595c26 Eric D. Helms
          elsif line.start_with?('default_login')
195 661ca339 Justin Sherrill
            config[:user] = line.split(':')[1].strip
196 24595c26 Eric D. Helms
          elsif line.start_with?('server_name')
197 3e3ad61c Partha Aji
            config[:url] = "https://#{line.split(':')[1].chomp.strip}"
198 24595c26 Eric D. Helms
          end
199
        end
200
      end
201 3e3ad61c Partha Aji
    elsif options[:auth_type] == 'oauth'
202 24595c26 Eric D. Helms
203
      File.open('/etc/pulp/server.conf') do |f|
204
        f.each_line do |line|
205
          if line.start_with?('oauth_secret')
206 661ca339 Justin Sherrill
            config[:oauth][:oauth_secret] = line.split(':')[1].strip
207 24595c26 Eric D. Helms
          elsif line.start_with?('oauth_key')
208 3e3ad61c Partha Aji
            config[:oauth][:oauth_key] = line.split(':')[1].strip
209 4d21ed02 Eric D. Helms
          elsif line.start_with?('default_login')
210 3e3ad61c Partha Aji
            config[:user] = line.split(':')[1].strip
211 24595c26 Eric D. Helms
          elsif line.start_with?('server_name')
212 3e3ad61c Partha Aji
            config[:url] = "https://#{line.split(':')[1].chomp.strip}"
213 24595c26 Eric D. Helms
          end
214
        end
215
      end
216 c3ba2452 Eric D. Helms
    else
217 661ca339 Justin Sherrill
      config[:http_auth][:password] = 'admin'
218
      config[:user]  = 'admin'
219 3e3ad61c Partha Aji
      config[:url]   = 'https://localhost'
220 24595c26 Eric D. Helms
    end
221 926b9789 Justin Sherrill
    config[:verify_ssl] = false
222 661ca339 Justin Sherrill
    TestRuncible.server = Runcible::Instance.new(config)
223 24595c26 Eric D. Helms
  end
224
225 f8bc785b Partha Aji
  def vcr_config(mode)
226 3e3ad61c Partha Aji
    if mode == 'all'
227 24595c26 Eric D. Helms
      configure_vcr(:all)
228 3e3ad61c Partha Aji
    elsif mode == 'new_episodes'
229 75cf119c Eric D. Helms
      configure_vcr(:new_episodes)
230 3e3ad61c Partha Aji
    elsif mode == 'once'
231 db369a17 Eric D. Helms
      configure_vcr(:once)
232 24595c26 Eric D. Helms
    else
233 75cf119c Eric D. Helms
      configure_vcr(:none)
234 24595c26 Eric D. Helms
    end
235
  end
236
end