Class: Inspec::Resources::DockerImage

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

Instance Method Summary collapse

Methods included from DockerObject

#exist?, #id

Constructor Details

#initialize(opts = {}) ⇒ DockerImage

Returns a new instance of DockerImage.



32
33
34
35
36
37
# File 'lib/inspec/resources/docker_image.rb', line 32

def initialize(opts = {})
  # do sanitizion of input values
  o = opts.dup
  o = { image: opts } if opts.is_a?(String)
  @opts = sanitize_options(o)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*hash_keys) ⇒ Object

method_missing handles when hash_keys are invoked to check information obtained on docker inspect [image_name]



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/inspec/resources/docker_image.rb', line 52

def method_missing(*hash_keys)
  # User can test the low-level inspect information in three ways:
  # Way 1: Serverspec style: its(['Config.Cmd']) { should include some_value }
  #        here, the value for hash_keys recieved is [:[], "Config.Cmd"]
  # Way 2: InSpec style: its(['Config','Cmd']) { should include some_value }
  #        here, the value for hash_keys recieved is [:[], "Config", "Cmd"]
  # Way 3: Mix of both: its(['GraphDriver.Data','MergedDir']) { should include some_value }
  #        here, the value for hash_keys recieved is [:[], "GraphDriver.Data", "MergedDir"]

  # hash_keys are passed to this method to evaluate the value
  image_hash_inspection(hash_keys)
end

Instance Method Details

#imageObject



39
40
41
# File 'lib/inspec/resources/docker_image.rb', line 39

def image
  "#{repo}:#{tag}" if object_info.entries.size == 1
end

#inspectionObject

inspection property allows to test any of the hash key-value pairs as part of the image_inspect_info



66
67
68
# File 'lib/inspec/resources/docker_image.rb', line 66

def inspection
  image_inspect_info
end

#repoObject



43
44
45
# File 'lib/inspec/resources/docker_image.rb', line 43

def repo
  object_info.repositories[0] if object_info.entries.size == 1
end

#resource_idObject



75
76
77
# File 'lib/inspec/resources/docker_image.rb', line 75

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

#tagObject



47
48
49
# File 'lib/inspec/resources/docker_image.rb', line 47

def tag
  object_info.tags[0] if object_info.entries.size == 1
end

#to_sObject



70
71
72
73
# File 'lib/inspec/resources/docker_image.rb', line 70

def to_s
  img = @opts[:image] || @opts[:id]
  "Docker Image #{img}"
end