diff options
Diffstat (limited to 'spec/support')
| -rw-r--r-- | spec/support/fixture_file_helper.rb | 14 | ||||
| -rw-r--r-- | spec/support/integration_test_helper.rb | 105 | ||||
| -rw-r--r-- | spec/support/matchers.rb | 14 | ||||
| -rw-r--r-- | spec/support/shared.rb | 19 |
4 files changed, 152 insertions, 0 deletions
diff --git a/spec/support/fixture_file_helper.rb b/spec/support/fixture_file_helper.rb new file mode 100644 index 0000000..5a9599f --- /dev/null +++ b/spec/support/fixture_file_helper.rb @@ -0,0 +1,14 @@ +module FixtureFileHelper + def fixture_file_content(path, data = {}) + content = IO.read(fixture_file(path)) + return content unless path.end_with?('.erb') + + ERB + .new(content) + .result(OpenStruct.new(data).send(:binding)) + end + + def fixture_file(path) + License::Management.root.join("spec/fixtures/#{path}") + end +end diff --git a/spec/support/integration_test_helper.rb b/spec/support/integration_test_helper.rb new file mode 100644 index 0000000..52693f2 --- /dev/null +++ b/spec/support/integration_test_helper.rb @@ -0,0 +1,105 @@ +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)[: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 = File.join(Dir.pwd, 'tmp', SecureRandom.uuid)) + FileUtils.mkdir_p(project_path) + @project_path = project_path + end + + def add_file(name, content = nil) + full_path = Pathname.new(File.join(project_path, 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) + Dir.chdir project_path do + execute({}, 'git', 'checkout', branch) + end + else + execute({}, 'git', 'clone', '--quiet', '--depth=1', '--single-branch', '--branch', branch, repo, project_path) + end + end + + def scan(env: {}) + return {} unless execute(env, './bin/docker-test', project_path) + + report_path = "#{project_path}/gl-license-management-report.json" + return {} unless File.exist?(report_path) + + Report.new(IO.read(report_path)) + 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 Dir.exist?(project_path) + 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 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 diff --git a/spec/support/matchers.rb b/spec/support/matchers.rb new file mode 100644 index 0000000..66bb92c --- /dev/null +++ b/spec/support/matchers.rb @@ -0,0 +1,14 @@ +RSpec::Matchers.define :match_schema do |version: '2.0'| + def schema_for(version) + License::Management.root.join("spec/fixtures/schema/v#{version}.json").to_s + end + + match do |actual| + !actual.nil? && (@errors = JSON::Validator.fully_validate(schema_for(version), actual.to_h)).empty? + end + + failure_message do |response| + "didn't match the schema for version #{version}" \ + " The validation errors were:\n#{@errors.join("\n")}" + end +end diff --git a/spec/support/shared.rb b/spec/support/shared.rb new file mode 100644 index 0000000..51b161a --- /dev/null +++ b/spec/support/shared.rb @@ -0,0 +1,19 @@ +RSpec.shared_examples "each report version" do |language, package_manager, branch = 'master'| + ['1.0', '1.1', '2.0'].each do |version| + context "when generating a `#{version}` report for #{package_manager}" do + let(:url) { "https://gitlab.com/gitlab-org/security-products/tests/#{language}-#{package_manager}.git" } + let(:expected_content) { JSON.parse(fixture_file_content("expected/#{language}/#{package_manager}/v#{version}.json")) } + + before do + runner.clone(url, branch: branch) + end + + it 'matches the expected report' do + actual = runner.scan(env: { 'LM_REPORT_VERSION' => version }) + + expect(JSON.pretty_generate(actual.to_h)).to eq(JSON.pretty_generate(expected_content)) + expect(actual).to match_schema(version: version) + end + end + end +end |
