summaryrefslogtreecommitdiff
path: root/spec/support/integration_test_helper.rb
blob: 5c9de946780ba34d45665ecc5a03d7d7128cebae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
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
module IntegrationTestHelper
  class Report
    attr_reader :report

    def initialize(raw)
      @report = JSON.parse(raw, symbolize_names: true)
    end

    def [](key)
      report[key]
    end

    def dependency_names
      report[:dependencies].map { |x| x[:name] }
    end

    def licenses_for(name)
      (find(name) || {}).fetch(:licenses, [])
    end

    def find(name)
      report[:dependencies].find do |dependency|
        dependency[:name] == name
      end
    end

    def nil?
      report.nil?
    end

    def to_hash
      to_h
    end

    def to_h
      report
    end
  end

  class IntegrationTestRunner
    attr_reader :project_path

    def initialize(project_path = Pathname.pwd.join('tmp').join(SecureRandom.uuid))
      FileUtils.mkdir_p(project_path)
      @project_path = Pathname(project_path)
    end

    def add_file(name, content = nil)
      full_path = project_path.join(name)
      FileUtils.mkdir_p(full_path.dirname)
      IO.write(full_path, block_given? ? yield : content)
    end

    def mount(dir:)
      FileUtils.cp_r("#{dir}/.", project_path)
    end

    def clone(repo, branch: 'master')
      if branch.match?(/\b[0-9a-f]{5,40}\b/)
        execute({}, 'git', 'clone', '--quiet', repo, project_path.to_s)
        Dir.chdir project_path do
          execute({}, 'git', 'checkout', branch)
        end
      else
        execute({}, 'git', 'clone', '--quiet', '--depth=1', '--single-branch', '--branch', branch, repo, project_path.to_s)
      end
    end

    def scan(env: {})
      return {} unless execute(env, './bin/docker-test', project_path.to_s)

      report_path = project_path.join('gl-license-scanning-report.json')
      return {} unless report_path.exist?

      Report.new(report_path.read)
    end

    def execute(env = {}, *args)
      Bundler.with_unbundled_env do
        system(env, *args, exception: true)
      end
    end

    def cleanup
      FileUtils.rm_rf(project_path) if project_path.exist?
    end
  end

  def private_npm_host
    @private_npm_host ||= ENV.fetch('PRIVATE_NPM_HOST').tap do |host|
      add_host(host, ENV.fetch('PRIVATE_NPM_IP'))
    end
  end

  def private_pypi_host
    @private_pypi_host ||= ENV.fetch('PRIVATE_PYPI_HOST').tap do |host|
      add_host(host, ENV.fetch('PRIVATE_PYPI_IP'))
    end
  end

  def private_maven_host
    @private_maven_host ||= ENV.fetch('PRIVATE_MAVEN_HOST').tap do |host|
      add_host(host, ENV.fetch('PRIVATE_MAVEN_IP'))
    end
  end

  def runner(*args)
    @runner ||= IntegrationTestRunner.new(*args)
  end

  def add_host(name, ip)
    return unless ENV['LM_HOME']
    return if system("grep #{name} /etc/hosts")

    system("echo '#{ip} #{name}' >> /etc/hosts")
  end
end