2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/resource"
|
|
|
|
require "inspec/resources/docker_container"
|
2017-04-24 14:47:03 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Inspec::Resources::DockerContainer" do
|
|
|
|
describe "docker_container" do
|
|
|
|
it "check container parsing for alpine" do
|
|
|
|
resource = load_resource("docker_container", "laughing_austin")
|
|
|
|
_(resource.id).must_equal "d94f854370d2b02912e8fc636502bc72b74fbd567a7eba3fc6a52045bb28904e"
|
|
|
|
_(resource.image).must_equal "alpine"
|
|
|
|
_(resource.repo).must_equal "alpine"
|
2017-06-11 10:16:10 +00:00
|
|
|
_(resource.tag).must_be_nil
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.command).must_equal "/bin/sh"
|
|
|
|
_(resource.ports).must_equal ""
|
2017-04-24 14:47:03 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check container parsing for alpine" do
|
|
|
|
resource = load_resource("docker_container", "sleepy_khorana")
|
|
|
|
_(resource.id).must_equal "3def9aa450f8bd772c3d5b07e27ec934e5f58575e955367a0aca2d93e0687536"
|
|
|
|
_(resource.image).must_equal "ubuntu:12.04"
|
|
|
|
_(resource.repo).must_equal "ubuntu"
|
|
|
|
_(resource.tag).must_equal "12.04"
|
|
|
|
_(resource.command).must_equal "/bin/bash"
|
|
|
|
_(resource.ports).must_equal ""
|
|
|
|
_(resource.labels).must_equal ["app=example", "version=1.5.4"]
|
2018-12-20 17:31:13 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns an empty array when parsing a container with no labels specified" do
|
|
|
|
resource = load_resource("docker_container", "heuristic_almeida")
|
2018-12-20 17:31:13 +00:00
|
|
|
_(resource.labels).must_equal []
|
2017-04-24 14:47:03 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check image containing repo with port and tag gives correct repo, image, and tag" do
|
|
|
|
resource = load_resource("docker_container", "heuristic_almeida")
|
|
|
|
_(resource.repo).must_equal "repo.example.com:5000/ubuntu"
|
|
|
|
_(resource.image).must_equal "repo.example.com:5000/ubuntu:14.04"
|
|
|
|
_(resource.tag).must_equal "14.04"
|
2017-08-10 12:57:45 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check image containing repo with port and no tag gives correct repo, image, and tag" do
|
|
|
|
resource = load_resource("docker_container", "laughing_lamport")
|
|
|
|
_(resource.repo).must_equal "repo.example.com:5000/ubuntu"
|
|
|
|
_(resource.image).must_equal "repo.example.com:5000/ubuntu"
|
2017-08-10 12:57:45 +00:00
|
|
|
_(resource.tag).must_be_nil
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "prints as a docker resource" do
|
|
|
|
resource = load_resource("docker_container", "laughing_austin")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(resource.to_s).must_equal "Docker Container laughing_austin"
|
2017-04-24 14:47:03 +00:00
|
|
|
end
|
2022-03-22 06:26:41 +00:00
|
|
|
|
2022-03-24 16:54:27 +00:00
|
|
|
# Test case for has_volume? matcher - Case 1: Volumes are mounted on the container
|
2022-03-22 06:26:41 +00:00
|
|
|
it "checks if a volume has been mounted for the docker resource" do
|
|
|
|
resource = load_resource("docker_container", "trusting_williams")
|
2022-03-23 17:13:02 +00:00
|
|
|
_(resource.has_volume?("/app", "/var/lib/docker/volumes/myvol2/_data")).must_equal true
|
2022-03-24 05:21:06 +00:00
|
|
|
_(resource.has_volume?("/app2", "/var/lib/docker/volumes/myvol3/_data")).must_equal true
|
2022-03-22 06:26:41 +00:00
|
|
|
end
|
2022-03-24 16:54:27 +00:00
|
|
|
|
|
|
|
# Test case for has_volume? matcher - Case 2: Volumes are not mounted on the container
|
|
|
|
it "checks exception when no volume has been mounted for the docker resource" do
|
|
|
|
resource = load_resource("docker_container", "fried_water")
|
|
|
|
ex = _ { resource.has_volume?("/app", "/var/lib/docker/volumes/myvol2/_data") }.must_raise(Inspec::Exceptions::ResourceFailed)
|
|
|
|
_(ex.message).must_include "Could not find any mounted volumes for your container"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Test case for has_volume? matcher - Case 3: The container doesn't exist
|
|
|
|
it "checks exception when no volume has been mounted for the docker resource and the container doesnt'e exist" do
|
|
|
|
resource = load_resource("docker_container", "non_existing_container")
|
|
|
|
ex = _ { resource.has_volume?("/app", "/var/lib/docker/volumes/myvol2/_data") }.must_raise(NoMethodError)
|
|
|
|
_(ex.message).must_include "undefined method `[]' for nil:NilClass"
|
|
|
|
end
|
2017-04-24 14:47:03 +00:00
|
|
|
end
|
|
|
|
end
|