mirror of
https://github.com/inspec/inspec
synced 2025-02-18 15:08:44 +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,
|
# download url into archive using opts,
|
||||||
# returns File object and content-type from HTTP headers
|
# returns File object and content-type from HTTP headers
|
||||||
def download_archive(url, opts)
|
def download_archive(url, opts)
|
||||||
archive = Tempfile.new('inspec-dl-')
|
|
||||||
archive.binmode
|
|
||||||
|
|
||||||
remote = open(
|
remote = open(
|
||||||
url,
|
url,
|
||||||
http_basic_authentication: [opts['user'] || '', opts['password'] || ''],
|
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
|
# download content
|
||||||
|
archive = Tempfile.new(['inspec-dl-', file_type])
|
||||||
|
archive.binmode
|
||||||
archive.write(remote.read)
|
archive.write(remote.read)
|
||||||
archive.rewind
|
archive.rewind
|
||||||
archive.close
|
archive.close
|
||||||
|
|
||||||
[archive, remote.meta['content-type']]
|
[archive, content_type]
|
||||||
end
|
|
||||||
|
|
||||||
def ensure_suffix(path, suffix)
|
|
||||||
return path if path.end_with?(suffix)
|
|
||||||
File.rename(path, path + suffix)
|
|
||||||
path + suffix
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def resolve_archive_path(archive_helper, path, opts)
|
def resolve_archive_path(archive_helper, path, opts)
|
||||||
|
@ -63,9 +75,7 @@ module Inspec::Targets
|
||||||
"#{archive_helper}. Internal error, please file a bugreport."
|
"#{archive_helper}. Internal error, please file a bugreport."
|
||||||
end
|
end
|
||||||
|
|
||||||
res = archive_helper.resolve(path, opts)
|
archive_helper.resolve(path, opts)
|
||||||
File.unlink(path)
|
|
||||||
res
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def resolve_archive(url, opts)
|
def resolve_archive(url, opts)
|
||||||
|
@ -74,11 +84,9 @@ module Inspec::Targets
|
||||||
# replace extension with zip if we detected a zip content type
|
# replace extension with zip if we detected a zip content type
|
||||||
case content_type
|
case content_type
|
||||||
when 'application/x-zip-compressed', 'application/zip'
|
when 'application/x-zip-compressed', 'application/zip'
|
||||||
path = ensure_suffix(archive.path, '.zip')
|
resolve_archive_path(ZipHelper, archive.path, opts)
|
||||||
resolve_archive_path(ZipHelper, path, opts)
|
|
||||||
when 'application/x-gzip', 'application/gzip'
|
when 'application/x-gzip', 'application/gzip'
|
||||||
path = ensure_suffix(archive.path, '.tar.gz')
|
resolve_archive_path(TarHelper, archive.path, opts)
|
||||||
resolve_archive_path(TarHelper, path, opts)
|
|
||||||
when nil
|
when nil
|
||||||
{}
|
{}
|
||||||
else
|
else
|
||||||
|
|
|
@ -15,6 +15,8 @@ SimpleCov.start do
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
|
require 'pathname'
|
||||||
|
require 'tempfile'
|
||||||
require 'zip'
|
require 'zip'
|
||||||
|
|
||||||
require 'utils/base_cli'
|
require 'utils/base_cli'
|
||||||
|
@ -255,22 +257,35 @@ class MockLoader
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.profile_tgz(name)
|
def self.profile_tgz(name)
|
||||||
path = "#{home}/mock/profiles/#{name}"
|
path = File.join(home, 'mock', 'profiles', name)
|
||||||
dst = "#{path}.tgz"
|
archive = Tempfile.new([name, '.tar.gz'])
|
||||||
FileUtils.rm(dst) if File.file?(dst)
|
dst = archive.path
|
||||||
`tar zcvf #{dst} #{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
|
dst
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.profile_zip(name, opts = {})
|
def self.profile_zip(name, opts = {})
|
||||||
path = "#{home}/mock/profiles/#{name}"
|
path = File.join(home, 'mock', 'profiles', name)
|
||||||
dst = "#{path}.zip"
|
archive = Tempfile.new([name, '.zip'])
|
||||||
FileUtils.rm(dst) if File.file?(dst)
|
dst = archive.path
|
||||||
Zip::File.open(dst, 'w') do |zipfile|
|
archive.close
|
||||||
Dir["#{path}/**/**"].reject { |f| f == dst }.each do |file|
|
|
||||||
zipfile.add(file.sub(path+'/', ''), file)
|
# rubyzip only works relative paths
|
||||||
end
|
files = Dir.glob("#{path}/**/*")
|
||||||
end
|
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
|
dst
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,11 +9,11 @@ describe Inspec::Targets::UrlHelper do
|
||||||
let(:url_helper) { Inspec::Targets::UrlHelper.new }
|
let(:url_helper) { Inspec::Targets::UrlHelper.new }
|
||||||
|
|
||||||
it 'handles http' do
|
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
|
end
|
||||||
|
|
||||||
it 'handles https' do
|
it 'handles https' do
|
||||||
url_helper.handles?('https://chef.io').must_equal true
|
url_helper.handles?('https://chef.io.zip').must_equal true
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns false if given an invalid URL' do
|
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][:type].must_equal :test
|
||||||
res[0][:content].wont_be_empty
|
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][:type].must_equal :metadata
|
||||||
res[1][:content].wont_be_empty
|
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
|
end
|
||||||
|
|
||||||
|
@ -129,11 +129,11 @@ describe Inspec::Targets::UrlHelper do
|
||||||
|
|
||||||
res[0][:type].must_equal :test
|
res[0][:type].must_equal :test
|
||||||
res[0][:content].wont_be_empty
|
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][:type].must_equal :metadata
|
||||||
res[1][:content].wont_be_empty
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue