Merge remote-tracking branch 'flapflap/de-fingerprint'
[tails-test.git] / features / support / hooks.rb
blob2f2f98c188c38c1db3055a5ab5690ed0301dfd4a
1 require 'fileutils'
2 require 'time'
3 require 'tmpdir'
5 # For @product tests
6 ####################
8 def delete_snapshot(snapshot)
9   if snapshot and File.exist?(snapshot)
10     File.delete(snapshot)
11   end
12 rescue Errno::EACCES => e
13   STDERR.puts "Couldn't delete background snapshot: #{e.to_s}"
14 end
16 def delete_all_snapshots
17   Dir.glob("#{$tmp_dir}/*.state").each do |snapshot|
18     delete_snapshot(snapshot)
19   end
20 end
22 BeforeFeature('@product') do |feature|
23   if File.exist?($tmp_dir)
24     if !File.directory?($tmp_dir)
25       raise "Temporary directory '#{$tmp_dir}' exists but is not a " +
26             "directory"
27     end
28     if !File.owned?($tmp_dir)
29       raise "Temporary directory '#{$tmp_dir}' must be owned by the " +
30             "current user"
31     end
32     FileUtils.chmod(0755, $tmp_dir)
33   else
34     begin
35       Dir.mkdir($tmp_dir)
36     rescue Errno::EACCES => e
37       raise "Cannot create temporary directory: #{e.to_s}"
38     end
39   end
40   delete_all_snapshots if !$keep_snapshots
41   if $tails_iso.nil?
42     raise "No Tails ISO image specified, and none could be found in the " +
43           "current directory"
44   end
45   if File.exist?($tails_iso)
46     # Workaround: when libvirt takes ownership of the ISO image it may
47     # become unreadable for the live user inside the guest in the
48     # host-to-guest share used for some tests.
50     if !File.world_readable?($tails_iso)
51       if File.owned?($tails_iso)
52         File.chmod(0644, $tails_iso)
53       else
54         raise "warning: the Tails ISO image must be world readable or be " +
55               "owned by the current user to be available inside the guest " +
56               "VM via host-to-guest shares, which is required by some tests"
57       end
58     end
59   else
60     raise "The specified Tails ISO image '#{$tails_iso}' does not exist"
61   end
62   puts "Testing ISO image: #{File.basename($tails_iso)}"
63   base = File.basename(feature.file, ".feature").to_s
64   $background_snapshot = "#{$tmp_dir}/#{base}_background.state"
65 end
67 AfterFeature('@product') do
68   delete_snapshot($background_snapshot) if !$keep_snapshots
69   VM.storage.clear_volumes if VM.storage
70 end
72 BeforeFeature('@product', '@old_iso') do
73   if $old_tails_iso.nil?
74     raise "No old Tails ISO image specified, and none could be found in the " +
75           "current directory"
76   end
77   if !File.exist?($old_tails_iso)
78     raise "The specified old Tails ISO image '#{$old_tails_iso}' does not exist"
79   end
80   if $tails_iso == $old_tails_iso
81     raise "The old Tails ISO is the same as the Tails ISO we're testing"
82   end
83   puts "Using old ISO image: #{File.basename($old_tails_iso)}"
84 end
86 # BeforeScenario
87 Before('@product') do
88   @screen = Sikuli::Screen.new
89   if File.size?($background_snapshot)
90     @skip_steps_while_restoring_background = true
91   else
92     @skip_steps_while_restoring_background = false
93   end
94   @theme = "gnome"
95   @os_loader = "MBR"
96 end
98 # AfterScenario
99 After('@product') do |scenario|
100   if (scenario.status != :passed)
101     time_of_fail = Time.now - $time_at_start
102     secs = "%02d" % (time_of_fail % 60)
103     mins = "%02d" % ((time_of_fail / 60) % 60)
104     hrs  = "%02d" % (time_of_fail / (60*60))
105     STDERR.puts "Scenario failed at time #{hrs}:#{mins}:#{secs}"
106     base = File.basename(scenario.feature.file, ".feature").to_s
107     tmp = @screen.capture.getFilename
108     out = "#{$tmp_dir}/#{base}-#{DateTime.now}.png"
109     FileUtils.mv(tmp, out)
110     STDERR.puts("Took screenshot \"#{out}\"")
111     if $pause_on_fail
112       STDERR.puts ""
113       STDERR.puts "Press ENTER to continue running the test suite"
114       STDIN.gets
115     end
116   end
117   if @sniffer
118     @sniffer.stop
119     @sniffer.clear
120   end
121   @vm.destroy if @vm
124 After('@product', '~@keep_volumes') do
125   VM.storage.clear_volumes
128 # For @source tests
129 ###################
131 # BeforeScenario
132 Before('@source') do
133   @orig_pwd = Dir.pwd
134   @git_clone = Dir.mktmpdir 'tails-apt-tests'
135   Dir.chdir @git_clone
138 # AfterScenario
139 After('@source') do
140   Dir.chdir @orig_pwd
141   FileUtils.remove_entry_secure @git_clone
145 # Common
146 ########
148 BeforeFeature('@product', '@source') do |feature|
149   raise "Feature #{feature.file} is tagged both @product and @source, " +
150         "which is an impossible combination"
153 at_exit do
154   delete_all_snapshots if !$keep_snapshots
155   VM.storage.clear_pool if VM.storage