2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/metadata"
|
2015-12-31 16:54:13 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "metadata with supported operating systems" do
|
2016-01-16 16:04:25 +00:00
|
|
|
let(:logger) { Minitest::Mock.new }
|
2017-02-13 18:04:46 +00:00
|
|
|
let(:empty_options) { {} }
|
2016-01-16 16:04:25 +00:00
|
|
|
|
2016-04-16 16:54:05 +00:00
|
|
|
def supports_meta(params)
|
2019-06-11 22:24:35 +00:00
|
|
|
res = Inspec::Metadata.from_yaml("mock", "---", nil, logger)
|
2016-04-16 16:54:05 +00:00
|
|
|
# manually inject supported parameters
|
|
|
|
res.params[:supports] = params
|
2019-06-11 22:24:35 +00:00
|
|
|
Inspec::Metadata.finalize(res, "mock", empty_options, logger)
|
2016-04-16 16:54:05 +00:00
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "running on ubuntu 14.04" do
|
2019-05-31 21:59:06 +00:00
|
|
|
let(:backend) { MockLoader.new(:ubuntu1404).backend }
|
2015-12-31 16:54:13 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "provides all metadata content" do
|
2017-05-10 13:16:40 +00:00
|
|
|
s = "---\nname: hello #{rand}"
|
2019-06-11 22:24:35 +00:00
|
|
|
res = Inspec::Metadata.from_yaml("mock", s, nil)
|
|
|
|
Inspec::Metadata.finalize(res, "mock", empty_options)
|
2017-05-10 13:16:40 +00:00
|
|
|
res.content.must_equal(s)
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "renders a YAML containing ERB" do
|
2018-08-30 16:56:06 +00:00
|
|
|
data = <<EOF
|
|
|
|
name: dummy
|
|
|
|
title: InSpec Profile
|
|
|
|
maintainer: The Authors
|
|
|
|
copyright: The Authors
|
|
|
|
copyright_email: you@example.com
|
|
|
|
license: Apache-2.0
|
|
|
|
summary: An InSpec Compliance Profile
|
|
|
|
version: 0.1.0
|
|
|
|
depends:
|
|
|
|
- name: inherit
|
|
|
|
url: "https://artifactory.com/artifactory/example-repo-local/inspec/0.4.1.tar.gz"
|
|
|
|
username: <%= ENV['USERNAME'] %>
|
|
|
|
password: <%= ENV['API_KEY'] %>
|
|
|
|
EOF
|
2019-06-11 22:24:35 +00:00
|
|
|
ENV["USERNAME"] = "dummy_user"
|
|
|
|
ENV["API_KEY"] = "dummy_pass"
|
|
|
|
res = Inspec::Metadata.from_yaml("mock", data, nil)
|
|
|
|
Inspec::Metadata.finalize(res, "mock", empty_options)
|
|
|
|
res.params[:name].must_equal "mock"
|
|
|
|
res.params[:depends][0][:name].must_equal "inherit"
|
|
|
|
res.params[:depends][0][:url].must_equal "https://artifactory.com/artifactory/example-repo-local/inspec/0.4.1.tar.gz"
|
|
|
|
res.params[:depends][0][:username].must_equal "dummy_user"
|
|
|
|
res.params[:depends][0][:password].must_equal "dummy_pass"
|
2018-08-30 16:56:06 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "finalizes a loaded metadata via Profile ID" do
|
|
|
|
res = Inspec::Metadata.from_yaml("mock", "---", nil)
|
|
|
|
Inspec::Metadata.finalize(res, "mock", empty_options)
|
|
|
|
res.params[:name].must_equal("mock")
|
2016-01-15 19:51:23 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "finalizes a loaded metadata via Profile ID and overwrites the ID" do
|
|
|
|
res = Inspec::Metadata.from_yaml("mock", "---\nname: hello", nil)
|
|
|
|
Inspec::Metadata.finalize(res, "mock", empty_options)
|
|
|
|
res.params[:name].must_equal("mock")
|
2016-01-15 19:51:23 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "reads the version from metadata" do
|
|
|
|
res = Inspec::Metadata.from_yaml("mock", "---\nversion: '1.1.0'", nil)
|
|
|
|
Inspec::Metadata.finalize(res, "mock", empty_options)
|
|
|
|
res.params[:version].must_equal("1.1.0")
|
2017-05-18 12:20:10 +00:00
|
|
|
res.valid_version?(res.params[:version]).must_equal(true)
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "does not accept invalid version from metadata" do
|
|
|
|
res = Inspec::Metadata.from_yaml("mock", "---\nversion: '1.1.0.1'", nil)
|
|
|
|
Inspec::Metadata.finalize(res, "mock", empty_options)
|
|
|
|
res.params[:version].must_equal("1.1.0.1")
|
2017-05-18 12:20:10 +00:00
|
|
|
res.valid_version?(res.params[:version]).must_equal(false)
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "finalizes a loaded metadata by turning strings into symbols" do
|
|
|
|
res = Inspec::Metadata.from_yaml("mock", "---\nauthor: world", nil)
|
|
|
|
Inspec::Metadata.finalize(res, "mock", empty_options)
|
|
|
|
res.params[:author].must_equal("world")
|
2016-01-15 19:51:23 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "sets a default name with the original target if there is no name, title, or profile_id" do
|
|
|
|
res = Inspec::Metadata.from_yaml("mock", "---", nil, logger)
|
|
|
|
options = { target: "/path/to/tests" }
|
2017-02-13 18:04:46 +00:00
|
|
|
Inspec::Metadata.finalize(res, nil, options, logger)
|
2019-06-11 22:24:35 +00:00
|
|
|
res.params[:name].must_equal("tests from .path.to.tests")
|
2017-02-13 18:04:46 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "does not overwrite an existing name when name exists and profile_id is nil" do
|
|
|
|
res = Inspec::Metadata.from_yaml("mock", "\nname: my_name", nil)
|
|
|
|
options = { target: "/path/to/tests" }
|
2017-02-13 18:04:46 +00:00
|
|
|
Inspec::Metadata.finalize(res, nil, options, logger)
|
2019-06-11 22:24:35 +00:00
|
|
|
res.params[:name].must_equal("my_name")
|
2017-02-13 18:04:46 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "does not set a default name if a title is provided and profile_id is nil" do
|
|
|
|
res = Inspec::Metadata.from_yaml("mock", "\ntitle: my_title", nil)
|
|
|
|
options = { target: "/path/to/tests" }
|
2017-02-13 18:04:46 +00:00
|
|
|
Inspec::Metadata.finalize(res, nil, options, logger)
|
2019-06-11 22:24:35 +00:00
|
|
|
res.params[:title].must_equal("my_title")
|
2017-02-13 18:04:46 +00:00
|
|
|
res.params[:name].must_be_nil
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads the support field from metadata" do
|
|
|
|
res = Inspec::Metadata.from_yaml("mock",
|
2016-01-15 19:51:23 +00:00
|
|
|
"---\nsupports:\n - os: ubuntu", nil)
|
2019-06-11 22:24:35 +00:00
|
|
|
res.params[:supports].must_equal([{ os: "ubuntu" }])
|
2016-01-15 19:51:23 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "makes sure the supports release field is a string" do
|
|
|
|
res = Inspec::Metadata.from_yaml("mock",
|
2016-11-20 18:38:27 +00:00
|
|
|
"---\nsupports:\n - release: 12.02", nil)
|
2019-06-11 22:24:35 +00:00
|
|
|
res.params[:supports].must_equal([{ release: "12.02" }])
|
2016-11-20 18:38:27 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "makes sure the supports release field is nil if not configured" do
|
|
|
|
res = Inspec::Metadata.from_yaml("mock",
|
2016-11-20 18:38:27 +00:00
|
|
|
"---\nsupports:\n - release: ", nil)
|
|
|
|
res.params[:supports].must_equal([{ release: nil }])
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "load a profile with empty supports clause" do
|
2016-04-16 16:54:05 +00:00
|
|
|
m = supports_meta(nil)
|
2018-01-02 19:04:13 +00:00
|
|
|
m.supports_platform?(backend).must_equal true
|
2015-12-31 16:57:59 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads a profile which supports os ubuntu" do
|
|
|
|
m = supports_meta({ "os" => "ubuntu" })
|
2018-01-02 19:04:13 +00:00
|
|
|
m.supports_platform?(backend).must_equal true
|
2015-12-31 16:54:13 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads a profile which supports os name ubuntu" do
|
|
|
|
m = supports_meta({ "os-name" => "ubuntu" })
|
2018-01-02 19:04:13 +00:00
|
|
|
m.supports_platform?(backend).must_equal true
|
2015-12-31 16:54:13 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads a profile which supports os family linux" do
|
|
|
|
m = supports_meta({ "os-family" => "linux" })
|
2018-01-02 19:04:13 +00:00
|
|
|
m.supports_platform?(backend).must_equal true
|
2015-12-31 16:54:13 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads a profile which supports release 14.04" do
|
|
|
|
m = supports_meta({ "release" => "14.04" })
|
2018-01-02 19:04:13 +00:00
|
|
|
m.supports_platform?(backend).must_equal true
|
2015-12-31 16:54:13 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "rejects a profile which supports release 12.04" do
|
|
|
|
m = supports_meta({ "release" => "12.04" })
|
2018-01-02 19:04:13 +00:00
|
|
|
m.supports_platform?(backend).must_equal false
|
2015-12-31 16:54:13 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads a profile which supports ubuntu 14.04" do
|
|
|
|
m = supports_meta({ "os-name" => "ubuntu", "release" => "14.04" })
|
2018-01-02 19:04:13 +00:00
|
|
|
m.supports_platform?(backend).must_equal true
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads a profile which supports ubuntu 14.*" do
|
|
|
|
m = supports_meta({ "os-name" => "ubuntu", "release" => "14.*" })
|
2018-01-02 19:04:13 +00:00
|
|
|
m.supports_platform?(backend).must_equal true
|
2015-12-31 16:54:13 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "rejects a profile which supports ubuntu 12.04" do
|
|
|
|
m = supports_meta({ "os-name" => "ubuntu", "release" => "12.04" })
|
2018-01-02 19:04:13 +00:00
|
|
|
m.supports_platform?(backend).must_equal false
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "rejects a profile which supports ubuntu 12.*" do
|
|
|
|
m = supports_meta({ "os-name" => "ubuntu", "release" => "12.*" })
|
2018-01-02 19:04:13 +00:00
|
|
|
m.supports_platform?(backend).must_equal false
|
2015-12-31 16:54:13 +00:00
|
|
|
end
|
2016-01-02 20:27:40 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads a profile which supports ubuntu float 14.04 as parsed by yml" do
|
|
|
|
m = supports_meta({ "os-name" => "ubuntu", "release" => 14.04 })
|
2018-01-02 19:04:13 +00:00
|
|
|
m.supports_platform?(backend).must_equal true
|
2016-11-20 18:38:27 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "reject unsupported os" do
|
|
|
|
m = supports_meta({ "os-name" => "windows" })
|
2018-01-02 19:04:13 +00:00
|
|
|
m.supports_platform?(backend).must_equal false
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads a profile which supports multiple families" do
|
2018-01-02 19:04:13 +00:00
|
|
|
m = supports_meta([
|
2019-06-11 22:24:35 +00:00
|
|
|
{ "os-family" => "windows" },
|
2019-07-09 00:20:30 +00:00
|
|
|
{ "os-family" => "unix" },
|
2018-01-02 19:04:13 +00:00
|
|
|
])
|
|
|
|
m.supports_platform?(backend).must_equal true
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads a profile which supports multiple names" do
|
2018-01-02 19:04:13 +00:00
|
|
|
m = supports_meta([
|
2019-06-11 22:24:35 +00:00
|
|
|
{ "os-family" => "windows", "os-name" => "windows_2000" },
|
2019-07-09 00:20:30 +00:00
|
|
|
{ "os-family" => "unix", "os-name" => "ubuntu" },
|
2018-01-02 19:04:13 +00:00
|
|
|
])
|
|
|
|
m.supports_platform?(backend).must_equal true
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "reject a profile which supports multiple families" do
|
2018-01-02 19:04:13 +00:00
|
|
|
m = supports_meta([
|
2019-06-11 22:24:35 +00:00
|
|
|
{ "os-family" => "windows" },
|
2019-07-09 00:20:30 +00:00
|
|
|
{ "os-family" => "redhat" },
|
2018-01-02 19:04:13 +00:00
|
|
|
])
|
|
|
|
m.supports_platform?(backend).must_equal false
|
2016-01-02 20:27:40 +00:00
|
|
|
end
|
2015-12-31 16:54:13 +00:00
|
|
|
end
|
2016-04-16 16:54:05 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "testing the supported runtime" do
|
2016-04-16 16:54:05 +00:00
|
|
|
let(:current_version) { Inspec::VERSION }
|
2016-09-19 16:58:30 +00:00
|
|
|
let(:next_version) { Gem::Version.new(current_version).bump.to_s }
|
2016-04-16 16:54:05 +00:00
|
|
|
|
2018-01-02 19:04:13 +00:00
|
|
|
def version_meta(params)
|
2019-06-11 22:24:35 +00:00
|
|
|
res = Inspec::Metadata.from_yaml("mock", "---", nil, logger)
|
2018-01-02 19:04:13 +00:00
|
|
|
res.params[:inspec_version] = params
|
2019-06-11 22:24:35 +00:00
|
|
|
Inspec::Metadata.finalize(res, "mock", empty_options, logger)
|
2018-01-02 19:04:13 +00:00
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns true on testing the current version" do
|
2018-01-02 19:04:13 +00:00
|
|
|
m = version_meta(current_version)
|
2016-04-16 16:54:05 +00:00
|
|
|
m.supports_runtime?.must_equal true
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns true on testing the current version" do
|
|
|
|
m = version_meta("= " + current_version)
|
2016-04-16 16:54:05 +00:00
|
|
|
m.supports_runtime?.must_equal true
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns true on testing >= current version" do
|
|
|
|
m = version_meta(">= " + current_version)
|
2016-04-16 16:54:05 +00:00
|
|
|
m.supports_runtime?.must_equal true
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns false on testing >= the next version" do
|
|
|
|
m = version_meta(">= " + next_version)
|
2016-04-16 16:54:05 +00:00
|
|
|
m.supports_runtime?.must_equal false
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns false on testing > the next version" do
|
|
|
|
m = version_meta("> " + next_version)
|
2016-04-16 16:54:05 +00:00
|
|
|
m.supports_runtime?.must_equal false
|
|
|
|
end
|
2019-08-27 02:42:27 +00:00
|
|
|
|
|
|
|
it "is included in valid?" do
|
|
|
|
m = version_meta("> #{next_version}")
|
|
|
|
refute m.valid?
|
|
|
|
end
|
2016-04-16 16:54:05 +00:00
|
|
|
end
|
2015-12-31 16:54:13 +00:00
|
|
|
end
|