Class: Sauce::TestBroker

Inherits:
Object
  • Object
show all
Defined in:
lib/sauce/parallel/test_broker.rb

Constant Summary collapse

CUCUMBER_CONFIG_FILES =
["./features/support/sauce_helper.rb"]
RSPEC_CONFIG_FILES =
["./spec/sauce_helper.rb", "./spec/spec_helper.rb"]
SAUCE_USERNAME =
ENV["SAUCE_USERNAME"]
SAUCE_ACCESS_KEY =
ENV["SAUCE_ACCESS_KEY"]
AUTH_DETAILS =
"#{SAUCE_USERNAME}:#{SAUCE_ACCESS_KEY}"

Class Method Summary collapse

Class Method Details

.concurrencyObject



69
70
71
72
# File 'lib/sauce/parallel/test_broker.rb', line 69

def self.concurrency
  response = RestClient.get "#{rest_jobs_url}/#{SAUCE_USERNAME}/limits"
  res = JSON.parse(response)["concurrency"]
end

.environment_mutexObject



19
20
21
# File 'lib/sauce/parallel/test_broker.rb', line 19

def self.environment_mutex
  @@m ||= Mutex.new
end

.find_config_file(primary_files, secondary_files) ⇒ Object



98
99
100
# File 'lib/sauce/parallel/test_broker.rb', line 98

def self.find_config_file(primary_files, secondary_files)
  find_in_file_list(primary_files) || find_in_file_list(secondary_files)
end

.find_in_file_list(list) ⇒ Object



102
103
104
# File 'lib/sauce/parallel/test_broker.rb', line 102

def self.find_in_file_list(list)
  list.find { |file_path| File.exists?(file_path) }
end

.group_index(group) ⇒ Object



57
58
59
# File 'lib/sauce/parallel/test_broker.rb', line 57

def self.group_index(group)
  @group_indexes[group].shift
end

.load_sauce_config(tool = :rspec) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sauce/parallel/test_broker.rb', line 78

def self.load_sauce_config(tool=:rspec)
  case tool
    when :rspec
      primary_files = RSPEC_CONFIG_FILES
      secondary_files = CUCUMBER_CONFIG_FILES
    when :cucumber
      primary_files = CUCUMBER_CONFIG_FILES
      secondary_files = RSPEC_CONFIG_FILES
  end

  configuration_file = self.find_config_file(primary_files, secondary_files)
  unless configuration_file
    possible_config_files = primary_files + secondary_files
    error_message = "Could not find Sauce configuration. Please make sure one of the following files exists:\n"
    error_message << possible_config_files.map { |file_path| "  - #{file_path}" }.join("\n")
    raise error_message
  end
  require configuration_file
end

.next_environment(group) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sauce/parallel/test_broker.rb', line 23

def self.next_environment(group)
  environment_mutex.synchronize do
    browsers = {}
    group.each do |file|
      file = "./" + file
      test_groups[file] ||= TestGroup.new(self.test_platforms)
      browsers[file] ||= []
      browsers[file] << test_groups[file].next_platform
    end

    on_windows = RbConfig::CONFIG['host_os'] =~ /cygwin|mswin|mingw|bccwin|wince|emx/
    format_string = on_windows ? "%s" : "'%s'"
    perfile_browsers = format_string % [JSON.generate(browsers)]

    return {:SAUCE_PERFILE_BROWSERS => perfile_browsers}
  end
end

.resetObject



12
13
14
15
16
17
# File 'lib/sauce/parallel/test_broker.rb', line 12

def self.reset
  if defined? @@platforms
    remove_class_variable(:@@platforms)
  end
  @groups = {}
end

.rest_jobs_urlObject



74
75
76
# File 'lib/sauce/parallel/test_broker.rb', line 74

def self.rest_jobs_url
  "https://#{AUTH_DETAILS}@saucelabs.com/rest/v1"
end

.test_groupsObject



41
42
43
# File 'lib/sauce/parallel/test_broker.rb', line 41

def self.test_groups
  @groups ||= {}
end

.test_groups=(groups) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sauce/parallel/test_broker.rb', line 45

def self.test_groups=(groups)
  @groups = groups.reduce({}) do |hash, g|
    hash[g] = TestGroup.new(self.test_platforms)
    hash
  end

  @group_indexes = groups.uniq.reduce({}) do |rh, g|
    rh[g] =(groups.each_index.select {|i| groups[i] == g})
    rh
  end
end

.test_platforms(tool = :rspec) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/sauce/parallel/test_broker.rb', line 61

def self.test_platforms(tool=:rspec)
  unless defined? @@platforms
    load_sauce_config(tool)
    @@platforms ||= Sauce.get_config[:browsers]
  end
  @@platforms
end