Class: MixpanelTesting::DockerProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/mixpaneltesting/docker.rb

Instance Method Summary collapse

Constructor Details

#initialize(browser, version = nil, debug = false) ⇒ DockerProvider

Returns a new instance of DockerProvider.



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
# File 'lib/mixpaneltesting/docker.rb', line 13

def initialize(browser, version=nil, debug=false)
  if !['firefox', 'chrome'].include?(browser)
    raise DockerProviderBrowserNotAvailable, "#{browser} not available"
  end

  @log = Logger.new(STDOUT)
  @docker_uri = URI(ENV['DOCKER_HOST'])
  @browser = browser
  @debug = debug
  @version = version.nil? ? "" : ":#{version}"
  @image_name = (@debug ?
                  "selenium/standalone-#{@browser}-debug#{@version}" :
                  "selenium/standalone-#{@browser}#{@version}")

  @threads = []

  @log.info ["Creating selenium docker, if you don't see Docker started",
             "message, try to remove mixpaneltesting docker with:",
             "docker rm -rf mixpaneltesting"].join('/n')
  # This settings is fully wired for boot2docker/docker-machines
  # We should change this to make compatible with other
  @container = Docker::Container.create(
    'Image' => @image_name,
    'name' => 'mixpaneltesting',  # Name given for helping with debug
    'ExposedPorts' => {
      '4444/tcp' => {},
      '5900/tcp' => {},
    },
    'HostConfig' => {
      'PortBindings' => {
        '4444/tcp' => [{ 'HostPort' => '4444'}], # Selenium Port
        '5900/tcp' => [{ 'HostPort' => '5900'}], # VNC Port for everyone
      }
    }
  )

end

Instance Method Details

#killObject



64
65
66
67
68
69
70
71
72
# File 'lib/mixpaneltesting/docker.rb', line 64

def kill
  @container.kill!
  @container.delete(:force => true)

  @threads.each { |thr|
    @log.info "Killing thread"
    thr.exit
  }
end

#open_vncObject



87
88
89
90
91
# File 'lib/mixpaneltesting/docker.rb', line 87

def open_vnc
  @threads.push Thread.new {
    `open #{vnc_uri}`
  }
end

#ready?Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/mixpaneltesting/docker.rb', line 74

def ready?
  puts selenium_uri
  Excon.get(selenium_uri).status == 302 rescue false
end

#selenium_uriObject



79
80
81
# File 'lib/mixpaneltesting/docker.rb', line 79

def selenium_uri
  "http://#{@docker_uri.host}:4444/wd/hub"
end

#startObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mixpaneltesting/docker.rb', line 51

def start
  @container.start

  (1..Settings.timeout).each { |i|
    sleep 1
    @log.info "Waiting to docker ready: #{i}"
    break if ready?
  }
  @log.info "Docker started"

  open_vnc if @debug
end

#vnc_uriObject



83
84
85
# File 'lib/mixpaneltesting/docker.rb', line 83

def vnc_uri
  "vnc://:secret@#{@docker_uri.host}:5900"
end