Project

General

Profile

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

runcible / test / test_runner.rb @ c022bb6f

1
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
require 'rubygems'
17
require 'logger'
18
require 'minitest/unit'
19
require 'minitest/autorun'
20
require 'mocha/setup'
21

    
22
require './test/vcr_setup'
23
require './lib/runcible'
24

    
25
begin
26
  require 'debugger'
27
rescue LoadError
28
  puts 'Debugging not enabled.'
29
end
30

    
31
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
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

    
51
      def run_with_vcr(args)
52
        options = self.class.respond_to?(:cassette_options) ? self.class.cassette_options : {}
53
        VCR.insert_cassette(cassette_name, options)
54
        to_ret = run_without_vcr(args)
55
        VCR.eject_cassette
56
        to_ret
57
      end
58

    
59
      alias_method_chain :run, :vcr
60

    
61
      class << self
62
        attr_accessor :support
63

    
64
        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

    
71
      def assert_async_response(response)
72
        support = @support || self.class.support
73
        fail '@support or @@support not defined' unless support
74

    
75
        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
        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

    
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
    end
97
  end
98
end
99

    
100
class CustomMiniTestRunner
101
  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
      if ENV['suite']
112
        suites = suites.select do |suite|
113
          suite.name == ENV['suite']
114
        end
115
      end
116
      before_suites
117
      super(suites, type)
118
    ensure
119
      after_suites
120
    end
121

    
122
    def _run_suite(suite, type)
123
      options = suite.respond_to?(:cassette_options) ? suite.cassette_options : {}
124
      if logging?
125
        puts "Running Suite #{suite.inspect} - #{type.inspect} "
126
      end
127
      if suite.respond_to?(:before_suite)
128
        VCR.use_cassette(suite.suite_cassette_name, options) do
129
          suite.before_suite
130
        end
131
      end
132
      super(suite, type)
133
    ensure
134
      if suite.respond_to?(:after_suite)
135
        VCR.use_cassette(suite.suite_cassette_name, options) do
136
          suite.after_suite
137
        end
138
      end
139
      if logging?
140
        puts "Completed Running Suite #{suite.inspect} - #{type.inspect} "
141
      end
142
    end
143

    
144
    def logging?
145
      ENV['logging']
146
    end
147
  end
148
end
149

    
150
class PulpMiniTestRunner
151
  def run_tests(suite, options = {})
152
    mode      = options[:mode] || 'none'
153
    test_name = options[:test_name] || nil
154
    auth_type = options[:auth_type] || 'http'
155
    logging   = options[:logging] || false
156

    
157
    MiniTest::Unit.runner = CustomMiniTestRunner::Unit.new
158

    
159
    if mode == 'all'
160
      runcible_config(:auth_type => auth_type, :logging => logging)
161
    else
162
      runcible_config(:logging => logging)
163
    end
164

    
165
    vcr_config(mode)
166

    
167
    if test_name && File.exist?(test_name)
168
      require test_name
169
    elsif test_name
170
      require "./test/#{test_name}_test.rb"
171
    else
172
      Dir["./test/#{suite}/*_test.rb"].each { |file| require file }
173
    end
174
  end
175

    
176
  def runcible_config(options)
177
    config = { :api_path   => '/pulp/api/v2/',
178
               :http_auth  => {}}
179

    
180
    if options[:logging] == 'true'
181
      log = ::Logger.new(STDOUT)
182
      log.level = Logger::DEBUG
183
      config[:logging] = { :logger => log,
184
                           :debug  => true,
185
                           :stdout => true}
186
    end
187

    
188
    if options[:auth_type] == 'http'
189

    
190
      File.open('/etc/pulp/server.conf') do |f|
191
        f.each_line do |line|
192
          if line.start_with?('default_password')
193
            config[:http_auth][:password] = line.split(':')[1].strip
194
          elsif line.start_with?('default_login')
195
            config[:user] = line.split(':')[1].strip
196
          elsif line.start_with?('server_name')
197
            config[:url] = "https://#{line.split(':')[1].chomp.strip}"
198
          end
199
        end
200
      end
201
    elsif options[:auth_type] == 'oauth'
202

    
203
      File.open('/etc/pulp/server.conf') do |f|
204
        f.each_line do |line|
205
          if line.start_with?('oauth_secret')
206
            config[:oauth][:oauth_secret] = line.split(':')[1].strip
207
          elsif line.start_with?('oauth_key')
208
            config[:oauth][:oauth_key] = line.split(':')[1].strip
209
          elsif line.start_with?('default_login')
210
            config[:user] = line.split(':')[1].strip
211
          elsif line.start_with?('server_name')
212
            config[:url] = "https://#{line.split(':')[1].chomp.strip}"
213
          end
214
        end
215
      end
216
    else
217
      config[:http_auth][:password] = 'admin'
218
      config[:user]  = 'admin'
219
      config[:url]   = 'https://localhost'
220
    end
221
    config[:verify_ssl] = false
222
    TestRuncible.server = Runcible::Instance.new(config)
223
  end
224

    
225
  def vcr_config(mode)
226
    if mode == 'all'
227
      configure_vcr(:all)
228
    elsif mode == 'new_episodes'
229
      configure_vcr(:new_episodes)
230
    elsif mode == 'once'
231
      configure_vcr(:once)
232
    else
233
      configure_vcr(:none)
234
    end
235
  end
236
end