Class: Inspec::Resources::DockerContainer

Inherits:
Object
  • Object
show all
Includes:
DockerObject
Defined in:
lib/inspec/resources/docker_container.rb

Instance Method Summary collapse

Methods included from DockerObject

#exist?, #id

Constructor Details

#initialize(opts = {}) ⇒ DockerContainer

Returns a new instance of DockerContainer.



33
34
35
36
37
38
39
40
# File 'lib/inspec/resources/docker_container.rb', line 33

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

Instance Method Details

#commandObject



71
72
73
74
75
76
# File 'lib/inspec/resources/docker_container.rb', line 71

def command
  return unless object_info.entries.length == 1

  cmd = object_info.commands[0]
  cmd.slice(1, cmd.length - 2)
end

#has_volume?(destination, source) ⇒ Boolean

has_volume? matcher checks if the volume specified in source path of host is mounted in destination path of docker

Returns:

  • (Boolean)

Raises:



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/inspec/resources/docker_container.rb', line 47

def has_volume?(destination, source)
  # volume_info is the hash which contains the low-level information about the container
  # if Mounts key is not present or is nil; raise exception
  raise Inspec::Exceptions::ResourceFailed, "Could not find any mounted volumes for your container" unless volume_info.Mounts[0]

  # Iterate through the list of mounted volumes and check if it matches with the given destination and source
  # is_mounted flag is used to handle to return explict boolean values of true or false
  is_mounted = false
  volume_info.Mounts.detect { |mount| is_mounted = mount.Destination == destination && mount.Source == source }
  is_mounted
end

#imageObject



78
79
80
# File 'lib/inspec/resources/docker_container.rb', line 78

def image
  object_info.images[0] if object_info.entries.length == 1
end

#labelsObject



63
64
65
# File 'lib/inspec/resources/docker_container.rb', line 63

def labels
  object_info.labels
end

#portsObject



67
68
69
# File 'lib/inspec/resources/docker_container.rb', line 67

def ports
  object_info.ports[0] if object_info.entries.length == 1
end

#repoObject



82
83
84
# File 'lib/inspec/resources/docker_container.rb', line 82

def repo
  parse_components_from_image(image)[:repo] if object_info.entries.size == 1
end

#resource_idObject



95
96
97
# File 'lib/inspec/resources/docker_container.rb', line 95

def resource_id
  object_info.ids[0] || @opts[:id] || @opts[:name] || ""
end

#running?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/inspec/resources/docker_container.rb', line 42

def running?
  status.downcase.start_with?("up") if object_info.entries.length == 1
end

#statusObject



59
60
61
# File 'lib/inspec/resources/docker_container.rb', line 59

def status
  object_info.status[0] if object_info.entries.length == 1
end

#tagObject



86
87
88
# File 'lib/inspec/resources/docker_container.rb', line 86

def tag
  parse_components_from_image(image)[:tag] if object_info.entries.size == 1
end

#to_sObject



90
91
92
93
# File 'lib/inspec/resources/docker_container.rb', line 90

def to_s
  name = @opts[:name] || @opts[:id]
  "Docker Container #{name}"
end