2017-04-24 14:47:03 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
#
|
|
|
|
# Copyright 2017, Christoph Hartmann
|
|
|
|
#
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Patrick Muench
|
|
|
|
# author: Dominik Richter
|
|
|
|
|
2018-01-23 20:30:14 +00:00
|
|
|
require_relative 'docker_object'
|
|
|
|
|
2017-04-24 14:47:03 +00:00
|
|
|
module Inspec::Resources
|
|
|
|
class DockerContainer < Inspec.resource(1)
|
2018-01-23 20:30:14 +00:00
|
|
|
include Inspec::Resources::DockerObject
|
|
|
|
|
2017-04-24 14:47:03 +00:00
|
|
|
name 'docker_container'
|
|
|
|
desc ''
|
|
|
|
example "
|
|
|
|
describe docker_container('an-echo-server') do
|
|
|
|
it { should exist }
|
|
|
|
it { should be_running }
|
|
|
|
its('id') { should_not eq '' }
|
|
|
|
its('image') { should eq 'busybox:latest' }
|
|
|
|
its('repo') { should eq 'busybox' }
|
|
|
|
its('tag') { should eq 'latest' }
|
|
|
|
its('ports') { should eq [] }
|
|
|
|
its('command') { should eq 'nc -ll -p 1234 -e /bin/cat' }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe docker_container(id: 'e2c52a183358') do
|
|
|
|
it { should exist }
|
|
|
|
it { should be_running }
|
|
|
|
end
|
|
|
|
"
|
|
|
|
|
|
|
|
def initialize(opts = {})
|
|
|
|
# if a string is provided, we expect it is the name
|
|
|
|
if opts.is_a?(String)
|
|
|
|
@opts = { name: opts }
|
|
|
|
else
|
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def running?
|
2018-01-23 20:30:14 +00:00
|
|
|
status.downcase.start_with?('up') if object_info.entries.length == 1
|
2017-04-24 14:47:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
2018-01-23 20:30:14 +00:00
|
|
|
object_info.status[0] if object_info.entries.length == 1
|
2017-04-24 14:47:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def labels
|
2018-01-23 20:30:14 +00:00
|
|
|
object_info.labels[0] if object_info.entries.length == 1
|
2017-04-24 14:47:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def ports
|
2018-01-23 20:30:14 +00:00
|
|
|
object_info.ports[0] if object_info.entries.length == 1
|
2017-04-24 14:47:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def command
|
2018-01-23 20:30:14 +00:00
|
|
|
return unless object_info.entries.length == 1
|
2017-11-21 07:49:41 +00:00
|
|
|
|
2018-01-23 20:30:14 +00:00
|
|
|
cmd = object_info.commands[0]
|
2017-11-21 07:49:41 +00:00
|
|
|
cmd.slice(1, cmd.length - 2)
|
2017-04-24 14:47:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def image
|
2018-01-23 20:30:14 +00:00
|
|
|
object_info.images[0] if object_info.entries.length == 1
|
2017-04-24 14:47:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def repo
|
2018-01-23 20:30:14 +00:00
|
|
|
parse_components_from_image(image)[:repo] if object_info.entries.size == 1
|
2017-04-24 14:47:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def tag
|
2018-01-23 20:30:14 +00:00
|
|
|
parse_components_from_image(image)[:tag] if object_info.entries.size == 1
|
2017-04-24 14:47:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
name = @opts[:name] || @opts[:id]
|
|
|
|
"Docker Container #{name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-01-23 20:30:14 +00:00
|
|
|
def object_info
|
2017-04-24 14:47:03 +00:00
|
|
|
return @info if defined?(@info)
|
|
|
|
opts = @opts
|
|
|
|
@info = inspec.docker.containers.where { names == opts[:name] || (!id.nil? && !opts[:id].nil? && (id == opts[:id] || id.start_with?(opts[:id]))) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|