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, verbose?, verbose?

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:



87
88
89
90
91
92
# File 'lib/cem_acpt/goss/api.rb', line 87

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_action_async(context = {}) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/cem_acpt/goss/api.rb', line 73

def run_action_async(context = {})
  context[:results] ||= Queue.new
  context[:hosts].each do |host|
    context[:results] << run_action(context[:internet], host, context[:action])
  end
  context[:results]
end

.run_actions_async(context = {}) ⇒ Queue

Run the specified actions against the specified hosts asynchronously.

Parameters:

  • context (Hash) (defaults to: {})

    The context for the test run.

  • [Array<String>] (Hash)

    a customizable set of options

Options Hash (context):

  • :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)

    The queue to push the results to.

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
# File 'lib/cem_acpt/goss/api.rb', line 41

def run_actions_async(context = {})
  actions = context[:actions] || []
  hosts = context[:hosts] || []
  results = context[:results] || Queue.new
  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, 'actions must be an Array' unless actions.is_a?(Array)

  actions.map!(&:to_sym)
  actions.select! { |action| ACTIONS.key?(action) }
  logger.info('CemAcpt::Goss::Api') do
    "Running test actions #{actions.join(', ')} against #{hosts.size} host(s)..."
  end
  task = Async do
    internet = Async::HTTP::Internet.new
    barrier = Async::Barrier.new
    barrier.async do
      hosts.each do |host|
        actions.each do |action|
          results << run_action(internet, host, action)
        end
      end
    end
    barrier.wait
  ensure
    internet&.close
  end
  task.wait
  logger.info('CemAcpt::Goss::Api') { 'Finished running test actions, returning results...' }
  results
end