Class: Kitchen::Verifier::Tox

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/verifier/tox.rb

Instance Method Summary collapse

Instance Method Details

#call(state) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
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
78
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/kitchen/verifier/tox.rb', line 30

def call(state)
  info("[#{name}] Verify on instance #{instance.name} with state=#{state}")
  root_path = (config[:windows] ? '%TEMP%\\kitchen' : '/tmp/kitchen')
  if ENV['KITCHEN_TESTS']
    ENV['KITCHEN_TESTS'].split(' ').each{|test| config[:tests].push(test)}
  end
  toxenv = instance.suite.name
  if config[:pytest]
    toxenv = "#{toxenv}-pytest"
    tests = config[:tests].join(' ')
  else
    toxenv = "#{toxenv}-runtests"
    tests = config[:tests].collect{|test| "-n #{test}"}.join(' ')
  end
  if config[:coverage]
    toxenv = "#{toxenv}-coverage"
  end

  if config[:enable_filenames] and ENV['CHANGE_TARGET'] and ENV['BRANCH_NAME']
    require 'git'
    repo = Git.open(Dir.pwd)
    config[:from_filenames] = repo.diff("origin/#{ENV['CHANGE_TARGET']}",
                                        "origin/#{ENV['BRANCH_NAME']}").name_status.keys.select{|file| file.end_with?('.py')}
  end

  if config[:junitxml]
    junitxml = File.join(root_path, config[:testingdir], 'artifacts', 'xml-unittests-output')
    if config[:pytest]
      junitxml = "--junitxml=#{File.join(junitxml, 'test-results.xml')}"
    else
      junitxml = "--xml=#{junitxml}"
    end
  end

  # Be sure to copy the remote artifacts directory to the local machine
  save = {
    "#{File.join(root_path, config[:testingdir], 'artifacts')}" => "#{Dir.pwd}/"
  }
  # Hash insert order matters, that's why we define a new one and merge
  # the one from config
  save.merge!(config[:save])

  command = [
    'tox -c',
    File.join(root_path, config[:testingdir], 'tox.ini'),
    "-e #{toxenv}",
    '--',
    "--output-columns=#{config[:output_columns]}",
    (config[:sysinfo] ? '--sysinfo' : ''),
    (config[:junitxml] ? junitxml : ''),
    (config[:windows] ? "--names-file=#{root_path}\\testing\\tests\\whitelist.txt" : ''),
    (config[:transport] ? "--transport=#{config[:transport]}" : ''),
    (config[:verbose] ? '-vv' : '-v'),
    (config[:run_destructive] ? "--run-destructive" : ''),
    (config[:ssh_tests] ? "--ssh-tests" : ''),
    (config[:proxy_tests] ? "--proxy-tests" : ''),
    config[:passthrough_opts].join(' '),
    (config[:from_filenames].any? ? "--from-filenames=#{config[:from_filenames].join(',')}" : ''),
    tests,
    '2>&1',
  ].join(' ')
  if config[:windows]
     command = "cmd.exe /c \"#{command}\" 2>&1"
  end
  info("Running Command: #{command}")
  instance.transport.connection(state) do |conn|
    begin
      if config[:windows]
        conn.execute('$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")')
        conn.execute("$env:PythonPath = [Environment]::ExpandEnvironmentVariables(\"#{root_path}\\testing\")")
      else
        begin
          conn.execute(sudo("chown -R $USER #{root_path}"))
        rescue => e
          error("Failed to chown #{root_path} :: #{e}")
        end
      end
      begin
        conn.execute(sudo(command))
      rescue => e
        info("Verify command failed :: #{e}")
      end
    ensure
      save.each do |remote, local|
        unless config[:windows]
          begin
            conn.execute(sudo("chmod -R +r #{remote}"))
          rescue => e
            error("Failed to chown #{remote} :: #{e}")
          end
        end
        begin
          info("Copying #{remote} to #{local}")
          conn.download(remote, local)
        rescue => e
          error("Failed to copy #{remote} to #{local} :: #{e}")
        end
      end
    end
  end
  debug("[#{name}] Verify completed.")
end