Module: CemAcpt::Goss::Api

Extended by:
Logging
Defined in:
lib/cem_acpt/goss/api.rb,
lib/cem_acpt/goss/api/action_response.rb

Overview

Holds methods for interacting with the Goss API running on a test node.

Defined Under Namespace

Modules: DurationHandler Classes: ActionResponse, ActionResponseResult, ActionResponseSummary

Constant Summary collapse

ACTIONS =

The actions that can be run against the Goss API. The key is the action name and the value is the port/endpoint of the action.

{
  acpt: '8080/acpt',
  idempotent: '8081/idempotent',
  noop: '8082/noop',
}.freeze

Constants included from Logging

Logging::LEVEL_MAP

Class Method Summary collapse

Methods included from Logging

current_log_config, current_log_config, current_log_format, current_log_format, current_log_level, current_log_level, included, logger, logger, new_log_config, new_log_config, new_log_formatter, new_log_formatter, new_log_level, new_log_level, new_logger, new_logger

Class Method Details

.action_uri(host, action) ⇒ URI

Create a URI for the specified action against the specified host.

Parameters:

  • host (String)

    The host to run the action against. This should be a public IP address or a DNS-resolvable name.

  • action (Symbol)

    The action to run.

Returns:

  • (URI)

    The URI for the action.



30
31
32
# File 'lib/cem_acpt/goss/api.rb', line 30

def action_uri(host, action)
  URI("http://#{host}:#{ACTIONS[action.to_sym]}")
end

.run_action(internet, host, action) ⇒ ActionResponse

Run the specified action against the specified host.

Parameters:

  • internet (Async::HTTP::Internet)

    The Async::HTTP::Internet object to use for the request.

  • host (String)

    The host to run the action against. This should be a public IP address or a DNS-resolvable name.

  • action (Symbol)

    The action to run.

Returns:



80
81
82
83
84
85
# File 'lib/cem_acpt/goss/api.rb', line 80

def run_action(internet, host, action)
  uri = action_uri(host, action)
  response = internet.get(uri.to_s)
  body = JSON.parse(response.read)
  ActionResponse.new(host, action, response.status, body)
end

.run_actions_async(hosts, results: Queue.new, only: [], except: []) ⇒ Queue

Run the specified actions against the specified hosts asynchronously.

Parameters:

  • hosts (Array<String>)

    The hosts to run the actions against. Each host should be a public IP address or a DNS-resolvable name.

  • results (Queue) (defaults to: Queue.new)

    The queue to push the results to.

  • only (Array<Symbol>) (defaults to: [])

    The actions to run.

  • except (Array<Symbol>) (defaults to: [])

    The actions to skip.

Returns:

  • (Queue)

    The queue of results.

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cem_acpt/goss/api.rb', line 41

def run_actions_async(hosts, results: Queue.new, only: [], except: [])
  raise ArgumentError, 'hosts must be an Array' unless hosts.is_a?(Array)
  raise ArgumentError, 'results must be a Queue-like object implementing #<<' unless results.respond_to?(:<<)
  raise ArgumentError, 'only must be an Array' unless except.is_a?(Array)
  raise ArgumentError, 'except must be an Array' unless except.is_a?(Array)
  logger.info('CemAcpt::Goss::Api') { "Running test actions against #{hosts.size} host(s)..." }
  only.map!(&:to_sym)
  except.map!(&:to_sym)
  only_specified = !only.empty?
  except_specified = !except.empty?
  task = Async do
    internet = Async::HTTP::Internet.new
    barrier = Async::Barrier.new
    barrier.async do
      hosts.each do |host|
        ACTIONS.each_key do |action|
          next if only_specified && !only.include?(action)
          next if except_specified && except.include?(action)

          results << run_action(internet, host, action)
        end
      end
    end
    barrier.wait
  ensure
    internet&.close
    results.close if results.respond_to?(:close)
  end
  task.wait
  logger.info('CemAcpt::Goss::Api') { 'Finished running test actions, returning results...' }
  results
end