2019-06-11 15:24:35 -07:00
|
|
|
require "helper"
|
|
|
|
require "inspec/resource"
|
|
|
|
require "inspec/resources/docker_image"
|
2017-04-24 16:47:03 +02:00
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
describe "Inspec::Resources::DockerImage" do
|
|
|
|
describe "docker_image" do
|
|
|
|
it "check docker image parsing" do
|
|
|
|
resource = load_resource("docker_image", "alpine")
|
|
|
|
_(resource.id).must_equal "sha256:4a415e3663882fbc554ee830889c68a33b3585503892cc718a4698e91ef2a526"
|
|
|
|
_(resource.tag).must_equal "latest"
|
|
|
|
_(resource.image).must_equal "alpine:latest"
|
|
|
|
_(resource.repo).must_equal "alpine"
|
2022-06-02 16:32:05 +05:30
|
|
|
_(resource.resource_id).must_equal "sha256:4a415e3663882fbc554ee830889c68a33b3585503892cc718a4698e91ef2a526"
|
2017-04-24 16:47:03 +02:00
|
|
|
end
|
|
|
|
|
2022-03-24 22:49:01 +05:30
|
|
|
# Test case for inspect image information handled by inspection and method_missing
|
2022-03-22 14:07:28 +05:30
|
|
|
it "check attributes returned by docker inspect [docker_image]" do
|
|
|
|
resource = load_resource("docker_image", "ubuntu:latest")
|
|
|
|
_(resource["Architecture"]).must_equal "arm64"
|
|
|
|
_(resource["Config.Cmd"]).must_include "bash"
|
2022-03-22 15:27:33 +05:30
|
|
|
_(resource.inspection).must_include "Architecture"
|
|
|
|
_(resource.inspection.Architecture).must_equal "arm64"
|
2022-06-02 17:14:35 +05:30
|
|
|
_(resource.resource_id).must_equal "ubuntu:latest"
|
2022-03-22 14:07:28 +05:30
|
|
|
end
|
|
|
|
|
2022-03-24 22:49:01 +05:30
|
|
|
# Test case for inspect image information with invalid keys
|
|
|
|
it "checks exception when key is invalid or doesn't exist as part of the inspect information" do
|
|
|
|
resource = load_resource("docker_image", "ubuntu:latest")
|
|
|
|
ex = _ { resource["Garbage.Key"] }.must_raise(Inspec::Exceptions::ResourceFailed)
|
|
|
|
_(ex.message).must_include "Garbage.Key is not a valid key for your image or has nil value."
|
|
|
|
end
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "prints as a docker_image resource" do
|
|
|
|
resource = load_resource("docker_image", "alpine")
|
2019-09-30 15:31:55 -07:00
|
|
|
_(resource.to_s).must_equal "Docker Image alpine:latest"
|
2017-04-24 16:47:03 +02:00
|
|
|
end
|
|
|
|
end
|
2017-12-01 04:24:15 -05:00
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
describe "#parse_components_from_image" do
|
|
|
|
let(:resource) { load_resource("docker_image", "alpine") }
|
2017-12-01 04:24:15 -05:00
|
|
|
let(:parsed) { resource.send(:parse_components_from_image, image_string) }
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
describe "a nil image string" do
|
2017-12-01 04:24:15 -05:00
|
|
|
let(:image_string) { nil }
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "returns an empty hash" do
|
2019-09-30 15:31:55 -07:00
|
|
|
_(parsed).must_equal({})
|
2017-12-01 04:24:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
describe "an image string containing a simple repo" do
|
|
|
|
let(:image_string) { "chef/inspec" }
|
2017-12-01 04:24:15 -05:00
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "returns correct data" do
|
2019-09-30 15:31:55 -07:00
|
|
|
_(parsed[:repo]).must_equal "chef/inspec"
|
|
|
|
_(parsed[:tag]).must_be_nil
|
2017-12-01 04:24:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
describe "parses an image string containing a repo with a port number" do
|
|
|
|
let(:image_string) { "localhost:5000/chef/inspec" }
|
2017-12-01 04:24:15 -05:00
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "returns correct data" do
|
2019-09-30 15:31:55 -07:00
|
|
|
_(parsed[:repo]).must_equal "localhost:5000/chef/inspec"
|
|
|
|
_(parsed[:tag]).must_be_nil
|
2017-12-01 04:24:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
describe "parses an image string containing a repo with a tag" do
|
|
|
|
let(:image_string) { "chef/inspec:1.46.3" }
|
2017-12-01 04:24:15 -05:00
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "returns correct data" do
|
2019-09-30 15:31:55 -07:00
|
|
|
_(parsed[:repo]).must_equal "chef/inspec"
|
|
|
|
_(parsed[:tag]).must_equal "1.46.3"
|
2017-12-01 04:24:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
describe "parses an image string containing a repo with a port number and a tag" do
|
|
|
|
let(:image_string) { "localhost:5000/chef/inspec:1.46.3" }
|
2017-12-01 04:24:15 -05:00
|
|
|
|
2019-06-11 15:24:35 -07:00
|
|
|
it "returns correct data" do
|
2019-09-30 15:31:55 -07:00
|
|
|
_(parsed[:repo]).must_equal "localhost:5000/chef/inspec"
|
|
|
|
_(parsed[:tag]).must_equal "1.46.3"
|
2017-12-01 04:24:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-24 16:47:03 +02:00
|
|
|
end
|