Class: Inspec::Resources::Cgroup

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/resources/cgroup.rb

Instance Method Summary collapse

Constructor Details

#initialize(cgroup_name) ⇒ Cgroup

Resource initialization.



19
20
21
22
23
24
25
26
27
# File 'lib/inspec/resources/cgroup.rb', line 19

def initialize(cgroup_name)
  raise Inspec::Exceptions::ResourceSkipped, "The `cgroup` resource is not supported on your OS yet." unless inspec.os.linux?

  @cgroup_name = cgroup_name
  @valid_queries, @valid_queries_split = [], []
  find_valid_queries
  # Used to track the method calls in an "its" query
  @cgroup_info_query = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(param) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/inspec/resources/cgroup.rb', line 37

def method_missing(param)
  # Add the latest param we've seen to the list and form the query with all the params we've seen so far.
  @cgroup_info_query << param.to_s
  query = @cgroup_info_query.join(".")

  # The ith level param must match with atleast one row's ith column of @valid_queries_split
  # Else there is no way, we would find any valid query in further iteration, so raise exception.
  if @valid_queries_split.map { |e| e[@cgroup_info_query.length - 1] }.include?(param.to_s)
    # If the query form so far is part of @valid_queries, we are good to trigger find_cgroup_info
    # else go for next level of param
    if @valid_queries.include?(query)
      @cgroup_info_query = []
      find_cgroup_info(query)
    else
      self
    end
  else
    @cgroup_info_query = []

    raise Inspec::Exceptions::ResourceFailed, "The query #{query} does not appear to be valid."
  end
end

Instance Method Details

#resource_idObject



29
30
31
# File 'lib/inspec/resources/cgroup.rb', line 29

def resource_id
  @cgroup_name
end

#to_sObject



33
34
35
# File 'lib/inspec/resources/cgroup.rb', line 33

def to_s
  "cgroup #{resource_id}"
end