Module: PDQTest::Instance

Defined in:
lib/pdqtest/instance.rb

Constant Summary collapse

TEST_DIR =
'/testcase'
@@keep_container =
false
@@active_container =
nil
@@image_name =
false
@@privileged =
false

Class Method Summary collapse

Class Method Details

.get_active_containerObject



14
15
16
# File 'lib/pdqtest/instance.rb', line 14

def self.get_active_container
  @@active_container
end

.get_keep_containerObject



18
19
20
# File 'lib/pdqtest/instance.rb', line 18

def self.get_keep_container
  @@keep_container
end

.get_privilegedObject



39
40
41
# File 'lib/pdqtest/instance.rb', line 39

def self.get_privileged
  @@privileged
end

.run(example = nil) ⇒ Object



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
# File 'lib/pdqtest/instance.rb', line 43

def self.run(example=nil)
  # needed to prevent timeouts from container.exec()
  Excon.defaults[:write_timeout] = 10000
  Excon.defaults[:read_timeout] = 10000
  status = true

  # remove reference to any previous test container
  @@active_container = nil

  if PDQTest::Puppet::find_examples().empty?
    Escort::Logger.output.puts "No acceptance tests found, annotate examples with #{PDQTest::Puppet::MAGIC_MARKER} to make some"
  else
    # process each supported OS
    test_platforms = @@image_name || Docker::acceptance_test_images
    Escort::Logger.output.puts "Acceptance test on #{test_platforms}..."
    test_platforms.each { |image_name|
      Escort::Logger.output.puts "--- start test with #{image_name} ---"
      @@active_container = PDQTest::Docker::new_container(TEST_DIR, image_name, @@privileged)
      Escort::Logger.output.puts "alive, running tests"
      status &= PDQTest::Puppet.run(@@active_container, example)

      if @@keep_container
        Escort::Logger.output.puts "finished build, container #{@@active_container.id} left on system"
        Escort::Logger.output.puts "  docker exec -ti #{@@active_container.id} bash "
      else
        PDQTest::Docker.cleanup_container(@@active_container)
        @@active_container = nil
      end

      Escort::Logger.output.puts "--- end test with #{image_name} (status: #{status})---"
    }
  end
  Escort::Logger.output.puts "overall acceptance test status=#{status}"
  status
end

.set_docker_image(image_name) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/pdqtest/instance.rb', line 26

def self.set_docker_image(image_name)
  @@image_name =
      if image_name
        Array(image_name.split(/,/))
      else
        false
      end
end

.set_keep_container(keep_container) ⇒ Object



22
23
24
# File 'lib/pdqtest/instance.rb', line 22

def self.set_keep_container(keep_container)
  @@keep_container = keep_container
end

.set_privileged(privileged) ⇒ Object



35
36
37
# File 'lib/pdqtest/instance.rb', line 35

def self.set_privileged(privileged)
  @@privileged = privileged
end

.shellObject



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
# File 'lib/pdqtest/instance.rb', line 79

def self.shell
  # pick the first test platform to test on as our shell - want to do a specific one
  # just list it with --image-name
  image_name = (@@image_name || Docker::acceptance_test_images).first
  Escort::Logger.output.puts "Opening a shell in #{image_name}"
  @@active_container = PDQTest::Docker::new_container(TEST_DIR, image_name, @@privileged)

  # In theory I should be able to get something like the code below to
  # redirect all input streams and give a makeshift interactive shell, howeve
  # I'm damned if I get get this to do anything at all, so instead go the
  # easy way and start the container running, then use system() to redirect
  # all streams using the regular docker command.  Works a treat!
  # @@active_container.tap(&:start).attach(:tty => true)
  # @@active_container.exec('bash', tty: true).tap(&:start).attach( :tty => true, :stdin => $stdin) { |out, err|
  #   puts out
  #   puts err
  # }
  system("docker exec -ti #{@@active_container.id} bash")
  if @@keep_container
    Escort::Logger.output.puts "finished build, container #{@@active_container.id} left on system"
    Escort::Logger.output.puts "  docker exec -ti #{@@active_container.id} bash "
  else
      PDQTest::Docker.cleanup_container(@@active_container)
      @@active_container = nil
  end
end