mirror of
https://github.com/inspec/inspec
synced 2024-11-26 14:40:26 +00:00
use ruby zip and tar for unit tests
This commit is contained in:
parent
17d4e1dc3c
commit
26276ca991
3 changed files with 58 additions and 35 deletions
|
@ -34,26 +34,38 @@ module Inspec::Targets
|
|||
# download url into archive using opts,
|
||||
# returns File object and content-type from HTTP headers
|
||||
def download_archive(url, opts)
|
||||
archive = Tempfile.new('inspec-dl-')
|
||||
archive.binmode
|
||||
|
||||
remote = open(
|
||||
url,
|
||||
http_basic_authentication: [opts['user'] || '', opts['password'] || ''],
|
||||
)
|
||||
|
||||
content_type = remote.meta['content-type']
|
||||
|
||||
# map for file types
|
||||
filetypes = {
|
||||
'application/x-zip-compressed' => 'tar.gz',
|
||||
'application/zip' => 'tar.gz',
|
||||
'application/x-gzip' => 'zip',
|
||||
'application/gzip' => 'zip',
|
||||
}
|
||||
|
||||
# ensure we have a file extension
|
||||
file_type = filetypes[content_type] unless content_type.nil?
|
||||
|
||||
# fall back to tar
|
||||
if file_type.nil?
|
||||
warn "Could not determine file type for content type #{content_type}"
|
||||
file_type = 'tar.gz'
|
||||
end
|
||||
|
||||
# download content
|
||||
archive = Tempfile.new(['inspec-dl-', file_type])
|
||||
archive.binmode
|
||||
archive.write(remote.read)
|
||||
archive.rewind
|
||||
archive.close
|
||||
|
||||
[archive, remote.meta['content-type']]
|
||||
end
|
||||
|
||||
def ensure_suffix(path, suffix)
|
||||
return path if path.end_with?(suffix)
|
||||
File.rename(path, path + suffix)
|
||||
path + suffix
|
||||
[archive, content_type]
|
||||
end
|
||||
|
||||
def resolve_archive_path(archive_helper, path, opts)
|
||||
|
@ -63,9 +75,7 @@ module Inspec::Targets
|
|||
"#{archive_helper}. Internal error, please file a bugreport."
|
||||
end
|
||||
|
||||
res = archive_helper.resolve(path, opts)
|
||||
File.unlink(path)
|
||||
res
|
||||
archive_helper.resolve(path, opts)
|
||||
end
|
||||
|
||||
def resolve_archive(url, opts)
|
||||
|
@ -74,11 +84,9 @@ module Inspec::Targets
|
|||
# replace extension with zip if we detected a zip content type
|
||||
case content_type
|
||||
when 'application/x-zip-compressed', 'application/zip'
|
||||
path = ensure_suffix(archive.path, '.zip')
|
||||
resolve_archive_path(ZipHelper, path, opts)
|
||||
resolve_archive_path(ZipHelper, archive.path, opts)
|
||||
when 'application/x-gzip', 'application/gzip'
|
||||
path = ensure_suffix(archive.path, '.tar.gz')
|
||||
resolve_archive_path(TarHelper, path, opts)
|
||||
resolve_archive_path(TarHelper, archive.path, opts)
|
||||
when nil
|
||||
{}
|
||||
else
|
||||
|
|
|
@ -15,6 +15,8 @@ SimpleCov.start do
|
|||
end
|
||||
|
||||
require 'fileutils'
|
||||
require 'pathname'
|
||||
require 'tempfile'
|
||||
require 'zip'
|
||||
|
||||
require 'utils/base_cli'
|
||||
|
@ -255,22 +257,35 @@ class MockLoader
|
|||
end
|
||||
|
||||
def self.profile_tgz(name)
|
||||
path = "#{home}/mock/profiles/#{name}"
|
||||
dst = "#{path}.tgz"
|
||||
FileUtils.rm(dst) if File.file?(dst)
|
||||
`tar zcvf #{dst} #{path}`
|
||||
path = File.join(home, 'mock', 'profiles', name)
|
||||
archive = Tempfile.new([name, '.tar.gz'])
|
||||
dst = archive.path
|
||||
archive.close
|
||||
|
||||
# generate relative paths
|
||||
files = Dir.glob("#{path}/**/*")
|
||||
relatives = files.map { |e| Pathname.new(e).relative_path_from(Pathname.new(path)).to_s }
|
||||
|
||||
require 'inspec/archive/tar'
|
||||
tag = Inspec::Archive::TarArchiveGenerator.new
|
||||
tag.archive(path, relatives, dst)
|
||||
|
||||
dst
|
||||
end
|
||||
|
||||
def self.profile_zip(name, opts = {})
|
||||
path = "#{home}/mock/profiles/#{name}"
|
||||
dst = "#{path}.zip"
|
||||
FileUtils.rm(dst) if File.file?(dst)
|
||||
Zip::File.open(dst, 'w') do |zipfile|
|
||||
Dir["#{path}/**/**"].reject { |f| f == dst }.each do |file|
|
||||
zipfile.add(file.sub(path+'/', ''), file)
|
||||
end
|
||||
end
|
||||
path = File.join(home, 'mock', 'profiles', name)
|
||||
archive = Tempfile.new([name, '.zip'])
|
||||
dst = archive.path
|
||||
archive.close
|
||||
|
||||
# rubyzip only works relative paths
|
||||
files = Dir.glob("#{path}/**/*")
|
||||
relatives = files.map { |e| Pathname.new(e).relative_path_from(Pathname.new(path)).to_s }
|
||||
|
||||
require 'inspec/archive/zip'
|
||||
zag = Inspec::Archive::ZipArchiveGenerator.new
|
||||
zag.archive(path, relatives, dst)
|
||||
dst
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,11 +9,11 @@ describe Inspec::Targets::UrlHelper do
|
|||
let(:url_helper) { Inspec::Targets::UrlHelper.new }
|
||||
|
||||
it 'handles http' do
|
||||
url_helper.handles?('http://chef.io').must_equal true
|
||||
url_helper.handles?('http://chef.io.tar.gz').must_equal true
|
||||
end
|
||||
|
||||
it 'handles https' do
|
||||
url_helper.handles?('https://chef.io').must_equal true
|
||||
url_helper.handles?('https://chef.io.zip').must_equal true
|
||||
end
|
||||
|
||||
it 'returns false if given an invalid URL' do
|
||||
|
@ -105,11 +105,11 @@ describe Inspec::Targets::UrlHelper do
|
|||
|
||||
res[0][:type].must_equal :test
|
||||
res[0][:content].wont_be_empty
|
||||
res[0][:ref].must_equal "#{archive_path}controls/filesystem_spec.rb"
|
||||
res[0][:ref].must_equal "controls/filesystem_spec.rb"
|
||||
|
||||
res[1][:type].must_equal :metadata
|
||||
res[1][:content].wont_be_empty
|
||||
res[1][:ref].must_equal "#{archive_path}inspec.yml"
|
||||
res[1][:ref].must_equal "inspec.yml"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -129,11 +129,11 @@ describe Inspec::Targets::UrlHelper do
|
|||
|
||||
res[0][:type].must_equal :test
|
||||
res[0][:content].wont_be_empty
|
||||
res[0][:ref].must_equal "#{archive_path}controls/filesystem_spec.rb"
|
||||
res[0][:ref].must_equal "controls/filesystem_spec.rb"
|
||||
|
||||
res[1][:type].must_equal :metadata
|
||||
res[1][:content].wont_be_empty
|
||||
res[1][:ref].must_equal "#{archive_path}inspec.yml"
|
||||
res[1][:ref].must_equal "inspec.yml"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue